Skip to content

Commit

Permalink
fix: crash when the YAML is not well formed
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabio committed Jun 16, 2021
1 parent 257e0fb commit 3e8a932
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
28 changes: 19 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func getKeyAtLine(parentNode yaml.Node, line int, path string) string {
return ""
}

func toValidationError(errorText string, node yaml.Node) ValidationError {
r := regexp.MustCompile(`^(line ([0-9]+): )`)
func toValidationError(errorText string, node *yaml.Node) ValidationError {
r := regexp.MustCompile(`(line ([0-9]+): )`)
matches := r.FindStringSubmatch(errorText)

line := 0
Expand All @@ -153,7 +153,10 @@ func toValidationError(errorText string, node yaml.Node) ValidationError {
errorText = "wrong type for this field"
}

key := getKeyAtLine(node, line, "")
var key string
if node != nil {
key = getKeyAtLine(*node, line, "")
}

return ValidationError{
Key: key,
Expand All @@ -163,7 +166,7 @@ func toValidationError(errorText string, node yaml.Node) ValidationError {
}
}

// Parse loads the yaml bytes and tries to parse it. Return an error if fails.
// ParseBytes loads the yaml bytes and tries to parse it. Return an error if fails.
func (p *Parser) ParseBytes(in []byte) error {
var ve ValidationErrors

Expand All @@ -178,18 +181,25 @@ func (p *Parser) ParseBytes(in []byte) error {

d := yaml.NewDecoder(bytes.NewReader(in))
d.KnownFields(true)
d.Decode(&node)
err := d.Decode(&node)

if err == nil && len(node.Content) > 0 {
node = *node.Content[0]
} else {
// YAML is malformed
ve = append(ve, toValidationError(err.Error(), nil))

node = *node.Content[0]
return ve;
}

// Decode the YAML into a PublicCode structure, so we get type errors
d = yaml.NewDecoder(bytes.NewReader(in))
d.KnownFields(true)
if err := d.Decode(&p.PublicCode); err != nil {
if err = d.Decode(&p.PublicCode); err != nil {
switch err.(type) {
case *yaml.TypeError:
for _, errorText := range err.(*yaml.TypeError).Errors {
ve = append(ve, toValidationError(errorText, node))
ve = append(ve, toValidationError(errorText, &node))
}
default:
ve = append(ve, newValidationError("", err.Error()))
Expand All @@ -198,7 +208,7 @@ func (p *Parser) ParseBytes(in []byte) error {

validate := publiccodeValidator.New()

err := validate.Struct(p.PublicCode)
err = validate.Struct(p.PublicCode)
if err != nil {
tagMap := map[string]string{
"gt": "must be more than",
Expand Down
1 change: 1 addition & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ func TestInvalidTestcasesV0_2(t *testing.T) {

// misc
"file_encoding.yml": ValidationErrors{ValidationError{"", "Invalid UTF-8", 0, 0}},
"invalid_yaml.yml": ValidationErrors{ValidationError{"", "yaml: did not find expected key", 18, 1}},
}

testFiles, _ := filepath.Glob("testdata/v0.2/invalid/*yml")
Expand Down
54 changes: 54 additions & 0 deletions testdata/v0.2/invalid/invalid_yaml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
publiccodeYmlVersion: "0.2"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"
releaseDate: "2017-04-15"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
eng:
localisedName: Medusa
genericName: Text Editor
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
# Wrong indentation for this key: this YAML file is invalid
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "community"

contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- eng

0 comments on commit 3e8a932

Please sign in to comment.