Skip to content

Commit

Permalink
Do not read from argv on non-CLI SAPIs
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Nov 5, 2024
1 parent ae232b0 commit 443f7d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
6 changes: 5 additions & 1 deletion SymfonyRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct(array $options = [])

if (isset($options['env'])) {
$_SERVER[$envKey] = $options['env'];
} elseif (isset($_SERVER['argv']) && class_exists(ArgvInput::class)) {
} elseif (empty($_GET) && isset($_SERVER['argv']) && class_exists(ArgvInput::class)) {
$this->options = $options;
$this->getInput();
}
Expand Down Expand Up @@ -216,6 +216,10 @@ protected static function register(GenericRuntime $runtime): GenericRuntime

private function getInput(): ArgvInput
{
if (!empty($_GET) && filter_var(ini_get('register_argc_argv'), \FILTER_VALIDATE_BOOL)) {
throw new \Exception('CLI applications cannot be run safely on non-CLI SAPIs with register_argc_argv=On.');
}

if (null !== $this->input) {
return $this->input;
}
Expand Down
8 changes: 5 additions & 3 deletions Tests/phpt/kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@

class TestKernel implements HttpKernelInterface
{
private $env;
private $var;

public function __construct(string $var)
public function __construct(string $env, string $var)
{
$this->env = $env;
$this->var = $var;
}

public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true): Response
{
return new Response('OK Kernel '.$this->var);
return new Response('OK Kernel (env='.$this->env.') '.$this->var);
}
}

return function (array $context) {
return new TestKernel($context['SOME_VAR']);
return new TestKernel($context['APP_ENV'], $context['SOME_VAR']);
};
2 changes: 1 addition & 1 deletion Tests/phpt/kernel.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/kernel.php';

?>
--EXPECTF--
OK Kernel foo_bar
OK Kernel (env=dev) foo_bar
18 changes: 18 additions & 0 deletions Tests/phpt/kernel_register_argc_argv.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Test HttpKernelInterface with register_argc_argv=1
--INI--
display_errors=1
register_argc_argv=1
--FILE--
<?php

// emulating PHP behavior with register_argc_argv=1
$_GET['-e_test'] = '';
$_SERVER['argc'] = 1;
$_SERVER['argv'] = [' ', '-e', 'test'];

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/kernel.php';

?>
--EXPECTF--
OK Kernel (env=dev) foo_bar

0 comments on commit 443f7d2

Please sign in to comment.