Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid path #598

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ end:
}

func parsePathDot(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) {
if b.root == nil || b.node == nil {
return nil, nil, 0, fmt.Errorf("required '$' character at first: %w", ErrInvalidPathString)
}
length := len(buf)
if cursor+1 < length && buf[cursor+1] == '.' {
b, buf, c, err := parsePathRecursive(b, buf, cursor)
Expand Down Expand Up @@ -119,6 +122,10 @@ end:
}

func parseQuotedKey(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) {
if b.root == nil || b.node == nil {
return nil, nil, 0, fmt.Errorf("required '$' character at first: %w", ErrInvalidPathString)
}

cursor++ // skip single quote
start := cursor
length := len(buf)
Expand Down Expand Up @@ -156,6 +163,10 @@ end:
}

func parsePathIndex(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) {
if b.root == nil || b.node == nil {
return nil, nil, 0, fmt.Errorf("required '$' character at first: %w", ErrInvalidPathString)
}

length := len(buf)
cursor++ // skip '[' character
if length <= cursor {
Expand Down
27 changes: 27 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,33 @@ building:
}
}

func TestInvalidPath(t *testing.T) {
tests := []struct {
name string
path string
}{
{
name: "missing root with dot",
path: ".foo",
},
{
name: "missing root with index",
path: "foo[0]",
},
{
name: "missing root with recursive",
path: "..foo",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if _, err := yaml.PathString(test.path); err == nil {
t.Fatal("expected error")
}
})
}
}

func ExamplePath_AnnotateSource() {
yml := `
a: 1
Expand Down
Loading