Skip to content

Commit 3c9a95c

Browse files
authored
fix: avoid filesystem null reads (#447)
1 parent 446933a commit 3c9a95c

2 files changed

Lines changed: 8 additions & 24 deletions

File tree

inc/Runtime/WordPressRuntimeInspector.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ public function ls( array $input ): array|\WP_Error {
125125
* @return array<string,mixed>|\WP_Error
126126
*/
127127
public function read( array $input ): array|\WP_Error {
128-
global $wp_filesystem;
129128
$path = (string) ( $input['path'] ?? '' );
130129
$max_size = $this->clampInt($input['max_size'] ?? self::DEFAULT_MAX_READ_SIZE, 1, self::DEFAULT_MAX_READ_SIZE);
131130
$offset = $this->clampInt($input['offset'] ?? 1, 1, PHP_INT_MAX);
@@ -151,11 +150,8 @@ public function read( array $input ): array|\WP_Error {
151150
);
152151
}
153152

154-
if ( is_readable($resolved['real_path']) ) {
155-
$sample = $wp_filesystem->get_contents($resolved['real_path'], false, null, 0, min($size, 8192));
156-
} else {
157-
$sample = false;
158-
}
153+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents,WordPress.PHP.NoSilencedErrors.Discouraged -- Path is validated by resolveAllowedPath().
154+
$sample = @file_get_contents($resolved['real_path'], false, null, 0, min($size, 8192));
159155
if ( false === $sample ) {
160156
return new \WP_Error('datamachine_runtime_unreadable', 'File is not readable.');
161157
}
@@ -164,11 +160,8 @@ public function read( array $input ): array|\WP_Error {
164160
return new \WP_Error('datamachine_runtime_binary_file', 'Binary file reading is denied.', array( 'path' => $resolved['relative_path'] ));
165161
}
166162

167-
if ( is_readable($resolved['real_path']) ) {
168-
$lines = file($resolved['real_path'], FILE_IGNORE_NEW_LINES);
169-
} else {
170-
$lines = false;
171-
}
163+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file,WordPress.PHP.NoSilencedErrors.Discouraged -- Path is validated by resolveAllowedPath().
164+
$lines = @file($resolved['real_path'], FILE_IGNORE_NEW_LINES);
172165
if ( false === $lines ) {
173166
return new \WP_Error('datamachine_runtime_unreadable', 'File is not readable.');
174167
}

inc/Workspace/WorkspaceWorktreeLifecycle.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,20 +1247,15 @@ private function try_rebase_worktree( string $wt_path, array &$response, bool $c
12471247
* @return string|null Branch name (e.g. `fix/foo`), or null when unknown.
12481248
*/
12491249
private function resolve_worktree_branch_from_head_file( string $wt_path ): ?string {
1250-
global $wp_filesystem;
12511250
$git_pointer = rtrim($wt_path, '/') . '/.git';
12521251
if ( ! is_file($git_pointer) && ! is_dir($git_pointer) ) {
12531252
return null;
12541253
}
12551254

12561255
$gitdir = null;
12571256
if ( is_file($git_pointer) ) {
1258-
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading .git pointer file in a controlled worktree.
1259-
if ( is_readable($git_pointer) ) {
1260-
$pointer = $wp_filesystem->get_contents($git_pointer);
1261-
} else {
1262-
$pointer = false;
1263-
}
1257+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents,WordPress.PHP.NoSilencedErrors.Discouraged -- Reading .git pointer file in a controlled worktree.
1258+
$pointer = @file_get_contents($git_pointer);
12641259
if ( false === $pointer ) {
12651260
return null;
12661261
}
@@ -1285,12 +1280,8 @@ private function resolve_worktree_branch_from_head_file( string $wt_path ): ?str
12851280
return null;
12861281
}
12871282

1288-
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading .git HEAD file in a controlled worktree.
1289-
if ( is_readable($head_file) ) {
1290-
$head = $wp_filesystem->get_contents($head_file);
1291-
} else {
1292-
$head = false;
1293-
}
1283+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents,WordPress.PHP.NoSilencedErrors.Discouraged -- Reading .git HEAD file in a controlled worktree.
1284+
$head = @file_get_contents($head_file);
12941285
if ( false === $head ) {
12951286
return null;
12961287
}

0 commit comments

Comments
 (0)