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

nil pointer deference when validating plugins and kong is offline or host is incorrect #137

Open
rgiaviti opened this issue Feb 23, 2022 · 0 comments
Assignees

Comments

@rgiaviti
Copy link
Contributor

When we try to execute a plugin schema validation using kongClient.Plugins.Validate(ctx, plugin) but Kong, for some reason, is unreachable (incorrect host, port...) go-kong is getting a nil pointer deference panic the application.

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

The equivalent curl is this:

curl --location --request POST 'http://localhost:8001/schemas/plugins/validate' \
--header 'Content-Type: application/json' \
--data-raw '{
   "name": "correlation-id",
   "config": {
   }
}'

I created a sample project to reproduce the problem here. I used go1.17 and go-kong version v0.28.1.

Doing some research, I think I found where go-kong is getting this nil deference error. In plugin_service.go, we have this snippet:

...
resp, err: = s.client.Do(ctx, req, nil)
if err != nil {
        // Arguably Kong should return a 422 Unprocessable Entity for a well-formed
        // HTTP request with a mangled plugin, but it doesn't, it returns a 400.
        // Hopefully (usually) we get a 400 because of a mangled plugin rather than
        // a mangled request, but we can't easily tell as messageFromBody masks errors
        if resp.StatusCode == http.StatusBadRequest {
            var apiError * APIError
            ok: = errors.As(err, & apiError)
            if !ok {
                return false, "", err
            }
            return false, apiError.message, nil
        }
        return false, "", err
...

If kong is not reachable, the resp var in resp, err: = s.client.Do(ctx, req, nil) is nil and the err is not nil. However, even with resp == nil, go-kong is checking the status code here: if resp.StatusCode == http.StatusBadRequest { causing the nil deference error. I don't know if there's more parts that could have this kind of error in go-kong. I only have used the plugin validation until now.

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

No branches or pull requests

2 participants