Skip to content

Optional internal Python error masking with whitelist in GraphQL responses #1550

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

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.

1 participant