Skip to content
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

[11.x] Handle spaces when validating a url #54550

Closed
wants to merge 4 commits into from

Conversation

jackbayliss
Copy link

@jackbayliss jackbayliss commented Feb 11, 2025

This closes #54546

Why

Str::isUrl is used for Url Validation, ie $request->validate(['url' => 'required|url']);

If you're validating urls from a url param, such as : http://local.test/example?url=https://laravel.com?query=taylor+otwell or http://local.test/example?url=https://laravel.com?query=taylor%20otwell

the url you get back is decoded by PHP : http://local.test/example?url=laravel.com?url=taylor otwell this then fails the url validation, despite being technically a valid url before PHP decoded it.

There are ways around it, but, it would be nice to use the request to validate it (in an ideal world).

What it adds

This PR adds a straight forward str_replace to handle spaces as felt the safest without touching the regex, the regex patterns already account for %20 too.

Also added some basic tests to show no regression etc.

Feel free to close if not useful 👍🏻

When passing in a string such as `http://google.com?query=john+wayne` into a route, the + or %20 etc gets escaped giving us `http://google.com?query=john wayne` which, if we're validating it's a valid url it technically is, but php has escaped it.
Copy link

Thanks for submitting a PR!

Note that draft PR's 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.

@jackbayliss jackbayliss changed the title [11.x] Handle spaces in when validating url [11.x] Handle spaces when validating a url Feb 11, 2025
@jackbayliss
Copy link
Author

jackbayliss commented Feb 11, 2025

Don't think this belongs as part of the framework, should probably be worked around if you're passing it as a param as feels like an edge case. Ie adding the str_replace yourself.

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.

URL validation fails when query parameters in validated URL contains space
1 participant