Skip to content

fix: error on raw C0 control characters in scalars - #715

Closed
mir-ashiq wants to merge 1 commit into
eemeli:mainfrom
mir-ashiq:fix/non-printable-c0-control-chars
Closed

mir-ashiq wants to merge 1 commit into
eemeli:mainfrom
mir-ashiq:fix/non-printable-c0-control-chars

Conversation

@mir-ashiq

Copy link
Copy Markdown

Fixes #703.

This adds a NON_PRINTABLE_CHAR error (a recoverable YAMLParseError) when the raw source of a scalar includes a C0 control character other than TAB, LF and CR, as those characters are excluded from the c-printable production of the YAML spec. As suggested in the issue, the check runs during composition in resolveFlowScalar() and resolveBlockScalar(), so every scalar style (plain, single-quoted, double-quoted, block literal and block folded) is covered, and each offending character is reported separately with its position.

Values that include such characters via double-quoted escape sequences (e.g. "a\x01b") keep parsing without errors, as those escapes are the spec-sanctioned way to represent control characters. Error recovery is unchanged: the scalar still resolves to its source text, and only doc.errors gains an entry.

The other c-printable exclusions (DEL, C1 control characters, U+FFFE, U+FFFF) are deliberately not flagged, as JSON allows those characters as-is within strings and this library aims to parse valid JSON as YAML; flagging them would break e.g. the y_string_unescaped_char_delete and y_string_nonCharacterInUTF-8_U+FFFF cases in the JSON test suite. Happy to extend the check (and escape those characters in the stringifier) if full c-printable enforcement is preferred.

Verification:

  • npm test — 3398 tests passed, including 11 new cases in tests/doc/errors.ts
  • npm run lint
  • npx prettier --check .
  • npm run test:types
  • npm run build
  • npm run test:dist — 3398 tests passed
  • npm run test:dist:types

ResolveFlowScalar() and resolveBlockScalar() now check the raw source of
each scalar for C0 control characters other than TAB, LF and CR, and
call onError() with a new NON_PRINTABLE_CHAR error code for each such
character. Previously such characters were silently included in the
resolved value, even though the characters excluded from the c-printable
production of the YAML spec should be verified as excluded.

The other c-printable exclusions (DEL, C1 control characters, U+FFFE,
U+FFFF) are deliberately not flagged, as JSON allows those characters
as-is within strings, and this library aims to parse valid JSON as YAML.

Fixes eemeli#703
Copilot AI lite review requested due to automatic review settings September 12, 2026 06:43

This comment was marked as spam.

@eemeli

eemeli commented Sep 12, 2026

Copy link
Copy Markdown
Owner

Please review the contributing guidelines and try again.

@eemeli eemeli closed this Sep 12, 2026
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

Successfully merging this pull request may close these issues.

No error for C0-control characters

3 participants