Skip to content

[HttpKernel] [FrameworkBundle] Introduce Rails-like default response headers to control browser-side security features #8515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function getConfigTreeBuilder()
$this->addValidationSection($rootNode);
$this->addAnnotationsSection($rootNode);
$this->addSerializerSection($rootNode);
$this->addResponseSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -414,4 +415,40 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode)
->end()
;
}

private function addResponseSection(ArrayNodeDefinition $rootNode)
{
$default = array(
'X-Content-Type-Options' => 'nosniff',
);

$example = array(
'X-Content-Type-Options' => 'nosniff',
'X-XSS-Protection' => '1; mode=block',
'X-Frame-Options' => 'SAMEORIGIN',
);

$rootNode
->children()
->arrayNode('response')
->info('response configuration')
->addDefaultsIfNotSet()
->fixXmlConfig('default_header')
->children()
->arrayNode('default_headers')
->normalizeKeys(false)
->useAttributeAsKey('name')
->example($example)
->defaultValue($default)
->beforeNormalization()
->ifNull()
->then(function($v) use ($default) { return $default; })
->end()
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('kernel.trusted_proxies', $config['trusted_proxies']);
$container->setParameter('kernel.default_locale', $config['default_locale']);

if (isset($config['response'])) {
$container->setParameter('kernel.default_response_headers', $config['response']['default_headers']);
}

if (!empty($config['test'])) {
$loader->load('test.xml');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<xsd:element name="translator" type="translator" minOccurs="0" maxOccurs="1" />
<xsd:element name="validation" type="validation" minOccurs="0" maxOccurs="1" />
<xsd:element name="annotations" type="annotations" minOccurs="0" maxOccurs="1" />
<xsd:element name="response" type="response" minOccurs="0" maxOccurs="1" />
</xsd:all>

<xsd:attribute name="http-method-override" type="xsd:boolean" />
Expand All @@ -30,6 +31,16 @@
<xsd:attribute name="test" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="response">
<xsd:sequence>
<xsd:element name="default-header" type="default-header" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="default-header" mixed="true">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="form">
<xsd:attribute name="enabled" type="xsd:boolean" />
</xsd:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<service id="response_listener" class="%response_listener.class%">
<tag name="kernel.event_subscriber" />
<argument>%kernel.charset%</argument>
<argument>%kernel.default_response_headers%</argument>
</service>

<service id="streamed_response_listener" class="%streamed_response_listener.class%">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ protected static function getBundleDefaultConfig()
),
'serializer' => array(
'enabled' => false
)
),
'response' => array(
'default_headers' => array(
'X-Content-Type-Options' => 'nosniff',
),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,11 @@
'debug' => true,
'file_cache_dir' => '%kernel.cache_dir%/annotations',
),
'ide' => 'file%%link%%format'
'ide' => 'file%%link%%format',
'response' => array(
'default_headers' => array(
'X-Foo' => 'Bar',
'X-Bar' => 'Foo',
),
),
));
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@
<framework:translator enabled="true" fallback="fr" />
<framework:validation enabled="true" cache="apc" />
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />
<framework:response>
<framework:default-header name="X-Foo">Bar</framework:default-header>
<framework:default-header name="X-Bar">Foo</framework:default-header>
</framework:response>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ framework:
debug: true
file_cache_dir: %kernel.cache_dir%/annotations
ide: file%%link%%format
response:
default_headers:
"X-Foo" : "Bar"
"X-Bar" : "Foo"
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,17 @@ public function testValidationPaths()
$this->assertStringEndsWith('TestBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'validation.xml', $xmlArgs[1]);
}

public function testDefaultResponseHeader()
{
$container = $this->createContainerFromFile('full');

$this->assertTrue($container->hasParameter('kernel.default_response_headers'), '->registerValidationConfiguration() loads validator.xml');
$this->assertEquals(array(
'X-Foo' => 'Bar',
'X-Bar' => 'Foo',
), $container->getParameter('kernel.default_response_headers'), '->registerValidationConfiguration() loads validator.xml');
}

protected function createContainer(array $data = array())
{
return new ContainerBuilder(new ParameterBag(array_merge(array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ class ResponseListener implements EventSubscriberInterface
{
private $charset;

public function __construct($charset)
private $defaultHeaders = array();

public function __construct($charset, $defaultHeaders = array())
{
$this->charset = $charset;
$this->defaultHeaders = $defaultHeaders;
}

/**
Expand All @@ -42,6 +45,9 @@ public function onKernelResponse(FilterResponseEvent $event)
}

$response = $event->getResponse();
foreach ($this->defaultHeaders as $key => $value) {
$response->headers->set($key, $value, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will replace any custom header set in the controller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, this method call sets false as the third argument ($replace), and it is already tested the existing header wouldn't be overwritten.

}

if (null === $response->getCharset()) {
$response->setCharset($this->charset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,23 @@ public function testFiltersSetsNonDefaultCharsetIfNotOverriddenOnNonTextContentT

$this->assertEquals('ISO-8859-15', $response->getCharset());
}

public function testFiltersSetsDefaultHeaders()
{
$listener = new ResponseListener('UTF-8', array(
'X-Add' => 'default-value',
'X-Not-Add' => 'default-value',
));
$this->dispatcher->addListener(KernelEvents::RESPONSE, array($listener, 'onKernelResponse'), 1);

$response = new Response();
$response->headers->set('X-Not-Add', 'user-value');

$request = Request::create('/');
$event = new FilterResponseEvent($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response);
$this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);

$this->assertEquals('default-value', $response->headers->get('X-Add'));
$this->assertEquals('user-value', $response->headers->get('X-Not-Add'));
}
}