This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
+10
-10
@@ -46,13 +46,13 @@ abstract class AbstractSurrogate implements SurrogateInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
return str_contains($value, sprintf('%s/1.0', strtoupper($this->getName())));
|
||||
return str_contains($value, \sprintf('%s/1.0', strtoupper($this->getName())));
|
||||
}
|
||||
|
||||
public function addSurrogateCapability(Request $request): void
|
||||
{
|
||||
$current = $request->headers->get('Surrogate-Capability');
|
||||
$new = sprintf('symfony="%s/1.0"', strtoupper($this->getName()));
|
||||
$new = \sprintf('symfony="%s/1.0"', strtoupper($this->getName()));
|
||||
|
||||
$request->headers->set('Surrogate-Capability', $current ? $current.', '.$new : $new);
|
||||
}
|
||||
@@ -63,20 +63,20 @@ abstract class AbstractSurrogate implements SurrogateInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
$pattern = sprintf('#content="[^"]*%s/1.0[^"]*"#', strtoupper($this->getName()));
|
||||
$pattern = \sprintf('#content="[^"]*%s/1.0[^"]*"#', strtoupper($this->getName()));
|
||||
|
||||
return (bool) preg_match($pattern, $control);
|
||||
}
|
||||
|
||||
public function handle(HttpCache $cache, string $uri, string $alt, bool $ignoreErrors): string
|
||||
{
|
||||
$subRequest = Request::create($uri, Request::METHOD_GET, [], $cache->getRequest()->cookies->all(), [], $cache->getRequest()->server->all());
|
||||
$subRequest = Request::create($uri, 'GET', [], $cache->getRequest()->cookies->all(), [], $cache->getRequest()->server->all());
|
||||
|
||||
try {
|
||||
$response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
|
||||
|
||||
if (!$response->isSuccessful() && Response::HTTP_NOT_MODIFIED !== $response->getStatusCode()) {
|
||||
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %d).', $subRequest->getUri(), $response->getStatusCode()));
|
||||
throw new \RuntimeException(\sprintf('Error when rendering "%s" (Status code is %d).', $subRequest->getUri(), $response->getStatusCode()));
|
||||
}
|
||||
|
||||
return $response->getContent();
|
||||
@@ -105,12 +105,12 @@ abstract class AbstractSurrogate implements SurrogateInterface
|
||||
$value = $response->headers->get('Surrogate-Control');
|
||||
$upperName = strtoupper($this->getName());
|
||||
|
||||
if (sprintf('content="%s/1.0"', $upperName) == $value) {
|
||||
if (\sprintf('content="%s/1.0"', $upperName) == $value) {
|
||||
$response->headers->remove('Surrogate-Control');
|
||||
} elseif (preg_match(sprintf('#,\s*content="%s/1.0"#', $upperName), $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace(sprintf('#,\s*content="%s/1.0"#', $upperName), '', $value));
|
||||
} elseif (preg_match(sprintf('#content="%s/1.0",\s*#', $upperName), $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace(sprintf('#content="%s/1.0",\s*#', $upperName), '', $value));
|
||||
} elseif (preg_match(\sprintf('#,\s*content="%s/1.0"#', $upperName), $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace(\sprintf('#,\s*content="%s/1.0"#', $upperName), '', $value));
|
||||
} elseif (preg_match(\sprintf('#content="%s/1.0",\s*#', $upperName), $value)) {
|
||||
$response->headers->set('Surrogate-Control', preg_replace(\sprintf('#content="%s/1.0",\s*#', $upperName), '', $value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpKernel\HttpCache;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class CacheWasLockedException extends \Exception
|
||||
{
|
||||
}
|
||||
+3
-3
@@ -41,14 +41,14 @@ class Esi extends AbstractSurrogate
|
||||
|
||||
public function renderIncludeTag(string $uri, ?string $alt = null, bool $ignoreErrors = true, string $comment = ''): string
|
||||
{
|
||||
$html = sprintf('<esi:include src="%s"%s%s />',
|
||||
$html = \sprintf('<esi:include src="%s"%s%s />',
|
||||
$uri,
|
||||
$ignoreErrors ? ' onerror="continue"' : '',
|
||||
$alt ? sprintf(' alt="%s"', $alt) : ''
|
||||
$alt ? \sprintf(' alt="%s"', $alt) : ''
|
||||
);
|
||||
|
||||
if ($comment) {
|
||||
return sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html);
|
||||
return \sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html);
|
||||
}
|
||||
|
||||
return $html;
|
||||
|
||||
+34
-20
@@ -17,6 +17,7 @@
|
||||
|
||||
namespace Symfony\Component\HttpKernel\HttpCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
@@ -35,6 +36,8 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
private ?ResponseCacheStrategyInterface $surrogateCacheStrategy = null;
|
||||
private array $options = [];
|
||||
private array $traces = [];
|
||||
private ?Request $forwardedRequest = null;
|
||||
private ?Request $backendRequest = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -87,7 +90,6 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
private ?SurrogateInterface $surrogate = null,
|
||||
array $options = [],
|
||||
) {
|
||||
|
||||
// needed in case there is a fatal error because the backend is too slow to respond
|
||||
register_shutdown_function($this->store->cleanup(...));
|
||||
|
||||
@@ -149,7 +151,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
{
|
||||
$log = [];
|
||||
foreach ($this->traces as $request => $traces) {
|
||||
$log[] = sprintf('%s: %s', $request, implode(', ', $traces));
|
||||
$log[] = \sprintf('%s: %s', $request, implode(', ', $traces));
|
||||
}
|
||||
|
||||
return implode('; ', $log);
|
||||
@@ -186,6 +188,8 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
// FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism
|
||||
if (HttpKernelInterface::MAIN_REQUEST === $type) {
|
||||
$this->traces = [];
|
||||
$this->forwardedRequest = null;
|
||||
$this->backendRequest = null;
|
||||
// Keep a clone of the original request for surrogates so they can access it.
|
||||
// We must clone here to get a separate instance because the application will modify the request during
|
||||
// the application flow (we know it always does because we do ourselves by setting REMOTE_ADDR to 127.0.0.1
|
||||
@@ -210,7 +214,19 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$this->record($request, 'reload');
|
||||
$response = $this->fetch($request, $catch);
|
||||
} else {
|
||||
$response = $this->lookup($request, $catch);
|
||||
$response = null;
|
||||
do {
|
||||
try {
|
||||
$response = $this->lookup($request, $catch);
|
||||
} catch (CacheWasLockedException) {
|
||||
}
|
||||
} while (null === $response);
|
||||
}
|
||||
|
||||
if (HttpKernelInterface::MAIN_REQUEST === $type) {
|
||||
// Expose the request actually handled by the backend (a sub-request on a cache miss)
|
||||
// to kernel.terminate listeners, as would happen behind a real reverse proxy.
|
||||
$this->backendRequest = $this->forwardedRequest ?? $request;
|
||||
}
|
||||
|
||||
$this->restoreResponseBody($request, $response);
|
||||
@@ -246,7 +262,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
}
|
||||
|
||||
if ($this->getKernel() instanceof TerminableInterface) {
|
||||
$this->getKernel()->terminate($request, $response);
|
||||
$this->getKernel()->terminate($this->backendRequest ?? $request, $response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -458,6 +474,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
|
||||
// always a "master" request (as the real master request can be in cache)
|
||||
$response = SubRequestHandler::handle($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST, $catch);
|
||||
$this->forwardedRequest = $request;
|
||||
|
||||
/*
|
||||
* Support stale-if-error given on Responses or as a config option.
|
||||
@@ -476,7 +493,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
* stale-if-error case even if they have a `s-maxage` Cache-Control directive.
|
||||
*/
|
||||
if (null !== $entry
|
||||
&& \in_array($response->getStatusCode(), [500, 502, 503, 504])
|
||||
&& \in_array($response->getStatusCode(), [500, 502, 503, 504], true)
|
||||
&& !$entry->headers->hasCacheControlDirective('no-cache')
|
||||
&& !$entry->mustRevalidate()
|
||||
) {
|
||||
@@ -558,23 +575,16 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->record($request, 'waiting');
|
||||
|
||||
// wait for the lock to be released
|
||||
if ($this->waitForLock($request)) {
|
||||
// replace the current entry with the fresh one
|
||||
$new = $this->lookup($request);
|
||||
$entry->headers = $new->headers;
|
||||
$entry->setContent($new->getContent());
|
||||
$entry->setStatusCode($new->getStatusCode());
|
||||
$entry->setProtocolVersion($new->getProtocolVersion());
|
||||
foreach ($new->headers->getCookies() as $cookie) {
|
||||
$entry->headers->setCookie($cookie);
|
||||
}
|
||||
} else {
|
||||
// backend is slow as hell, send a 503 response (to avoid the dog pile effect)
|
||||
$entry->setStatusCode(503);
|
||||
$entry->setContent('503 Service Unavailable');
|
||||
$entry->headers->set('Retry-After', 10);
|
||||
throw new CacheWasLockedException(); // unwind back to handle(), try again
|
||||
}
|
||||
// backend is slow as hell, send a 503 response (to avoid the dog pile effect)
|
||||
$entry->setStatusCode(503);
|
||||
$entry->setContent('503 Service Unavailable');
|
||||
$entry->headers->set('Retry-After', 10);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -705,7 +715,11 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
$path .= '?'.$qs;
|
||||
}
|
||||
|
||||
return $request->getMethod().' '.$path;
|
||||
try {
|
||||
return $request->getMethod().' '.$path;
|
||||
} catch (SuspiciousOperationException) {
|
||||
return '_BAD_METHOD_ '.$path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+28
-30
@@ -51,7 +51,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
private array $ageDirectives = [
|
||||
'max-age' => null,
|
||||
's-maxage' => null,
|
||||
'expires' => null,
|
||||
'expires' => false,
|
||||
];
|
||||
|
||||
public function add(Response $response): void
|
||||
@@ -79,15 +79,30 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
return;
|
||||
}
|
||||
|
||||
$isHeuristicallyCacheable = $response->headers->hasCacheControlDirective('public');
|
||||
$maxAge = $response->headers->hasCacheControlDirective('max-age') ? (int) $response->headers->getCacheControlDirective('max-age') : null;
|
||||
$this->storeRelativeAgeDirective('max-age', $maxAge, $age, $isHeuristicallyCacheable);
|
||||
$sharedMaxAge = $response->headers->hasCacheControlDirective('s-maxage') ? (int) $response->headers->getCacheControlDirective('s-maxage') : $maxAge;
|
||||
$this->storeRelativeAgeDirective('s-maxage', $sharedMaxAge, $age, $isHeuristicallyCacheable);
|
||||
|
||||
$expires = $response->getExpires();
|
||||
$expires = null !== $expires ? (int) $expires->format('U') - (int) $response->getDate()->format('U') : null;
|
||||
$this->storeRelativeAgeDirective('expires', $expires >= 0 ? $expires : null, 0, $isHeuristicallyCacheable);
|
||||
|
||||
// See https://datatracker.ietf.org/doc/html/rfc7234#section-4.2.2
|
||||
// If a response is "public" but does not have maximum lifetime, heuristics might be applied.
|
||||
// Do not store NULL values so the final response can have more limiting value from other responses.
|
||||
$isHeuristicallyCacheable = $response->headers->hasCacheControlDirective('public')
|
||||
&& null === $maxAge
|
||||
&& null === $sharedMaxAge
|
||||
&& null === $expires;
|
||||
|
||||
if (!$isHeuristicallyCacheable || null !== $maxAge || null !== $expires) {
|
||||
$this->storeRelativeAgeDirective('max-age', $maxAge, $expires, $age);
|
||||
}
|
||||
|
||||
if (!$isHeuristicallyCacheable || null !== $sharedMaxAge || null !== $expires) {
|
||||
$this->storeRelativeAgeDirective('s-maxage', $sharedMaxAge, $expires, $age);
|
||||
}
|
||||
|
||||
if (null !== $expires) {
|
||||
$this->ageDirectives['expires'] = true;
|
||||
}
|
||||
|
||||
if (false !== $this->lastModified) {
|
||||
$lastModified = $response->getLastModified();
|
||||
@@ -146,9 +161,9 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
}
|
||||
}
|
||||
|
||||
if (is_numeric($this->ageDirectives['expires'])) {
|
||||
if ($this->ageDirectives['expires'] && null !== $maxAge) {
|
||||
$date = clone $response->getDate();
|
||||
$date = $date->modify('+'.($this->ageDirectives['expires'] + $this->age).' seconds');
|
||||
$date = $date->modify('+'.$maxAge.' seconds');
|
||||
$response->setExpires($date);
|
||||
}
|
||||
}
|
||||
@@ -170,7 +185,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
|
||||
// Etag headers cannot be merged, they render the response uncacheable
|
||||
// by default (except if the response also has max-age etc.).
|
||||
if (null === $response->getEtag() && \in_array($response->getStatusCode(), [200, 203, 300, 301, 410])) {
|
||||
if (null === $response->getEtag() && \in_array($response->getStatusCode(), [200, 203, 300, 301, 410], true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -198,33 +213,16 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
* we have to subtract the age so that the value is normalized for an age of 0.
|
||||
*
|
||||
* If the value is lower than the currently stored value, we update the value, to keep a rolling
|
||||
* minimal value of each instruction.
|
||||
*
|
||||
* If the value is NULL and the isHeuristicallyCacheable parameter is false, the directive will
|
||||
* not be set on the final response. In this case, not all responses had the directive set and no
|
||||
* value can be found that satisfies the requirements of all responses. The directive will be dropped
|
||||
* from the final response.
|
||||
*
|
||||
* If the isHeuristicallyCacheable parameter is true, however, the current response has been marked
|
||||
* as cacheable in a public (shared) cache, but did not provide an explicit lifetime that would serve
|
||||
* as an upper bound. In this case, we can proceed and possibly keep the directive on the final response.
|
||||
* minimal value of each instruction. If the value is NULL, the directive will not be set on the final response.
|
||||
*/
|
||||
private function storeRelativeAgeDirective(string $directive, ?int $value, int $age, bool $isHeuristicallyCacheable): void
|
||||
private function storeRelativeAgeDirective(string $directive, ?int $value, ?int $expires, int $age): void
|
||||
{
|
||||
if (null === $value) {
|
||||
if ($isHeuristicallyCacheable) {
|
||||
/*
|
||||
* See https://datatracker.ietf.org/doc/html/rfc7234#section-4.2.2
|
||||
* This particular response does not require maximum lifetime; heuristics might be applied.
|
||||
* Other responses, however, might have more stringent requirements on maximum lifetime.
|
||||
* So, return early here so that the final response can have the more limiting value set.
|
||||
*/
|
||||
return;
|
||||
}
|
||||
if (null === $value && null === $expires) {
|
||||
$this->ageDirectives[$directive] = false;
|
||||
}
|
||||
|
||||
if (false !== $this->ageDirectives[$directive]) {
|
||||
$value = min($value ?? \PHP_INT_MAX, $expires ?? \PHP_INT_MAX);
|
||||
$value -= $age;
|
||||
$this->ageDirectives[$directive] = null !== $this->ageDirectives[$directive] ? min($this->ageDirectives[$directive], $value) : $value;
|
||||
}
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ class Ssi extends AbstractSurrogate
|
||||
|
||||
public function renderIncludeTag(string $uri, ?string $alt = null, bool $ignoreErrors = true, string $comment = ''): string
|
||||
{
|
||||
return sprintf('<!--#include virtual="%s" -->', $uri);
|
||||
return \sprintf('<!--#include virtual="%s" -->', $uri);
|
||||
}
|
||||
|
||||
public function process(Request $request, Response $response): Response
|
||||
|
||||
+18
-14
@@ -43,8 +43,8 @@ class Store implements StoreInterface
|
||||
protected string $root,
|
||||
private array $options = [],
|
||||
) {
|
||||
if (!is_dir($this->root) && !@mkdir($this->root, 0777, true) && !is_dir($this->root)) {
|
||||
throw new \RuntimeException(sprintf('Unable to create the store directory (%s).', $this->root));
|
||||
if (!is_dir($this->root) && !@mkdir($this->root, 0o777, true) && !is_dir($this->root)) {
|
||||
throw new \RuntimeException(\sprintf('Unable to create the store directory (%s).', $this->root));
|
||||
}
|
||||
$this->keyCache = new \SplObjectStorage();
|
||||
$this->options['private_headers'] ??= ['Set-Cookie'];
|
||||
@@ -75,7 +75,7 @@ class Store implements StoreInterface
|
||||
|
||||
if (!isset($this->locks[$key])) {
|
||||
$path = $this->getPath($key);
|
||||
if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
|
||||
if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0o777, true) && !is_dir(\dirname($path))) {
|
||||
return $path;
|
||||
}
|
||||
$h = fopen($path, 'c');
|
||||
@@ -206,13 +206,9 @@ class Store implements StoreInterface
|
||||
|
||||
// read existing cache entries, remove non-varying, and add this one to the list
|
||||
$entries = [];
|
||||
$vary = $response->headers->get('vary');
|
||||
$vary = implode(', ', $response->headers->all('vary'));
|
||||
foreach ($this->getMetadata($key) as $entry) {
|
||||
if (!isset($entry[1]['vary'][0])) {
|
||||
$entry[1]['vary'] = [''];
|
||||
}
|
||||
|
||||
if ($entry[1]['vary'][0] != $vary || !$this->requestsMatch($vary ?? '', $entry[0], $storedEnv)) {
|
||||
if (!$this->requestsMatch($vary ?? '', $entry[0], $storedEnv)) {
|
||||
$entries[] = $entry;
|
||||
}
|
||||
}
|
||||
@@ -278,7 +274,7 @@ class Store implements StoreInterface
|
||||
*/
|
||||
private function requestsMatch(?string $vary, array $env1, array $env2): bool
|
||||
{
|
||||
if (!$vary) {
|
||||
if ('' === ($vary ?? '')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -305,7 +301,7 @@ class Store implements StoreInterface
|
||||
return [];
|
||||
}
|
||||
|
||||
return unserialize($entries) ?: [];
|
||||
return unserialize($entries, ['allowed_classes' => false]) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,7 +375,7 @@ class Store implements StoreInterface
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
|
||||
if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0o777, true) && !is_dir(\dirname($path))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -405,7 +401,7 @@ class Store implements StoreInterface
|
||||
}
|
||||
}
|
||||
|
||||
@chmod($path, 0666 & ~umask());
|
||||
@chmod($path, 0o666 & ~umask());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -427,7 +423,15 @@ class Store implements StoreInterface
|
||||
*/
|
||||
protected function generateCacheKey(Request $request): string
|
||||
{
|
||||
return 'md'.hash('sha256', $request->getUri());
|
||||
$key = $request->getUri();
|
||||
|
||||
if ('QUERY' === $request->getMethod()) {
|
||||
// add null byte to separate the URI from the body and avoid boundary collisions
|
||||
// which could lead to cache poisoning
|
||||
$key .= "\0".$request->getContent();
|
||||
}
|
||||
|
||||
return 'md'.hash('sha256', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -51,16 +51,16 @@ class SubRequestHandler
|
||||
$trustedValues = [];
|
||||
foreach (array_reverse($request->getClientIps()) as $ip) {
|
||||
$trustedIps[] = $ip;
|
||||
$trustedValues[] = sprintf('for="%s"', $ip);
|
||||
$trustedValues[] = \sprintf('for="%s"', $ip);
|
||||
}
|
||||
if ($ip !== $remoteAddr) {
|
||||
$trustedIps[] = $remoteAddr;
|
||||
$trustedValues[] = sprintf('for="%s"', $remoteAddr);
|
||||
$trustedValues[] = \sprintf('for="%s"', $remoteAddr);
|
||||
}
|
||||
|
||||
// set trusted values, reusing as much as possible the global trusted settings
|
||||
if (Request::HEADER_FORWARDED & $trustedHeaderSet) {
|
||||
$trustedValues[0] .= sprintf(';host="%s";proto=%s', $request->getHttpHost(), $request->getScheme());
|
||||
$trustedValues[0] .= \sprintf(';host="%s";proto=%s', $request->getHttpHost(), $request->getScheme());
|
||||
$request->headers->set('Forwarded', $v = implode(', ', $trustedValues));
|
||||
$request->server->set('HTTP_FORWARDED', $v);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user