-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Don't use eval() to render ESI/SSI #50238
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -636,7 +636,21 @@ private function restoreResponseBody(Request $request, Response $response) | |
if ($response->headers->has('X-Body-File')) { | ||
include $response->headers->get('X-Body-File'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why still have a case relying on including a file here. Is this something we should also try to change ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or maybe combining There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need to change this: the required file cannot come from any user input, and we do validate the name of the file, so that arbitrary file inclusion is not possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, from my understanding of re-reading this code, this Do we have functional tests covering the case of a processing of ESI tags alongside a cached response of HttpCache ? Because I suspect that this is the case that is now broken (we would write the cache with boundaries instead of PHP code but read it as if it was PHP code). |
||
} else { | ||
eval('; ?>'.$response->getContent().'<?php ;'); | ||
$content = $response->getContent(); | ||
|
||
if (substr($content, -24) === $boundary = substr($content, 0, 24)) { | ||
$j = strpos($content, $boundary, 24); | ||
echo substr($content, 24, $j - 24); | ||
$i = $j + 24; | ||
|
||
while (false !== $j = strpos($content, $boundary, $i)) { | ||
[$uri, $alt, $ignoreErrors, $part] = explode("\n", substr($content, $i, $j - $i), 4); | ||
$i = $j + 24; | ||
|
||
echo $this->surrogate->handle($this, $uri, $alt, $ignoreErrors); | ||
echo $part; | ||
} | ||
} | ||
} | ||
|
||
$response->setContent(ob_get_clean()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,7 +102,7 @@ public function testMultilineEsiRemoveTagsAreRemoved() | |
$response = new Response('<esi:remove> <a href="http://www.example.com">www.example.com</a> </esi:remove> Keep this'."<esi:remove>\n <a>www.example.com</a> </esi:remove> And this"); | ||
$this->assertSame($response, $esi->process($request, $response)); | ||
|
||
$this->assertEquals(' Keep this And this', $response->getContent()); | ||
$this->assertEquals(' Keep this And this', substr($response->getContent(), 24, -24)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like a BC break not suitable in a patch release as projects might use the Esi class directly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a security-related fix, I'd better break existing implems that do that so that they can adjust. (I also doubt this will hit anyone in practice 🤞 ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then the boundary length should at least be a public constant, so that they don't have to hardcode this |
||
} | ||
|
||
public function testCommentTagsAreRemoved() | ||
|
@@ -113,7 +113,7 @@ public function testCommentTagsAreRemoved() | |
$response = new Response('<esi:comment text="some comment >" /> Keep this'); | ||
$this->assertSame($response, $esi->process($request, $response)); | ||
|
||
$this->assertEquals(' Keep this', $response->getContent()); | ||
$this->assertEquals(' Keep this', substr($response->getContent(), 24, -24)); | ||
} | ||
|
||
public function testProcess() | ||
|
@@ -124,23 +124,27 @@ public function testProcess() | |
$response = new Response('foo <esi:comment text="some comment" /><esi:include src="..." alt="alt" onerror="continue" />'); | ||
$this->assertSame($response, $esi->process($request, $response)); | ||
|
||
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'alt\', true) ?>'."\n", $response->getContent()); | ||
$content = explode(substr($response->getContent(), 0, 24), $response->getContent()); | ||
$this->assertSame(['', 'foo ', "...\nalt\n1\n", ''], $content); | ||
$this->assertEquals('ESI', $response->headers->get('x-body-eval')); | ||
|
||
$response = new Response('foo <esi:comment text="some comment" /><esi:include src="foo\'" alt="bar\'" onerror="continue" />'); | ||
$this->assertSame($response, $esi->process($request, $response)); | ||
|
||
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'foo\\\'\', \'bar\\\'\', true) ?>'."\n", $response->getContent()); | ||
$content = explode(substr($response->getContent(), 0, 24), $response->getContent()); | ||
$this->assertSame(['', 'foo ', "foo'\nbar'\n1\n", ''], $content); | ||
|
||
$response = new Response('foo <esi:include src="..." />'); | ||
$this->assertSame($response, $esi->process($request, $response)); | ||
|
||
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent()); | ||
$content = explode(substr($response->getContent(), 0, 24), $response->getContent()); | ||
$this->assertSame(['', 'foo ', "...\n\n\n", ''], $content); | ||
|
||
$response = new Response('foo <esi:include src="..."></esi:include>'); | ||
$this->assertSame($response, $esi->process($request, $response)); | ||
|
||
$this->assertEquals('foo <?php echo $this->surrogate->handle($this, \'...\', \'\', false) ?>'."\n", $response->getContent()); | ||
$content = explode(substr($response->getContent(), 0, 24), $response->getContent()); | ||
$this->assertSame(['', 'foo ', "...\n\n\n", ''], $content); | ||
} | ||
|
||
public function testProcessEscapesPhpTags() | ||
|
@@ -151,7 +155,8 @@ public function testProcessEscapesPhpTags() | |
$response = new Response('<?php <? <% <script language=php>'); | ||
$this->assertSame($response, $esi->process($request, $response)); | ||
|
||
$this->assertEquals('<?php echo "<?"; ?>php <?php echo "<?"; ?> <?php echo "<%"; ?> <?php echo "<s"; ?>cript language=php>', $response->getContent()); | ||
$content = explode(substr($response->getContent(), 0, 24), $response->getContent()); | ||
$this->assertSame(['', '<?php <? <% <script language=php>', ''], $content); | ||
} | ||
|
||
public function testProcessWhenNoSrcInAnEsi() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of putting the boundaries around the content of the returned response, which force the caller to be aware of it to remove it (even if the content has no ESI tag), we might put the boundary in a header
X-Body-Boundary
(that the caller can still remove), which might make the removal easierThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The caller has to be aware of the content in any cases.
Putting it before+after allows a quick check to ensure it's correct in HttpCache:
if (substr($content, -24) === $boundary = substr($content, 0, 24)) {