Cookie
-
1993 ushered in
img
Label, this isweb
Resource loading for the first time<img src="foo.jpg" /> <img src="https://imgs.developpaper.com/imgs/foo.jpg" />
-
1994
Set-Cookie: foo=bar
-
1995
<img src="https://imgs.developpaper.com/imgs/foo.jpg" /> <script src="https://somewhere.com/foo.js" ></script> <iframe src="https://somewhere.com/foo.html" />
At first, such a design would lead to serious problems: whether it is a website, as long as it contains the above label, it will carry cookies to visit.
Not to mention such a request.
<form action="http://somewhere.com./submit"></form>
thereforehttp
adoptOrigin
Use this header to restrict access
-
1999
//Predecessor of XMLHttpRequest new ActiveXObject('Microsoft.XMLHTTP');
-
2008
var req = new XMLHttpRequest() req.addEventListener('load', loadListener); req.open('GET', 'https://example.com/data.txt'); req.send();
Now?
const res = await fetch('http://example.com/data.txt', {
credentials: 'include',
});
Now we have used this method to request, but it is also limited by whether the server responds(the following is why some servers respond)。
Cross domain resource sharing (CORS)
We passaccess-control-allow-origin
To determine which resources can be accessed across domains, such as settingsaccess-control-allow-origin: *
Allow all resources to be accessed. And by settingcredential
To decide whether to carry itcookie
。
access-control-allow-credentials: true
Decide whether to allow you to visitcookie
。 But at the same time, you must clearly passaccess-control-allow-origin
Indicates the web address accessed across domains.
These headers have been set in the request we send through the browser, and we don’t need to pay special attention. For example, we pass<form>
When sending a request,Content-Type
Has been set to, for examplex-www-form-urlencoded
。
A little bit of knowledge: you can send settings as
Content-type: text/plain
ofPOST
Request. For example, through<form>
To sendx-www-form-urlencoded
Empty line spacing.
Referrer
When you jump from one website to another, there will be this header
Origin
Referrer
Unreliable, so there isOrigin
This header information.Origin
Appears in cross domain requests.
Generally speaking, we limitaccess-control-allow-origin
The website is safe enough.
except
CORS
have toGET
Other than the requestGET
No requestsOrigin
。 Can be judged byOrigin
Know whether this is a cross domain request.
Dangerous cookies across domains
When we don’t bringcookie
Many problems may arise when making a request. First we need to know what’s going onaccess-control-allow-origin: *
It’s safe when, for example, it’s distributed at homeIoT
Device, local request. In addition, it may causeCSRF
Attack.
Restrict cookies
For example, through image tracking. When you visitother.com
When,<img />
Will allow settingcookie
For tracking.
<!-- site: other.com -->
<img src="https://statics.com/user-avatar-a.png" />
set upSameSite: Strict | Lax | None
。Lax
not allowimg
、 iframe
、 AJAX
、 POST
Form carryingcookie
(current site: other. Com)。 howeverLax
allow<a>
Links, preload requests,GET
The form can be carried in these three casescookie
。
Set-cookie: sessionid=1234567; SameSite=Lax; HttpOnly; Max-Age=3153600
SameSite: Strict
This will result in no cross domaincookie
For example, in nongithub
Jump togithub
Will not carrycookie
, at this timegithub
Not logged in will be displayed.
httponly
: cannot passjavasript
obtaincookie
Cross domain resource policy (Corp)
Cross-Origin-Resource-Policy: same-site
Mark assame-site
Your resources can only be loaded from the same site.
Cross-Origin-Resource-Policy
The response header instructs the browser to block passive cross domain / cross site requests for the specified resource.
Add in the following requestCORP
The header information of the picture will fail to load.
<!-- site: other.com -->
<img src="https://statics.com/user-avatar-a.png" />
Cross-Origin-Resource-Policy: same-site | same-origin |cross-origin
CORB
The following scenario cannot pass through the samecookie
Handle, although you cancookie
Put onhtml
Request in progress.
<img src="https://example.com/secret.html" />
This request will stillhtml
The file request is put into the process to indicate that the current request processing is completed. This may cause some problems.Meltdown and Spectre
Cross-Origin-Read-Blocking
Prevent cross domain request actions and cross domain returns. For example, the above request will not enter the processing process of the browser.
Before entering the process, the request will pay attention tomine type
If it isnosniff
(nosniff
The browser will be blocked forcontent-type
Some optimizations of).Chrome
Browser forhtml
Requests for files that are not cross domain will be blocked.
CORB
No header information
Cross domain embedding strategy (COEP)
Cross-Origin-Embedder-Policy: require-corp
Documents can only load resources from the same source, or are explicitly marked as resources that can be loaded from another source.
In order to load resources from other sources, cross domain resource sharing needs to be supported(CORS)
Or cross domain resource policy(CORP)
。
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Resource-Policy: cross-origin