fix(parsing): validate %YAML directive parameters and reject duplicates

The %YAML directive takes exactly one parameter, the version, and at most
one may appear per document. The scanner checked only that a valid version
was present: it ignored any further tokens, so '%YAML 1.2 foo' was
accepted, and it did not track that a %YAML directive had already been
seen, so a repeated directive was accepted too. It now rejects an extra
parameter (allowing only a trailing comment) and a second %YAML directive,
resetting the per-document flag at each document boundary. A well-formed
directive, with or without a trailing comment, is unaffected.

Measured against the official YAML test-suite, parse-level conformance
rises from 251/333 (75.4%) to 253/333 (76.0%) with no regression.
This commit is contained in:
Mihaela M 2026-06-27 16:33:03 +02:00
parent 97584dc57c
commit 4c77b17f9f
5 changed files with 57 additions and 2 deletions

View File

@ -23,6 +23,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- The `%YAML` directive is now validated. It takes exactly one parameter, the version, optionally
followed by a comment, and at most one may appear per document; an extra parameter
(`%YAML 1.2 foo`) or a second `%YAML` directive is now rejected. Previously any tokens after the
version were ignored and duplicate directives were accepted. A well-formed directive, with or
without a trailing comment, is unaffected. Lifts official test-suite parse-level conformance
from 251/333 (75.4%) to 253/333 (76.0%), with no regression.
- A flow collection no longer accepts an empty entry. A comma separates entries, so one must
precede it; a leading comma (`[ , a ]`), a repeated comma (`[ a, , b ]`), or the same in a flow
mapping is now rejected, while a single trailing comma (`[ a, b, ]`) and empty collections

View File

@ -20,6 +20,8 @@ public extension PureYAML.Parsing {
case nestingTooDeep(line: Int, column: Int)
case commentNotPrecededByWhitespace(line: Int, column: Int)
case invalidEscape(sequence: String, line: Int)
case malformedDirective(name: String, line: Int)
case duplicateDirective(name: String, line: Int)
public var description: String {
switch self {
@ -61,6 +63,10 @@ public extension PureYAML.Parsing {
"a comment must be preceded by whitespace at line \(line), column \(column)"
case let .invalidEscape(sequence, line):
"invalid escape sequence '\(sequence)' at line \(line)"
case let .malformedDirective(name, line):
"malformed directive '\(name)' at line \(line)"
case let .duplicateDirective(name, line):
"duplicate directive '\(name)' at line \(line)"
}
}
@ -104,6 +110,10 @@ public extension PureYAML.Parsing {
"commentNotPrecededByWhitespace"
case .invalidEscape:
"invalidEscape"
case .malformedDirective:
"malformedDirective"
case .duplicateDirective:
"duplicateDirective"
}
}
@ -118,7 +128,9 @@ public extension PureYAML.Parsing {
let .unterminatedQuotedString(line),
let .unsupportedDirective(_, line),
let .unsupportedMultiDocumentStream(line),
let .invalidEscape(_, line):
let .invalidEscape(_, line),
let .malformedDirective(_, line),
let .duplicateDirective(_, line):
line
case let .expectedNode(line, _),
let .expectedScalarKey(line, _),
@ -144,7 +156,9 @@ public extension PureYAML.Parsing {
.unterminatedQuotedString,
.unsupportedDirective,
.unsupportedMultiDocumentStream,
.invalidEscape:
.invalidEscape,
.malformedDirective,
.duplicateDirective:
nil
case let .expectedNode(_, column),
let .expectedScalarKey(_, column),

View File

@ -58,12 +58,14 @@ extension PureYAML.Parsing.Scanner {
state.hasDocumentContent = false
state.hasDocumentStartMarker = true
state.isDocumentClosed = false
state.hasYAMLDirective = false
}
func resetForDocumentEnd(_ state: inout State) {
state.hasDocumentContent = false
state.hasDocumentStartMarker = false
state.isDocumentClosed = true
state.hasYAMLDirective = false
}
func scanDirective(_ state: inout State) throws {
@ -83,6 +85,15 @@ extension PureYAML.Parsing.Scanner {
guard parts.indices.contains(1), parts[1] == "1.1" || parts[1] == "1.2" else {
throw PureYAML.Parsing.ParseError.incompatibleYAMLDirective(line: start.line)
}
// `%YAML` takes exactly one parameter, the version; only a comment may follow it.
guard parts.count == 2 || parts[2].hasPrefix("#") else {
throw PureYAML.Parsing.ParseError.malformedDirective(name: name, line: start.line)
}
// At most one `%YAML` directive is allowed per document.
guard !state.hasYAMLDirective else {
throw PureYAML.Parsing.ParseError.duplicateDirective(name: name, line: start.line)
}
state.hasYAMLDirective = true
case "%TAG":
guard parts.count >= 3 else {
return

View File

@ -48,6 +48,7 @@ extension PureYAML.Parsing.Scanner {
var hasDocumentContent = false
var hasDocumentStartMarker = false
var isDocumentClosed = false
var hasYAMLDirective = false
var flowDepth = 0
var flowContexts: [PureYAML.Parsing.ScannerFlowContext] = []
var validImplicitIndentation: Set<Int> = []

View File

@ -0,0 +1,23 @@
@testable import PureYAML
import Testing
@Suite("%YAML directive validation")
struct YAMLDirectiveTests {
@Test("A malformed or repeated %YAML directive is rejected", arguments: [
"%YAML 1.2 foo\n---\nx\n", // an extra parameter after the version
"%YAML 1.2\n%YAML 1.2\n---\nx\n", // a second %YAML directive
"%YAML 1.3\n---\nx\n", // an unsupported version
])
func test_invalidYAMLDirectiveIsRejected(_ yaml: String) throws {
#expect(throws: PureYAML.Parsing.ParseError.self) {
try PureYAML.parse(yaml)
}
}
@Test("A well-formed %YAML directive, with an optional trailing comment, is accepted")
func test_validYAMLDirectiveAccepted() throws {
let expected = PureYAML.Model.Value.string("x")
#expect(try PureYAML.parse("%YAML 1.2\n---\nx\n") == expected)
#expect(try PureYAML.parse("%YAML 1.2 # version\n---\nx\n") == expected)
}
}