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

Backslash-escaped non-printable character in double-quoted string throws exception when quoted-strings are only-when-needed #700

Open
jmknoble opened this issue Nov 25, 2024 · 2 comments

Comments

@jmknoble
Copy link

The following line of YAML:

  echo_format: "\u001b[1;34m+ {command}\u001b[0m"

with the following .yamllint config:

  quoted-strings:
    quote-type: any
    required: only-when-needed

Throws an exception (shown below).

Yamllint version: yamllint 1.35.1
Python version: Python 3.12.7

Dependency versions:

└── yamllint v1.35.1 (group: dev)
    ├── pathspec v0.12.1
    └── pyyaml v6.0.2

Reproducible problem documented at https://github.com/jmknoble/yamllint-quoted-escaped-nonprintable-char-problem .

The problem seems to be in yamllint.rules.quoted_strings._quotes_are_needed() . My hunch is that the already-decoded string is being passed, which yaml.reader.check_printable() interprets as unprintable.

Traceback:

Traceback (most recent call last):
  File "/Users/jmknoble/.local/bin/yamllint", line 8, in <module>
    sys.exit(run())
             ^^^^^
  File "/Users/jmknoble/.local/share/uv/tools/yamllint/lib/python3.12/site-packages/yamllint/cli.py", line 227, in run
    prob_level = show_problems(problems, file, args_format=args.format,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jmknoble/.local/share/uv/tools/yamllint/lib/python3.12/site-packages/yamllint/cli.py", line 102, in show_problems
    for problem in problems:
                   ^^^^^^^^
  File "/Users/jmknoble/.local/share/uv/tools/yamllint/lib/python3.12/site-packages/yamllint/linter.py", line 199, in _run
    for problem in get_cosmetic_problems(buffer, conf, filepath):
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jmknoble/.local/share/uv/tools/yamllint/lib/python3.12/site-packages/yamllint/linter.py", line 136, in get_cosmetic_problems
    for problem in rule.check(rule_conf,
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jmknoble/.local/share/uv/tools/yamllint/lib/python3.12/site-packages/yamllint/rules/quoted_strings.py", line 301, in check
    not _quotes_are_needed(token.value,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jmknoble/.local/share/uv/tools/yamllint/lib/python3.12/site-packages/yamllint/rules/quoted_strings.py", line 212, in _quotes_are_needed
    loader = yaml.BaseLoader('key: ' + string)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jmknoble/.local/share/uv/tools/yamllint/lib/python3.12/site-packages/yaml/loader.py", line 14, in __init__
    Reader.__init__(self, stream)
  File "/Users/jmknoble/.local/share/uv/tools/yamllint/lib/python3.12/site-packages/yaml/reader.py", line 74, in __init__
    self.check_printable(stream)
  File "/Users/jmknoble/.local/share/uv/tools/yamllint/lib/python3.12/site-packages/yaml/reader.py", line 143, in check_printable
    raise ReaderError(self.name, position, ord(character),
yaml.reader.ReaderError: unacceptable character #x001b: special characters are not allowed
  in "<unicode string>", position 5
@adrienverge
Copy link
Owner

adrienverge commented Nov 26, 2024

Hello @jmknoble,

Thanks for the report!

I confirm the bug, reproducable with:

$ yamllint -d 'rules: {quoted-strings: {required: only-when-needed}}' - <<<'key: "\u001b"'
…
yaml.reader.ReaderError: unacceptable character #x001b: special characters are not allowed
  in "<unicode string>", position 5

A contribution to fix this would be welcome 👍

@jmknoble
Copy link
Author

jmknoble commented Dec 5, 2024

A contribution to fix this would be welcome

@adrienverge, let me know whether anything else is needed for the contributed fix in #703.

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

No branches or pull requests

2 participants