When a page is embedded in an iframe of another website, where should the intercept be written in the response header?
When a page is embedded by another website using an iframe, intercept the script written in the response header, not in a script segment in the page. The two available options are: X-Frame-Options and the frame-ancestors directive in the content security policy.The former only has two reliable values, DENY and SAMEORIGIN, while the latter can express 'which sources are allowed to embed'. If you just need 'no one can embed' or 'only same-origin embedding is allowed', the former is sufficient.Once it is necessary to allow a specific partner, frame-ancestors must be used.
The problem caused by embedding the page is not a display issue
The risk of being nested lies in the fact that the user sees the interface of our site, but the operation is arranged in someone else's page. The cooperative promotion pages, aggregation sites, and even pages disguised as normal sites can put login boxes, comment forms into an invisible frame, and lead the user's click to the real function of our site. This kind of problem is generally called clickjacking, and its conditions are very simple - the page is allowed to be embedded, and there are interactive functions on the page.
So, the decision of whether to block or not is not based on whether anyone is embedded, but on whether there are interactive actions on this page.
The applicable scope of each of the two response headers.
The two reliable values of X-Frame-Options: DENY means the document cannot be loaded in any frame, regardless of same origin or cross-origin; SAMEORIGIN means that embedding is only allowed if all上级 frames are of the same origin with this page. The writing style of ALLOW-FROM specifying the source has been deprecated, and the specification document clearly states that modern browsers will completely ignore the response header containing this instruction - that is to say, once ALLOW-FROM is written, the entire intercept is equivalent to not being added, rather than degraded to only allowing that source.
To express 'Allow embedding from some sources', you need to use the frame-ancestors directive in the content security policy header, which defines 'which parent pages can embed this page using frame, iframe, object, or embed'. The document also hints that when a more comprehensive choice than X-Frame-Options is needed, frame-ancestors should be used instead.
Embedding strategy table by page type
| Page Type | Suggested tier | Reason |
|---|---|---|
| Background and login-related pages | DENY | Only direct access is allowed, any nesting is unreasonable |
| Interactive form page for messages, comments, etc. | DENY or SAMEORIGIN | Forms are most easily borrowed in nested states. |
| Product and business handling page. | SAMEORIGIN | Allow internal frames of the site, deny external sites. |
| Content pages that need to be distributed externally. | frame-ancestors specify the origin. | Allow partners to embed, while tightening the scope of origins. |
A common mistake in writing policies is 'one-size-fits-all DENY' which also closes legitimate distribution scenarios, or vice versa, write ALLOW-FROM for convenience but think it has already cleared the whitelist.
The existing injection protection in the station does not reach this layer
Content sites usually consider injection risks as basic items: AnQiCMS内置JWT authentication, content sensitive word filtering and anti-crawling interference code, used to prevent SQL injection and XSS attacks; comments and reviews also have content review and sensitive word filtering before being published. These two layers handle whether the submitted content can be executed and can be published.
The issue of nesting is the opposite: the content itself is clean, but the page appears in someone else's frame, and the user and context are no longer controllable. Therefore, it cannot be solved by sensitive word filtering or content review, but only by the source declaration strategy in the response header. Conversely, if the site adds headers at the reverse proxy or server layer, it is necessary to confirm whether the three types of pages (background, form pages, distribution pages) use the same configuration - classifying by path is more cost-effective than one configuration for the entire site.
Common questions
Question: Is it feasible to add a script to the top of the page to prohibit nesting?Answer: It is not feasible as a defense line. The script needs to be executed to take effect, while the nested page can first lay out the outer styles and occlusion; the declaration must take effect as soon as the response reaches the browser, that is, written in the response header.
Question: Can two response headers be written at the same time?Answer: Yes. During the transition period, it is a common practice to issue them simultaneously, using X-Frame-Options to cover the old environments that do not support fine-grained instructions, and using frame-ancestors to express the real strategy; but it is necessary to confirm that the semantics of both do not conflict, to avoid one DENY and one allowing.
Ask: How can you judge if your embedding function fails after adding the interceptor?First, see which layer fails: The same-origin page embedded in the site page should be exempt from SAMEORIGIN; if a third-party component puts content into the frame for display, it belongs to the distribution scenario, and it should be tightened by origin instead of being turned off all at once.