You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Partial and invalid URLs are also accepted, parse_url() tries its best to parse them correctly.
That best effort fails here, and in many other cases, and I don't think there is any reliable way to correctly parse all partial URLs (except maybe for some clear and restricting definition of "partial"). You might be better off using a regex to parse URLs. parse_url() can't do this for BC reasons.
IMO this is "Invalid" or "Won't fix", or maybe a feature request.
I believe the parse_url() function should be fixed. url starting with // are used everywhere on the web. And regular expressions are slower + require additional checks if they are made less complex.
parse_url is ok-ish for known urls (e.g. from config file), anything from user should go through packages like league/uri anyway.
As for this specific url: it is technically valid as a relative url, so if parse_url can parse relative paths, should also parse that url. OP is probably referring to use of //... in 3rd-party scripts in the past, but at this point https:// should be used instead. And this works because url is treated as relative to page origin. If you try to parse url in JS you will get error, but if you also provide base url, the parsing is successful.
As for this specific url: it is technically valid as a relative url, so if parse_url can parse relative paths, should also parse that url.
The problem is the way php_url_parse_ex2() is implemented; it is not a proper parser, but rather does some guesswork (which is supposed to work for absolute URLs, but may fail badly for others). In this case it looks for the first : and checks whether this is preceeded by a valid scheme. Obviously, this is not the case, so it checks whether the following characters form a valid port, what is the case.
Now try to fix that without breaking existing code which may even rely on parsing invalid URLs. Best of luck! :)
So, in my opinion, parse_url() is beyond repair and needs to be replaced by some sane URI parser (adhering to whatever specification). I'm aware that I sound like a broken record.
Before port you can have only double slash - if there is anything more - it's not a port. I think it's still possible to write proper url parsing which can handle invalid urls, but it cannot relay on : as a major part, it should be some state-based algorithm. NodeJS implementation could probably parse invalid urls, but it's way more complex. Even league/uri package is capable of parsing broken URLs. Maybe this is the place for replacement of parse_url with object-based internal extension. new URL(...) with isValid etc, but with performance of native modules.
Can one not add some kind of hard default like if the url starts with // then it is assumed https:// and port 443
or what am I missing here?
I guess safer even is http and port 80 as the onus is then on the web server to redirect to https if it is available.
I agree with @cmb69, especially if there have been previous failed attempts to fix similar issues. Additionally, I think trying to fix invalid URLs is inherently unsafe specifically because the correct behavior is undefined and different parsers might fix URLs in different ways which could be exploitable. IMO it would be better to introduce a new parser that closely follows the specification and rejects invalid URLs.
Activity
cmb69 commentedon Jan 6, 2022
The documentation states:
That best effort fails here, and in many other cases, and I don't think there is any reliable way to correctly parse all partial URLs (except maybe for some clear and restricting definition of "partial"). You might be better off using a regex to parse URLs. parse_url() can't do this for BC reasons.
IMO this is "Invalid" or "Won't fix", or maybe a feature request.
MioVisman commentedon Jan 6, 2022
I believe the parse_url() function should be fixed. url starting with // are used everywhere on the web. And regular expressions are slower + require additional checks if they are made less complex.
This simple regex
is 2-3 times slower than parse_url() on my computer.
So far I made such a bike:
KapitanOczywisty commentedon Jan 6, 2022
Regex is crazy fast if properly written (backtracking is the common performance killer; debug with https://regex101.com/ or https://www.debuggex.com/), and even 20 times is nothing, unless the only job of your app is parsing urls. Do not optimize prematurely.
parse_urlis ok-ish for known urls (e.g. from config file), anything from user should go through packages like league/uri anyway.As for this specific url: it is technically valid as a relative url, so if
parse_urlcan parse relative paths, should also parse that url. OP is probably referring to use of//...in 3rd-party scripts in the past, but at this pointhttps://should be used instead. And this works because url is treated as relative to page origin. If you try to parse url in JS you will get error, but if you also provide base url, the parsing is successful.Edit:
With regex variant, named groups are pretty useful.
cmb69 commentedon Jan 6, 2022
The problem is the way
php_url_parse_ex2()is implemented; it is not a proper parser, but rather does some guesswork (which is supposed to work for absolute URLs, but may fail badly for others). In this case it looks for the first:and checks whether this is preceeded by a valid scheme. Obviously, this is not the case, so it checks whether the following characters form a valid port, what is the case.Now try to fix that without breaking existing code which may even rely on parsing invalid URLs. Best of luck! :)
So, in my opinion,
parse_url()is beyond repair and needs to be replaced by some sane URI parser (adhering to whatever specification). I'm aware that I sound like a broken record.KapitanOczywisty commentedon Jan 7, 2022
Before port you can have only double slash - if there is anything more - it's not a port. I think it's still possible to write proper url parsing which can handle invalid urls, but it cannot relay on
:as a major part, it should be some state-based algorithm. NodeJS implementation could probably parse invalid urls, but it's way more complex. Even league/uri package is capable of parsing broken URLs. Maybe this is the place for replacement ofparse_urlwith object-based internal extension.new URL(...)withisValidetc, but with performance of native modules.ErikThiart commentedon Feb 3, 2022
Can one not add some kind of hard default like if the url starts with
//then it is assumedhttps://and port443or what am I missing here?
I guess safer even is
httpand port80as the onus is then on the web server to redirect tohttpsif it is available.iluuu1994 commentedon May 5, 2022
I agree with @cmb69, especially if there have been previous failed attempts to fix similar issues. Additionally, I think trying to fix invalid URLs is inherently unsafe specifically because the correct behavior is undefined and different parsers might fix URLs in different ways which could be exploitable. IMO it would be better to introduce a new parser that closely follows the specification and rejects invalid URLs.
Added workaround over php/php-src#7890
Add working example and example from phpGH-7890
TimWolla commentedon Apr 11, 2026
Please migrate to the new ext/uri classes (
Uri\*namespace) instead. See also php/doc-en#5477.