-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
URL validation fails when query parameters in validated URL contains space #54546
Comments
You should url encode an url set as query param: https%3A%2F%2Fwww.foo.com%2F%3Futm_campaign%3Dsome%20campaign |
I think this is down to PHP decoding it, and then when it's validating the space is included, which then makes it invalid.. It does seem odd. Maybe a custom validator is a better way around it.. or maybe it'd be better to post it rather than a url param, that way it doesn't get decoded. Not sure if it belongs in the framework as it's an edgecase I guess, I did open a PR but closed it after some thought. |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
I have reviewed the issue regarding URL validation failing when query parameters contain spaces. This behavior stems from how PHP processes query parameters, where spaces encoded as %20 or + are converted back to spaces, leading to potential validation failures. Proposed Solution: To address this, consider encoding the URL before validation to ensure spaces are properly represented. Alternatively, adjust the validation logic to account for spaces in query parameters. Implementation Details: In your controller, you can encode the URL before validation:
This approach ensures that spaces and other special characters are properly encoded, allowing the url validation rule to function as expected. Considerations: User Experience: Ensure that encoding the URL doesn't affect user experience, especially if the URL is displayed back to the user. Security: Always sanitize and validate URLs to prevent potential security vulnerabilities. By implementing this solution, URL validation should handle spaces in query parameters correctly. |
Laravel Version
11.41.3
PHP Version
8.4.3
Database Driver & Version
No response
Description
For some reason it seems that calling
/link?url=https://www.foo.com/?utm_campaign=some%20campaign
in my application will fail to validate theurl
parameter there. CallingStr::isUrl('
https://www.foo.com/?utm_campaign=some%20campaign')` directly in tinker returnstrue
though. However, dumping$request->input('url')
in the controller reveals that the%20
character there has done MIA. That also happens if I use+
as a space encoder.Steps To Reproduce
routes/web.php
:tests/Feature/UrlValidatorTest.php
:Test 2 and 4 will then fail here.
Is this supposed to fail like this? My understanding is that
https://www.foo.com/?utm_campaign=some%20campaign
is a completely valid URL though (and directly callingStr::isUrl('https://www.foo.com/?utm_campaign=some%20campaign')
confirms that). Or should spaces in query parameters always be double encoded when passing them to Laravel for validation?One workaround/hack is to add these lines before the validation is done:
Reproduction repo: https://github.com/carestad/laravel-url-validation-bug
The text was updated successfully, but these errors were encountered: