Read this article to get an idea of how dangerous it can be to blindly accept macro's in spreadsheets. Be it MS Excel or Google spreadsheets, they all suffer.

You cannot blame CSV for it. CSV is just passive data.

Once you load or open a CSV file into something dangerous as a spreadsheet program that allows formula's to be execcuted on open, all bets are off. Or are they?

The upcoming Text::CSV_XS has added a new feature to optional take actions when a field contains a leading =, which to most spreadsheet programs indicates a formula.

On both parsing and generating CSV, you will be able to specify what you want to do (where "formula" does not go beyond the fact that the field starts with a =):

Code speaks loader than words ...

$ cat formula.csv a,b,c 1,=2+3,4 6,,7,=8+9,

Parsing

$ perl -MCSV -e'dcsv (in => "formula.csv")' [ [ 'a', 'b', 'c' ], [ '1', '=2+3', '4' ], [ '6', '', '7', '=8+9', '' ] ] $ perl -MCSV -e'dcsv (in => "formula.csv", formula => "none")' [ [ 'a', 'b', 'c' ], [ '1', '=2+3', '4' ], [ '6', '', '7', '=8+9', '' ] ] $ perl -MCSV -e'dcsv (in => "formula.csv", formula => "die")' Formulas are forbidden $ perl -MCSV -e'dcsv (in => "formula.csv", formula => "croak")' Formulas are forbidden $ perl -MCSV -e'dcsv (in => "formula.csv", formula => "diag")' Field 2 in record 1 contains formula '=2+3' Field 4 in record 2 contains formula '=8+9' [ [ 'a', 'b', 'c' ], [ '1', '=2+3', '4' ], [ '6', '', '7', '=8+9', '' ] ] $ perl -MCSV -e'dcsv (in => "formula.csv", formula => "empty")' [ [ 'a', 'b', 'c' ], [ '1', '', '4' ], [ '6', '', '7', '', '' ] ] $ perl -MCSV -e'dcsv (in => "formula.csv", formula => "undef")' [ [ 'a', 'b', 'c' ], [ '1', undef, '4' ], [ '6', '', '7', undef, '' ] ]

Generating

$ perl -MCSV -e'dcsv (in => [["a","b","c"],[1,"=2+3",4],[6,"",7,"=8+9" +]], quote_empty => 1)' a,b,c 1,=2+3,4 6,"",7,=8+9 1 $ perl -MCSV -e'dcsv (in => [["a","b","c"],[1,"=2+3",4],[6,"",7,"=8+9" +]], quote_empty => 1, formula => "none")' a,b,c 1,=2+3,4 6,"",7,=8+9 1 $ perl -MCSV -e'dcsv (in => [["a","b","c"],[1,"=2+3",4],[6,"",7,"=8+9" +]], quote_empty => 1, formula => "die")' a,b,c Formulas are forbidden Exit 255 $ perl -MCSV -e'dcsv (in => [["a","b","c"],[1,"=2+3",4],[6,"",7,"=8+9" +]], quote_empty => 1, formula => "croak")' a,b,c Formulas are forbidden Exit 255 $ perl -MCSV -e'dcsv (in => [["a","b","c"],[1,"=2+3",4],[6,"",7,"=8+9" +]], quote_empty => 1, formula => "diag")' a,b,c Field 1 contains formula '=2+3' 1,=2+3,4 Field 3 contains formula '=8+9' 6,"",7,=8+9 1 $ perl -MCSV -e'dcsv (in => [["a","b","c"],[1,"=2+3",4],[6,"",7,"=8+9" +]], quote_empty => 1, formula => "empty")' a,b,c 1,"",4 6,"",7,"" 1 $ perl -MCSV -e'dcsv (in => [["a","b","c"],[1,"=2+3",4],[6,"",7,"=8+9" +]], quote_empty => 1, formula => "undef")' a,b,c 1,,4 6,"",7, 1

I'm pretty pleased with the diagnostics

$ cat formula.csv a,b,c 1,=2+3,4 6,,7,=8+9, $ perl -MCSV -e'$_ = dcsv (in => "formula.csv", bom => 1, formula => " +diag")' Field 2 (column: 'b') in record 1 contains formula '=2+3' Field 4 in record 2 contains formula '=8+9'

Expect this to be available by next week.


Enjoy, Have FUN! H.Merijn

In reply to Be prepared for CSV injections in spreadsheet by Tux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.