dqyl2374 2018-10-23 13:39
浏览 35
已采纳

如何说服Zend Framework发送重复的标题?

With Content-Security-Policy headers there is often a need to send more than one such header or to union merge these headers before sending them. This arises from the fact that each module/package of an application may define its own CSP.

Right now ZF3 doesn't seem to have a way to handle such a scenario. If I try to add multple CSP headers, they keep overwriting each other so that only the last added header is sent.

Code to reproduce the issue

$headers = $controller->getResponse()->getHeaders();
$headers->addHeader(new ContentSecurityPolicy($someDirectives));
$headers->addHeader(new ContentSecurityPolicy($someOtherDirectives));

Expected results

The expected result is a response with two CSP headers (OR a union merged CSP).

Actual results

The second addition overwrites the first, the response only contains that one CSP.

Question

How can I make ZF3 send multple headers with the same fieldname?


For more information about this problem, also see my own issue on github https://github.com/zendframework/zend-http/issues/159

  • 写回答

3条回答 默认 最新

  • douao2019 2018-11-03 13:52
    关注

    You should be able to create a simple workaround using GenericMultipleHeader as a reference (and changing comma delimiter to semicolon):

    class MultiContentSecurityPolicy extends ContentSecurityPolicy implements MultipleHeaderInterface {
    
        public static function fromString($headerLine)
        {
            list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine);
            if (strpos($fieldValue, ';')) {
                $headers = [];
                foreach (explode(';', $fieldValue) as $multiValue) {
                    $headers[] = new static($fieldName, $multiValue);
                }
                return $headers;
            } else {
                $header = new static($fieldName, $fieldValue);
                return $header;
            }
        }
    
        public function toStringMultipleHeaders(array $headers)
        {
            $name  = $this->getFieldName();
            $values = [$this->getFieldValue()];
            foreach ($headers as $header) {
                if (! $header instanceof static) {
                    throw new Exception\InvalidArgumentException(
                        'This method toStringMultipleHeaders was expecting an array of headers of the same type'
                    );
                }
                $values[] = $header->getFieldValue();
            }
            return $name . ': ' . implode(';', $values) . "
    ";
        }
    
    }
    

    Then use that class instead of ContentSecurityPolicy:

    $headers = $controller->getResponse()->getHeaders();
    $headers->addHeader(new MultiContentSecurityPolicy($someDirectives));
    $headers->addHeader(new MultiContentSecurityPolicy($someOtherDirectives));
    

    Since Zend checks the interface rather than the class, should work fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元