From e26fe9078e8879006fdc4be1f34983d084ccf114 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Wed, 13 May 2015 14:29:47 +0300 Subject: [PATCH] Process astral characters correctly According to YAML 1.2 spec, #xD800-#xDFFF unicode range is considered non-printables. But unicode range #x10000-#x10FFFF, represented in UTF-16 by the same characters is not. NOTE: Non-printable characters are still allowed in quoted scalars, added a pending test for this case. Fixes: https://github.com/nodeca/js-yaml/issues/191 --- lib/js-yaml/loader.js | 2 +- test/units/character-set.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/units/character-set.js diff --git a/lib/js-yaml/loader.js b/lib/js-yaml/loader.js index af1b99b3..1012ff56 100644 --- a/lib/js-yaml/loader.js +++ b/lib/js-yaml/loader.js @@ -23,7 +23,7 @@ var CHOMPING_STRIP = 2; var CHOMPING_KEEP = 3; -var PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uD800-\uDFFF\uFFFE\uFFFF]/; +var PATTERN_NON_PRINTABLE = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x84\x86-\x9F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/; var PATTERN_NON_ASCII_LINE_BREAKS = /[\x85\u2028\u2029]/; var PATTERN_FLOW_INDICATORS = /[,\[\]\{\}]/; var PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i; diff --git a/test/units/character-set.js b/test/units/character-set.js new file mode 100644 index 00000000..86116ca9 --- /dev/null +++ b/test/units/character-set.js @@ -0,0 +1,25 @@ +'use strict'; + + +var assert = require('assert'); +var yaml = require('../../'); + + +test('Allow astral characters', function () { + assert.deepEqual(yaml.load('𝑘𝑒𝑦: 𝑣𝑎𝑙𝑢𝑒'), { '𝑘𝑒𝑦': '𝑣𝑎𝑙𝑢𝑒' }); +}); + +test('Forbid non-printable characters', function () { + assert.throws(function () { yaml.load('\x01'); }, yaml.YAMLException); + assert.throws(function () { yaml.load('\x7f'); }, yaml.YAMLException); + assert.throws(function () { yaml.load('\x9f'); }, yaml.YAMLException); +}); + +test('Forbid lone surrogates', function () { + assert.throws(function () { yaml.load('\udc00\ud800'); }, yaml.YAMLException); +}); + +test.skip('Allow non-printable characters inside quoted scalars', function () { + assert.strictEqual(yaml.load('"\x7f\x9f\udc00\ud800"'), '\x7f\x9f\udc00\ud800'); +}); +