[12.x] Prevent XSS vulnerabilities by excluding SVGs by default in image validation #54331
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the default
image
validation rule in Laravel is susceptible to Cross-Site Scripting (XSS) attacks, as it includes SVG files without explicit sanitization. An attacker could upload a malicious SVG file, such as:When rendered directly, this file could execute arbitrary JavaScript, leading to potential data theft, session hijacking, or other malicious behavior.
Examples of Applications Manually Excluding SVGs
To mitigate this vulnerability, some developers already manually exclude SVGs from the allowed file types:
Many applications NOT excluding SVGs
However, many Laravel projects remain vulnerable due to relying on the default behavior of the
image
rule. A quick GitHub search shows over 500 public repos, of which nearly all of them do not exclude SVGs through additional rules.Examples include:
These examples demonstrate how this vulnerability can affect widely used features like avatars, which are often displayed across an application.
Why this justifies a Breaking Change
To address this vulnerability effectively, the default behavior of the
image
rule must be changed to exclude SVGs by default. This approach ensures:image
validation rule for user-uploaded files. A secure default will protect applications from XSS without requiring developers to manually exclude SVGs.Proposed Solution
Reject SVGs by Default:
image
rule will no longer accept SVG files unless explicitly allowed using the newallow_svg
parameter.Opt-In SVG Support:
or