goss-org / goss

Quick and Easy server testing/validation
https://goss.rocks
Apache License 2.0
5.62k stars 472 forks source link

Nil pointer dereference when gjson nested key itself doesn't match #982

Open sed-i opened 1 month ago

sed-i commented 1 month ago

Describe the bug Using gjson matching, if the key does not exist in content, goss panics.

How To Reproduce

# repro.yaml
matching:
  example:
    content: '{"this": {"is": {"just": {"a": "test"}}}}'
    matches:
      gjson:
        this.is.typo:
          and:
            - {have-key: "a"}
$ goss -g repro.yaml validate

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x84e53b]

goroutine 23 [running]:
github.com/goss-org/goss/matchers.(*AndMatcher).FailureResult(0x4119db?, {0x0?, 0x0?})
    /home/travis/gopath/src/github.com/goss-org/goss/matchers/and.go:32 +0x5b
github.com/goss-org/goss/matchers.(*WithSafeTransformMatcher).FailureResult(0xb567f8?, {0xa676c0, 0xc000330c30})
    /home/travis/gopath/src/github.com/goss-org/goss/matchers/with_safe_transform.go:45 +0x1bc
github.com/goss-org/goss/matchers.(*AndMatcher).FailureResult(0xc0000a4630?, {0xa676c0?, 0xc000330c30?})
    /home/travis/gopath/src/github.com/goss-org/goss/matchers/and.go:32 +0x62
github.com/goss-org/goss/resource.ValidateGomegaValue({_, _}, {_, _}, {_, _}, {_, _}, _)
    /home/travis/gopath/src/github.com/goss-org/goss/resource/validate.go:199 +0x788
github.com/goss-org/goss/resource.ValidateValue({_, _}, {_, _}, {_, _}, {_, _}, _)
    /home/travis/gopath/src/github.com/goss-org/goss/resource/validate.go:126 +0x174
github.com/goss-org/goss/resource.(*Matching).Validate(0xc000320c00, 0x0?)
    /home/travis/gopath/src/github.com/goss-org/goss/resource/matching.go:62 +0x15a
github.com/goss-org/goss.validate.func2()
    /home/travis/gopath/src/github.com/goss-org/goss/validate.go:184 +0x86
created by github.com/goss-org/goss.validate in goroutine 1
    /home/travis/gopath/src/github.com/goss-org/goss/validate.go:181 +0x187

Expected Behavior To get a regular goss F failure with details on the mismatched key.

Actual Behavior

panic: runtime error: invalid memory address or nil pointer dereference

Environment:

aelsabbahy commented 1 month ago

Thanks for filing this and for providing a minimal reproducible example, I am able to reproduce the issue.

panic == immediate bug =)

I've marked this as approved and will take a look tomorrow. Many thanks for catching this. Interestingly, it seems to only happen with the and, for example.. this does not panic:

matching:
  example:
    content: '{"this": {"is": {"just": {"a": "test"}}}}'
    matches:
      gjson:
        this.is.typo:
          - {have-key: "a"}

output:

$ goss v
F

Failures/Skipped:

Matching: example: matches:
Error
    matchers.Gjson{Path:"this.is.typo"}: Path not found: this.is.typo
the transform chain was
    [{"gjson":{"Path":"this.is.typo"}}]
the raw value was
    "{\"this\": {\"is\": {\"just\": {\"a\": \"test\"}}}}"

Total Duration: 0.000s
Count: 1, Failed: 1, Skipped: 0