Skip to content

Commit 43b6783

Browse files
authored
Merge pull request #271 from php-school/dont-inherit-env
Don't inherit env otherwise we leak all secrets
2 parents 48c532d + 1b02243 commit 43b6783

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/ExerciseRunner/CgiRunner.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ private function executePhpFile(string $fileName, RequestInterface $request, str
198198
*/
199199
private function getProcess(string $fileName, RequestInterface $request): Process
200200
{
201-
$env = [
201+
$env = $this->getDefaultEnv();
202+
$env += [
202203
'REQUEST_METHOD' => $request->getMethod(),
203204
'SCRIPT_FILENAME' => $fileName,
204205
'REDIRECT_STATUS' => 302,
@@ -224,6 +225,20 @@ private function getProcess(string $fileName, RequestInterface $request): Proces
224225
return Process::fromShellCommandline($cmd, null, $env, null, 10);
225226
}
226227

228+
/**
229+
* We need to reset env entirely, because Symfony inherits it. We do that by setting all
230+
* the current env vars to false
231+
*
232+
* @return array<string, false>
233+
*/
234+
private function getDefaultEnv(): array
235+
{
236+
$env = array_map(fn () => false, $_ENV);
237+
$env + array_map(fn () => false, $_SERVER);
238+
239+
return $env;
240+
}
241+
227242
/**
228243
* Verifies a solution by invoking PHP via the `php-cgi` binary, populating all the super globals with
229244
* the information from the request objects returned from the exercise. The exercise can return multiple

src/ExerciseRunner/CliRunner.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,27 @@ private function getPhpProcess(string $fileName, ArrayObject $args): Process
137137
return new Process(
138138
$args->prepend($fileName)->prepend($this->phpLocation)->getArrayCopy(),
139139
dirname($fileName),
140-
['XDEBUG_MODE' => 'off'],
140+
$this->getDefaultEnv() + ['XDEBUG_MODE' => 'off'],
141141
null,
142142
10
143143
);
144144
}
145145

146+
/**
147+
* We need to reset env entirely, because Symfony inherits it. We do that by setting all
148+
* the current env vars to false
149+
*
150+
* @return array<string, false>
151+
*/
152+
private function getDefaultEnv(): array
153+
{
154+
$env = array_map(fn () => false, $_ENV);
155+
$env + array_map(fn () => false, $_SERVER);
156+
157+
return $env;
158+
}
159+
160+
146161
/**
147162
* Verifies a solution by invoking PHP from the CLI passing the arguments gathered from the exercise
148163
* as command line arguments to PHP.

0 commit comments

Comments
 (0)