Skip to content

Commit 61604c3

Browse files
authored
Validate site slug and site URL in site create and generate commands (#632)
1 parent 11b395a commit 61604c3

3 files changed

Lines changed: 113 additions & 7 deletions

File tree

‎features/site-create.feature‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,67 @@ Feature: Create a new site on a WP multisite
250250
| blog_id | url |
251251
| 1 | https://example.com/ |
252252
| 2 | http://testsite.example.com/ |
253+
254+
Scenario: Error when invalid slug containing special characters is provided
255+
Given a WP multisite install
256+
257+
When I try `wp site create --slug='x$(touch /tmp/wpcli_poc)'`
258+
Then STDERR should be:
259+
"""
260+
Error: Slug may only contain letters, numbers, and dashes.
261+
"""
262+
And the return code should be 1
263+
264+
Scenario: Error when invalid domain format is provided in site-url
265+
Given a WP multisite install
266+
267+
When I try `wp site create --site-url='http://invalid$domain.com/site'`
268+
Then STDERR should be:
269+
"""
270+
Error: Invalid domain format in --site-url.
271+
"""
272+
And the return code should be 1
273+
274+
Scenario: Error when malformed domain with double dots is provided in site-url
275+
Given a WP multisite install
276+
277+
When I try `wp site create --site-url='http://example..com/site'`
278+
Then STDERR should be:
279+
"""
280+
Error: Invalid domain format in --site-url.
281+
"""
282+
And the return code should be 1
283+
284+
Scenario: Error when invalid path format is provided in site-url
285+
Given a WP multisite install
286+
287+
When I try `wp site create --site-url='http://example.com/invalid_path'`
288+
Then STDERR should be:
289+
"""
290+
Error: Invalid path format in --site-url.
291+
"""
292+
And the return code should be 1
293+
294+
Scenario: Error when duplicate slashes are provided in site-url path
295+
Given a WP multisite install
296+
297+
When I try `wp site create --site-url='http://example.com//foo'`
298+
Then STDERR should be:
299+
"""
300+
Error: Invalid path format in --site-url.
301+
"""
302+
And the return code should be 1
303+
304+
Scenario: Error when unsupported components are provided in site-url
305+
Given a WP multisite install
306+
307+
When I try `wp site create --site-url='http://example.com:8080/site'`
308+
Then STDERR should be:
309+
"""
310+
Error: Invalid URL format. User credentials, ports, query parameters, and fragments are not supported in --site-url.
311+
"""
312+
And the return code should be 1
313+
314+
315+
316+

‎features/site-generate.feature‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,14 @@ Feature: Generate new WordPress sites
105105
"""
106106
And STDOUT should be empty
107107
And the return code should be 1
108+
109+
Scenario: Error when invalid slug containing special characters is provided
110+
Given a WP multisite install
111+
112+
When I try `wp site generate --slug='x$(touch /tmp/wpcli_poc)'`
113+
Then STDERR should be:
114+
"""
115+
Error: Slug may only contain letters, numbers, and dashes.
116+
"""
117+
And the return code should be 1
118+

‎src/Site_Command.php‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ public function create( $args, $assoc_args ) {
579579

580580
if ( $has_site_url ) {
581581
$parsed_url = wp_parse_url( $assoc_args['site-url'] );
582-
if ( ! isset( $parsed_url['host'] ) ) {
582+
if ( ! is_array( $parsed_url ) || ! isset( $parsed_url['host'] ) || ! is_string( $parsed_url['host'] ) ) {
583583
WP_CLI::error( 'Invalid URL format. Please provide a valid URL (e.g., http://site.example.com).' );
584584
}
585585

@@ -588,9 +588,33 @@ public function create( $args, $assoc_args ) {
588588
WP_CLI::error( 'Invalid URL scheme. Only http and https schemes are supported.' );
589589
}
590590

591+
// Reject unsupported URL components (user, pass, port, query, fragment)
592+
$unsupported = array_intersect_key( $parsed_url, array_flip( [ 'user', 'pass', 'port', 'query', 'fragment' ] ) );
593+
if ( ! empty( $unsupported ) ) {
594+
WP_CLI::error( 'Invalid URL format. User credentials, ports, query parameters, and fragments are not supported in --site-url.' );
595+
}
596+
591597
// Sanitize domain and path
598+
$raw_path = isset( $parsed_url['path'] ) && is_string( $parsed_url['path'] ) ? $parsed_url['path'] : '/';
592599
$custom_domain = sanitize_text_field( $parsed_url['host'] );
593-
$custom_path = isset( $parsed_url['path'] ) ? sanitize_text_field( '/' . ltrim( $parsed_url['path'], '/' ) ) : '/';
600+
$custom_path = sanitize_text_field( '/' . ltrim( $raw_path, '/' ) );
601+
602+
$domain_parts = explode( '.', $custom_domain );
603+
$valid_domain = true;
604+
foreach ( $domain_parts as $part ) {
605+
if ( '' === $part || ! preg_match( '/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$/', $part ) ) {
606+
$valid_domain = false;
607+
break;
608+
}
609+
}
610+
611+
if ( ! $valid_domain ) {
612+
WP_CLI::error( 'Invalid domain format in --site-url.' );
613+
}
614+
615+
if ( ! preg_match( '|^[a-zA-Z0-9/-]+$|', $custom_path ) || false !== strpos( $raw_path, '//' ) ) {
616+
WP_CLI::error( 'Invalid path format in --site-url.' );
617+
}
594618

595619
// Ensure path ends with /
596620
if ( '/' !== substr( $custom_path, -1 ) ) {
@@ -613,7 +637,12 @@ public function create( $args, $assoc_args ) {
613637
$base = strtolower( $base );
614638
} else {
615639
// For subdirectory installs, derive slug from the last part of the path.
616-
$path_parts = array_filter( explode( '/', trim( $custom_path, '/' ) ) );
640+
$path_parts = array_filter(
641+
explode( '/', trim( $custom_path, '/' ) ),
642+
function ( $part ) {
643+
return '' !== $part;
644+
}
645+
);
617646
$base = (string) array_pop( $path_parts );
618647

619648
// If base is empty (root path), require explicit slug.
@@ -651,9 +680,10 @@ public function create( $args, $assoc_args ) {
651680
$public = ! Utils\get_flag_value( $assoc_args, 'private' );
652681

653682
// Sanitize
654-
if ( preg_match( '|^([a-zA-Z0-9-])+$|', $base ) ) {
655-
$base = strtolower( $base );
683+
if ( ! preg_match( '|^([a-zA-Z0-9-])+$|D', $base ) ) {
684+
WP_CLI::error( 'Slug may only contain letters, numbers, and dashes.' );
656685
}
686+
$base = strtolower( $base );
657687

658688
// If not a subdomain install, make sure the domain isn't a reserved word
659689
if ( ! is_subdomain_install() ) {
@@ -792,9 +822,10 @@ public function generate( $args, $assoc_args ) {
792822

793823
// Base.
794824
$base = $assoc_args['slug'];
795-
if ( preg_match( '|^([a-zA-Z0-9-])+$|', $base ) ) {
796-
$base = strtolower( $base );
825+
if ( ! preg_match( '|^([a-zA-Z0-9-])+$|D', $base ) ) {
826+
WP_CLI::error( 'Slug may only contain letters, numbers, and dashes.' );
797827
}
828+
$base = strtolower( $base );
798829

799830
$is_subdomain_install = is_subdomain_install();
800831
// If not a subdomain install, make sure the domain isn't a reserved word

0 commit comments

Comments
 (0)