Skip to content

[SECURITY] Implement a feature to disable the suggestion when a GraphQL query fails#3411

Merged
StevenACoffman merged 2 commits into99designs:masterfrom
tomoikey:impl/disable_suggestion
Dec 8, 2024
Merged

[SECURITY] Implement a feature to disable the suggestion when a GraphQL query fails#3411
StevenACoffman merged 2 commits into99designs:masterfrom
tomoikey:impl/disable_suggestion

Conversation

@tomoikey
Copy link
Copy Markdown
Collaborator

@tomoikey tomoikey commented Dec 8, 2024

Description

Hello! I recently contributed to gqlparser to disable the suggestion feature for security reasons. Since gqlgen also uses the updated version of gqlparser, I have made modifications to allow gqlgen users to disable the suggestion feature as well.

The suggestion feature can be convenient from the client's perspective, but it may pose security risks. Therefore, developers using gqlgen should have the option to enable or disable this feature as needed.

Thanks.

The relevant pull request

vektah/gqlparser#319

Changes Made

  • Added a configuration option to enable or disable the suggestion feature in gqlgen.

I have:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs)

Comment on lines +172 to +186
func TestExecutorDisableSuggestion(t *testing.T) {
exec := testexecutor.New()
t.Run("by default, the error message will include suggestions", func(t *testing.T) {
resp := query(exec, "", "{nam}")
assert.Equal(t, "", string(resp.Data))
assert.Equal(t, "input:1: Cannot query field \"nam\" on type \"Query\". Did you mean \"name\"?\n", resp.Errors.Error())
})

t.Run("disable suggestion, the error message will not include suggestions", func(t *testing.T) {
exec.SetDisableSuggestion(true)
resp := query(exec, "", "{nam}")
assert.Equal(t, "", string(resp.Data))
assert.Equal(t, "input:1: Cannot query field \"nam\" on type \"Query\".\n", resp.Errors.Error())
})
}
Copy link
Copy Markdown
Collaborator Author

@tomoikey tomoikey Dec 8, 2024

Choose a reason for hiding this comment

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

Since the settings of the Executor are being modified, I have prepared a separate test from the TestExecutor functions. This ensures that it does not affect anyone writing tests in the future.


parserTokenLimit int
parserTokenLimit int
disableSuggestion bool
Copy link
Copy Markdown
Collaborator Author

@tomoikey tomoikey Dec 8, 2024

Choose a reason for hiding this comment

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

Unless SetDisableSuggestion(true) is called, the behavior will remain exactly the same as before, so it does not affect backward compatibility.
Only those who are aware of this feature will be able to turn off the suggestions.

@StevenACoffman StevenACoffman changed the title [SECURITY PROBLEM] Implement a feature to disable the suggestion when a GraphQL query fails [SECURITY] Implement a feature to disable the suggestion when a GraphQL query fails Dec 8, 2024
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 74.14% (-0.06%) from 74.201%
when pulling 0821523 on tomoikey:impl/disable_suggestion
into b2ed608 on 99designs:master.

@StevenACoffman
Copy link
Copy Markdown
Collaborator

Thanks for this (and the gqlparser contribution)!

Just noticed you are in Yokohama, I miss the food there, so be sure to appreciate it for me! I'm always wistful when my Ramen here doesn't come with wood-ear mushrooms like in sanmamen.

@StevenACoffman StevenACoffman merged commit 3114065 into 99designs:master Dec 8, 2024
@tomoikey
Copy link
Copy Markdown
Collaborator Author

tomoikey commented Dec 8, 2024

@StevenACoffman
I love ramen too, so if you ever come to Yokohama, please let me show you around! 😎 Thanks again for all your help with gqlparser!

@jedevc
Copy link
Copy Markdown

jedevc commented Dec 17, 2024

@StevenACoffman @tomoikey I think this might have accidentally been a breaking change. Maybe I'm wrong though!

Previously, gqlgen didn't import github.com/vektah/gqlparser/v2/validator/rules, so the rules that were added on init were never actually added (unless imported separately by the user). I think this means that now, anytime that gqlgen is imported, all validation is enabled, without an easy way to turn it off? We're directly using the github.com/vektah/gqlparser/v2/parser package, and

(obviously, yes, users like me should just make validation pass on schemas, but I'm not entirely sure if this change was intentional?)

EDIT: investigating more, it seems like this was probably always the intended behavior, and that validate should always have imported the rules like this. But due to importing bits-and-pieces of the library piece-meal, we never actually ended up importing these rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants