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: Missing required fields cause panic #162

Merged
merged 1 commit into from
Jun 1, 2020

Conversation

cwurm
Copy link
Contributor

@cwurm cwurm commented May 29, 2020

The validation logic sometimes does not catch missing required fields, resulting in a panic. This updates the schema generation to fix it.

E.g. the following config:

services:
- host: httpbin.org
  #name: service
  routes:
  - headers:
      my-header:
      - httpbin.org
    #name: route
    plugins:
    - name: key-auth
consumers:
- keyauth_credentials:
  - key: foo
  #username: consumer
upstreams:
- hash_on: none
  #name: upstream

will cause segfaults like the following for any of the commented out lines above:

$ deck diff -s kong.yaml
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x141b947]

goroutine 7 [running]:
github.com/hbagdi/deck/file.(*stateBuilder).services(0xc0003cfb28)
	/home/hbagdi/deck/file/builder.go:392 +0x5d7
github.com/hbagdi/deck/file.(*stateBuilder).build(0xc0003cfb28, 0x0, 0x0, 0x0)
	/home/hbagdi/deck/file/builder.go:53 +0xfa
github.com/hbagdi/deck/file.Get(0xc000388000, 0xc00006d200, 0x1, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	/home/hbagdi/deck/file/reader.go:51 +0x130
github.com/hbagdi/deck/cmd.syncMain(0xc00005c060, 0x1, 0x1, 0xc0000f9d01, 0xa, 0x0, 0x0)
	/home/hbagdi/deck/cmd/common.go:102 +0x325
github.com/hbagdi/deck/cmd.glob..func1(0x1ad8440, 0xc00000c0e0, 0x0, 0x2, 0x0, 0x0)
	/home/hbagdi/deck/cmd/diff.go:25 +0x56
github.com/spf13/cobra.(*Command).execute(0x1ad8440, 0xc00000c0a0, 0x2, 0x2, 0x1ad8440, 0xc00000c0a0)
	/home/hbagdi/go/pkg/mod/github.com/spf13/cobra@v0.0.7/command.go:838 +0x453
github.com/spf13/cobra.(*Command).ExecuteC(0x1ad79c0, 0x0, 0x0, 0x0)
	/home/hbagdi/go/pkg/mod/github.com/spf13/cobra@v0.0.7/command.go:943 +0x317
github.com/spf13/cobra.(*Command).Execute(...)
	/home/hbagdi/go/pkg/mod/github.com/spf13/cobra@v0.0.7/command.go:883
github.com/hbagdi/deck/cmd.Execute.func2(0xc00005c010, 0xc000028110)
	/home/hbagdi/deck/cmd/root.go:59 +0x2d
created by github.com/hbagdi/deck/cmd.Execute
	/home/hbagdi/deck/cmd/root.go:58 +0xba

Also switches the order in which one of two fields are required to have id last so it gives a more meaningful error message - only the first is displayed, e.g.:

$ ./deck diff -s kong.yaml
Error: reading file: validating file content: 7 errors occurred:
	upstreams.0: name is required
	services.0: Must validate at least one schema (anyOf)
	services.0: name is required
	services.0.routes.0: Must validate at least one schema (anyOf)
	services.0.routes.0: name is required
	consumers.0: Must validate at least one schema (anyOf)
	consumers.0: username is required

Copy link
Member

@hbagdi hbagdi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks for the clean patch.

@hbagdi hbagdi merged commit 5baedb5 into Kong:master Jun 1, 2020
rainest pushed a commit that referenced this pull request Apr 21, 2021
The validation logic sometimes does not catch missing required fields, resulting in a panic. This updates the schema generation to fix it.

Example:
```yaml
services:
- host: httpbin.org
  #name: service
  routes:
  - headers:
      my-header:
      - httpbin.org
    #name: route
    plugins:
    - name: key-auth
consumers:
- keyauth_credentials:
  - key: foo
  #username: consumer
upstreams:
- hash_on: none
  #name: upstream
```

will cause segfaults like the following for any of the commented out lines above:
```ShellSession
$ deck diff -s kong.yaml
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x141b947]

goroutine 7 [running]:
github.com/hbagdi/deck/file.(*stateBuilder).services(0xc0003cfb28)
	/home/hbagdi/deck/file/builder.go:392 +0x5d7
github.com/hbagdi/deck/file.(*stateBuilder).build(0xc0003cfb28, 0x0, 0x0, 0x0)
	/home/hbagdi/deck/file/builder.go:53 +0xfa
github.com/hbagdi/deck/file.Get(0xc000388000, 0xc00006d200, 0x1, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	/home/hbagdi/deck/file/reader.go:51 +0x130
github.com/hbagdi/deck/cmd.syncMain(0xc00005c060, 0x1, 0x1, 0xc0000f9d01, 0xa, 0x0, 0x0)
	/home/hbagdi/deck/cmd/common.go:102 +0x325
github.com/hbagdi/deck/cmd.glob..func1(0x1ad8440, 0xc00000c0e0, 0x0, 0x2, 0x0, 0x0)
	/home/hbagdi/deck/cmd/diff.go:25 +0x56
github.com/spf13/cobra.(*Command).execute(0x1ad8440, 0xc00000c0a0, 0x2, 0x2, 0x1ad8440, 0xc00000c0a0)
	/home/hbagdi/go/pkg/mod/github.com/spf13/cobra@v0.0.7/command.go:838 +0x453
github.com/spf13/cobra.(*Command).ExecuteC(0x1ad79c0, 0x0, 0x0, 0x0)
	/home/hbagdi/go/pkg/mod/github.com/spf13/cobra@v0.0.7/command.go:943 +0x317
github.com/spf13/cobra.(*Command).Execute(...)
	/home/hbagdi/go/pkg/mod/github.com/spf13/cobra@v0.0.7/command.go:883
github.com/hbagdi/deck/cmd.Execute.func2(0xc00005c010, 0xc000028110)
	/home/hbagdi/deck/cmd/root.go:59 +0x2d
created by github.com/hbagdi/deck/cmd.Execute
	/home/hbagdi/deck/cmd/root.go:58 +0xba
```

Also switches the order in which one of two fields are required to have `id` last so it gives a more meaningful error message - only the first is displayed, e.g.:
```ShellSession
$ ./deck diff -s kong.yaml
Error: reading file: validating file content: 7 errors occurred:
	upstreams.0: name is required
	services.0: Must validate at least one schema (anyOf)
	services.0: name is required
	services.0.routes.0: Must validate at least one schema (anyOf)
	services.0.routes.0: name is required
	consumers.0: Must validate at least one schema (anyOf)
	consumers.0: username is required
```

From #162
AntoineJac pushed a commit that referenced this pull request Jan 23, 2024
The validation logic sometimes does not catch missing required fields, resulting in a panic. This updates the schema generation to fix it.

Example:
```yaml
services:
- host: httpbin.org
  #name: service
  routes:
  - headers:
      my-header:
      - httpbin.org
    #name: route
    plugins:
    - name: key-auth
consumers:
- keyauth_credentials:
  - key: foo
  #username: consumer
upstreams:
- hash_on: none
  #name: upstream
```

will cause segfaults like the following for any of the commented out lines above:
```ShellSession
$ deck diff -s kong.yaml
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x141b947]

goroutine 7 [running]:
github.com/hbagdi/deck/file.(*stateBuilder).services(0xc0003cfb28)
	/home/hbagdi/deck/file/builder.go:392 +0x5d7
github.com/hbagdi/deck/file.(*stateBuilder).build(0xc0003cfb28, 0x0, 0x0, 0x0)
	/home/hbagdi/deck/file/builder.go:53 +0xfa
github.com/hbagdi/deck/file.Get(0xc000388000, 0xc00006d200, 0x1, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	/home/hbagdi/deck/file/reader.go:51 +0x130
github.com/hbagdi/deck/cmd.syncMain(0xc00005c060, 0x1, 0x1, 0xc0000f9d01, 0xa, 0x0, 0x0)
	/home/hbagdi/deck/cmd/common.go:102 +0x325
github.com/hbagdi/deck/cmd.glob..func1(0x1ad8440, 0xc00000c0e0, 0x0, 0x2, 0x0, 0x0)
	/home/hbagdi/deck/cmd/diff.go:25 +0x56
github.com/spf13/cobra.(*Command).execute(0x1ad8440, 0xc00000c0a0, 0x2, 0x2, 0x1ad8440, 0xc00000c0a0)
	/home/hbagdi/go/pkg/mod/github.com/spf13/cobra@v0.0.7/command.go:838 +0x453
github.com/spf13/cobra.(*Command).ExecuteC(0x1ad79c0, 0x0, 0x0, 0x0)
	/home/hbagdi/go/pkg/mod/github.com/spf13/cobra@v0.0.7/command.go:943 +0x317
github.com/spf13/cobra.(*Command).Execute(...)
	/home/hbagdi/go/pkg/mod/github.com/spf13/cobra@v0.0.7/command.go:883
github.com/hbagdi/deck/cmd.Execute.func2(0xc00005c010, 0xc000028110)
	/home/hbagdi/deck/cmd/root.go:59 +0x2d
created by github.com/hbagdi/deck/cmd.Execute
	/home/hbagdi/deck/cmd/root.go:58 +0xba
```

Also switches the order in which one of two fields are required to have `id` last so it gives a more meaningful error message - only the first is displayed, e.g.:
```ShellSession
$ ./deck diff -s kong.yaml
Error: reading file: validating file content: 7 errors occurred:
	upstreams.0: name is required
	services.0: Must validate at least one schema (anyOf)
	services.0: name is required
	services.0.routes.0: Must validate at least one schema (anyOf)
	services.0.routes.0: name is required
	consumers.0: Must validate at least one schema (anyOf)
	consumers.0: username is required
```

From #162
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants