-
Notifications
You must be signed in to change notification settings - Fork 2k
GraphQLSchema does not completely validate names #4362
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
Comments
Hi @ab-pm! Are you using the |
I personally don't, I just thought it would be useful. I just looked through the source of graphql-js (as the GraphQL reference implementation) to see whether there is something useful to validate strings as |
Just looking this over, the strange naming of this function seems to be an artifact of refactoring, see #3288 We moved most of the validation of naming into the type constructors, so that except for the underscoring, conformity to the spec is guaranteed. There is no context in that PR as to why it is preferable to fail at schema construction for name errors (rather than leaving that to the validation step with well formatted errors). Totally guessing => perhaps @IvanGoncharov was motivated by a potential security concern with prototype pollution and so wanted to make sure that even non-validated schemas wouldn't have this issue? Things to do:
|
Ah, that's great, I didn't see graphql-js/src/type/definition.ts Line 870 in a7db60b
So I guess you could close the issue as resolved regarding my doubt
however the todos you mention seem very sensible. I might add
|
I think this is good design to have constructors validate the arguments and not allow invalid instances to be constructed in the first place. The schema construction does not need to validate that again, it only needs to check the invariants across the whole schema. As for "well formatted errors", if I construct types with invalid names in my code I'd rather have that fail via an exception with a stack trace than at some later step. And for reporting the error locations in a schema file, this doesn't really apply anyway since the parser would have already rejected these names. If anything, you could consider adding an optional Edit: interestingly, #3288 more or less undid #1132. I still think it's good - the latter was motivated by #1080 ("split up these steps to ensure we can do just the validation when we know the schema is valid.") but this is futile if the constructors are exported as the public interface. Non-validating constructors would need to be private (package-scoped). |
Actually, there's also a problem with the deprecation notice by @IvanGoncharov in graphql-js/src/utilities/assertValidName.ts Lines 8 to 12 in a7db60b
graphql-js/src/type/introspection.ts Lines 207 to 208 in a7db60b
|
At least one motivation for doing validation separately within On the other hand, you mention the tension, that there is a benefit in fast-failing for common developer errors where the call to I see the tension, but find myself falling on the other side of it => I want schema construction at runtime to be as fast as possible. If it were up to me, I would vote for moving this validation back out of the constructor. |
Uh oh!
There was an error while loading. Please reload this page.
The
validateName
function that is used to validate lots of schema parts is currently only checking that names are not reserved:graphql-js/src/type/validate.ts
Lines 206 to 217 in 6b253e7
It should also check that the
name
complies with https://spec.graphql.org/October2021/#Name, e.g. by testing against the regex/^(?!__)[A-Za-z_][A-Za-z0-9_]*$/
.Otherwise it's possible to construct schemas (via the constructor, not by parsing) that upon printing would lead to invalid syntax, or fields which could never be queried.(Not the case, see below)The text was updated successfully, but these errors were encountered: