Skip to content

Don't inherit env otherwise we leak all secrets #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/ExerciseRunner/CgiRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ private function executePhpFile(string $fileName, RequestInterface $request, str
*/
private function getProcess(string $fileName, RequestInterface $request): Process
{
$env = [
$env = $this->getDefaultEnv();
$env += [
'REQUEST_METHOD' => $request->getMethod(),
'SCRIPT_FILENAME' => $fileName,
'REDIRECT_STATUS' => 302,
Expand All @@ -224,6 +225,20 @@ private function getProcess(string $fileName, RequestInterface $request): Proces
return Process::fromShellCommandline($cmd, null, $env, null, 10);
}

/**
* We need to reset env entirely, because Symfony inherits it. We do that by setting all
* the current env vars to false
*
* @return array<string, false>
*/
private function getDefaultEnv(): array
{
$env = array_map(fn () => false, $_ENV);
$env + array_map(fn () => false, $_SERVER);

return $env;
}

/**
* Verifies a solution by invoking PHP via the `php-cgi` binary, populating all the super globals with
* the information from the request objects returned from the exercise. The exercise can return multiple
Expand Down
17 changes: 16 additions & 1 deletion src/ExerciseRunner/CliRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,27 @@ private function getPhpProcess(string $fileName, ArrayObject $args): Process
return new Process(
$args->prepend($fileName)->prepend($this->phpLocation)->getArrayCopy(),
dirname($fileName),
['XDEBUG_MODE' => 'off'],
$this->getDefaultEnv() + ['XDEBUG_MODE' => 'off'],
null,
10
);
}

/**
* We need to reset env entirely, because Symfony inherits it. We do that by setting all
* the current env vars to false
*
* @return array<string, false>
*/
private function getDefaultEnv(): array
{
$env = array_map(fn () => false, $_ENV);
$env + array_map(fn () => false, $_SERVER);

return $env;
}


/**
* Verifies a solution by invoking PHP from the CLI passing the arguments gathered from the exercise
* as command line arguments to PHP.
Expand Down