Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,12 @@
->setWrapText(true);
} else {
// Explicitly set the type of the value to string for values that start with '=' to prevent it being interpreted as formulas
if (is_string($value) && str_starts_with($value, '=')) {
if (is_string($value)) {
$activeWorksheet->getCell([$column, $row])
->setValueExplicit($value);
->setValueExplicit($fileFormat === 'csv'
? $this->escapeCSV($value)
: $value,
);
} else {
$activeWorksheet->setCellValue([$column, $row], $value);
}
Expand All @@ -360,6 +363,19 @@
return file_get_contents($exportedFile);
}

/**
* Escape a value for writing it to a CSV file.
* This is needed to ensure the CSV, when loaded into an spreadsheet application, does not execute potential formulas.
*/
private function escapeCSV(string $value): string {
$BAD_CHARACTERS = ['=', '+', '-', '@', "\t", "\r"];
if (strlen($value) > 0 && in_array(mb_str_split($value)[0], $BAD_CHARACTERS)) {
// Escape the value by adding a leading single quote
return "'$value";

Check warning on line 374 in lib/Service/SubmissionService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/SubmissionService.php#L374

Added line #L374 was not covered by tests
}
return $value;
}

/**
* Validate all answers against the questions
* @param array $questions Array of the questions of the form
Expand Down
Loading