Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect localhost/127.0.0.1 code in multiple files #399

Merged
merged 7 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions includes/Checker/Checks/Abstract_File_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,69 @@ final protected static function file_preg_match( $pattern, array $files, array &
return false;
}

/**
* Returns matched files performing a regular expression match on the file contents of the given list of files.
*
* @since 1.0.2
*
* @param string $pattern The pattern to search for.
* @param array $files List of absolute file paths.
* @return array|bool Array of file paths and matched string/pattern if matches were found, false otherwise.
*/
final protected static function files_preg_match( $pattern, array $files ) {
$matched_files = array();

foreach ( $files as $file ) {
$matches = array();

$matched_file_name = self::file_preg_match( $pattern, array( $file ), $matches );

if ( false !== $matched_file_name ) {
$matched_files[] = array( $matched_file_name, $matches[0] );
}
}

return count( $matched_files ) > 0 ? $matched_files : false;
}

/**
* Returns matched files performing a regular expression match on the file contents of the given list of files with line and column information.
*
* @since 1.0.2
*
* @param string $pattern The pattern to search for.
* @param array $files List of absolute file paths.
* @return array|bool Array of file paths and matched string/pattern if matches were found, false otherwise.
*/
final protected static function files_preg_match_all( $pattern, array $files ) {
$matched_files = array();

foreach ( $files as $file ) {
$matches = array();

$contents = self::file_get_contents( $file );

preg_match_all( $pattern, $contents, $matches, PREG_OFFSET_CAPTURE );

if ( is_array( $matches ) && ! empty( $matches ) ) {
foreach ( $matches[0] as $match ) {
list( $before ) = str_split( $contents, $match[1] );

$exploded = explode( PHP_EOL, $before );
$last_item = end( $exploded );

$matched_files[] = array(
'file' => $file,
'line' => count( $exploded ),
'column' => strlen( $last_item ) + 1,
);
}
}
}

return count( $matched_files ) > 0 ? $matched_files : false;
}

/**
* Performs a check indicating if the needle is contained in the file contents of the given list of files.
*
Expand Down
21 changes: 13 additions & 8 deletions includes/Checker/Checks/Localhost_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ public function get_categories() {
*/
protected function check_files( Check_Result $result, array $files ) {
$php_files = self::filter_files_by_extension( $files, 'php' );
$file = self::file_preg_match( '#https?://(localhost|127.0.0.1)#', $php_files );
if ( $file ) {
$this->add_result_error_for_file(
$result,
__( 'Do not use Localhost/127.0.0.1 in your code.', 'plugin-check' ),
'localhost_code_detected',
$file
);
$files = self::files_preg_match_all( '#https?:\/\/(localhost|127.0.0.1|(.*\.local(host)?))\/#', $php_files );

if ( ! empty( $files ) ) {
foreach ( $files as $file ) {
$this->add_result_error_for_file(
$result,
__( 'Do not use Localhost/127.0.0.1 in your code.', 'plugin-check' ),
'localhost_code_detected',
$file['file'],
$file['line'],
$file['column']
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$var = 'This file contains https://127.0.0.1/example url.';
$sample_url = 'Sample URL is http://docker.local/example here.';
$custom_url = 'Custom URL https://docker.localhost/example here.';
22 changes: 17 additions & 5 deletions tests/phpunit/tests/Checker/Checks/Localhost_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@ public function test_run_with_errors() {

$this->assertNotEmpty( $errors );
$this->assertArrayHasKey( 'load.php', $errors );
$this->assertEquals( 1, $check_result->get_error_count() );
$this->assertArrayHasKey( 'another.php', $errors );
$this->assertSame( 4, $check_result->get_error_count() );

$this->assertArrayHasKey( 0, $errors['load.php'] );
$this->assertArrayHasKey( 0, $errors['load.php'][0] );
$this->assertArrayHasKey( 'code', $errors['load.php'][0][0][0] );
$this->assertEquals( 'localhost_code_detected', $errors['load.php'][0][0][0]['code'] );
$this->assertArrayHasKey( 19, $errors['load.php'] );
$this->assertArrayHasKey( 24, $errors['load.php'][19] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][19][24], array( 'code' => 'localhost_code_detected' ) ) );

$this->assertArrayHasKey( 2, $errors['another.php'] );
$this->assertArrayHasKey( 35, $errors['another.php'][2] );
$this->assertCount( 1, wp_list_filter( $errors['another.php'][2][35], array( 'code' => 'localhost_code_detected' ) ) );

$this->assertArrayHasKey( 3, $errors['another.php'] );
$this->assertArrayHasKey( 30, $errors['another.php'][3] );
$this->assertCount( 1, wp_list_filter( $errors['another.php'][3][30], array( 'code' => 'localhost_code_detected' ) ) );

$this->assertArrayHasKey( 4, $errors['another.php'] );
$this->assertArrayHasKey( 27, $errors['another.php'][4] );
$this->assertCount( 1, wp_list_filter( $errors['another.php'][4][27], array( 'code' => 'localhost_code_detected' ) ) );
}
}
Loading