Skip to content

Commit 3b1f197

Browse files
LukeTowersclaude
andcommitted
Harden phpcs utilities: escape filenames instead of only spaces
The phpcs-pr/phpcs-push helpers built the PHPCS command by concatenating changed filenames with only spaces backslash-escaped, so other shell metacharacters in a crafted filename could be interpreted by the shell. Pass each path through escapeshellarg() instead (and drop the now-redundant space-escaping loop). The empty-diff early exit is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent c14326e commit 3b1f197

2 files changed

Lines changed: 6 additions & 12 deletions

File tree

.github/workflows/utilities/phpcs-pr

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@ if (empty($argv[1])) {
1616
$fileList = shell_exec('git diff --name-only --diff-filter=ACMR origin/' . $argv[1] . ' HEAD');
1717
$files = array_filter(explode("\n", $fileList));
1818

19-
foreach ($files as &$file) {
20-
if (strpos($file, ' ') !== false) {
21-
$file = str_replace(' ', '\\ ', $file);
22-
}
23-
}
24-
2519
// no changes found in diff, early exit
2620
if (!count($files)) {
2721
fwrite(STDOUT, "\e[0;32mFound no changed files.\e[0m");
2822
fwrite(STDOUT, "\n");
2923
exit(0);
3024
}
3125

26+
// Escape each path so filenames are passed to the shell safely.
27+
$files = array_map('escapeshellarg', $files);
28+
3229
// Run all changed files through the PHPCS code sniffer and generate a CSV report
3330
$csv = shell_exec('phpcs --colors -nq --report="csv" --extensions="php" ' . implode(' ', $files));
3431
$lines = array_map(function ($row) {

.github/workflows/utilities/phpcs-push

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@ if (empty($argv[1])) {
1616
$fileList = shell_exec('git show --name-only --pretty="" --diff-filter=ACMR ' . $argv[1]);
1717
$files = array_filter(explode("\n", $fileList));
1818

19-
foreach ($files as &$file) {
20-
if (strpos($file, ' ') !== false) {
21-
$file = str_replace(' ', '\\ ', $file);
22-
}
23-
}
24-
2519
// no changes found in diff, early exit
2620
if (!count($files)) {
2721
fwrite(STDOUT, "\e[0;32mFound no changed files.\e[0m");
2822
fwrite(STDOUT, "\n");
2923
exit(0);
3024
}
3125

26+
// Escape each path so filenames are passed to the shell safely.
27+
$files = array_map('escapeshellarg', $files);
28+
3229
// Run all changed files through the PHPCS code sniffer and generate a CSV report
3330
$csv = shell_exec('phpcs --colors -nq --report="csv" --extensions="php" ' . implode(' ', $files));
3431
$lines = array_map(function ($row) {

0 commit comments

Comments
 (0)