-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluator.php
More file actions
174 lines (147 loc) · 5.17 KB
/
Copy pathEvaluator.php
File metadata and controls
174 lines (147 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
/*
* This file is part of the univeros/framework
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Altair\Eval;
use Altair\Eval\Runner\SecurityProfile;
use Altair\Eval\Runner\SubprocessResult;
use Altair\Eval\Runner\SubprocessRunner;
use Altair\Eval\Runner\WrapperBuilder;
/**
* The top-level eval orchestrator.
*
* For one request it: prepares a one-shot temp directory inside the project's
* `.altair/eval/` tree, generates the wrapper PHP and stages an empty result
* file, builds the `php -d` guard flags + env from a {@see SecurityProfile},
* spawns the subprocess under a wall-clock budget, reads the structured result
* the wrapper wrote, and folds everything into an {@see EvalResult}. Cleanup
* runs even if the subprocess died — the temp tree never leaks.
*
* The Evaluator is stateless; one instance can serve many requests
* concurrently provided each request has its own project root.
*/
final readonly class Evaluator
{
public function __construct(
private SubprocessRunner $runner = new SubprocessRunner(),
private WrapperBuilder $builder = new WrapperBuilder(),
private string $phpBinary = 'php',
) {}
public function evaluate(EvalRequest $request): EvalResult
{
$temp = $this->prepareTempDir($request->projectRoot);
$wrapper = $temp . '/wrapper.php';
$resultFile = $temp . '/result.json';
$snippetFile = $temp . '/snippet.php';
try {
file_put_contents($snippetFile, "<?php\n" . $request->snippet);
file_put_contents($wrapper, $this->builder->build($request, $resultFile, $snippetFile));
touch($resultFile);
$profile = new SecurityProfile($request);
$allowedBase = realpath($request->projectRoot);
$command = [
$this->phpBinary,
...$profile->phpFlags(\is_string($allowedBase) ? $allowedBase : $request->projectRoot),
$wrapper,
];
$sub = $this->runner->run(
$command,
$request->projectRoot,
$this->mergeEnv($profile->envVars()),
$request->timeoutMs,
);
return $this->buildResult($this->readPayload($resultFile), $sub);
} finally {
$this->cleanup($temp);
}
}
private function prepareTempDir(string $projectRoot): string
{
$base = $projectRoot . '/.altair/eval';
if (!is_dir($base)) {
mkdir($base, 0o700, true);
}
$dir = $base . '/' . uniqid('', true);
mkdir($dir, 0o700, true);
return $dir;
}
/**
* Inherit the parent's environment (PATH, locale, DB_*, etc.) so the
* snippet's container resolves the host's real services, then layer the
* eval-specific guards on top.
*
* @param array<string, string> $extra
*
* @return array<string, string>
*/
private function mergeEnv(array $extra): array
{
return [...getenv(), ...$extra];
}
/**
* @return array<string, mixed>|null
*/
private function readPayload(string $resultFile): ?array
{
if (!is_file($resultFile)) {
return null;
}
$contents = (string) file_get_contents($resultFile);
if ($contents === '') {
return null;
}
$decoded = json_decode($contents, true);
return \is_array($decoded) ? $decoded : null;
}
/**
* @param array<string, mixed>|null $payload
*/
private function buildResult(?array $payload, SubprocessResult $sub): EvalResult
{
$result = isset($payload['result']) && \is_array($payload['result']) ? $payload['result'] : null;
$exception = isset($payload['exception']) && \is_array($payload['exception']) ? $payload['exception'] : null;
$stdout = isset($payload['stdout']) && \is_string($payload['stdout']) ? $payload['stdout'] : $sub->stdout;
$duration = isset($payload['duration_ms']) && \is_int($payload['duration_ms']) ? $payload['duration_ms'] : $sub->durationMs;
$memoryPeak = isset($payload['memory_peak_bytes']) && \is_int($payload['memory_peak_bytes']) ? $payload['memory_peak_bytes'] : 0;
return new EvalResult(
$result,
$stdout,
$sub->stderr,
$exception,
$duration,
$memoryPeak,
$sub->exitCode,
$sub->timedOut,
);
}
private function cleanup(string $dir): void
{
if (!is_dir($dir)) {
return;
}
$entries = scandir($dir);
if ($entries === false) {
@rmdir($dir);
return;
}
foreach ($entries as $entry) {
if ($entry === '.') {
continue;
}
if ($entry === '..') {
continue;
}
$path = $dir . '/' . $entry;
if (is_dir($path) && !is_link($path)) {
$this->cleanup($path);
} else {
@unlink($path);
}
}
@rmdir($dir);
}
}