Skip to content

Incorrect parsing of options with escaped quotes  #484

Open

Description

@jettdc

trying to parse the string:

const curl = `curl -X POST "https://api.example.com/data" -H "Content-Type: application/json" -d "{\\"key\\": \\"This string has an escaped backslash: \\\\ and a newline: \\\\n\\"}"`;

Which is equivalent to the valid curl command

curl -X POST "https://api.example.com/data" -H "Content-Type: application/json" -d "{\"key\": \"This string has an escaped backslash: \\ and a newline: \\n\"}"

But when I parse it, my args array looks like the following:

_: [
        'curl',
        'https://api.example.com/data',
        'string',
        'has',
        'an',
        'escaped',
        'backslash:',
        '\\\\',
        'and',
        'a',
        'newline:',
        '\\\\n\\"}"'
      ],

Am I missing something or is this a bug?

Activity

  1. shadowspawn commented on Sep 26, 2023

    @shadowspawn
    Member

    yargs-parser does not support escaping embedded quotes, but it does support nesting quotes. So you can have " inside ' quotes.

    I am not sure what quoting you would want for the slash and newline, but this quoting at least reads everything after the -d as the option value:

    import parser from 'yargs-parser'
    const curl = `curl -X POST "https://api.example.com/data" -H "Content-Type: application/json" -d '{"key": "This string has an escaped backslash: \\\\ and a newline: \\\\n"}'`;
    const argv = parser(curl);
    console.log(argv);
    % node index.mjs
    {
      _: [ 'curl', '"https://api.example.com/data"' ],
      X: 'POST',
      H: 'Content-Type: application/json',
      d: '{"key": "This string has an escaped backslash: \\\\ and a newline: \\\\n"}'
    }
  2. jettdc commented on Sep 26, 2023

    @jettdc
    Author

    Unfortunately these strings are coming from user input, and since the mentioned curl command is a valid curl command/bash/input, I need to be able to support it

    Is there a reason it doesn't support escaping embedded quotes?

  3. shadowspawn commented on Sep 28, 2023

    @shadowspawn
    Member

    If you can access the command after it is parsed by the shell into separate arguments then you avoid the problem that different shells/platforms and yargs split up command-line into arguments in different ways. (I realise this may not be possible in your use case, but I thought worth mentioning as a general approach.)

    Related: #300 #385

    Is there a reason it doesn't support escaping embedded quotes?

    I suspect just hasn't come up enough.

    A package I have used for some quoting work is: https://www.npmjs.com/package/shell-quote

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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