Skip to content

[BUG] JSON.stringify should not be used for shell escaping #14

Closed
@jbms

Description

@jbms

run-script uses JSON.stringify to quote/escape command arguments:

cmd = pkg.scripts[event] + args.map(a => ` ${JSON.stringify(a)}`).join('')

However, shells don't use the same quoting rule as JSON, which leads to incorrect behavior. In particular, literal newline characters (NL) get converted to \n (BACKSLASH N) by JSON.stringify, but bash does not treat \n as an escape sequence, and as a result the script receives \n (BACKSLASH N) as its argument.

To fix this problem, shell escaping should be used instead of JSON stringify.

I ran into this calling a script that invokes webpack with a --define argument containing a literal newline (NL) character. npm 7 converts this to a \n, which results in webpack substituting invalid javascript code leading to a parse error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions