fix(csv): prevent formula injection in CSV exports (CWE-1236) - #926
Open
nikolas-sapa wants to merge 2 commits into
Open
nikolas-sapa wants to merge 2 commits into
nikolas-sapa wants to merge 2 commits into
Conversation
When exporting benchmark results to CSV, hyperfine writes the command string and parameter values verbatim. Spreadsheet applications like Excel, LibreOffice Calc, and Google Sheets interpret cells beginning with '=', '+', '-', '@', tab, or carriage return as formulas, which can lead to formula injection (CWE-1236) when parameter values come from external input. This change adds a sanitize_csv_value() helper that prefixes such values with a single quote to force literal text interpretation, and applies it to both the command field and all parameter_* columns. Fixes sharkdp#915 Signed-off-by: Nikolas Sapa <84yk8btb9f@privaterelay.appleid.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When exporting benchmark results to CSV, hyperfine writes the command string and parameter values verbatim into CSV cells. Spreadsheet applications (Excel, LibreOffice Calc, Google Sheets) interpret cells beginning with
=,+,-,@, tab, or carriage return as formulas, which can lead to formula injection (CWE-1236) when parameter values come from external input.For example,
--parameter-list payload '=1+1'exported to CSV contains the literal, unescaped text=1+1, which is evaluated as a live formula when the file is opened in a spreadsheet.Fix
Add a
sanitize_csv_value()helper that prefixes values beginning with formula-triggering characters with a single quote ('), forcing spreadsheet applications to display them as literal text. The helper is applied to both thecommandfield and allparameter_*columns.This is the standard mitigation for CWE-1236 and does not affect values that don't begin with dangerous characters. Numeric fields (mean, stddev, etc.) are unaffected since they are serialized from
f64values which cannot produce formula-interpretable strings.Testing
Added two new tests:
test_csv_formula_injection_sanitization: Integration test that verifies a parameter value of=1+1is properly escaped in the CSV output as'=1+1.test_sanitize_csv_value: Unit test covering all six dangerous prefixes (=,+,-,@,\t,\r) and several safe values that should pass through unchanged.All existing tests pass (3 unit + 39 integration).