WordPress.org

Make WordPress Core

Opened 6 months ago

Last modified 6 months ago

#52428 new enhancement

Create a Filter for Blocking Outgoing HTTP Calls | Single URL

Reported by: jetxpert Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 2.7
Component: HTTP API Keywords: close 2nd-opinion
Focuses: Cc:

Description

Good Day!

Per subject, kindly create a new filter that will allow blocking Outgoing HTTP calls for single or defined URLs. Hopefully, prior to the release of WP 5.7.

Details here https://wordpress.org/support/topic/how-to-block-outgoing-http-call-single-url/

and

here https://make.wordpress.org/core/2020/11/23/wordpress-5-7-whats-on-your-wishlist/.

Sorry for the repeat entries. Many sources available :)

Cheers!

Change History (5)

#1 @TimothyBlynJacobs
6 months ago

  • Component changed from General to HTTP API
  • Keywords close reporter-feedback added; dev-feedback needs-unit-tests needs-design removed
  • Version changed from 5.6 to 2.7

Hi @jetxpert,

Welcome to trac! This should be doable with the existing pre_http_request filter. For example:

add_filter( 'pre_http_request', function ( $result, $args, $url ) {
	if ( wp_parse_url( $url, PHP_URL_HOST ) === 'github.com' ) {
		return new WP_Error( 'http_request_not_executed', __( 'User has blocked requests through HTTP.' ) );
	}

	return $result;
}, 10, 3 );

#2 @jetxpert
6 months ago

  • Keywords needs-testing added; 2nd-opinion close removed

@TimothyBlynJacobs,

Thank you so much. We'll give it a spin and report back.

Questions:

(1) Can your proposed solution be converted to a "define" type filter we can enter into our wpconfig.php file?

(2) We do not want to report back a WP Error. How can we modify your snippet to reflect this?

(3) To add another (i.e, second or third) URL, how would we modify your snippet?

PS: We miss you over at iThemes Security (forum). Help appreciated. Let us know where we can donate :)

Again, thank you.

Cheers!

Last edited 6 months ago by jetxpert (previous) (diff)

#3 @jetxpert
6 months ago

  • Keywords needs-testing removed

@TimothyBlynJacobs,

Reporting back. Your snippet works like a charm. Thank you.

Can you kindly reply to our questions?

Thank you.

#4 @jetxpert
6 months ago

@TimothyBlynJacobs,

Update:

Questions (2) and (3) resolved.

Only need help with Question (1).

Thank you.

PS: This item added to WP 5.7's wish list https://make.wordpress.org/core/2020/11/23/wordpress-5-7-whats-on-your-wishlist/.

Last edited 6 months ago by jetxpert (previous) (diff)

#5 @TimothyBlynJacobs
6 months ago

  • Keywords close 2nd-opinion added; reporter-feedback removed

You could do something like this:

define( 'MY_BLOCKED_HOSTS', 'github.com,gitlab.com' );

add_filter( 'pre_http_request', function ( $result, $args, $url ) {
	$hosts = wp_parse_list( MY_BLOCKED_HOSTS );

	if ( in_array( wp_parse_url( $url, PHP_URL_HOST ), $hosts, true ) ) {
		return new WP_Error( 'http_request_not_executed', __( 'User has blocked requests through HTTP.' ) );
	}

	return $result;
}, 10, 3 );

FYI: I'm adding the close keyword to indicate that this probably doesn't need changing in WordPress Core. The 2nd-opinion is for another Core contributor to take a look and see if they disagree.

Note: See TracTickets for help on using tickets.