[12.x] Add option to hide detailed validation errors in JSON responses#59125
[12.x] Add option to hide detailed validation errors in JSON responses#59125MElkmeshi wants to merge 1 commit intolaravel:12.xfrom
Conversation
Adds a HIDE_VALIDATION_ERRORS env option that, when enabled, omits field names and per-field error messages from JSON validation responses. This prevents API field enumeration via empty request bodies.
|
Thanks for submitting a PR! Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
Why then a
On the other hand, this could be a gradual setting:
You would usually have an API key or a CSRF token or the like, so simply sending |
Summary
This is more of a discussion than a ready-to-merge PR. I'd love to hear maintainers' thoughts on this.
The Problem
Laravel's default JSON validation error response exposes exact field names and validation rules. This makes API enumeration trivial for attackers — just send an empty
{}body to any endpoint:Response:
{ "message": "The customer id field is required.", "errors": { "customer_id": ["The customer id field is required."], "device_id": ["The device id field is required."], "password": ["The password field is required."] } }Without any authentication or prior knowledge, an attacker now knows the exact schema. I've seen this on production banking APIs.
The Proposal
Add a
HIDE_VALIDATION_ERRORSenv variable (opt-in, defaults tofalse). When enabled, JSON validation responses become:{ "message": "The given data was invalid." }No field names, no per-field errors, no enumeration.
Usage
What Changed
config/app.php— newhide_validation_errorsconfig key reading from envHandler::invalidJson()— respects the config to omit detailed errorsDiscussion Points
APP_DEBUG(hide errors when debug is false)?I understand this is opinionated — just wanted to start the conversation around making this easier out of the box.
Test Plan