Skip to content

Commit 2434907

Browse files
minor #61951 [Serializer] Escape values starting with line feed when using csv_escape_formulas (Peter Mead)
This PR was merged into the 7.4 branch. Discussion ---------- [Serializer] Escape values starting with line feed when using `csv_escape_formulas` | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT Change `CsvEncoder` so that it follows the latest OWASP guidelines on CSV injection. When escape formulas is enabled it will escape values starting with a line feed. This is in addition other the other starting characters of equals, plus, minus, at sign, tab, and carriage return. [OWASP: CSV Injection](https://owasp.org/www-community/attacks/CSV_Injection) Commits ------- d544c8737a0 [Serializer] CsvEncoder to escape values starting with line feed when escape formulas is enabled
2 parents b663f42 + b1cf3bd commit 2434907

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Encoder/CsvEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
3939

4040
private const UTF8_BOM = "\xEF\xBB\xBF";
4141

42-
private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r"];
42+
private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r", "\n"];
4343

4444
private array $defaultContext = [
4545
self::DELIMITER_KEY => ',',

Tests/Encoder/CsvEncoderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,15 @@ public function testEncodeFormulas()
310310
$this->encoder->encode(["\ttab"], 'csv')
311311
);
312312

313+
$this->assertSame(<<<'CSV'
314+
0
315+
"'
316+
line feed"
317+
318+
CSV,
319+
$this->encoder->encode(["\nline feed"], 'csv')
320+
);
321+
313322
$this->assertSame(<<<'CSV'
314323
0
315324
"'=1+2"";=1+2"
@@ -438,6 +447,17 @@ public function testEncodeFormulasWithSettingsPassedInContext()
438447
])
439448
);
440449

450+
$this->assertSame(<<<'CSV'
451+
0
452+
"'
453+
line feed"
454+
455+
CSV,
456+
$this->encoder->encode(["\nline feed"], 'csv', [
457+
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
458+
])
459+
);
460+
441461
$this->assertSame(<<<'CSV'
442462
0
443463
"'=1+2"";=1+2"

0 commit comments

Comments
 (0)