#23605 closed defect (bug) (fixed)
esc_url() strips spaces instead of encoding them
Reported by: | johnbillion | Owned by: | johnbillion |
---|---|---|---|
Milestone: | 4.4 | Priority: | normal |
Severity: | normal | Version: | 2.8 |
Component: | Formatting | Keywords: | has-patch |
Focuses: | Cc: |
Description
If I pass a URL into esc_url()
that contains a space, the space is stripped instead of encoded.
To reproduce:
$url = 'http://example.com/foo bar/'; echo '<pre>'; var_dump( $url ); var_dump( esc_url( $url ) ); echo '</pre>';
The resulting URL ends up as http://example.com/foobar/
instead of the expected http://example.com/foo%20bar/
Attachments (2)
Change History (31)
#2
@
12 years ago
- Cc bananastalktome@… added
The stripping spaces behavior is actually reflected in the unit tests, according to the test at source:trunk/tests/formatting/EscUrl.php@1219#L8, it seems originally added in [UT331]. Seems unusual, and I wonder if the test should be changed to reflect the desired behavior instead (encoding spaces)?
#3
@
12 years ago
test_spaces()
was originally added in [226/tests], modified in [229/tests] and [273/tests].
#4
@
12 years ago
I would quite like this fixed as it is driving me mad, I have resorted for the moment using a str_replace on the string in a custom function I wrote.
My function goes like this:
function jc_encode_spaces($string){ return str_replace(' ', '%20', $string); }
Not Idea but it does the job
#5
@
12 years ago
Space is an invalid character in URLs, so it should be escaped just like any other invalid character. Stripping them is absolutely the wrong thing to do.
#6
@
12 years ago
Agreed, perhaps it should be a parmeter ? You can set how it encodes the URL i.e Strip Space / Encode Spaces.
PHP built in functions use a bit mask to do this.
#7
@
12 years ago
Aren't we mixing up escaping for display and actual encoding here? PHPDoc for esc_url()
does indicate that it removes characters, not encodes them. Seems like there are any number of characters that are stripped rather than encoded, not just spaces, for what it's worth.
#8
@
12 years ago
Is there actually a away to encode spaces ? i.e " " becomes %20. I must say I expect most of the URI based functions to do this and not to simply strip away the spaces so they don't point to the correct resource.
#9
@
11 years ago
I'm admittedly not a seasoned developer, but I too want to register my dismay with the esc_url() function. If WordPress wants us to use this type of function on a religious basis as suggested by the documentation, then it should help, not hinder.
Regardless how accurately the Codex may document that it "escapes" versus "encodes" or whatever other technical nuances one may wish to apply, the fact is that what we want is a function that reliably returns what we give it ready to output safely. Discarding perfectly valid spaces is hardly accomplishing that!
As is, I'll have to wrap this in my own function to first encode characters before I can give it to esc_url(). Unless I'm really missing the point, it seems very unhelpful as is.
#11
follow-up:
↓ 12
@
11 years ago
- Milestone changed from Awaiting Review to Future Release
If we're going to encode spaces, should we be encoding other characters too? I'm not necessarily against this fix — if a space is passed to esc_url(), we know exactly what it should be and it shouldn't be a big deal to encode it — but I'm wondering where that stops.
#12
in reply to:
↑ 11
;
follow-up:
↓ 15
@
11 years ago
Replying to nacin:
If we're going to encode spaces, should we be encoding other characters too? I'm not necessarily against this fix — if a space is passed to esc_url(), we know exactly what it should be and it shouldn't be a big deal to encode it — but I'm wondering where that stops.
Can't we just lean on rawurlencode()
to encode everything properly?
#13
@
9 years ago
Since esc_url
is also used when printing out links & href
attributes, My opinion is that it should not make the inputted url wrong, so I would prefer that spaces were encoded to perhaps %20
or +
#14
@
9 years ago
- Keywords needs-patch needs-unit-tests added
- Milestone changed from Future Release to 4.4
- Version set to 2.8
#15
in reply to:
↑ 12
@
9 years ago
Replying to betzster:
Can't we just lean on
rawurlencode()
to encode everything properly?
Nope. The purpose of esc_url appears to be removal of non-printing special characters. If we make it an alias of rawurlencode, it will also encode colons and slashes, making most URLs invalid.
#16
follow-up:
↓ 20
@
9 years ago
The above patch adds a str_replace call as the first thing in esc_url. This makes sure that spaces are not stripped but rather replaced by %20
. It also updates the tests to reflect these changes.
It may be worth deciding what happens for multiple spaces, should these stay in the form of %20%20
or be stripped down to a single %20
?
#19
@
9 years ago
- Owner set to johnbillion
- Resolution set to fixed
- Status changed from new to closed
In 33858:
#20
in reply to:
↑ 16
@
9 years ago
Replying to enshrined:
It may be worth deciding what happens for multiple spaces, should these stay in the form of
%20%20
or be stripped down to a single%20
?
Thanks for the patch! Multiple consecutive spaces are fine, it's just the encoding of them (or rather, non-removal of them) that esc_url()
is concerned with.
#23
@
9 years ago
Ahh yes, sorry. Completely missed this one. Don't know why it didn't fail on me for some reason
#24
@
9 years ago
- Keywords has-patch added
- Resolution fixed deleted
- Status changed from closed to reopened
Also noted in ticket:21749:2.