Skip to content

Commit 2b2e773

Browse files
authored
Merge pull request #988 from cloudinary/feature/filterable-urls
Wrap `rest_url`, `site_url` and `home_url` so that we can filter these securely
2 parents 59ef87c + 177c6ef commit 2b2e773

19 files changed

Lines changed: 122 additions & 44 deletions

php/cache/class-file-system.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Cloudinary\Cache;
99

1010
use Cloudinary\Plugin;
11+
use Cloudinary\Utils;
1112

1213
/**
1314
* Class File System.
@@ -62,7 +63,7 @@ protected function setup_paths() {
6263
content_url() => $this->wp_file_system->wp_content_dir(),
6364
admin_url() => $this->wp_file_system->abspath() . 'wp-admin/',
6465
includes_url() => $this->wp_file_system->abspath() . 'wp-includes/',
65-
home_url() => $this->wp_file_system->abspath(),
66+
Utils::home_url() => $this->wp_file_system->abspath(),
6667
);
6768
foreach ( $paths as $url => $path ) {
6869
$this->paths[ trailingslashit( $url ) ] = trailingslashit( $path );

php/class-assets.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -433,19 +433,7 @@ public function activate_parent( $url ) {
433433
* @return string
434434
*/
435435
public function clean_path( $path ) {
436-
/**
437-
* Filter the home url.
438-
*
439-
* @hook cloudinary_home_url
440-
* @since 3.2.0
441-
*
442-
* @param $home_url {string} The home url.
443-
*
444-
* @return {string}
445-
*/
446-
$home_url = apply_filters( 'cloudinary_home_url', home_url() );
447-
448-
$home = Utils::clean_url( trailingslashit( $home_url ) );
436+
$home = Utils::clean_url( trailingslashit( Utils::home_url() ) );
449437
$path = str_replace( $home, '', Utils::clean_url( $path ) );
450438
if ( empty( Utils::pathinfo( $path, PATHINFO_EXTENSION ) ) ) {
451439
$path = urldecode( trailingslashit( $path ) );
@@ -629,7 +617,7 @@ public function get_asset_storage_folder( $url_id ) {
629617
$url_id = $this->media->local_url( $url_id );
630618
}
631619
$url = $this->clean_path( $url_id );
632-
$domain = wp_parse_url( home_url(), PHP_URL_HOST );
620+
$domain = wp_parse_url( Utils::home_url(), PHP_URL_HOST );
633621
$folder = wp_normalize_path( dirname( trim( $url, './' ) ) );
634622
if ( ! empty( $domain ) ) {
635623
$folder = path_join( $domain, $folder );
@@ -1101,8 +1089,8 @@ protected function create_asset( $url, $parent_id ) {
11011089
require_once ABSPATH . 'wp-admin/includes/image.php';
11021090
require_once ABSPATH . 'wp-admin/includes/media.php';
11031091

1104-
$full_url = urldecode( home_url() . wp_parse_url( $url, PHP_URL_PATH ) );
1105-
$file_path = urldecode( str_replace( home_url(), untrailingslashit( ABSPATH ), $full_url ) );
1092+
$full_url = urldecode( Utils::home_url() . wp_parse_url( $url, PHP_URL_PATH ) );
1093+
$file_path = urldecode( str_replace( Utils::home_url(), untrailingslashit( ABSPATH ), $full_url ) );
11061094
if ( ! file_exists( $file_path ) ) {
11071095
return false;
11081096
}

php/class-cache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function __construct( Plugin $plugin ) {
102102
parent::__construct( $plugin );
103103
$this->file_system = new File_System( $plugin );
104104
if ( $this->file_system->enabled() ) {
105-
$this->cache_folder = wp_parse_url( get_site_url(), PHP_URL_HOST );
105+
$this->cache_folder = wp_parse_url( Utils::site_url(), PHP_URL_HOST );
106106
$this->media = $this->plugin->get_component( 'media' );
107107
$this->connect = $this->plugin->get_component( 'connect' );
108108
$this->api = $this->plugin->get_component( 'api' );
@@ -239,7 +239,7 @@ protected function register_hooks() {
239239
* @return array
240240
*/
241241
public function prevent_caching_internal_requests( $args, $url ) {
242-
$home = strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) );
242+
$home = strtolower( wp_parse_url( Utils::home_url(), PHP_URL_HOST ) );
243243
$request = strtolower( wp_parse_url( $url, PHP_URL_HOST ) );
244244
if ( $home === $request ) {
245245
$args['headers']['x-cld-cache'] = time();

php/class-connect.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function rest_save_wizard( WP_REST_Request $request ) {
215215
if ( ! empty( $url ) ) {
216216
// Warm the last uploaded items in the media library.
217217
wp_safe_remote_request(
218-
rest_url( 'wp/v2/media' ),
218+
Utils::rest_url( 'wp/v2/media' ),
219219
array(
220220
'timeout' => 0.1,
221221
'blocking' => false,
@@ -1023,7 +1023,7 @@ public static function test_rest_api_connectivity() {
10231023
),
10241024
);
10251025

1026-
$url = rest_url( REST_API::BASE . '/test_rest_api' );
1026+
$url = Utils::rest_url( REST_API::BASE . '/test_rest_api' );
10271027
$response = wp_safe_remote_get( $url, $args );
10281028

10291029
if ( is_wp_error( $response ) ) {

php/class-deactivation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public function enqueue_scripts() {
334334
'cloudinary-deactivation',
335335
'CLD_Deactivate',
336336
array(
337-
'endpoint' => rest_url( REST_API::BASE . '/' . self::$internal_endpoint ),
337+
'endpoint' => Utils::rest_url( REST_API::BASE . '/' . self::$internal_endpoint ),
338338
'nonce' => wp_create_nonce( 'wp_rest' ),
339339
)
340340
);

php/class-delivery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ protected function is_allowed_type( $ext ) {
15891589
public function validate_url( $url ) {
15901590
static $home;
15911591
if ( ! $home ) {
1592-
$home = wp_parse_url( home_url( '/' ) );
1592+
$home = wp_parse_url( Utils::home_url( '/' ) );
15931593
}
15941594
$parts = wp_parse_url( $url );
15951595
if ( empty( $parts['host'] ) ) {

php/class-extensions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public function register_extensions() {
249249
*/
250250
public function setup() {
251251
$data = array(
252-
'url' => rest_url( REST_API::BASE . '/extension' ),
252+
'url' => Utils::rest_url( REST_API::BASE . '/extension' ),
253253
'nonce' => wp_create_nonce( 'wp_rest' ),
254254
);
255255
foreach ( $this->settings->get_settings() as $setting ) {

php/class-media-library.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function enqueue_assets() {
8181
wp_enqueue_script( 'cloudinary' );
8282

8383
$params = array(
84-
'fetch_url' => rest_url( REST_API::BASE . '/asset' ),
84+
'fetch_url' => Utils::rest_url( REST_API::BASE . '/asset' ),
8585
'nonce' => wp_create_nonce( 'wp_rest' ),
8686
);
8787

php/class-media.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,14 @@ public function is_media( $attachment_id ) {
329329
* @return bool
330330
*/
331331
public function is_local_media( $attachment_id ) {
332-
$local_host = wp_parse_url( get_site_url(), PHP_URL_HOST );
332+
$local_host = wp_parse_url( Utils::site_url(), PHP_URL_HOST );
333333
$guid = get_the_guid( $attachment_id );
334334

335335
// Maybe GUID is a path.
336336
if ( ! filter_var( $guid, FILTER_VALIDATE_URL ) ) {
337-
$url = home_url( $guid );
337+
$url = Utils::home_url( $guid );
338338
if ( $this->maybe_file_exist_in_url( $url ) ) {
339-
$guid = home_url( $guid );
339+
$guid = Utils::home_url( $guid );
340340
}
341341
}
342342

@@ -2269,7 +2269,7 @@ public function down_sync_asset() {
22692269
$asset = $this->get_asset_payload();
22702270
// Set a base array for pulling an asset if needed.
22712271
$base_return = array(
2272-
'fetch' => rest_url( REST_API::BASE . '/asset' ),
2272+
'fetch' => Utils::rest_url( REST_API::BASE . '/asset' ),
22732273
'uploading' => true,
22742274
'src' => $asset['src'],
22752275
'url' => $asset['url'],

php/class-rest-api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function rest_can_connect() {
7979
*/
8080
public function background_request( $endpoint, $params = array(), $method = 'POST' ) {
8181

82-
$url = rest_url( static::BASE . '/' . $endpoint );
82+
$url = Utils::rest_url( static::BASE . '/' . $endpoint );
8383
// Setup a call for a background sync.
8484
$params['nonce'] = wp_create_nonce( 'wp_rest' );
8585
$args = array(

0 commit comments

Comments
 (0)