[13.x] enhancement/feat: add securityCheck parameter to previousPath method for optional security enhancement #57508
+450
−4
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.
Optional
$securityCheckparameter for thepreviousPath()methodThis PR brings a security enhancement to the
previousPath()method inUrlGeneratorwith better security check and origin validation, which resolves the issue #57456.This is an improved version of the PR #57487 - as @taylorotwell stated that this change can be a breaking change for some devs, so now I developed it as an opt-in feature with a new parameter
$securityCheckand I am submitting this into themasterbranch as a a security enhancement/feature for the next major release v13. Details are below.Issue Summary
The
previousPath()method is expected to return only the path of the previous URL (the doc SS attached for clarity). However, the existing implementation inpreviousPath()method returns the URL, when the request origin is different from the app origin. This happened because it extracted the path from the previous URL without checking that the request originated from the same origin or not and the path extracting logic only worked for the same origin requests. So, this was making applications open to potential security issues including:javascript:,data:etc)And considering these potential security implications of returning the full external URL, I brought this change as a security enhancement to provide developers with more control over the previous URL path.
Solution in Brief
I have added a new parameter
$securityCheckto thepreviousPath()method, which when set totrue, will enable the security checks to validate the origin of the previous URL and also check if that URL path is dangerous or not. If the origin is different from the app origin or detects as dangerous, the method will return the fallback value or the root path (/) instead of the previous URL path.And, if the parameter is set to
false(which I kept as thedefaultvalue), the method will work exactly as it did before, returning the previous URL path like before without any security checks, thus bringing no change in the behavior.So, this implementation provides a fully backward-compatible, and opt-in security enhancement for the
previousPath()method:previousPath()method maintains the existing/old behavior by default - no breaking changes.$securityCheckparameter (previousPath($fallback = false, $securityCheck = false))javascript:,data:,file:), when origin is different from the app origin.parse_url()for reliabilityNew Changes Benefits
$securityCheckparameterTest Coverage
Added comprehensive tests covering both backward compatibility and security modes:
Backward Compatibility Tests:
Security Mode Tests:
javascript:,data:,file:)Implementation Details
As stated above, when the optional
$securityCheckparameter is enabled by devs, then the method applies extra security validations while maintaining the same API contract.Usage Examples:
Note: This change is designed for the master branch (next major release) to provide developers time to adopt the security enhancements. The opt-in approach ensures no immediate breaking changes while offering a clear migration path for enhanced security and better DX. Hope this helps the devs in their future projects.
Let me know if you need any further clarification or tests from my side or if u have any suggestions to improve my approach. Thank you.