Make WordPress Core

Opened 17 months ago

Last modified 4 months 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:
PR Number:


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 5 months ago.

Download all attachments as: .zip

Change History (5)

#1 @johnbillion
5 months ago

  • Keywords good-first-bug added

5 months ago

#2 @mjnewman
5 months 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
5 months ago

  • Keywords needs-testing added

#4 @mjnewman
4 months ago

Hi @johnbillion wanted to check-in and see if my proposed patch was along the lines of what you were looking for. Happy to make any adjustments or updates as necessary. Thanks!

Note: See TracTickets for help on using tickets.