Files
fewo-stommer/tests/Support/Process/HostileProbe.php
T

67 lines
3.6 KiB
PHP

<?php
namespace Tests\Support\Process;
use App\Channels\Mail\MailboxReader;
use App\Process\Tester\EffectLedger;
use App\Process\Tester\SandboxContext;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Throwable;
/**
* Negative tests of the process tester (docs/Prozessarchitektur/Architektur/04 §7.2): runs inside the tester process
* and deliberately tries one forbidden access (environment FEWO_SANDBOX_PROBE_CASE). Only registered in the test suite
* (SandboxBootstrapper accepts a probe only with APP_ENV=testing). Records what it observed in the ledger.
*/
final class HostileProbe
{
public function handle(SandboxContext $context): void
{
$case = (string) getenv('FEWO_SANDBOX_PROBE_CASE');
$operational = (string) getenv('FEWO_SANDBOX_PROBE_FILE');
try {
match ($case) {
'db_other_connection' => DB::connection('mysql')->select('select 1'),
'db_new_connection' => (function () use ($operational): void {
config(['database.connections.evil' => ['driver' => 'sqlite', 'database' => $operational, 'prefix' => '']]);
DB::connection('evil')->insert('insert into probe (value) values (?)', ['evil']);
})(),
'db_redirected_sandbox' => (function () use ($operational): void {
config(['database.connections.sandbox.database' => $operational]);
DB::purge('sandbox');
DB::connection('sandbox')->insert('insert into probe (value) values (?)', ['evil']);
})(),
'db_operational_table' => DB::table('probe')->insert(['value' => 'evil']),
'network' => Http::get('https://example.org/'),
'webhook' => Http::withHeaders(['X-Signature' => 'x'])->post('https://n8n.example/webhook/channel-task', ['task' => 1]),
'imap' => app(MailboxReader::class)->fetchAll(),
'mail_other_mailer' => Mail::mailer('smtp')->raw('Test', fn ($message) => $message->to('someone@example.org')),
'file_outside' => Storage::build(['driver' => 'local', 'root' => base_path()])->put('sandbox-probe.txt', 'evil'),
'cache_other_store' => Cache::store('database')->put('probe', 'evil'),
'signed_link' => EffectLedger::record('probe', ['url' => URL::temporarySignedRoute('offers.show', now()->addDay(), ['booking' => 1, 'v' => 1])]),
'environment' => EffectLedger::record('probe', [
'db_password_env' => getenv('DB_PASSWORD'),
'connections' => array_keys((array) config('database.connections')),
'mailers' => array_keys((array) config('mail.mailers')),
'smtp_password' => config('mail.mailers.smtp.password'),
'app_key_differs' => config('app.key') !== getenv('FEWO_SANDBOX_PROBE_PARENT_KEY'),
'bookings' => DB::table('bookings')->count(),
'guests' => DB::table('guests')->count(),
]),
default => null,
};
EffectLedger::record('probe', ['case' => $case, 'outcome' => 'no exception']);
} catch (Throwable $exception) {
EffectLedger::record('probe', ['case' => $case, 'outcome' => class_basename($exception), 'message' => mb_substr($exception->getMessage(), 0, 200)]);
throw $exception;
}
}
}