-
Notifications
You must be signed in to change notification settings - Fork 11
Fix/csv formula sanitization #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lucas-amiaud
wants to merge
2
commits into
master
Choose a base branch
from
fix/csv-formula-sanitization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 117 additions & 93 deletions
210
src/main/java/com/coreoz/windmill/exports/exporters/csv/CsvExporter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,105 +1,129 @@ | ||
| package com.coreoz.windmill.exports.exporters.csv; | ||
|
|
||
| import com.coreoz.windmill.exports.config.ExportMapping; | ||
| import com.coreoz.windmill.files.BomCharset; | ||
| import com.opencsv.CSVWriter; | ||
| import lombok.SneakyThrows; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.io.OutputStreamWriter; | ||
| import java.util.List; | ||
|
|
||
| import com.coreoz.windmill.files.BomCharset; | ||
| import com.coreoz.windmill.exports.config.ExportMapping; | ||
| import com.opencsv.CSVWriter; | ||
|
|
||
| import lombok.SneakyThrows; | ||
|
|
||
| public class CsvExporter<T> { | ||
|
|
||
| private final Iterable<T> rows; | ||
| private final ExportMapping<T> mapping; | ||
| private final ExportCsvConfig exportConfig; | ||
| private CSVWriter csvWriter; | ||
|
|
||
| public CsvExporter(Iterable<T> rows, ExportMapping<T> mapping, ExportCsvConfig exportConfig) { | ||
| this.rows = rows; | ||
| this.mapping = mapping; | ||
| this.exportConfig = exportConfig; | ||
| } | ||
|
|
||
| /** | ||
| * Write the export file in an existing {@link OutputStream}. | ||
| * | ||
| * This {@link OutputStream} will not be closed automatically: | ||
| * it should be closed manually after this method is called. | ||
| * | ||
| * @throws IOException if anything can't be written. | ||
| */ | ||
| @SneakyThrows | ||
| public OutputStream writeTo(OutputStream outputStream) { | ||
| csvWriter = new CSVWriter( | ||
| new OutputStreamWriter(outputStream, exportConfig.getCharset().getCharset()), | ||
| exportConfig.getSeparator(), | ||
| exportConfig.getQuoteChar(), | ||
| exportConfig.getEscapeChar(), | ||
| exportConfig.getLineEnd() | ||
| ); | ||
| writeBom(outputStream); | ||
| writeRows(); | ||
| return outputStream; | ||
| } | ||
|
|
||
| /** | ||
| * @throws IOException if anything can't be written. | ||
| */ | ||
| public byte[] toByteArray() { | ||
| ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); | ||
| writeTo(byteOutputStream); | ||
| return byteOutputStream.toByteArray(); | ||
| } | ||
|
|
||
| // internals | ||
|
|
||
| @SneakyThrows | ||
| private void writeBom(OutputStream outputStream) { | ||
| BomCharset encodingCharset = exportConfig.getCharset(); | ||
| if (encodingCharset != null) { | ||
| private final Iterable<T> rows; | ||
| private final ExportMapping<T> mapping; | ||
| private final ExportCsvConfig exportConfig; | ||
| private CSVWriter csvWriter; | ||
|
|
||
| public CsvExporter(Iterable<T> rows, ExportMapping<T> mapping, ExportCsvConfig exportConfig) { | ||
| this.rows = rows; | ||
| this.mapping = mapping; | ||
| this.exportConfig = exportConfig; | ||
| } | ||
|
|
||
| /** | ||
| * Write the export file in an existing {@link OutputStream}. | ||
| * <p> | ||
| * This {@link OutputStream} will not be closed automatically: | ||
| * it should be closed manually after this method is called. | ||
| * | ||
| * @throws IOException if anything can't be written. | ||
| */ | ||
| @SneakyThrows | ||
| public OutputStream writeTo(OutputStream outputStream) { | ||
| csvWriter = new CSVWriter( | ||
| new OutputStreamWriter(outputStream, exportConfig.getCharset().getCharset()), | ||
| exportConfig.getSeparator(), | ||
| exportConfig.getQuoteChar(), | ||
| exportConfig.getEscapeChar(), | ||
| exportConfig.getLineEnd() | ||
| ); | ||
| writeBom(outputStream); | ||
| writeRows(); | ||
| return outputStream; | ||
| } | ||
|
|
||
| /** | ||
| * @throws IOException if anything can't be written. | ||
| */ | ||
| public byte[] toByteArray() { | ||
| ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); | ||
| writeTo(byteOutputStream); | ||
| return byteOutputStream.toByteArray(); | ||
| } | ||
|
|
||
| // internals | ||
|
|
||
| @SneakyThrows | ||
| private void writeBom(OutputStream outputStream) { | ||
| BomCharset encodingCharset = exportConfig.getCharset(); | ||
| if (encodingCharset != null) { | ||
| encodingCharset.writeBomBytes(outputStream); | ||
| } | ||
| } | ||
|
|
||
| private void writeRows() { | ||
| writeHeaderRow(); | ||
|
|
||
| for(T row : rows) { | ||
| writeRow(row); | ||
| } | ||
| } | ||
|
|
||
| private void writeHeaderRow() { | ||
| List<String> headerColumn = mapping.headerColumns(); | ||
| if(!headerColumn.isEmpty()) { | ||
| String[] csvRowValues = new String[headerColumn.size()]; | ||
| for (int i = 0; i < headerColumn.size(); i++) { | ||
| csvRowValues[i] = stringValue(headerColumn.get(i)); | ||
| } | ||
| csvWriter.writeNext(csvRowValues,exportConfig.isApplyQuotesToAll()); | ||
| } | ||
| } | ||
|
|
||
| @SneakyThrows | ||
| private void writeRow(T row) { | ||
| String[] csvRowValues = new String[mapping.columnsCount()]; | ||
| for (int i = 0; i < mapping.columnsCount(); i++) { | ||
| csvRowValues[i] = stringValue(mapping.cellValue(i, row)); | ||
| } | ||
| csvWriter.writeNext(csvRowValues, exportConfig.isApplyQuotesToAll()); | ||
| csvWriter.flush(); | ||
| } | ||
|
|
||
| private String stringValue(final Object object) { | ||
| if (object == null) { | ||
| return ""; | ||
| } | ||
| return object.toString(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void writeRows() { | ||
| writeHeaderRow(); | ||
|
|
||
| for (T row : rows) { | ||
| writeRow(row); | ||
| } | ||
| } | ||
|
|
||
| private void writeHeaderRow() { | ||
| List<String> headerColumn = mapping.headerColumns(); | ||
| if (!headerColumn.isEmpty()) { | ||
| String[] csvRowValues = new String[headerColumn.size()]; | ||
| for (int i = 0; i < headerColumn.size(); i++) { | ||
| csvRowValues[i] = sanitizeValue(headerColumn.get(i), stringValue(headerColumn.get(i))); | ||
| } | ||
| csvWriter.writeNext(csvRowValues, exportConfig.isApplyQuotesToAll()); | ||
| } | ||
| } | ||
|
|
||
| @SneakyThrows | ||
| private void writeRow(T row) { | ||
| String[] csvRowValues = new String[mapping.columnsCount()]; | ||
| List<String> headerColumns = mapping.headerColumns(); | ||
| for (int i = 0; i < mapping.columnsCount(); i++) { | ||
| String columnName = i < headerColumns.size() ? headerColumns.get(i) : null; | ||
| csvRowValues[i] = sanitizeValue(columnName, stringValue(mapping.cellValue(i, row))); | ||
| } | ||
| csvWriter.writeNext(csvRowValues, exportConfig.isApplyQuotesToAll()); | ||
| csvWriter.flush(); | ||
| } | ||
|
|
||
| private String sanitizeValue(String columnName, String value) { | ||
| if ( | ||
| exportConfig.isSanitizeFormulas() | ||
| && (columnName == null || !exportConfig.getFieldNamesExcludedFromSanitization().contains(columnName)) | ||
| && isDangerousValue(value) | ||
| ) { | ||
| return "'" + value; | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| private static boolean isDangerousValue(String value) { | ||
| if (value == null || value.isEmpty()) { | ||
| return false; | ||
| } | ||
| char firstChar = value.charAt(0); | ||
| return firstChar == '=' | ||
| || firstChar == '+' | ||
| || firstChar == '-' | ||
| || firstChar == '@' | ||
| || firstChar == '\t' | ||
| || firstChar == '\r'; | ||
| } | ||
|
|
||
| private static String stringValue(final Object object) { | ||
| if (object == null) { | ||
| return ""; | ||
| } | ||
| return object.toString(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/test/java/com/coreoz/windmill/exports/exporters/csv/CsvFormulaInjectionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package com.coreoz.windmill.exports.exporters.csv; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
|
|
||
| import lombok.Getter; | ||
| import org.junit.Test; | ||
|
|
||
| import com.coreoz.windmill.Windmill; | ||
| import com.coreoz.windmill.exports.config.ExportHeaderMapping; | ||
|
|
||
| public class CsvFormulaInjectionTest { | ||
|
|
||
| @Test | ||
| public void should_sanitize_formula_injection_by_default() { | ||
| String formula = "=SUM(1,2)"; | ||
| byte[] csvExport = Windmill | ||
| .export(Collections.singletonList(new RowData(formula))) | ||
| .withHeaderMapping(new ExportHeaderMapping<RowData>().add("col", RowData::getValue)) | ||
| .asCsv() | ||
| .toByteArray(); | ||
|
|
||
| String result = new String(csvExport, StandardCharsets.UTF_8); | ||
| // On s'attend à ce que la formule soit préfixée par ' | ||
| assertThat(result).contains("'=SUM(1,2)"); | ||
| } | ||
|
|
||
| @Test | ||
| public void should_sanitize_other_dangerous_characters() { | ||
| for (String prefix : Arrays.asList("+", "-", "@", "\t", "\r")) { | ||
| String value = prefix + "danger"; | ||
| byte[] csvExport = Windmill | ||
| .export(Collections.singletonList(new RowData(value))) | ||
| .withHeaderMapping(new ExportHeaderMapping<RowData>().add("col", RowData::getValue)) | ||
| .asCsv() | ||
| .toByteArray(); | ||
|
|
||
| String result = new String(csvExport, StandardCharsets.UTF_8); | ||
| assertThat(result).as("Should sanitize value starting with " + prefix).contains("'" + prefix + "danger"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void should_not_sanitize_formula_if_disabled_globally() { | ||
| String formula = "=SUM(1,2)"; | ||
| byte[] csvExport = Windmill | ||
| .export(Collections.singletonList(new RowData(formula))) | ||
| .withHeaderMapping(new ExportHeaderMapping<RowData>().add("col", RowData::getValue)) | ||
| .asCsv( | ||
| ExportCsvConfig.builder() | ||
| .disableFormulasSanitization().build() | ||
| ) | ||
| .toByteArray(); | ||
|
|
||
| String result = new String(csvExport, StandardCharsets.UTF_8); | ||
| assertThat(result) | ||
| .contains("\"=SUM(1,2)\"") | ||
| .doesNotContain("'=SUM(1,2)"); | ||
| } | ||
|
|
||
| @Test | ||
| public void should_not_sanitize_formula_for_specific_fields() { | ||
| String formula = "=SUM(1,2)"; | ||
| byte[] csvExport = Windmill | ||
| .export(Collections.singletonList(new RowData(formula))) | ||
| .withHeaderMapping(new ExportHeaderMapping<RowData>().add("safe", RowData::getValue).add("unsafe", RowData::getValue)) | ||
| .asCsv( | ||
| ExportCsvConfig.builder() | ||
| .disableFormulasSanitizationForFields("safe").build() | ||
| ) | ||
| .toByteArray(); | ||
|
|
||
| String result = new String(csvExport, StandardCharsets.UTF_8); | ||
| assertThat(result) | ||
| // "safe" column should not be sanitized | ||
| .contains("\"=SUM(1,2)\"") | ||
| // "unsafe" column should be sanitized | ||
| .contains("\"'=SUM(1,2)\""); | ||
| } | ||
|
|
||
| private static class RowData { | ||
| @Getter | ||
| private final String value; | ||
|
|
||
| public RowData(String value) { | ||
| this.value = value; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C'est un peu overkill non?