Skip to content

Commit 04c0ee0

Browse files
swissspidyclaude
andauthored
Harden eval-file against failures while preparing the file (#98)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent acfc851 commit 04c0ee0

2 files changed

Lines changed: 98 additions & 11 deletions

File tree

‎features/eval.feature‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,91 @@ Feature: Evaluating PHP code and files.
271271
eval()'d code
272272
"""
273273

274+
Scenario: Eval-file will not replace __FILE__ and __DIR__ in heredoc and nowdoc strings
275+
Given an empty directory
276+
And a script.php file:
277+
"""
278+
<?php
279+
echo <<<TXT
280+
heredoc: __FILE__ __DIR__
281+
282+
TXT;
283+
echo <<<'TXT'
284+
nowdoc: __FILE__ __DIR__
285+
286+
TXT;
287+
echo 'code: ' . basename( __FILE__ ) . ' ' . basename( __DIR__ );
288+
"""
289+
290+
When I run `wp eval-file script.php --skip-wordpress`
291+
Then STDOUT should contain:
292+
"""
293+
heredoc: __FILE__ __DIR__
294+
"""
295+
And STDOUT should contain:
296+
"""
297+
nowdoc: __FILE__ __DIR__
298+
"""
299+
And STDOUT should contain:
300+
"""
301+
code: script.php
302+
"""
303+
And STDOUT should not contain:
304+
"""
305+
eval()'d code
306+
"""
307+
308+
Scenario: Eval-file can handle large files with long strings
309+
Given an empty directory
310+
And a generate.php file:
311+
"""
312+
<?php
313+
$links = str_repeat( '<a href="https://example.com/">it\'s "quoted"</a>' . PHP_EOL, 20000 );
314+
$text = str_repeat( 'a', 1024 * 1024 );
315+
316+
$script = "<?php\n";
317+
$script .= "\$links = <<<HTML\n{$links}\nHTML;\n";
318+
$script .= "\$text = '{$text}';\n";
319+
$script .= "echo basename( __FILE__ ), PHP_EOL, strlen( \$text ), PHP_EOL;\n";
320+
321+
file_put_contents( 'script.php', $script );
322+
"""
323+
324+
When I run `wp eval-file generate.php --skip-wordpress`
325+
And I run `wp eval-file script.php --skip-wordpress`
326+
Then STDOUT should be:
327+
"""
328+
script.php
329+
1048576
330+
"""
331+
And STDERR should be empty
332+
333+
Scenario: Eval-file only removes the shebang line at the start of the file
334+
Given an empty directory
335+
And a script.php file:
336+
"""
337+
#!/usr/bin/env wp
338+
<?php
339+
echo <<<TXT
340+
#!not a shebang
341+
TXT;
342+
echo PHP_EOL, __LINE__;
343+
"""
344+
345+
When I run `wp eval-file script.php --skip-wordpress`
346+
Then STDOUT should contain:
347+
"""
348+
#!not a shebang
349+
"""
350+
And STDOUT should contain:
351+
"""
352+
6
353+
"""
354+
But STDOUT should not contain:
355+
"""
356+
#!/usr/bin/env
357+
"""
358+
274359
Scenario: Eval with --hook flag
275360
Given a WP install
276361

‎src/EvalFile_Command.php‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55

66
class EvalFile_Command extends WP_CLI_Command {
77

8-
/**
9-
* Regular expression pattern to match the shell shebang.
10-
*
11-
* @var string
12-
*/
13-
const SHEBANG_PATTERN = '/^(#!.*)$/m';
14-
158
/**
169
* Loads and executes a PHP file.
1710
*
@@ -101,14 +94,23 @@ private static function execute_eval( $file, $positional_args, $use_include ) {
10194
} elseif ( $use_include ) {
10295
include $file;
10396
} else {
104-
$file_contents = (string) file_get_contents( $file );
97+
$file_contents = file_get_contents( $file );
98+
99+
if ( false === $file_contents ) {
100+
WP_CLI::error( "Could not read '$file'." );
101+
}
105102

106103
// Adjust for __FILE__ and __DIR__ magic constants.
107-
$file_contents = Path::replace_path_consts( $file_contents, $file );
104+
try {
105+
$file_contents = Path::replace_path_consts( $file_contents, $file );
106+
} catch ( RuntimeException $e ) {
107+
WP_CLI::error( "Could not prepare '$file' for evaluation: " . $e->getMessage() );
108+
}
108109

109-
// Check for and remove she-bang.
110+
// Remove a shebang line, but keep the line break so line numbers stay intact.
110111
if ( 0 === strncmp( $file_contents, '#!', 2 ) ) {
111-
$file_contents = preg_replace( static::SHEBANG_PATTERN, '', $file_contents );
112+
$line_break = strpos( $file_contents, "\n" );
113+
$file_contents = false === $line_break ? '' : substr( $file_contents, $line_break );
112114
}
113115

114116
eval( '?>' . $file_contents );

0 commit comments

Comments
 (0)