Skip to content

Commit

Permalink
escape formula inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Apr 21, 2022
1 parent 29f1dd5 commit bbda4be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/helpers/ExportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,24 @@ protected static function generateRow(array $row, $delimiter, $enclose)
}

/**
* Undocumented function
*
* @param [type] $value
* @return void
*
* @param string $value
* @return string
* @see https://owasp.org/www-community/attacks/CSV_Injection
*/
public static function sanitizeValue($value)
{
return str_replace([
'";', '",', '"', "'"
], '', trim($value));
$value = str_replace([
'"',
], [
'""',
], trim($value));

$firstChar = substr($value, 0, 1);
if (in_array($firstChar, ['=', '+', '-', '@', PHP_EOL, "\t", "\n"])) {
$value = StringHelper::replaceFirst($firstChar, "'$firstChar", $value);
}

return $value;
}
}
3 changes: 2 additions & 1 deletion tests/helpers/ExportHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ public function testSpecialCharsEncoding()
{
$content = ExportHelper::csv([
['&', "'", 'a"b"c'],
['nix', 'nix', '=1+2";=1+2']
], [], false);

$this->assertSameTrimmed('"&","","abc"', $content);
$this->assertSameTrimmed('"&","\'","a""b""c" "nix","nix","\'=1+2"";=1+2"', $content);
}
}

0 comments on commit bbda4be

Please sign in to comment.