-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssert-YamlMergeGraph.ps1
More file actions
125 lines (113 loc) · 4.46 KB
/
Copy pathAssert-YamlMergeGraph.ps1
File metadata and controls
125 lines (113 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function Assert-YamlMergeGraph {
<#
.SYNOPSIS
Validates the merged representation graph and its resource budgets.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[AllowEmptyCollection()]
[object[]] $Documents,
[Parameter(Mandatory)]
[int] $Depth,
[Parameter(Mandatory)]
[int] $MaxNodes,
[Parameter(Mandatory)]
[int] $MaxAliases,
[Parameter(Mandatory)]
[int] $MaxScalarLength,
[Parameter(Mandatory)]
[int] $MaxTagLength,
[Parameter(Mandatory)]
[int] $MaxTotalTagLength
)
$visited = [System.Collections.Generic.HashSet[int]]::new()
$pending = [System.Collections.Generic.Stack[object]]::new()
foreach ($document in $Documents) {
$pending.Push([pscustomobject]@{ Node = $document; Depth = 1 })
}
$nodeCount = 0
$aliasCount = 0
$totalTagLength = 0L
while ($pending.Count -gt 0) {
$item = $pending.Pop()
$node = $item.Node
if (-not $visited.Add($node.Id)) {
continue
}
$nodeCount++
if ($nodeCount -gt $MaxNodes) {
throw (New-YamlMergeException -Node $node -ErrorId 'YamlMergeNodeLimitExceeded' -Message (
"The merged YAML graph exceeds the configured limit of $MaxNodes nodes."
))
}
if ($item.Depth -gt $Depth) {
throw (New-YamlMergeException -Node $node -ErrorId 'YamlMergeDepthLimitExceeded' -Message (
"The merged YAML graph exceeds the configured depth of $Depth."
))
}
if ($node.Kind -eq 'Alias') {
$aliasCount++
if ($aliasCount -gt $MaxAliases) {
throw (New-YamlMergeException -Node $node -ErrorId 'YamlMergeAliasLimitExceeded' -Message (
"The merged YAML graph exceeds the configured limit of $MaxAliases aliases."
))
}
$pending.Push([pscustomobject]@{ Node = $node.Target; Depth = $item.Depth })
continue
}
$tagLength = ([string] $node.Tag).Length
if ($tagLength -gt $MaxTagLength) {
throw (New-YamlMergeException -Node $node -ErrorId 'YamlMergeTagLimitExceeded' -Message (
"A tag in the merged YAML graph exceeds the configured limit of $MaxTagLength characters."
))
}
$totalTagLength += $tagLength
if ($totalTagLength -gt $MaxTotalTagLength) {
throw (New-YamlMergeException -Node $node -ErrorId 'YamlMergeTagLimitExceeded' -Message (
"The merged YAML graph exceeds the configured cumulative tag limit of " +
"$MaxTotalTagLength characters."
))
}
if ($node.Kind -eq 'Scalar') {
if ((Get-YamlRuneCount -Text ([string] $node.Value)) -gt $MaxScalarLength) {
throw (New-YamlMergeException -Node $node `
-ErrorId 'YamlMergeScalarLimitExceeded' -Message (
"A scalar in the merged YAML graph exceeds the configured limit of " +
"$MaxScalarLength characters."
))
}
continue
}
if ($node.Kind -eq 'Sequence') {
for ($index = $node.Items.Count - 1; $index -ge 0; $index--) {
$pending.Push([pscustomobject]@{
Node = $node.Items[$index]
Depth = $item.Depth + 1
})
}
continue
}
for ($index = $node.Entries.Count - 1; $index -ge 0; $index--) {
$pending.Push([pscustomobject]@{
Node = $node.Entries[$index].Value
Depth = $item.Depth + 1
})
$pending.Push([pscustomobject]@{
Node = $node.Entries[$index].Key
Depth = $item.Depth + 1
})
}
}
$fingerprintCache = [System.Collections.Generic.Dictionary[int, string]]::new()
$fingerprintHasher = [System.Security.Cryptography.SHA256]::Create()
try {
foreach ($document in $Documents) {
Test-YamlNodeGraph -Node $document `
-Visited ([System.Collections.Generic.HashSet[int]]::new()) `
-FingerprintCache $fingerprintCache -FingerprintHasher $fingerprintHasher
}
} finally {
$fingerprintHasher.Dispose()
}
}