Skip to content

Commit dcbcf38

Browse files
authored
Merge pull request #8922 from kenjis/add-permission-check
chore: add file permission check workflow
2 parents f2b8892 + e0d2cff commit dcbcf38

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

Diff for: .github/workflows/test-file-permissions.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Check File Permissions
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9+
cancel-in-progress: true
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
permission-check:
16+
name: Check File Permission
17+
runs-on: ubuntu-22.04
18+
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Detect unnecessary execution permissions
24+
run: php utils/check_permission_x.php

Diff for: utils/check_permission_x.php

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Utils;
15+
16+
require __DIR__ . '/../system/Test/bootstrap.php';
17+
18+
use CodeIgniter\CLI\CLI;
19+
use RecursiveDirectoryIterator;
20+
use RecursiveIteratorIterator;
21+
use RuntimeException;
22+
23+
function findExecutableFiles($dir)
24+
{
25+
$execFileList = [
26+
'admin/release-userguide',
27+
'admin/release-deploy',
28+
'admin/apibot',
29+
'admin/alldocs',
30+
'admin/release',
31+
'admin/docbot',
32+
'admin/release-notes.bb',
33+
'admin/release-revert',
34+
'admin/starter/builds',
35+
'user_guide_src/add-edit-this-page',
36+
];
37+
38+
$executableFiles = [];
39+
40+
// Check if the directory exists
41+
if (! is_dir($dir)) {
42+
throw new RuntimeException('No such directory: ' . $dir);
43+
}
44+
45+
// Create a Recursive Directory Iterator
46+
$iterator = new RecursiveIteratorIterator(
47+
new RecursiveDirectoryIterator($dir)
48+
);
49+
50+
// Iterate over each item in the directory
51+
foreach ($iterator as $fileinfo) {
52+
// Check if the item is a file and is executable
53+
if ($fileinfo->isFile() && is_executable($fileinfo->getPathname())) {
54+
$filePath = $fileinfo->getPathname();
55+
56+
// Check allow list
57+
if (in_array($filePath, $execFileList, true)) {
58+
continue;
59+
}
60+
61+
if (str_ends_with($filePath, '.sh')) {
62+
continue;
63+
}
64+
65+
$executableFiles[] = $filePath;
66+
}
67+
}
68+
69+
return $executableFiles;
70+
}
71+
72+
// Main
73+
chdir(__DIR__ . '/../');
74+
75+
$dirs = ['admin', 'app', 'system', 'tests', 'user_guide_src', 'utils', 'writable'];
76+
77+
$executableFiles = [];
78+
79+
foreach ($dirs as $dir) {
80+
$executableFiles = array_merge($executableFiles, findExecutableFiles($dir));
81+
}
82+
83+
if ($executableFiles !== []) {
84+
CLI::write('Files with unnecessary execution permissions were detected:', 'light_gray', 'red');
85+
86+
foreach ($executableFiles as $file) {
87+
CLI::write('- ' . $file);
88+
}
89+
90+
exit(1);
91+
}
92+
93+
CLI::write('No files with unnecessary execution permissions were detected.', 'black', 'green');
94+
95+
exit(0);

0 commit comments

Comments
 (0)