Last active
February 21, 2026 17:04
-
-
Save afragen/ece38aafd57634b49e428c23baa2d551 to your computer and use it in GitHub Desktop.
WordPress function to check if URL is SSRF safe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function is_url_ssrf_safe( $url ) { | |
| $allowed_protocols = [ 'http', 'https' ]; | |
| $scheme = parse_url( $url, PHP_URL_SCHEME ); | |
| if ( ! in_array( $scheme, $allowed_protocols, true ) ) { | |
| return new WP_Error( 'invalid_protocol', __( 'The URL provided uses an unsupported protocol.', 'textdomain' ) ); | |
| } | |
| $ip = gethostbyname( parse_url( $url, PHP_URL_HOST ) ); | |
| if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) === false ) { | |
| return new WP_Error( 'private_ip', __( 'The URL provided resolves to a private or reserved IP address.', 'textdomain' ) ); | |
| } | |
| if ( ! filter_var( $url, FILTER_SANITIZE_URL ) || ! filter_var( $url, FILTER_VALIDATE_URL ) ) { | |
| return new WP_Error( 'invalid_url', __( 'The URL provided is not valid.', 'textdomain' ) ); | |
| } | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment