Skip to content

Commit 5cae87a

Browse files
committed
Security review and code audit fixes
1 parent 1610958 commit 5cae87a

22 files changed

Lines changed: 868 additions & 92 deletions

‎public/assets/sandbox-ui.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ function bindAjax() {
5050
.slice(0, 500);
5151
throw new Error(detail || 'QuickInstall returned an invalid response. Check the UI error log.');
5252
}
53-
dashboard.innerHTML = data.html;
54-
bindAjax();
53+
if (!data || typeof data !== 'object') {
54+
throw new Error('QuickInstall returned an invalid response object.');
55+
}
56+
if (typeof data.html === 'string' && data.html !== '') {
57+
dashboard.innerHTML = data.html;
58+
bindAjax();
59+
}
5560
showActionResult(data, context);
5661
scrollLog();
5762
} catch (error) {

‎src/QuickInstall/Sandbox/Application.php‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use InvalidArgumentException;
1414
use RuntimeException;
15+
use Throwable;
1516

1617
class Application
1718
{
@@ -36,8 +37,13 @@ public function run(array $argv): int
3637
return 0;
3738
}
3839

40+
$operationLock = null;
3941
try
4042
{
43+
if ($this->mutatesWorkspace($command))
44+
{
45+
$operationLock = $this->project->lockOperations();
46+
}
4147
switch ($command)
4248
{
4349
case 'help':
@@ -128,10 +134,27 @@ public function run(array $argv): int
128134
}
129135
finally
130136
{
131-
$this->printUpdateNotice($command, $argv);
137+
$this->project->unlockOperations($operationLock);
138+
try
139+
{
140+
$this->printUpdateNotice($command, $argv);
141+
}
142+
catch (Throwable $e)
143+
{
144+
// Update notifications must never change command success or failure.
145+
}
132146
}
133147
}
134148

149+
private function mutatesWorkspace(string $command): bool
150+
{
151+
return in_array($command, [
152+
'init', 'source:fetch', 'source:remove', 'source:prune',
153+
'board:create', 'board:start', 'board:stop', 'board:destroy', 'board:seed',
154+
'ext:mount', 'ext:unmount', 'style:mount', 'style:unmount',
155+
], true);
156+
}
157+
135158
private function init(): int
136159
{
137160
$created = $this->project->init();

‎src/QuickInstall/Sandbox/BoardRunner.php‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,35 @@ public function stop(string $name): void
5454
public function destroy(string $name): void
5555
{
5656
$this->project->board($name);
57-
$compose = $this->project->composePath($name);
58-
if (file_exists($compose))
59-
{
60-
$this->run(['docker', 'compose', '-f', $compose, 'down', '--volumes', '--remove-orphans', '--rmi', 'local']);
61-
}
57+
$this->removeContainers($name, true);
6258

6359
$this->project->deleteTree($this->project->boardPath($name));
6460
$this->project->deleteTree($this->project->runtimePath($name));
6561
$this->project->deleteTree($this->project->dbPath($name));
6662
$this->project->removeBoard($name);
6763
}
6864

65+
public function prepareReplacement(string $name): void
66+
{
67+
$this->project->board($name);
68+
$this->removeContainers($name, false);
69+
}
70+
71+
private function removeContainers(string $name, bool $removeImage): void
72+
{
73+
$compose = $this->project->composePath($name);
74+
if (file_exists($compose))
75+
{
76+
$command = ['docker', 'compose', '-f', $compose, 'down', '--volumes', '--remove-orphans'];
77+
if ($removeImage)
78+
{
79+
$command[] = '--rmi';
80+
$command[] = 'local';
81+
}
82+
$this->run($command);
83+
}
84+
}
85+
6986
public function status(string $name): string
7087
{
7188
$compose = $this->project->composePath($name);
@@ -193,7 +210,7 @@ protected function enableConfigPhpDebug(string $boardPath, bool $displayLoadTime
193210
$contents = $this->setConfigDefine($contents, 'PHPBB_DISPLAY_LOAD_TIME', "@define('PHPBB_DISPLAY_LOAD_TIME', true);");
194211
}
195212

196-
file_put_contents($configPath, $contents);
213+
$this->writeFile($configPath, $contents, 'board config');
197214
}
198215

199216
protected function enableYamlDebug(string $boardPath): void
@@ -241,7 +258,7 @@ protected function enableYamlDebug(string $boardPath): void
241258
$contents = rtrim($contents) . "\n\nparameters:\n" . implode("\n", $lines) . "\n";
242259
}
243260

244-
file_put_contents($configPath, $contents);
261+
$this->writeFile($configPath, $contents, 'board debug config');
245262
}
246263

