Skip to content

Add support for safe HTTP preference - RFC 8674 #34847

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

Merged
merged 1 commit into from
Jan 10, 2020
Merged
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
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* Deprecate `Response::create()`, `JsonResponse::create()`,
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
`__construct()` instead)
* added `Request::preferSafeContent()` and `Response::setContentSafe()` to handle "safe" HTTP preference
according to [RFC 8674](https://tools.ietf.org/html/rfc8674)

5.0.0
-----
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ class Request
private $isHostValid = true;
private $isForwardedValid = true;

/**
* @var bool|null
*/
private $isSafeContentPreferred;

private static $trustedHeaderSet = -1;

private static $forwardedParams = [
Expand Down Expand Up @@ -1702,6 +1707,29 @@ public function isXmlHttpRequest()
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
}

/**
* Checks whether the client browser prefers safe content or not according to RFC8674.
*
* @see https://tools.ietf.org/html/rfc8674
*/
public function preferSafeContent(): bool
{
if (null !== $this->isSafeContentPreferred) {
return $this->isSafeContentPreferred;
}

if (!$this->isSecure()) {
// see https://tools.ietf.org/html/rfc8674#section-3
$this->isSafeContentPreferred = false;

return $this->isSafeContentPreferred;
}

$this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe');

return $this->isSafeContentPreferred;
}

/*
* The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24)
*
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,22 @@ public static function closeOutputBuffers(int $targetLevel, bool $flush): void
}
}

/**
* Mark a response as safe according to RFC8674.
*
* @see https://tools.ietf.org/html/rfc8674
*/
public function setContentSafe(bool $safe = true): void
{
if ($safe) {
$this->headers->set('Preference-Applied', 'safe');
} elseif ('safe' === $this->headers->get('Preference-Applied')) {
$this->headers->remove('Preference-Applied');
}

$this->setVary('Prefer', false);
}

/**
* Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9.
*
Expand Down
58 changes: 58 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2325,6 +2325,64 @@ public function trustedProxiesRemoteAddr()
[null, ['REMOTE_ADDR', '2.2.2.2'], ['2.2.2.2']],
];
}

/**
* @dataProvider preferSafeContentData
*/
public function testPreferSafeContent($server, bool $safePreferenceExpected)
{
$request = new Request([], [], [], [], [], $server);

$this->assertEquals($safePreferenceExpected, $request->preferSafeContent());
}

public function preferSafeContentData()
{
return [
[[], false],
[
[
'HTTPS' => 'on',
],
false,
],
[
[
'HTTPS' => 'off',
'HTTP_PREFER' => 'safe',
],
false,
],
[
[
'HTTPS' => 'on',
'HTTP_PREFER' => 'safe',
],
true,
],
[
[
'HTTPS' => 'on',
'HTTP_PREFER' => 'unknown-preference',
],
false,
],
[
[
'HTTPS' => 'on',
'HTTP_PREFER' => 'unknown-preference=42, safe',
],
true,
],
[
[
'HTTPS' => 'on',
'HTTP_PREFER' => 'safe, unknown-preference=42',
],
true,
],
];
}
}

class RequestContentProxy extends Request
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,24 @@ public function testReasonPhraseDefaultsAgainstIana($code, $reasonPhrase)
{
$this->assertEquals($reasonPhrase, Response::$statusTexts[$code]);
}

public function testSetContentSafe()
{
$response = new Response();

$this->assertFalse($response->headers->has('Preference-Applied'));
$this->assertFalse($response->headers->has('Vary'));

$response->setContentSafe();

$this->assertSame('safe', $response->headers->get('Preference-Applied'));
$this->assertSame('Prefer', $response->headers->get('Vary'));

$response->setContentSafe(false);

$this->assertFalse($response->headers->has('Preference-Applied'));
$this->assertSame('Prefer', $response->headers->get('Vary'));
}
}

class StringableObject
Expand Down