From 3d7d34aeb2bc27dc386dc5ace6d5600c5ac1d67a Mon Sep 17 00:00:00 2001 From: RoomWithOutRoof <166608075+Jah-yee@users.noreply.github.com> Date: Fri, 1 May 2026 19:14:30 +0800 Subject: [PATCH] fix & test: throw error on zero-indentation block scalar (close #280) (#746) --- lib/loader.js | 7 +++++++ test/issues/0144.js | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/loader.js b/lib/loader.js index 25abc808..692b1d3d 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -902,6 +902,13 @@ function readBlockScalar(state, nodeIndent) { continue; } + // Zero-indentation block scalar is not allowed. + // If textIndent is still 0 at this point, it means no explicit indentation + // indicator was given and no indentation was detected in content lines. + if (!detectedIndent && textIndent === 0) { + throwError(state, 'missing indentation for block scalar'); + } + // End of the scalar. if (state.lineIndent < textIndent) { diff --git a/test/issues/0144.js b/test/issues/0144.js index 4abfb6a9..1abc90c9 100644 --- a/test/issues/0144.js +++ b/test/issues/0144.js @@ -1,10 +1,10 @@ 'use strict'; - var assert = require('assert'); var yaml = require('../../'); it('Infinite loop when attempting to parse multi-line scalar document that is not indented', function () { - assert.strictEqual(yaml.load('--- |\nfoo\n'), 'foo\n'); + // Block scalar with zero indentation should throw an error (issue #280) + assert.throws(() => yaml.load('--- |\nfoo\n'), /missing indentation for block scalar/); });