Skip to content

Commit 5bfeffc

Browse files
authored
Merge pull request #1616 from cloudflare/parse
Fix panic when parsing labels:[]
2 parents 1cce7a2 + 9cbd277 commit 5bfeffc

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

docs/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v0.77.1
4+
5+
### Fixed
6+
7+
- Fixed a panic when parsing incomplete rules using relaxed mode.
8+
39
## v0.77.0
410

511
### Added

internal/parser/parser.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,12 @@ func (p Parser) parseRule(node *yaml.Node, offsetLine, offsetColumn int, content
374374
}
375375
}
376376

377-
for _, entry := range []struct {
378-
part *yaml.Node
379-
key string
380-
}{
381-
{key: labelsKey, part: labelsNode},
382-
{key: annotationsKey, part: annotationsNode},
383-
} {
384-
if entry.part != nil && !isTag(entry.part.ShortTag(), mapTag) {
385-
return invalidValueError(lines, entry.part.Line+offsetLine, entry.key, describeTag(mapTag), describeTag(entry.part.ShortTag()))
386-
}
377+
if (recordPart != nil || alertPart != nil) && exprPart != nil && labelsNode != nil && !isTag(labelsNode.ShortTag(), mapTag) {
378+
return invalidValueError(lines, labelsNode.Line+offsetLine, labelsKey, describeTag(mapTag), describeTag(labelsNode.ShortTag()))
379+
}
380+
381+
if alertPart != nil && exprPart != nil && annotationsNode != nil && !isTag(annotationsNode.ShortTag(), mapTag) {
382+
return invalidValueError(lines, annotationsNode.Line+offsetLine, annotationsKey, describeTag(mapTag), describeTag(annotationsNode.ShortTag()))
387383
}
388384

389385
if ok, perr, plines := validateStringMap(labelsKey, labelsNodes, offsetLine, lines); !ok {
@@ -631,6 +627,9 @@ type yamlMap struct {
631627
}
632628

633629
func mappingNodes(node *yaml.Node) []yamlMap {
630+
if node.Kind != yaml.MappingNode {
631+
return nil
632+
}
634633
m := make([]yamlMap, 0, len(node.Content)/2)
635634
for i := 0; i < len(node.Content); i += 2 {
636635
m = append(m, yamlMap{key: node.Content[i], val: node.Content[i+1]})

internal/parser/parser_test.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,6 +2942,8 @@ groups:
29422942
- name: foo
29432943
rules:
29442944
- labels: !!binary "SGVsbG8sIFdvcmxkIQ=="
2945+
record: foo
2946+
expr: foo
29452947
`),
29462948
strict: true,
29472949
output: parser.File{
@@ -2950,7 +2952,7 @@ groups:
29502952
Name: "foo",
29512953
Rules: []parser.Rule{
29522954
{
2953-
Lines: diags.LineRange{First: 8, Last: 8},
2955+
Lines: diags.LineRange{First: 8, Last: 10},
29542956
Error: parser.ParseError{
29552957
Line: 8,
29562958
Err: errors.New("labels value must be a mapping, got binary data instead"),
@@ -3564,6 +3566,8 @@ groups:
35643566
- name: foo
35653567
rules:
35663568
- labels: !!binary "SGVsbG8sIFdvcmxkIQ=="
3569+
record: foo
3570+
expr: foo
35673571
`),
35683572
strict: true,
35693573
output: parser.File{
@@ -3572,7 +3576,7 @@ groups:
35723576
Name: "foo",
35733577
Rules: []parser.Rule{
35743578
{
3575-
Lines: diags.LineRange{First: 5, Last: 5},
3579+
Lines: diags.LineRange{First: 5, Last: 7},
35763580
Error: parser.ParseError{
35773581
Line: 5,
35783582
Err: errors.New("labels value must be a mapping, got binary data instead"),
@@ -5131,6 +5135,37 @@ groups:
51315135
},
51325136
},
51335137
},
5138+
{
5139+
input: []byte(`
5140+
apiVersion: v1
5141+
kind: ConfigMap
5142+
metadata:
5143+
labels:
5144+
shard: "0"
5145+
total-shards: "1"
5146+
name: shard-jobs
5147+
namespace: foo
5148+
data:
5149+
jobs.yaml: |
5150+
jobs:
5151+
- name: foo
5152+
interval: 1m
5153+
queries:
5154+
- name: xxx
5155+
help: ''
5156+
labels:
5157+
- account_id
5158+
- bucket
5159+
- cache_status
5160+
mtls_identity:
5161+
cert_path: /etc/identity/tls.crt
5162+
key_path: /etc/identity/tls.key
5163+
`),
5164+
output: parser.File{
5165+
Groups: nil,
5166+
IsRelaxed: true,
5167+
},
5168+
},
51345169
}
51355170

51365171
alwaysEqual := cmp.Comparer(func(_, _ any) bool { return true })

0 commit comments

Comments
 (0)