247264
protected function setConfigDefine(string $contents, string $name, string $line): string
@@ -364,15 +381,24 @@ protected function seedMarker(string $name, string $preset): string
364381
protected function deleteSeedMarker(string $name, string $preset): void
365382
{
366383
$marker = $this->seedMarker($name, $preset);
367-
if (file_exists($marker))
384+
if (file_exists($marker) && !unlink($marker))
368385
{
369-
unlink($marker);
386+
throw new RuntimeException("Unable to delete seed marker: $marker");
370387
}
371388
}
372389

373390
protected function writeSeedMarker(string $name, string $preset): void
374391
{
375-
file_put_contents($this->seedMarker($name, $preset), gmdate('c') . "\n");
392+
$marker = $this->seedMarker($name, $preset);
393+
$this->writeFile($marker, gmdate('c') . "\n", 'seed marker');
394+
}
395+
396+
private function writeFile(string $path, string $contents, string $label): void
397+
{
398+
if (file_put_contents($path, $contents, LOCK_EX) !== strlen($contents))
399+
{
400+
throw new RuntimeException("Unable to write $label: $path");
401+
}
376402
}
377403

378404
protected function run(array $command): void

‎src/QuickInstall/Sandbox/BoardService.php‎

Lines changed: 120 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use InvalidArgumentException;
1414
use RuntimeException;
15+
use Throwable;
1516

