Skip to content

Commit

Permalink
Prevent csv/xls injection (sonata-project#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Wentzell committed Jan 6, 2021
1 parent 95ed305 commit ada9b89
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Writer/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ public function write(array $data): void
EXCEPTION);
}

// prevent csv injection
$patterns = ['/^=/', '/^\+/', '/^-/', '/^@/'];
$replace = ['!=', '!+', '!-', '!@'];
foreach ($data as $key => $value) {
$data[$key] = preg_replace($patterns, $replace, $value);
}

$result = @fputcsv($this->file, $data, $this->delimiter, $this->enclosure, $this->escape);

if (!$result) {
Expand Down
5 changes: 4 additions & 1 deletion src/Writer/XlsWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ public function write(array $data): void
$this->init($data);

fwrite($this->file, '<tr>');
// prevent xls injection
$patterns = ['/^=/', '/^\+/', '/^-/', '/^@/'];
$replace = ['!=', '!+', '!-', '!@'];
foreach ($data as $value) {
fwrite($this->file, sprintf('<td>%s</td>', $value));
fwrite($this->file, sprintf('<td>%s</td>', preg_replace($patterns, $replace, $value)));
}
fwrite($this->file, '</tr>');

Expand Down

0 comments on commit ada9b89

Please sign in to comment.