Skip to content

Commit 196802c

Browse files
yaml: leave the non-printable character check to igname
igname now rejects DEL, the C1 control characters,U+FFFE and U+FFFF outside of quoted scalars, including in comments, anchors and tags.
1 parent 9fcdf21 commit 196802c

3 files changed

Lines changed: 11 additions & 52 deletions

File tree

‎doc/api/formats.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ Invalid YAML throws an [`ERR_INVALID_YAML`][] `SyntaxError` with the line and
9898
column, counted in characters. The parser rejects nesting deeper than 1000
9999
levels, lone surrogates in a string input, binary input that is not valid
100100
UTF-8, and C0 control characters other than tab and line breaks.
101-
Plain and block scalars must also be printable: DEL, the C1 control characters
102-
other than NEL, U+FFFE and U+FFFF throw there. Quoted scalars accept them.
101+
DEL, the C1 control characters other than NEL, U+FFFE and U+FFFF are accepted
102+
only in quoted scalars.
103103

104104
With `throwIfInvalid: false` the parser returns `undefined` instead of
105105
building an error. A document never has the value `undefined`, so the result

‎src/node_yaml.cc‎

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include "igname/igname.hpp"
88

9-
#include <cstdio>
109
#include <memory>
1110
#include <optional>
1211
#include <string>
@@ -43,29 +42,6 @@ constexpr int kMaxKeyLength = 1024;
4342
// Mappings with more keys than this use a hash table to find duplicates.
4443
constexpr size_t kLinearKeySearch = 16;
4544

46-
// Returns the first character of `text` that is outside of the printable
47-
// characters of YAML 1.2 (DEL, the C1 control characters other than NEL,
48-
// U+FFFE and U+FFFF), or 0. The parser already rejects the C0 control
49-
// characters.
50-
uint32_t FindNonPrintable(std::string_view text) {
51-
const size_t length = text.size();
52-
for (size_t i = 0; i < length; i++) {
53-
const uint8_t c = static_cast<uint8_t>(text[i]);
54-
if (c < 0x7F) continue;
55-
if (c == 0x7F) return c;
56-
if (c == 0xC2 && i + 1 < length) {
57-
const uint8_t next = static_cast<uint8_t>(text[i + 1]);
58-
if (next >= 0x80 && next <= 0x9F && next != 0x85) return next;
59-
} else if (c == 0xEF && i + 2 < length &&
60-
static_cast<uint8_t>(text[i + 1]) == 0xBF) {
61-
const uint8_t last = static_cast<uint8_t>(text[i + 2]);
62-
if (last == 0xBE) return 0xFFFE;
63-
if (last == 0xBF) return 0xFFFF;
64-
}
65-
}
66-
return 0;
67-
}
68-
6945
constexpr std::string_view kScalarTags[] = {
7046
"str", "int", "float", "bool", "null"};
7147

@@ -153,20 +129,6 @@ class ValueBuilder : public igname::BaseHandler {
153129
igname::ScalarStyle style,
154130
const igname::NodeInfo& info) {
155131
if (extra_document_) return FailExtraDocument(info.mark);
156-
// Quoted scalars may hold any character other than the C0 control
157-
// characters. Plain and block scalars are limited to printable ones.
158-
if (style != igname::ScalarStyle::kSingleQuoted &&
159-
style != igname::ScalarStyle::kDoubleQuoted) {
160-
const uint32_t c = FindNonPrintable(text);
161-
if (c != 0) {
162-
char hex[16];
163-
snprintf(hex, sizeof(hex), "%x", c);
164-
return Fail(
165-
info.mark,
166-
std::string("Character U+") + hex + " is not allowed in YAML");
167-
}
168-
}
169-
170132
igname::ScalarValue scalar;
171133
switch (igname::ResolveScalar(text, style, info.tag, &scalar)) {
172134
case igname::ResolveStatus::kOk:

‎test/parallel/test-yaml.js‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ for (const [input, position] of [
303303
['\uFEFF{\u00e9: 1, \u00e9: 2}', 'line 1, column 8'],
304304
['x: 1\r\n\u00e9: [\u{1F600}, *nope]', 'line 2, column 8'],
305305
['a: 1\rb: 2\r\u20ac\u20ac: !!int z', 'line 3, column 5'],
306-
['a: 1\n\u00e9\u00e9: [\u00e9, a\x7Fb]', 'line 2, column 9'],
307306
['\u00e9\u00e9: [1', 'line 1, column 7'],
308307
]) {
309308
for (const value of [input, Buffer.from(input)]) {
@@ -336,20 +335,18 @@ for (const c of ['\x01', '\x07', '\x1f']) {
336335
}
337336
}
338337

339-
// DEL, the C1 control characters and the non-characters are not printable:
340-
// they are rejected in plain and block scalars and accepted in quoted ones.
341-
for (const [c, hex] of [['\x7f', '7f'], ['\x80', '80'], ['\x9f', '9f'], ['\ufffe', 'fffe'], ['\uffff', 'ffff']]) {
342-
const expected = {
343-
code: 'ERR_INVALID_YAML',
344-
name: 'SyntaxError',
345-
message: new RegExp(`Character U\\+${hex} is not allowed in YAML \\(line \\d+, column \\d+\\)`),
346-
};
347-
for (const input of [`a${c}b`, `k: a${c}b`, `a${c}b: 1`, `[a${c}b]`, `|\n a${c}b`, `>\n a${c}b`, `caf\u00e9${c}`]) {
348-
assert.throws(() => parse(input), expected);
338+
// DEL, the C1 control characters and the non-characters are accepted in
339+
// quoted scalars only. The parser checks it, also in comments.
340+
for (const c of ['\x7f', '\x80', '\ufffe']) {
341+
for (const input of [`a${c}b`, `a: 1 # ${c}`]) {
342+
assert.throws(() => parse(input), {
343+
code: 'ERR_INVALID_YAML',
344+
name: 'SyntaxError',
345+
message: / \(line 1, column \d+\)$/,
346+
});
349347
assert.strictEqual(parse(input, { throwIfInvalid: false }), undefined);
350348
}
351349
assert.strictEqual(parse(`"a${c}b"`), `a${c}b`);
352-
assert.strictEqual(parse(`'a${c}b'`), `a${c}b`);
353350
}
354351
// NEL, the other characters above U+009F and escapes are accepted.
355352
assert.strictEqual(parse('a\u0085b \u00a0\u00e9\ufffd\u{1F600}'), 'a\u0085b \u00a0\u00e9\ufffd\u{1F600}');

0 commit comments

Comments
 (0)