1617
class BoardService
1718
{
@@ -27,27 +28,40 @@ public function __construct(Project $project, ?Output $output = null)
2728
public function create(string $name, string $version = 'latest', string $db = 'mariadb', int $port = 8080, string $populate = 'none', bool $debug = false, bool $replace = false): array
2829
{
2930
$this->project->init();
31+
$this->project->assertName($name, 'board');
3032
$selection = $this->resolveSourceSelection($version);
3133
$boards = $this->project->boards();
32-
if (isset($boards[$name]))
34+
$existingName = null;
35+
foreach (array_keys($boards) as $registeredName)
3336
{
37+
if ($this->project->namesEqual((string) $registeredName, $name))
38+
{
39+
$existingName = (string) $registeredName;
40+
break;
41+
}
42+
}
43+
if ($existingName !== null)
44+
{
45+
if ($existingName !== $name)
46+
{
47+
throw new InvalidArgumentException("Board already exists: $existingName. Board names are case-insensitive.");
48+
}
3449
if (!$replace)
3550
{
3651
throw new InvalidArgumentException("Board already exists: $name. Use board:destroy first, or pass --replace to recreate it.");
3752
}
38-
39-
$this->createBoardRunner()->destroy($name);
4053
}
4154

4255
foreach ($this->project->boards() as $board)
4356
{
44-
if (($board['name'] ?? '') !== $name && (int) ($board['port'] ?? 0) === $port)
57+
if (!$this->project->namesEqual((string) ($board['name'] ?? ''), $name) && (int) ($board['port'] ?? 0) === $port)
4558
{
4659
throw new InvalidArgumentException("Port $port is already used by board: {$board['name']}");
4760
}
4861
}
4962

50-
if ($this->isPortInUse($port))
63+
$reusingOwnPort = $existingName !== null && (int) ($boards[$existingName]['port'] ?? 0) === $port;
64+
if (!$reusingOwnPort && $this->isPortInUse($port))
5165
{
5266
throw new InvalidArgumentException("Port $port is already in use on this host.");
5367
}
@@ -59,12 +73,6 @@ public function create(string $name, string $version = 'latest', string $db = 'm
5973
throw new RuntimeException("Unable to determine PHP runtime for source: {$source['source_key']}");
6074
}
6175

62-
$boardDir = $this->project->boardPath($name);
63-
if (!is_dir($boardDir) && !mkdir($boardDir, 0775, true) && !is_dir($boardDir))
64-
{
65-
throw new RuntimeException("Unable to create board directory: $boardDir");
66-
}
67-
6876
$config = [
6977
'phpbb' => $source['version'],
7078
'phpbb_source' => $source['source_key'],
@@ -80,30 +88,113 @@ public function create(string $name, string $version = 'latest', string $db = 'm
8088
'extensions' => [],
8189
'styles' => [],
8290
];
91+
$backups = [];
92+
if ($existingName !== null)
93+
{
94+
$this->createBoardRunner()->prepareReplacement($name);
95+
$backups = $this->backupBoardState($name);
96+
}
8397

84-
$paths = $this->createDockerComposeWriter()->write($name, $config);
85-
$board = [
86-
'name' => $name,
87-
'phpbb' => $source['version'],
88-
'phpbb_source' => $source['source_key'],
89-
'phpbb_branch' => $source['phpbb_branch'],
90-
'php' => $php,
91-
'db' => $db,
92-
'port' => $port,
93-
'url' => "http://localhost:$port/",
94-
'path' => $boardDir,
95-
'populate' => $populate,
96-
'debug' => $debug,
97-
'extensions' => [],
98-
'styles' => [],
99-
'created_at' => gmdate('c'),
100-
];
98+
$boardDir = $this->project->boardPath($name);
99+
try
100+
{
101+
if (!is_dir($boardDir) && !mkdir($boardDir, 0775, true) && !is_dir($boardDir))
102+
{
103+
throw new RuntimeException("Unable to create board directory: $boardDir");
104+
}
105+
106+
$paths = $this->createDockerComposeWriter()->write($name, $config);
107+
$board = [
108+
'name' => $name,
109+
'phpbb' => $source['version'],
110+
'phpbb_source' => $source['source_key'],
111+
'phpbb_branch' => $source['phpbb_branch'],
112+
'php' => $php,
113+
'db' => $db,
114+
'port' => $port,
115+
'url' => "http://localhost:$port/",
116+
'path' => $boardDir,
117+
'populate' => $populate,
118+
'debug' => $debug,
119+
'extensions' => [],
120+
'styles' => [],
121+
'created_at' => gmdate('c'),
122+
];
123+
124+
$this->project->appendBoard($board);
125+
}
126+
catch (Throwable $e)
127+
{
128+
$this->restoreBoardState($backups);
129+
throw $e;
130+
}
101131

102-
$this->project->appendBoard($board);
132+
$this->discardBoardBackups($backups);
103133

104134
return ['board' => $board, 'paths' => $paths];
105135
}
106136

137+
private function backupBoardState(string $name): array
138+
{
139+
$token = str_replace('.', '', uniqid('replace-', true));
140+
$backups = [];
141+
foreach ([$this->project->boardPath($name), $this->project->runtimePath($name), $this->project->dbPath($name)] as $path)
142+
{
143+
$backups[$path] = null;
144+
if (!file_exists($path) && !is_link($path))
145+
{
146+
continue;
147+
}
148+
149+
$backup = $path . '.' . $token;
150+
if (!@rename($path, $backup))
151+
{
152+
$this->restoreBoardState(array_filter($backups, 'is_string'));
153+
throw new RuntimeException("Unable to preserve existing board during replacement: $path");
154+
}
155+
$backups[$path] = $backup;
156+
}
157+
158+
return $backups;
159+
}
160+
161+
private function restoreBoardState(array $backups): void
162+
{
163+
foreach ($backups as $path => $backup)
164+
{
165+
if (file_exists($path) || is_link($path))
166+
{
167+
$this->project->deleteTree($path);
168+
}
169+
if (is_string($backup) && (file_exists($backup) || is_link($backup)) && !@rename($backup, $path))
170+
{
171+
throw new RuntimeException("Unable to restore board after failed replacement: $path");
172+
}
173+
}
174+
}
175+
176+
private function discardBoardBackups(array $backups): void
177+
{
178+
foreach ($backups as $backup)
179+
{
180+
if (!is_string($backup))
181+
{
182+
continue;
183+
}
184+
try
185+
{
186+
$this->project->deleteTree($backup);
187+
}
188+
catch (RuntimeException $e)
189+
{
190+
if ($this->output !== null)
191+
{
192+
$this->output->error("Warning: unable to remove replacement backup $backup: {$e->getMessage()}\n");
193+
}
194+
}
195+
}
196+
}
197+
107198
private function resolveSourceSelection(string $version): array
108199
{
109200
try

‎src/QuickInstall/Sandbox/DockerComposeWriter.php‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public function write(string $name, array $config): array
3434
$dockerfile = $runtimeDir . '/Dockerfile';
3535
$entrypoint = $runtimeDir . '/entrypoint.sh';
3636

37-
file_put_contents($installConfig, $this->installConfig($name, $config));
38-
file_put_contents($compose, $this->compose($name, $config));
39-
file_put_contents($dockerfile, $this->dockerfile($config));
40-
file_put_contents($entrypoint, $this->entrypoint());
41-
if (PHP_OS_FAMILY !== 'Windows')
37+
$this->writeFile($installConfig, $this->installConfig($name, $config));
38+
$this->writeFile($compose, $this->compose($name, $config));
39+
$this->writeFile($dockerfile, $this->dockerfile($config));
40+
$this->writeFile($entrypoint, $this->entrypoint());
41+
if ((PHP_OS_FAMILY !== 'Windows') && !chmod($entrypoint, 0755))
4242
{
43-
chmod($entrypoint, 0755);
43+
throw new RuntimeException("Unable to make entrypoint executable: $entrypoint");
4444
}
4545

4646
return [
@@ -51,6 +51,15 @@ public function write(string $name, array $config): array
5151
];
5252
}
5353

54+
private function writeFile(string $path, string $contents): void
55+
{
56+
$contents = str_replace(["\r\n", "\r"], "\n", $contents);
57+
if (file_put_contents($path, $contents, LOCK_EX) !== strlen($contents))
58+
{
59+
throw new RuntimeException("Unable to write runtime file: $path");
60+
}
61+
}
62+
5463
private function installConfig(string $name, array $config): string
5564
{
5665
$dbms = $this->dbms($config['db']);

0 commit comments

Comments
 (0)