Skip to content

Commit a24c58a

Browse files
Close final representation edge cases
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent dc8bda3 commit a24c58a

14 files changed

Lines changed: 66 additions & 15 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function Assert-YamlNoByteOrderMark {
2+
<#
3+
.SYNOPSIS
4+
Rejects a raw byte order mark outside a quoted scalar or document prefix.
5+
#>
6+
[CmdletBinding()]
7+
param (
8+
[Parameter(Mandatory)]
9+
[AllowEmptyString()]
10+
[string] $Text,
11+
12+
[Parameter(Mandatory)]
13+
[pscustomobject] $Mark
14+
)
15+
16+
if ($Text.IndexOf([char] 0xFEFF) -ge 0) {
17+
throw (New-YamlException -Start $Mark -End $Mark -ErrorId 'YamlInvalidByteOrderMark' -Message (
18+
'A raw YAML byte order mark is only allowed in a quoted scalar or document prefix.'
19+
))
20+
}
21+
}

src/functions/private/ConvertFrom-YamlByteOrderMark.ps1

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,7 @@ function ConvertFrom-YamlByteOrderMark {
3232
continue
3333
}
3434

35-
$before = $Text.Substring(0, $index)
36-
$line = ([regex]::Matches($before, "`n")).Count
37-
$lastBreak = $before.LastIndexOf("`n", [System.StringComparison]::Ordinal)
38-
$column = if ($lastBreak -lt 0) { $before.Length } else { $before.Length - $lastBreak - 1 }
39-
$mark = New-YamlMark -Index $index -Line $line -Column $column
40-
throw (New-YamlException -Start $mark -End $mark -ErrorId 'YamlInvalidByteOrderMark' -Message (
41-
'A YAML byte order mark is only allowed at the start of the stream or a document prefix.'
42-
))
35+
[void] $builder.Append($Text[$index])
4336
}
4437

4538
return $builder.ToString()

