简体   繁体   English

预告片标头中的 Content-Security-Policy 标头不起作用

[英]Content-Security-Policy Header in trailer header isn't working

I setup a simple nodejs http-server that stream-processes an html request.我设置了一个简单的 nodejs http 服务器,用于流式处理 html 请求。 As the html is streamed, it extracts any inline content into a seperate element and calculates its hashes.当 html 被流式传输时,它会将任何内联内容提取到一个单独的元素中并计算其哈希值。 In the final step, a trailer-header is senc containing the original csp + the new hashes.在最后一步中,包含原始 csp + 新哈希的预告片头是 senc。

However, the browser (all: Chrome, Firefox, Edge) does not honour the csp!但是,浏览器(所有:Chrome、Firefox、Edge)不支持 csp!

The above in pseudo-code (node-js like):上面的伪代码(类似于 node-js):

const server = http.createServer((reqest, response) => {
    response.setHeader('Transfer-Encoding', 'chunked');
    response.setHeader('content-type', 'text/html');
    response.setHeader('Trailer', 'content-security-policy');
    const stream = getHTMLSAXStream();
    stream.on('data', function(element) {
        // extract inlines and save, e.g. styles.push(element.style);
        // then remove the attr: e.g. element.style = undefined;
        // then on the stripped html: e.g. response.write(element.toHTML())
    });
    stream.on('end', function() {
        const stylefile= `${styles.join("\n")}`;
        const url = getUrlForString(stylefile); // make this file available on a temporary url
        response.write(`<link rel="stylesheet" href="${url}">`)
        response.addTrailers({ 'content-security-policy': mergeWithDefaultCSP("style-src: sha256-${sha256(stylefile)}") });
        response.end(); // send response
    })

As per MDN docs on trailers some headers are disallowed, however couldn't find a reason why the content-security-policy shouldn't be allowed.根据预告片上的 MDN 文档,不允许使用某些标题,但找不到不应允许内容安全策略的原因。 More specifically, as per trailer header and csp spec :更具体地说,根据预告片标题csp 规范

A sender MUST NOT generate a trailer that contains a field necessary for message framing (eg, Transfer-Encoding and Content-Length), routing (eg, Host), request modifiers (eg, controls and conditionals in Section 5 of [RFC7231]), authentication (eg, see [RFC7235] and [RFC6265]), response control data (eg, see Section 7.1 of [RFC7231]), or determining how to process the payload (eg, Content-Encoding, Content-Type, Content-Range, and Trailer).发送方不得生成包含消息帧(例如,传输编码和内容长度)、路由(例如,主机)、请求修饰符(例如,[RFC7231] 的第 5 节中的控制和条件)所必需的字段的尾部、身份验证(例如,参见 [RFC7235] 和 [RFC6265])、响应控制数据(例如,参见 [RFC7231] 的第 7.1 节),或确定如何处理有效载荷(例如,内容编码、内容类型、内容-范围和预告片)。

The CSP is not used for message framing, it is not used for routing, it is not used as a request modifier, not used for authentication and isn't used for processing the payload (only used after processing the payload, aka the html) - in short, I don't see a reason it shouldn't work! CSP 不用于消息成帧,不用于路由,不用作请求修饰符,不用于身份验证,也不用于处理负载(仅在处理负载后使用,也称为 html) - 简而言之,我看不出它不应该工作的原因!

Does anyone know more?有人知道更多吗? Have I missed anything?我错过了什么吗?

To get around this, currently I'm using the following workaround (I'd like to get rid of):为了解决这个问题,目前我正在使用以下解决方法(我想摆脱):

  • don't use hashes, whitelist by domain (eg all scripts are coming from the same domain)不要使用哈希,按域列入白名单(例如,所有脚本都来自同一个域)
  • use nounces instead of hashes (won't play well with cdns though)使用 nounces 而不是散列(尽管与 cdns 配合得不好)

More on the background, why am I doing this at all: I have a cms that allows using raw html (incl. inline-styles and script tags) which I frequently use (else I'd need to deploy again, etc etc).更多关于背景的信息,我为什么要这样做:我有一个允许使用我经常使用的原始 html(包括内联样式和脚本标签)的 cms(否则我需要再次部署等)。 On the other hand I'd like a good working CSP (eg when user-generated comments are loaded onto the page from an api with javascript (not in the backend, that would defeat the purpose!), just in case. Therefore I'd like to allow only my own inline-style and script tags, but no-others. The above addresses this adequatly.另一方面,我想要一个好的工作 CSP(例如,当用户生成的评论从带有 javascript 的 api 加载到页面上时(不在后端,这会破坏目的!),以防万一。因此我我想只允许我自己的 inline-style 和 script 标签,但不允许其他标签。上面充分解决了这个问题。

No matter what the spec says, it's up to browsers to support this and last I header browser support for trailing headers is very limited: Do any browsers support trailers sent in chunked encoding responses?无论规范怎么说,这取决于浏览器是否支持这一点,而最后一个 I 标头浏览器对尾随标头的支持非常有限: 是否有任何浏览器支持以分块编码响应发送的尾标

Additionally I don't think it makes sense for CSP for two reasons:此外,我认为这对 CSP 没有意义,原因有两个:

  1. HTML is often streamed (as you state you are doing) and the browser will render as the HTML comes in. To then retrospectively apply the CSP to already rendered content would be pointless - the damage has been done. HTML 通常是流式传输的(正如您所说的那样)并且浏览器将在 HTML 进入时呈现。然后将 CSP 追溯应用于已呈现的内容将毫无意义 - 损害已经造成。

  2. Multiple CSPs are additive and not replacing.多个 CSP 是附加的而不是替代的。 Ie it the most restrictive CSP that matters.即它是最重要的限制性 CSP。 So if you've a basic CSP and then want to add a nonce that is not possible AFAIK.因此,如果您有一个基本的 CSP,然后想添加一个 AFAIK 不可能的随机数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM