Skip to content
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

feat(csv-stringify): Add escape_formulas to defend against injection attacks #380

Merged
merged 2 commits into from Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/csv-stringify/lib/api/index.js
Expand Up @@ -125,7 +125,7 @@ const stringifier = function(options, state, info){
}else{
atlanteh marked this conversation as resolved.
Show resolved Hide resolved
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)];
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options;
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter, escape_formulas} = options;
if('' === value && '' === field){
let quotedMatch = quoted_match && quoted_match.filter(quoted_match => {
if(typeof quoted_match === 'string'){
Expand Down Expand Up @@ -158,6 +158,9 @@ const stringifier = function(options, state, info){
}
});
quotedMatch = quotedMatch && quotedMatch.length > 0;
if (escape_formulas && ['=', '+', '-', '@', '\t', '\r'].includes(value[0])) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this happen in the __onField function ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think __onField is only in csv-parse. not csv-stringify

value = `'${value}`;
}
const shouldQuote = containsQuote === true || containsdelimiter || containsRecordDelimiter || quoted || quotedString || quotedMatch;
if(shouldQuote === true && containsEscape === true){
const regexp = escape === '\\'
Expand Down
6 changes: 6 additions & 0 deletions packages/csv-stringify/lib/api/normalize_options.js
Expand Up @@ -50,6 +50,12 @@ const normalize_options = function(opts) {
}else{
// todo
}
// Normalize option `escape_formulas`
if(options.escape_formulas === undefined || options.escape_formulas === null){
options.escape_formulas = false;
}else{
// todo
}
// Normalize option `quoted_empty`
if(options.quoted_empty === undefined || options.quoted_empty === null){
options.quoted_empty = undefined;
Expand Down
4 changes: 4 additions & 0 deletions packages/csv-stringify/lib/index.d.ts
Expand Up @@ -105,6 +105,10 @@ export interface Options extends stream.TransformOptions {
* defaults to 'auto' (discovered in source or 'unix' if no source is specified).
*/
record_delimiter?: RecordDelimiter
/**
* Boolean, default to false, if true, fields that begin with `=`, `+`, `-`, `@`, `\t`, or `\r` will be prepended with a `'` to protected agains csv injection attacks
*/
escape_formulas?: boolean
}

export class Stringifier extends stream.Transform {
Expand Down
38 changes: 38 additions & 0 deletions packages/csv-stringify/test/option.escape_formulas.coffee
@@ -0,0 +1,38 @@

import { stringify } from '../lib/index.js'

describe 'Option `escape_formulas`', ->

it 'should escape =,+,-,@,\t,\r signs', (next) ->
stringify [
[ '=a',1]
[ '+b',2]
[ '-c',3]
[ '@d',4]
[ '\te',5]
[ '\rf',6]
[ 'g',7]
], escape_formulas: true, eof: false, (err, data) ->
return next err if err
data.should.eql """
'=a,1
'+b,2
'-c,3
'@d,4
'\te,5
'\rf,6
g,7
"""
next()

it 'should first escape_formulas, then quoted', (next) ->
stringify [
[ '=a',1]
[ 'b',2]
], escape_formulas: true, quoted: true, eof: false, (err, data) ->
return next err if err
data.should.eql """
"'=a","1"
"b","2"
"""
next()