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

Add allow list for file filtering function #289

Merged
merged 4 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 38 additions & 0 deletions misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,44 @@ function vipgoci_filter_file_path(
break;
}
}
} else if (
ingeniumed marked this conversation as resolved.
Show resolved Hide resolved
( null !== $filter ) &&
( isset( $filter['include_folders'] ) )
){
ingeniumed marked this conversation as resolved.
Show resolved Hide resolved
ingeniumed marked this conversation as resolved.
Show resolved Hide resolved
/*
* Loop through all include-folders.
*/
foreach (
$filter['include_folders'] as $tmp_include_folder_item
) {
/*
* Note: All 'include_folders' options should lack '/' at the
* end and beginning.
*
* $filename we expect to be a relative path.
*/
$file_folders_match = strpos(
$filename,
$tmp_include_folder_item . '/'
);

/*
* If it's a match, that folder is to be not skipped.
* Otherwise, it's skipped.
*
ingeniumed marked this conversation as resolved.
Show resolved Hide resolved
* There can only be 1 match with the filename so the
* moment that happens, we break out.
*/
if (
( false !== $file_folders_match ) &&
( is_numeric( $file_folders_match ) )
) {
$file_folders_match = false;
break;
} else {
$file_folders_match = true;
}
}
}

/*
Expand Down
94 changes: 94 additions & 0 deletions tests/unit/MiscFilterFilePathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,100 @@ public function testFilterFilePath5() {

}

/**
* @covers ::vipgoci_filter_file_path
*/
public function testFilterFilePath6() {
$file_name = 'folder1/file1.txt';

$this->assertFalse(
vipgoci_filter_file_path(
$file_name,
array(
'include_folders' => array(
'folder2',
)
)
)
);

$this->assertTrue(
vipgoci_filter_file_path(
$file_name,
array(
'include_folders' => array(
'folder1',
)
)
)
);
}

/**
* @covers ::vipgoci_filter_file_path
*/
public function testFilterFilePath7() {
$file_name = 'my/unit-tests/folder1/subfolder/file1.txt';

$this->assertFalse(
vipgoci_filter_file_path(
$file_name,
array(
'include_folders' => array(
'folder200',
'folder3000',
'folder4000/folder5000/folder6000',
'SubFolder' // Note: capital 'F'
),
)
)
);

$this->assertTrue(
vipgoci_filter_file_path(
$file_name,
array(
'include_folders' => array(
'unit-tests/folder1/subfolder', // Note: Unlike skip_folders, this is allowed when it's not at root level
),
)
)
);

$this->assertTrue(
vipgoci_filter_file_path(
$file_name,
array(
'include_folders' => array(
'somefoldertesting/otherfolder/foobar123',
'somefoldertesting/otherfolder/foobar321',
'my/unit-tests/folder1/subfolder',
),
)
)
);

$this->assertTrue(
vipgoci_filter_file_path(
$file_name,
array(
'include_folders' => array(
'my/unit-tests',
),
)
)
);

$this->assertFalse(
vipgoci_filter_file_path(
$file_name,
array(
'include_folders' => array(
'test',
),
)
)
);

}
}