Make WordPress Core

Opened 13 months ago

Last modified 3 weeks ago

#44317 new enhancement

wp_safe_redirect() and wp_redirect() shouldn't allow non-3xx status codes

Reported by: johnbillion Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: good-first-bug has-patch needs-testing
Focuses: Cc:


It might not be immediately obvious that the below code has a serious bug in it:

wp_safe_redirect( $url, 404 );

The wp_safe_redirect() and wp_redirect() functions should trigger an error if an HTTP status code is passed in that isn't in the 3xx range. The code above can cause much head scratching when you're presented with a 404 with no output.

I think it would make sense to trigger a wp_die() error message in this situation, to ensure maximum chance of visibility to the developer.

Attachments (1)

44317.diff (3.0 KB) - added by mjnewman 4 weeks ago.

Download all attachments as: .zip

Change History (4)

#1 @johnbillion
5 weeks ago

  • Keywords good-first-bug added

4 weeks ago

#2 @mjnewman
4 weeks ago

  • Keywords has-patch added; needs-patch removed

Added check_http_status_code() function to check if an HTTP status code is within a specific range, such as 3XX, or a multiple ranges, such as 2XX and 3XX. Includes option to wp_die(). Returns the HTTP status code if it's within the specified range.

Added check_http_status_code() to wp_redirect() after the 'wp_redirect_status' filter is applied.

#3 @mjnewman
3 weeks ago

  • Keywords needs-testing added
Note: See TracTickets for help on using tickets.