From f0f205bd20534bfd31f12a29d23c1d7cf60fc899 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Tue, 29 Dec 2020 16:09:03 +0300 Subject: [PATCH] Fix parsing of invalid block mappings close https://github.com/nodeca/js-yaml/issues/418 --- CHANGELOG.md | 1 + lib/loader.js | 2 +- test/issues/0418.js | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/issues/0418.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f44da6b3..e2cd11d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Tags are now url-decoded in `load()` and url-encoded in `dump()` (previously usage of custom non-ascii tags may have led to invalid YAML that can't be parsed). - Anchors now work correctly with empty nodes, #301. +- Fix incorrect parsing of invalid block mapping syntax, #418. ## [3.14.1] - 2020-12-07 diff --git a/lib/loader.js b/lib/loader.js index f5adab25..b6dad5b3 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -1153,7 +1153,7 @@ function readBlockMapping(state, nodeIndent, flowIndent) { ch = state.input.charCodeAt(state.position); } - if (state.lineIndent > nodeIndent && (ch !== 0)) { + if ((state.line === _line || state.lineIndent > nodeIndent) && (ch !== 0)) { throwError(state, 'bad indentation of a mapping entry'); } else if (state.lineIndent < nodeIndent) { break; diff --git a/test/issues/0418.js b/test/issues/0418.js new file mode 100644 index 00000000..94754dcf --- /dev/null +++ b/test/issues/0418.js @@ -0,0 +1,11 @@ +'use strict'; + + +const assert = require('assert'); +const yaml = require('../../'); + + +it('should error on invalid indentation in mappings', function () { + assert.throws(() => yaml.load('foo: "1" bar: "2"'), /bad indentation of a mapping entry/); + assert.throws(() => yaml.load('- "foo" - "bar"'), /bad indentation of a sequence entry/); +});