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条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备