Skip to content

Conversation

DrillableBit
Copy link

By default, Graphene returns Django errors (e.g., ValidationError) inside the GraphQL data object, while GraphQLErrors -including internal Python exceptions like ZeroDivisionError - are returned outside the data field.

For public APIs, exposing full internal error details can be a security risk and a poor user experience. Currently, Graphene lacks built-in support for masking such unhandled internal exceptions.

This PR introduces an opt-in error masking feature that:

  • Masks internal exceptions with a generic error message by default

  • Allows whitelisting of specific exception classes to be returned fully

  • Enables configuration via GRAPHENE_ERRORS settings, supporting custom messages and whitelisted error classes

This improves API security and usability by preventing leakage of sensitive error information while still exposing known, safe errors.

- Introduce GRAPHENE_ERRORS settings for masking error messages in GraphQL responses
- Support masking sensitive errors while allowing whitelisted exceptions to show full details
- Enable customization of generic error message for better user experience and security
@DrillableBit
Copy link
Author

There might be an error in this PR - in that it might require native GraphQLErrors to be whitelisted when masking is set to True.

If there's any interest in this PR, I'll fix it, but I ended up making a custom view for my project.

@syrusakbary
Copy link
Member

The upcoming changes into graphql-server will make of this a non-issue, as they will make most of the current error behavior fixed.

I'll keep the PR opened until graphene-django gets updated with the new server

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