Skip to content

Conversation

@rhcarvalho
Copy link
Contributor

Based on #86 (comment) / #86 (comment), to make it easier to customize the existing CSV modules, in particular to add formula escaping (on user code).

MyApp.CSV,
NimbleCSV.RFC4180.options()
|> Keyword.merge(
escape_formula: %{~w(@ + - = \\t \\r) => "'"},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason ExDoc needed the \\t and \\r escaping, otherwise it would produce a broken code snippet by interpreting the tab and carriage return.

)
"""
@callback options() :: keyword()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it a callback to match the other parts of the API.

* `:escape_formula` - an optional map of formula prefixes to escape sequences.
When `nil` (the default), formula escaping is disabled. For example,
`%{~w(@ + - = \t \r) => "'"}` escapes fields starting with `@`, `+`, `-`, `=`,
tab, or carriage return by prefixing them with `'`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded this to improve flow. Similar to :trim_bom, :escape_formula requires more explanation so ends up being multiple sentences. Still, like all options, it doesn't end with punctuation.

follow the [OWASP recommendations](https://owasp.org/www-community/attacks/CSV_Injection):
escape_formula: %{["@", "+", "-", "=", "\t", "\r"] => "'"}
escape_formula: %{~w(@ + - = \t \r) => "'"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed for consistency.

@josevalim josevalim merged commit 41c909b into dashbitco:master Jun 30, 2025
0 of 2 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@rhcarvalho rhcarvalho deleted the options branch June 30, 2025 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants