The browser will mark whether the request is from this site or an external site, and how the server can prevent cross-site requests based on this.
The browser indicates whether the request is from the same site or an external site, the server blocks cross-site request forgery based on this, relying on several source markers that cannot be changed by the front end. The specification leaves four positions for this set of headers, with the values automatically filled in by the browser before sending the request. The web script cannot create new fields with the same name, nor can it change existing content, so the server can use it as a relatively可信 signal to judge whether the request is from its own page or borrowed by another site to carry login state.
Who added this set of request headers?
This set of headers is called fetch metadata request headers, which is also known as the pull metadata request headers, and the browser writes it for you before actually sending out the request.The four members are Sec-Fetch-Site (site relationship), Sec-Fetch-Mode (initiation mode), Sec-Fetch-User (whether initiated by the user), Sec-Fetch-Dest (target type).The specifications classify them into the category of forbidden request headers, meaning that web pages cannot customize headers with the same name, and the method of forging a source marker has been blocked.
There is also a boundary that is easy to ignore, which is the trusted address: the browser only attaches this set of headers to requests deemed trustworthy, and sites running on plaintext addresses do not receive them. Therefore, the signals at the server end actually have two states, one is explicitly written as cross-site, and the other is completely without this set of headers, and the two need to be handled separately; missing cannot be taken as a reason to allow.
What do the four values mean?
Sec-Fetch-Site specifies the relationship between the initiator and the recipient, divided into four types: same-origin, same-site, cross-site, and no initiator. The meaning and handling are listed in the table below. Same-origin requires that the protocol, host, and port be completely consistent, while same-site refers to the host name being the same or a subdomain.
| Value | The relationship indicated | Typical scenarios | Common handling on the server |
|---|---|---|---|
| same-origin (same origin) | The protocol, host, and port are completely consistent | Interface calls initiated from the page on this site | Pass according to normal business logic |
| same-site (same site) | The host name is the same or a subdomain | The main page calls the interface subdomain | Read and write are separated, relaxed according to the nature of the interface |
| cross-site (cross-site) | Both parties have no site relationship | Request sent from an external site with a login state | Write operation is directly rejected |
| none (no initiator) | Cannot see the source page | Direct access from the address bar, bookmarks open | Only place browser navigation requests |
These three headers answer other aspects. Sec-Fetch-Mode indicates the mode in which the request is issued, whether it is page navigation, embedded resource, or interface call; Sec-Fetch-Dest indicates what type of target the request wants to fall on, whether it is a document, script, or data; Sec-Fetch-User only appears in a few requests triggered directly by user actions, indicating that this is a jump caused by a user's action. These items combined can distinguish between the interface calls of this site and the automatic submission of external pages.
How to write an access strategy based on context on the server?
A feasible way is to declare for each type of interface which contexts it accepts. Interfaces with login status usually only accept requests from the same origin and the same station. Read requests according to business needs can be relaxed, and requests with cross-site tags should be directly rejected without going around to verify tokens, because the source relationship itself has already been established. For header missing, it needs to be set up separately according to the nature of the interface: pages that can be opened directly in the browser can accept missing headers, and pure interface paths can be rejected to avoid interpreting the absence of signals as beneficial signals.
This set of headers has an easily overlooked impact, which is caching. Since the passage is determined by the context, the same address may get different results in different contexts, and the cache must also distinguish these contexts, otherwise, the rejected response may be reused on normal requests, and vice versa. A safe approach is to let the interface for passing according to the context carry the source information into the cache key, or simply exclude such interfaces from the shared cache.
Is it complementary to the write operation confirmation rather than a substitute?
This marking answers who initiated this request, and does not answer whether the initiator has the permission to do this. It can prevent others from secretly sending a write operation using the login state, but it cannot prevent a legitimate user from clicking an inappropriate button on their own page, so it is just a face-saving measure and cannot replace the confirmation before writing.
Within the AI interface side (MCP) of the station, there is round-level approval for write operations, which will merge a batch of changes into one confirmation. The changes will only take effect after the person nods in this round. The two layers of objects do not overlap: the source tag manages who initiated the request, and the round-level approval manages whether the person needs to nod for this change. The former filters out external sources at the service endpoint, and the latter gathers batch changes into the same confirmation point on the agent side, and no one can replace each other.
Common questions
Can this group of headers block all scenarios of cross-site request forgery?Cannot. It only handles the part that can be determined by the browser, the headers are never received at the plaintext address, and constraints other than the same-domain subdomain are needed. The permission judgment and confirmation before write operation still need to be done together in the business layer.
How should we judge if there is no such header in the request?First, check if the service address is trustworthy, the browser will not send it under plaintext address. When the client directly connects outside the browser, this header is naturally missing, and we can only return to the original authorization and confirmation mechanism, treating the missing as unavailable signal rather than as an approval.
Does a cross-site marked request must have a problem?It is not necessarily. Normal navigation caused by third-party embedding, page jump from partner pages, and external links all carry cross-site tags. It is a safe practice to use it only for rejecting write operations, and read requests and navigation should be judged again by mode and target type.