src/functions/private/ConvertTo-YamlQuotedText.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function ConvertTo-YamlQuotedText {
5050
[void] $builder.Append('\"')
5151
} elseif ($code -eq 92) {
5252
[void] $builder.Append('\\')
53-
} elseif ($code -lt 0x20 -or $code -eq 0x7F -or
53+
} elseif ($code -lt 0x20 -or $code -eq 0x7F -or $code -eq 0xFEFF -or
5454
$code -eq 0xFFFE -or $code -eq 0xFFFF -or
5555
($code -ge 0x80 -and $code -le 0x9F)) {
5656
[void] $builder.Append(('\u{0:X4}' -f $code))

src/functions/private/Get-YamlEffectiveTag.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ function Get-YamlEffectiveTag {
1414
[object] $Value
1515
)
1616

17-
if (-not [string]::IsNullOrEmpty($Node.Tag) -and
18-
-not $Node.Tag.Equals('!', [System.StringComparison]::Ordinal)) {
17+
if (-not [string]::IsNullOrEmpty($Node.Tag)) {
1918
return $Node.Tag
2019
}
2120
if ($Node.Kind.Equals('Sequence', [System.StringComparison]::Ordinal)) {
@@ -24,7 +23,7 @@ function Get-YamlEffectiveTag {
2423
if ($Node.Kind.Equals('Mapping', [System.StringComparison]::Ordinal)) {
2524
return 'tag:yaml.org,2002:map'
2625
}
27-
if ($Node.Tag.Equals('!', [System.StringComparison]::Ordinal)) {
26+
if ($Node.HasUnknownTag) {
2827
return 'tag:yaml.org,2002:str'
2928
}
3029

src/functions/private/Read-YamlBlockKey.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function Read-YamlBlockKey {
6262
return $node
6363
}
6464

65+
Assert-YamlNoByteOrderMark -Text $rest -Mark $start
6566
$first = $rest[0]
6667
if ($first -in @(',', '[', ']', '{', '}', '#', '&', '*', '!', '|', '>', "'", '"', '%', '@', '`') -or
6768
($first -in @('-', '?', ':') -and (

src/functions/private/Read-YamlBlockMapping.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ function Read-YamlBlockMapping {
191191
$key = Read-YamlBlockKey -Context $Context -Text $keyText -Line $lineNumber `
192192
-Column $contentColumn -Depth ($Depth + 1)
193193
}
194-
if ($colon -gt 0 -and (Get-YamlRuneCount -Text $keyText) -gt 1024) {
194+
if ($colon -gt 0 -and
195+
(Get-YamlRuneCount -Text $content.Substring(0, $colon)) -gt 1024) {
195196
$mark = New-YamlMark -Index ($Context.LineStarts[$lineNumber] + $contentColumn + $colon) `
196197
-Line $lineNumber -Column ($contentColumn + $colon)
197198
throw (New-YamlException -Start $mark -End $mark -ErrorId 'YamlInvalidImplicitKey' -Message (

src/functions/private/Read-YamlBlockScalar.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function Read-YamlBlockScalar {
9797
$decodedLength = 0
9898
while ($Context.LineIndex -lt $Context.Lines.Count) {
9999
$line = $Context.Lines[$Context.LineIndex]
100+
Assert-YamlNoByteOrderMark -Text $line -Mark $start
100101
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)') {
101102
break
102103
}

src/functions/private/Read-YamlFlowNode.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ function Read-YamlFlowNode {
9090
Move-YamlCursor -Cursor $Cursor -Context $Context
9191
}
9292
$anchor = $Context.Text.Substring($anchorStart, $Cursor.Index - $anchorStart)
93+
Assert-YamlNoByteOrderMark -Text $anchor -Mark $start
9394
if ([string]::IsNullOrEmpty($anchor)) {
9495
throw (New-YamlException -Start $start -End $start -ErrorId 'YamlInvalidAnchor' -Message (
9596
'A YAML anchor name is missing.'
@@ -137,6 +138,7 @@ function Read-YamlFlowNode {
137138
Move-YamlCursor -Cursor $Cursor -Context $Context
138139
}
139140
$alias = $Context.Text.Substring($aliasStart, $Cursor.Index - $aliasStart)
141+
Assert-YamlNoByteOrderMark -Text $alias -Mark $start
140142
$Context.AliasCount++
141143
if ($Context.AliasCount -gt $Context.MaxAliases) {
142144
throw (New-YamlException -Start $start -End $start -ErrorId 'YamlAliasLimitExceeded' -Message (
@@ -544,6 +546,12 @@ function Read-YamlFlowNode {
544546
}
545547
while ($Cursor.Index -lt $Context.Text.Length) {
546548
$character = $Context.Text[$Cursor.Index]
549+
if ($character -ceq [char] 0xFEFF) {
550+
$mark = New-YamlMark -Index $Cursor.Index -Line $Cursor.Line -Column $Cursor.Column
551+
throw (New-YamlException -Start $mark -End $mark -ErrorId 'YamlInvalidByteOrderMark' -Message (
552+
'A raw YAML byte order mark is only allowed in a quoted scalar or document prefix.'
553+
))
554+
}
547555
if ($character -in @(',', '[', ']', '{', '}')) {
548556
break
549557
}

src/functions/private/Read-YamlNodeProperty.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ function Read-YamlNodeProperty {
8484
$position++
8585
}
8686
$anchor = $Text.Substring($start, $position - $start)
87+
Assert-YamlNoByteOrderMark -Text $anchor -Mark (
88+
New-YamlMark -Index ($Context.LineStarts[$Line] + $Column + $start) -Line $Line `
89+
-Column ($Column + $start)
90+
)
8791
if ([string]::IsNullOrEmpty($anchor) -or $anchor.IndexOfAny(@('[', ']', '{', '}', ',')) -ge 0) {
8892
$mark = New-YamlMark -Index ($Context.LineStarts[$Line] + $Column + $start - 1) -Line $Line `
8993
-Column ($Column + $start - 1)

src/functions/private/Read-YamlPlainScalar.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function Read-YamlPlainScalar {
3737
$parts = [System.Collections.Generic.List[object]]::new()
3838
$firstComment = Find-YamlCommentStart -Text $FirstText
3939
$firstValue = (Get-YamlContentWithoutComment -Text $FirstText).Trim(' ', "`t")
40+
Assert-YamlNoByteOrderMark -Text $firstValue -Mark $start
4041
$firstCharacter = if ($firstValue.Length -gt 0) { $firstValue[0] } else { [char] 0 }
4142
$forbiddenFirst = $firstCharacter -in @(
4243
',', '[', ']', '{', '}', '#', '&', '*', '!', '|', '>', "'", '"', '%', '@', '`'
@@ -91,6 +92,7 @@ function Read-YamlPlainScalar {
9192
break
9293
}
9394
$trimmedContent = $content.Trim(' ', "`t")
95+
Assert-YamlNoByteOrderMark -Text $trimmedContent -Mark $start
9496
$separatorLength = if ($pendingBreaks -gt 0) { $pendingBreaks } else { 1 }
9597
if ($decodedLength + $separatorLength + $trimmedContent.Length -gt
9698
$Context.MaxScalarLength) {

0 commit comments

Comments
 (0)