Make WordPress Core

Opened 12 years ago

Closed 5 years ago

Last modified 3 years ago

#20771 closed enhancement (wontfix)

esc_url() instead of esc_html() in wp_nonce_url()

Reported by: jkudish's profile jkudish Owned by: johnbillion's profile johnbillion
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: Formatting Keywords: needs-unit-tests needs-patch
Focuses: Cc:


The wp_nonce_url() function currently uses esc_html() in its output, which doesn't really seem to be the appropriate escaping function since it's generating a URL.

Attached patch changes the output to use esc_url()

Attachments (2)

20771-1.diff (518 bytes) - added by jkudish 12 years ago.
20771-2.diff (19.2 KB) - added by jkudish 12 years ago.
remove all occurrences of esc_url( wp_nonce_url( ... ) )

Download all attachments as: .zip

Change History (26)

12 years ago

#1 follow-up: @SergeyBiryukov
12 years ago

  • Keywords 3.5-early added
  • Milestone changed from Awaiting Review to Future Release

wp_specialchars() was added in [3974] and changed to esc_html() in [11380].

In come cases, wp_nonce_url() result is already escaped with esc_url() on output:

We should probably review all the instances.

#2 in reply to: ↑ 1 @jkudish
12 years ago

Replying to SergeyBiryukov:

In come cases, wp_nonce_url() result is already escaped with esc_url() on output:
We should probably review all the instances.

We could remove all the uses of esc_url( wp_nonce_url( ... ) ), but there isn't anything technically wrong with escaping twice. It's being overly cautious for sure, but not "wrong".

That being said, the revised attached patch removes all such occurrences.

This got me thinking about something though... Is there a good reason why other functions that generate URLs (e.g. admin_url(), includes_url(), etc...) don't use esc_url() in their output?

Last edited 12 years ago by jkudish (previous) (diff)

12 years ago

remove all occurrences of esc_url( wp_nonce_url( ... ) )

#3 @SergeyBiryukov
12 years ago

  • Keywords dev-feedback added

#4 @TobiasBg
12 years ago

  • Keywords 3.6-early added; 3.5-early removed

This is probably too late for 3.5, due to implications that this may have. This should however be considered for early 3.6.

#5 @SergeyBiryukov
11 years ago

  • Milestone changed from Future Release to 3.6

#6 @SergeyBiryukov
11 years ago

  • Keywords commit added

20771-1.diff seems good.

Related: #23266

#7 @SergeyBiryukov
11 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 23411:

Use correct escaping function. props jkudish. fixes #20771.

#8 @johnbillion
11 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

This change has introduced an encoding bug, albeit an indirect one.

esc_html() encodes ampersands as & but esc_url() encodes ampersands as &. Passing a URL through add_query_arg() will mangle the URL if it contains ampersands encoded as '&'.

My User Switching plugin has broken since this change because the plugin uses add_query_arg() on a URL that's already been passed through wp_nonce_url().


$url = add_query_arg( 'action', 'foo', wp_login_url() );
$url = wp_nonce_url( $url, 'foo' );
$url = add_query_arg( 'redirect_to', 'bar', $url );

The resulting URL will end up as:;_wpnonce=abc123

instead of:"

Notice that the _wpnonce parameter is stripped and treated as a URL hash instead of a query arg.

I'd like to recommend for now that r23411 is reverted. However, the core cause of the problem is due to the fact that wp_nonce_url() should be sanitising the URL but not encoding it. This means it should use esc_url_raw() instead of esc_url() or esc_html().

I'm going to open another ticket later today to address the core issue as it's present in several places but generally goes unnoticed.

Version 0, edited 11 years ago by johnbillion (next)

#9 @nacin
11 years ago

We should ideally fix add_query_arg() to work for both & and &. At a glance, though, I'm not sure I see where add_query_arg() handles the former even right now.

We should also consider just using & in esc_url(). I can't think of a particular context where esc_url() may be used (and esc_html() isn't) where & is not a recognized entity.

For now, I agree with revert. I have considered this change more than once, and each time, avoided it as something would break.

#10 @nacin
11 years ago

In 23637:

Revert [23411] until encoding differences are worked out. see #20771.

#11 @ryan
11 years ago

  • Milestone changed from 3.6 to Future Release

#13 @chriscct7
9 years ago

  • Keywords 3.6-early commit removed

#14 @johnbillion
9 years ago

In 33851:

Add some more passing unit tests for esc_url() in preparation for upcoming changes.

See #23605, #20771, #16859

#15 @johnbillion
9 years ago

In 33855:

More unit tests for esc_url() and esc_url_raw().

See #23605, #20771, #16859

#16 @johnbillion
9 years ago

  • Keywords needs-unit-tests needs-patch added; has-patch dev-feedback removed
  • Milestone changed from Future Release to 4.4
  • Owner changed from SergeyBiryukov to johnbillion
  • Status changed from reopened to accepted

#17 @johnbillion
9 years ago

#31335 was marked as a duplicate.

#18 @johnbillion
9 years ago

  • Milestone changed from 4.4 to Future Release

#19 @omarreiss
8 years ago

Created #36397 to deal with the bug in add_query_args which causes numbered html entities to be mistaken for hash fragments.

#20 @johnjamesjacoby
7 years ago

I think esc_url doing HTML escaping is a bit dumb

I agree with this statement – I just ran into this again working on a plugin.

Core is surprisingly consistent as to how it wraps wp_nonce_url() in esc_url(); there are only a handful of places where the & replacement in wp_nonce_url() is actually necessary.

users.php#L161 is an old-school @nacin top 40 classic for the ages, according to the Casey Kasem revision history.

Otherwise, as dumb as it looks, it's working OK.

wp_nonce_url() does seem, to me, like it's a function that doesn't know if it should echo or return, and so it escapes but doesn't output anything, which I don't like (at least use _get_ in the name.)

I think if anyone runs into this in the future and wants direction, my preferred approach would be to:

  • Introduce a new function that doesn't escape, doesn't swap the &, and handles only the adding of the query nonce to a URL
  • Use this new function all across everything where wp_nonce_url() is currently used
  • Replace query-string URL patterns with arrays using add_query_arg() – this is alot of code
  • Somehow convince everyone this is actually worth doing compared to leaving it be

TL;DR - I think this can be closed either as wontfix or maybelater.

This ticket was mentioned in Slack in #core by jjj. View the logs.

7 years ago

#22 @ocean90
7 years ago

#41413 was marked as a duplicate.

#23 @johnbillion
5 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from accepted to closed

#24 @juliobox
3 years ago

Why not adding a new param in this function?
wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce', $context = 'display' );
When $context is 'display', the default value (hello retrocompat), we let the esc_html() since it's for displaying.
But it's not for display, like "redirect", esc_url() instead.
And if no context is given, no sanitize.
I think that way everyone is happy, we can still use it, retrocompat ok but still with a new possibility to use it for redirection without creating a new one or break anything.

cc @johnbillion @johnjamesjacoby

Note: See TracTickets for help on using tickets.