- Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks.
- If the site doesn't offer the CSP header, browsers likewise use the standard same-origin policy.
- A CSP compatible browser will then only execute scripts loaded in source files received from those allowed domains, ignoring all other scripts (including inline scripts and event-handling HTML attributes).
https://content-security-policy.com/
- The
default-srcdirective defines the default policy for fetching resources. script-srcdefines valid sources of JavaScript.styles-srcdefines valid sources of stylesheets or CSS.img-srcdefines valid sources of images.object-srcdefines valid sources of images.media-srcdefines valid sources of images.frame-ancestorsdefines valid sources for embedding the resource using<frame><iframe><object><embed><applet>.upgrade-insecure-requestsautomatically Converts urls from http to https.
- Implement HTTP response header from server or use meta-tags in HTML.
- The HTTP
Strict-Transport-Securityresponse header (often abbreviated as HSTS) informs browsers that the site should only be accessed using HTTPS, and that any future attempts to access it using HTTP should automatically be converted to HTTPS.
- check
what issection.
- Implement HTTP response header
- The
X-Content-Type-Optionsresponse HTTP header is a marker used by the server to indicate that the MIME types advertised in theContent-Typeheaders should be followed and not be changed.
- The header allows you to avoid MIME type sniffing by saying that the MIME types are deliberately configured.
- Implement HTTP response header
- The
X-Frame-OptionsHTTP response header can be used to indicate whether a browser should be allowed to render a page in a<frame>,<iframe>,<embed>or<object>.
- It will prevent browser to render certain tags (
iframe,frameetc.)
- Implement HTTP response header
1. Same Origin Policy (https://portswigger.net/web-security/cors/same-origin-policy)
-
The same-origin policy is a critical security mechanism that restricts how a document or script loaded by one origin can interact with a resource from another origin. It helps isolate potentially malicious documents, reducing possible attack vectors.
-
Two URLs have the same origin if the protocol, port (if specified), and host are the same for both.
http://normal-website.com/example/example.html
| URL accessed | Access permitted? |
|---|---|
http://normal-website.com/example/ |
Yes: same scheme, domain, and port |
http://normal-website.com/example2/ |
Yes: same scheme, domain, and port |
https://normal-website.com/example/ |
No: different scheme and port |
http://en.normal-website.com/example/ |
No: different domain |
http://www.normal-website.com/example/ |
No: different domain |
http://normal-website.com:8080/example/ |
No: different port |
- When a browser sends an HTTP request from one origin to another, any cookies, including authentication session cookies, relevant to the other domain are also sent as part of the request.
- Without the same-origin policy, if you visited a malicious website, it would be able to read your emails from GMail, private messages from Facebook, etc.
- It is implemented on the browser level to guarantee no unauthorized cross-origin communication that could lead to a malicious script on one website obtaining access to sensitive data on another. Or, to put it differently, it prevents the reusing of authenticated user sessions across websites and read access to responses from different origins.
- Due to legacy requirements, the same-origin policy is more relaxed when dealing with cookies, so they are often accessible from all subdomains of a site even though each subdomain is technically a different origin.
- An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to a user's web browser.
- Certain cookies flag such as
secure,httponlyandsamesite. A cookie with theHttpOnlyattribute is inaccessible to the JavaScriptDocument.cookieAPI; it's only sent to the server. - Use the
HttpOnlyattribute to prevent access to cookie values via JavaScript. - A cookie with the
Secureattribute is only sent to the server with an encrypted request over the HTTPS protocol. It's never sent with unsecured HTTP (except on localhost). - The
SameSiteattribute lets servers specify whether/when cookies are sent with cross-site requests. It helps to mitigateCSRF. strict= If a cookie is set with theSameSite=Strictattribute, browsers will not send it in any cross-site requests.Lax=LaxSameSite restrictions means that browsers will send the cookie in cross-site requests, if request isGETand the request resulted from a top-level navigation by the user, such as clicking on a link.none= It disables restrictions as a result, browsers will send this cookie in all requests to the site that issued it, even those that were triggered by completely unrelated third-party sites.
- It can be done at the development level, setting up a flags on cookies.