Skip to content

Commit

Permalink
[HttpKernel] Ensure HttpCache::getTraceKey() does not throw exception
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx authored and nicolas-grekas committed Nov 20, 2024
1 parent 91c97d9 commit 455dfd3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -715,7 +716,11 @@ private function getTraceKey(Request $request): string
$path .= '?'.$qs;
}

return $request->getMethod().' '.$path;
try {
return $request->getMethod().' '.$path;
} catch (SuspiciousOperationException $e) {
return '_BAD_METHOD_ '.$path;
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public function testPassesOnNonGetHeadRequests()
$this->assertFalse($this->response->headers->has('Age'));
}

public function testPassesSuspiciousMethodRequests()
{
$this->setNextResponse(200);
$this->request('POST', '/', ['HTTP_X-HTTP-Method-Override' => '__CONSTRUCT']);
$this->assertHttpKernelIsCalled();
$this->assertResponseOk();
$this->assertTraceNotContains('stale');
$this->assertTraceNotContains('invalid');
$this->assertFalse($this->response->headers->has('Age'));
}

public function testInvalidatesOnPostPutDeleteRequests()
{
foreach (['post', 'put', 'delete'] as $method) {
Expand Down

0 comments on commit 455dfd3

Please sign in to comment.