fix(linter): 🐛 yaml parser with escape in regex
This commit is contained in:
@@ -24,6 +24,8 @@ func Parse(path string) (*Pipeline, error) {
|
||||
|
||||
// ParseBytes parses YAML from an in-memory byte slice.
|
||||
func ParseBytes(data []byte) (*Pipeline, error) {
|
||||
data = sanitizeYAMLEscapes(data)
|
||||
|
||||
// First pass: parse into a yaml.Node document to extract job keys with
|
||||
// their exact source line numbers (key nodes carry the line, value nodes
|
||||
// carry the body we decode into Job / map[string]any).
|
||||
@@ -75,3 +77,63 @@ func ParseBytes(data []byte) (*Pipeline, error) {
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// sanitizeYAMLEscapes rewrites double-quoted YAML strings, replacing the \/
|
||||
// escape sequence (unrecognised by gopkg.in/yaml.v3) with \\/ so that the
|
||||
// parser produces a literal backslash+slash — preserving regex patterns like
|
||||
// /^us\// that appear in GitLab CI if: expressions.
|
||||
func sanitizeYAMLEscapes(data []byte) []byte {
|
||||
type state int
|
||||
const (
|
||||
stOutside state = iota
|
||||
stSingleQ
|
||||
stDoubleQ
|
||||
stEscape
|
||||
)
|
||||
|
||||
out := make([]byte, 0, len(data))
|
||||
s := stOutside
|
||||
|
||||
for i := 0; i < len(data); i++ {
|
||||
b := data[i]
|
||||
switch s {
|
||||
case stOutside:
|
||||
out = append(out, b)
|
||||
switch b {
|
||||
case '"':
|
||||
s = stDoubleQ
|
||||
case '\'':
|
||||
s = stSingleQ
|
||||
}
|
||||
case stSingleQ:
|
||||
out = append(out, b)
|
||||
if b == '\'' {
|
||||
if i+1 < len(data) && data[i+1] == '\'' {
|
||||
// '' inside a single-quoted string is an escaped single-quote
|
||||
out = append(out, data[i+1])
|
||||
i++
|
||||
} else {
|
||||
s = stOutside
|
||||
}
|
||||
}
|
||||
case stDoubleQ:
|
||||
out = append(out, b)
|
||||
switch b {
|
||||
case '\\':
|
||||
s = stEscape
|
||||
case '"':
|
||||
s = stOutside
|
||||
}
|
||||
case stEscape:
|
||||
if b == '/' {
|
||||
// \/ is not recognised by yaml.v3; rewrite as \\/ which
|
||||
// the parser resolves to a literal backslash + slash.
|
||||
out = append(out, '\\', '/')
|
||||
} else {
|
||||
out = append(out, b)
|
||||
}
|
||||
s = stDoubleQ
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user