@@ -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