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

conftest read json file failed due to json file contains BOM #862

Open
jinweili1 opened this issue Sep 1, 2023 · 3 comments
Open

conftest read json file failed due to json file contains BOM #862

jinweili1 opened this issue Sep 1, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@jinweili1
Copy link

Hi,

We met some problems when using conftest. If the json file contains BOM, conftest will throw Error: running test: get configurations: get configurations: parser unmarshal: unmarshal json: invalid character 'ï' looking for beginning of value error.
After removing them, conftest works.
So I think conftest need to support it, and could you fix this problem?

@boranx
Copy link
Member

boranx commented Sep 8, 2023

Hi,

Thanks for reporting the issue.
That sounds like a small enhancement to yaml/json parser
Is there an example JSON you could share with us that contains BOM? In a way, it can be used in the testing to prove conftest can parse requested form

@boranx boranx added bug Something isn't working good first issue Good for newcomers labels Sep 8, 2023
@jinweili1
Copy link
Author

sure, I will share it
testBOM.zip

@jalseth
Copy link
Member

jalseth commented Sep 18, 2023

This is an upstream issue with the Go JSON parser, it is not specific to conftest. I don't think we should update conftest with custom code for this, the JSON supplied to conftest should be compatible with Go's JSON parser.

$ jq '.' testBOM.json
{
    "test":  "test"
}

$ xxd testBOM.json
00000000: efbb bf7b 0d0a 2020 2020 2274 6573 7422  ...{..    "test"
00000010: 3a20 2022 7465 7374 220d 0a7d 0d0a       :  "test"..}..

$ conftest verify -p foo.rego -d testBOM.json
Error: running verification: load: load documents: 1 error occurred during loading: testBOM.json: invalid character 'ï' looking for beginning of value

$ jq '.' foo.json
{
  "foo": "bar"
}

$ xxd foo.json
00000000: 7b0a 2020 2266 6f6f 223a 2022 6261 7222  {.  "foo": "bar"
00000010: 0a7d 0a                                  .}.

$ conftest verify -p foo.rego -d foo.json

1 test, 1 passed, 0 warnings, 0 failures, 0 exceptions, 0 skipped
$ cat foo.go
package main

import (
  "fmt"
  "os"

  "encoding/json"
)

func main() {
  bytes, err := os.ReadFile("testBOM.json")
  if err != nil {
    panic(err)
  }
  j := make(map[string]any)
  if err := json.Unmarshal(bytes, j); err != nil {
    panic(err)
  }
  fmt.Printf("%+v\n", j)
}

$ go run foo.go
panic: invalid character 'ï' looking for beginning of value

goroutine 1 [running]:
main.main()
	/Users/james/Downloads/foo.go:17 +0xc4
exit status 2

@jinweili1 At this point, I think you can update your JSON input to not have the extra bytes, or file a bug upstream with the Go maintainers.

@boranx boranx removed the good first issue Good for newcomers label Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants