Skip to content

Commit 6699d54

Browse files
authored
Merge pull request #900 from nikunj8866/fix/890-improve-url-validation
Improve url validation to check duplicate protocol
2 parents 737300c + 494fd8c commit 6699d54

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,17 @@ public function run( Check_Result $result ) {
485485
* @return bool true if the URL is valid, otherwise false.
486486
*/
487487
private function is_valid_url( $url ) {
488-
return filter_var( $url, FILTER_VALIDATE_URL ) === $url && str_starts_with( $url, 'http' );
488+
if ( filter_var( $url, FILTER_VALIDATE_URL ) !== $url || ! str_starts_with( $url, 'http' ) ) {
489+
return false;
490+
}
491+
492+
// Detect duplicated protocol (e.g., "https://http://example.com/").
493+
$parsed_url = wp_parse_url( $url );
494+
if ( isset( $parsed_url['scheme'] ) && str_contains( substr( $url, strlen( $parsed_url['scheme'] ) + 3 ), '://' ) ) {
495+
return false;
496+
}
497+
498+
return true;
489499
}
490500

491501
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Plugin Name: Test Header fields with errors
4+
* Plugin URI: https://example.com/sample-link
5+
* Description: Here is a short description of the plugin.
6+
* Requires at least: Recent version
7+
* Requires PHP: Latest version
8+
* Author: WordPress Performance Team
9+
* Author URI: https://http://example.com/
10+
* Text Domain: test-mismathed-textdomain-here
11+
* Domain Path: /nonexistent-folder
12+
* Network: random-value
13+
* GitHub Plugin URI: johndoe/package
14+
* Requires Plugins: Example Plugin, OtherPlugin
15+
*
16+
* @package test-plugin-header-fields-with-errors
17+
*/

tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php

+16
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,20 @@ public function test_run_without_errors_requires_at_least_latest_version() {
167167

168168
$this->assertEmpty( $errors );
169169
}
170+
171+
public function test_run_with_errors_duplicated_protocol_is_valid_url() {
172+
$check = new Plugin_Header_Fields_Check();
173+
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-header-fields-duplicated-protocol-with-errors/load.php' );
174+
$check_result = new Check_Result( $check_context );
175+
176+
$check->run( $check_result );
177+
178+
$errors = $check_result->get_errors();
179+
180+
$filtered_items = wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_author_uri' ) );
181+
182+
$this->assertCount( 1, $filtered_items );
183+
$this->assertStringContainsString( 'Author URI', $filtered_items[1]['message'] );
184+
$this->assertStringContainsString( 'is not valid', $filtered_items[1]['message'] );
185+
}
170186
}

0 commit comments

Comments
 (0)