Skip to content

Commit 9067fdc

Browse files
security #cve-2019-11325 [VarExporter] fix exporting some strings (nicolas-grekas)
This PR was merged into the 4.2 branch.
2 parents 81fce3e + 0524868 commit 9067fdc

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

src/Symfony/Component/VarExporter/Internal/Exporter.php

+19-18
Original file line numberDiff line numberDiff line change
@@ -212,27 +212,28 @@ public static function export($value, $indent = '')
212212
$subIndent = $indent.' ';
213213

214214
if (\is_string($value)) {
215-
$code = var_export($value, true);
216-
217-
if (false !== strpos($value, "\n") || false !== strpos($value, "\r")) {
218-
$code = strtr($code, [
219-
"\r\n" => "'.\"\\r\\n\"\n".$subIndent.".'",
220-
"\r" => "'.\"\\r\"\n".$subIndent.".'",
221-
"\n" => "'.\"\\n\"\n".$subIndent.".'",
222-
]);
223-
}
215+
$code = sprintf("'%s'", addcslashes($value, "'\\"));
224216

225-
if (false !== strpos($value, "\0")) {
226-
$code = str_replace('\' . "\0" . \'', '\'."\0".\'', $code);
227-
$code = str_replace('".\'\'."', '', $code);
228-
}
217+
$code = preg_replace_callback('/([\0\r\n]++)(.)/', function ($m) use ($subIndent) {
218+
$m[1] = sprintf('\'."%s".\'', str_replace(
219+
["\0", "\r", "\n", '\n\\'],
220+
['\0', '\r', '\n', '\n"'."\n".$subIndent.'."\\'],
221+
$m[1]
222+
));
229223

230-
if (false !== strpos($code, "''.")) {
231-
$code = str_replace("''.", '', $code);
232-
}
224+
if ("'" === $m[2]) {
225+
return substr($m[1], 0, -2);
226+
}
227+
228+
if ('n".\'' === substr($m[1], -4)) {
229+
return substr_replace($m[1], "\n".$subIndent.".'".$m[2], -2);
230+
}
231+
232+
return $m[1].$m[2];
233+
}, $code, -1, $count);
233234

234-
if (".''" === substr($code, -3)) {
235-
$code = rtrim(substr($code, 0, -3));
235+
if ($count && 0 === strpos($code, "''.")) {
236+
$code = substr($code, 3);
236237
}
237238

238239
return $code;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
return '\'BOOM\''."\n"
4+
.'.var_dump(123)//\'';

src/Symfony/Component/VarExporter/Tests/Fixtures/multiline-string.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
return [
44
"\0\0\r\n"
5-
.'A' => 'B'."\r"
6-
.'C'."\n"
5+
.'A' => 'B'."\r".'C'."\n"
76
."\n",
87
];

src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function testExport(string $testName, $value, bool $staticValueExpected =
109109
public function provideExport()
110110
{
111111
yield ['multiline-string', ["\0\0\r\nA" => "B\rC\n\n"], true];
112+
yield ['lf-ending-string', "'BOOM'\n.var_dump(123)//'", true];
112113

113114
yield ['bool', true, true];
114115
yield ['simple-array', [123, ['abc']], true];

0 commit comments

Comments
 (0)