From 767750776fe0d12442ab829689e5192555dc0945 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Tue, 24 Nov 2026 10:26:53 -0500 Subject: [PATCH 1/7] Gateway access: local-profile.yml --- .github/workflows/local-profile.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/local-profile.yml b/.github/workflows/local-profile.yml index 73d2d395..b19d8e8e 100644 --- a/.github/workflows/local-profile.yml +++ b/.github/workflows/local-profile.yml @@ -71,6 +71,7 @@ jobs: - run: php tests/local/admin-file-sync-profile.php - run: php tests/local/admin-file-writer.php timeout-minutes: 2 + - run: php tests/local/gateway-access-proposal.php - run: php -l local-bot.php supervision: if: github.event_name != 'workflow_dispatch' || !inputs.native_smoke From 2516b79c844bbebbb198af0a7c49499970595c41 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Tue, 24 Nov 2026 10:26:55 -0500 Subject: [PATCH 2/7] Gateway access: gateway-access-proposals.md --- docs/gateway-access-proposals.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 docs/gateway-access-proposals.md diff --git a/docs/gateway-access-proposals.md b/docs/gateway-access-proposals.md new file mode 100644 index 00000000..1703b446 --- /dev/null +++ b/docs/gateway-access-proposals.md @@ -0,0 +1,30 @@ +# Gateway access proposal mode (not an activation guide) + +This optional bot adapter submits the existing authenticated Verified-role +projection as one atomic approved/whitelist request. It does not grant +access, allocate a gateway revision, change bans, start a process, or send +a network command. An inbox receipt is **not** a receipt from the gateway. + +For a PortableProfile with one enabled server, use its existing +`region_group` and `follow_front_door: true` binding. When a matching +Civ13 host consumer exists, `access_file_sync.gateway_proposals: true` +selects the adapter; the existing access sync mode, target policies and +verified mapping requirements still apply. The server's canonical +approved/whitelist target paths are used for policy validation only. The +adapter does not read or write those text files or fall back to them. +The bot's legacy regional approved-copy path skips the opted-in group. + +The destination is fixed: `/front-door/access-inbox/proposal.json`. +Operators must separate this inbox from the authoritative snapshot's directory +and restrict both using OS permissions: only the authorized bot instance may +replace proposals, only the gateway host may commit revisions, and neither +should accept data from a region or a public endpoint. The request contains +exactly `group`, `approved`, and `whitelist`; it deliberately contains no +bot-controlled revision or command/target names. An explicitly allowed empty +list is represented by a newline, not a missing or empty file. + +Keep this disabled until a matching gateway host consumer and region readers +are available for the selected group. Civ13 #7102 currently supplies the +gateway authority and consumer for persistent relay groups, not this inbox +adapter or TDM gameplay enforcement. The local tests cover synthetic group +binding for both TDM and Persistence, not live authentication or admission. From c5b5548a1e86451d482371ceb79bca902b7ce160 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Tue, 24 Nov 2026 10:26:57 -0500 Subject: [PATCH 3/7] Gateway access: AccessFileSync.php --- src/Civ13/AccessFileSync.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Civ13/AccessFileSync.php b/src/Civ13/AccessFileSync.php index b0146157..4064c583 100644 --- a/src/Civ13/AccessFileSync.php +++ b/src/Civ13/AccessFileSync.php @@ -3,6 +3,7 @@ namespace Civ13; use Civ13\Server\AccessListWriter; +use Civ13\Server\GatewayAccessProposal; use React\Promise\PromiseInterface; use function React\Promise\all; use function React\Promise\resolve; @@ -19,7 +20,7 @@ final class AccessFileSync private array $capturedRecords = []; private \Closure $clock; private \Closure $writer; - private ?AccessListWriter $publisher = null; + private AccessListWriter|GatewayAccessProposal|null $publisher = null; private array $preview = ['state' => 'unavailable']; private readonly array $settings; private readonly object $server; @@ -71,7 +72,14 @@ public static function validateConfiguration(array $settings, string $guildId, a throw new \InvalidArgumentException('Explicit separate Verified policy and server SQL destination required'); } } - new AccessListWriter($settings['targets'], $settings['max_bytes']); // Constructor is pure. + // Keep existing explicit target/empty policy validation; gateway mode never opens these legacy files. + new AccessListWriter($settings['targets'], $settings['max_bytes']); + if (array_key_exists('gateway_group_config', $settings)) { + $binding = GatewayAccessProposal::binding($settings['gateway_group_config']); + if (str_replace('\\', '/', $settings['server_basedir']) !== $binding['gateway']) { + throw new \InvalidArgumentException('Access sync must follow the dedicated gateway'); + } + } } public function lastPreview(): array @@ -110,7 +118,12 @@ private function __construct( $this->guildId=$app->civ13_guild_id; $this->roleIds=$app->role_ids; $this->clock=\Closure::fromCallable($clock ?? time(...)); - if ($settings['mode']==='publish' && $writer===null) { $this->publisher=new AccessListWriter($settings['targets'],$settings['max_bytes']); } + if ($settings['mode']==='publish' && $writer===null) { + $config = $settings['gateway_group_config'] ?? null; + $this->publisher = $config === null + ? new AccessListWriter($settings['targets'], $settings['max_bytes']) + : new GatewayAccessProposal($config, GatewayAccessProposal::binding($config), $settings['targets'], $settings['max_bytes']); + } $this->writer=\Closure::fromCallable($writer ?? $this->publisher ?? static fn (): array => ['state'=>'unchanged']); $this->coordinator=new AdminSyncCoordinator(true, function (): PromiseInterface { From 497c555b9a554ddb8f265bc96c8c49224d903cf9 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Tue, 24 Nov 2026 10:26:59 -0500 Subject: [PATCH 4/7] Gateway access: Civ13.php --- src/Civ13/Civ13.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Civ13/Civ13.php b/src/Civ13/Civ13.php index 313364fb..ba809b31 100644 --- a/src/Civ13/Civ13.php +++ b/src/Civ13/Civ13.php @@ -1009,7 +1009,7 @@ private function publishRegionApprovedLists(Verifier $verifier): void $directories = []; foreach ($this->enabled_gameservers as $gameserver) { $group = $gameserver->regionGroupFile(); - if ($group === '') { + if ($group === '' || $group === ($this->options['access_file_sync']['gateway_group_config'] ?? null)) { continue; } $directories[] = rtrim(str_replace('\\', '/', $gameserver->basedir), '/'); From 377017642b650ef5530f7a8fc81c08c1973fe8b7 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Tue, 24 Nov 2026 10:27:01 -0500 Subject: [PATCH 5/7] Gateway access: PortableProfile.php --- src/Civ13/PortableProfile.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Civ13/PortableProfile.php b/src/Civ13/PortableProfile.php index 4b1becea..f5c40648 100644 --- a/src/Civ13/PortableProfile.php +++ b/src/Civ13/PortableProfile.php @@ -208,12 +208,24 @@ private static function accessFileSyncOptions(array $profile, array $options): a // An unconfigured existing profile may boot; attach refuses before any listener/I/O. if (array_keys($settings) === ['enabled']) { return ['enabled' => true]; } $keys = array_keys($settings); sort($keys); - if ($keys !== ['debounce_seconds','enabled','max_bytes','mode','refresh_timeout_seconds','targets'] + if (!in_array($keys, [['debounce_seconds','enabled','max_bytes','mode','refresh_timeout_seconds','targets'], + ['debounce_seconds','enabled','gateway_proposals','max_bytes','mode','refresh_timeout_seconds','targets']], true) || ($options['verification_read_only'] ?? null) !== true || count($profile['servers'] ?? []) !== 1) { throw new \InvalidArgumentException('Explicit bounded read-only access synchronization required'); } $key = array_key_first($profile['servers']); $server = $profile['servers'][$key]; if (($server['enabled'] ?? null) !== true) { throw new \InvalidArgumentException('Access server must be explicitly enabled'); } + $gateway = $settings['gateway_proposals'] ?? false; + if (array_key_exists('gateway_proposals', $settings) && !is_bool($settings['gateway_proposals'])) { + throw new \InvalidArgumentException('Boolean gateway proposal flag required'); + } + unset($settings['gateway_proposals']); + if ($gateway) { + if (($server['follow_front_door'] ?? null) !== true || !is_string($server['region_group'] ?? null)) { + throw new \InvalidArgumentException('Gateway proposals require a followed shard group'); + } + $settings['gateway_group_config'] = $server['region_group']; + } $settings['server_key'] = $key; $settings['server_basedir'] = $server['basedir']; AccessFileSync::validateConfiguration($settings, $options['civ13_guild_id'] ?? '', $options['role_ids'] ?? [], $options['admin_file_sync']['enabled'] ?? false); return $settings; From a2bd60e4dc68dc91046ff66eeffde08d0f74cc42 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Tue, 24 Nov 2026 10:27:03 -0500 Subject: [PATCH 6/7] Gateway access: GatewayAccessProposal.php --- src/Civ13/Server/GatewayAccessProposal.php | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/Civ13/Server/GatewayAccessProposal.php diff --git a/src/Civ13/Server/GatewayAccessProposal.php b/src/Civ13/Server/GatewayAccessProposal.php new file mode 100644 index 00000000..c5c06d65 --- /dev/null +++ b/src/Civ13/Server/GatewayAccessProposal.php @@ -0,0 +1,99 @@ + 'not_attempted']; + + public function __construct(private readonly string $config, private readonly array $binding, private readonly array $targets, private readonly int $maxBytes) {} + + public static function binding(string $config): array + { + if (!PortableProfile::absoluteAdminFile($config) || is_link($config) || !is_file($config)) { + throw new \InvalidArgumentException('Explicit local gateway group configuration required'); + } + $bytes = @file_get_contents($config, false, null, 0, 1_048_577); + $group = is_string($bytes) && strlen($bytes) <= 1_048_576 + ? json_decode(preg_replace('/^\xEF\xBB\xBF/', '', $bytes), true, 32) : null; + if (!is_array($group) || ($group['gateway_verified_access'] ?? null) !== true + || !is_string($group['group'] ?? null) || !preg_match('/^[a-z0-9_-]{1,32}$/D', $group['group']) + || !PortableProfile::absoluteAdminFile($group['root'] ?? null) + || !is_int($group['relay_port'] ?? null) || $group['relay_port'] < 1 || $group['relay_port'] > 65535 + || !is_string($group['gate'] ?? null) || !preg_match('/^[a-z0-9_-]{1,32}$/D', $group['gate']) + || !is_array($group['regions'] ?? null)) { + throw new \InvalidArgumentException('Gateway verified access must be enabled in the bound shard group'); + } + $gates = array_values(array_filter($group['regions'], static fn ($region): bool => is_array($region) && ($region['id'] ?? null) === $group['gate'])); + if (count($gates) !== 1 || ($gates[0]['kind'] ?? null) !== 'GATEWAY') { + throw new \InvalidArgumentException('Dedicated gateway required'); + } + $root = rtrim(str_replace('\\', '/', $group['root']), '/'); + $directory = $root.'/front-door/access-inbox'; + clearstatcache(true); + $resolved = realpath($directory); + if (!is_dir($directory) || $resolved === false || !self::samePath($directory, $resolved) + || is_link($directory) || is_link(dirname($directory))) { + throw new \InvalidArgumentException('Provision a protected local gateway access inbox first'); + } + return ['group' => $group['group'], 'directory' => $directory, 'gateway' => $root.'/'.$group['gate']]; + } + + private static function samePath(string $a, string $b): bool + { + $a = str_replace('\\', '/', $a); $b = str_replace('\\', '/', $b); + return PHP_OS_FAMILY === 'Windows' ? strcasecmp($a, $b) === 0 : $a === $b; + } + + public function lastReceipt(): array { return $this->receipt; } + + public function __invoke(string $wire, int $generation, callable $canCommit): array + { + $stage = null; + $stream = null; + try { + $data = AccessListProjection::decode($wire); + if ($data['unmapped_role_holders'] !== 0) { return $this->receipt = ['state' => 'blocked', 'reason' => 'unmapped-role-holders']; } + if ($generation < 0 || $canCommit() !== true) { return $this->receipt = ['state' => 'superseded']; } + foreach ($data['outputs'] as $kind => $content) { + if (strlen($content) > $this->maxBytes || ($content === "\n" && !$this->targets[$kind]['allow_empty'])) { + return $this->receipt = ['state' => 'blocked', 'reason' => 'output-policy']; + } + } + $payload = json_encode(['group' => $this->binding['group']] + $data['outputs'], JSON_THROW_ON_ERROR)."\n"; + if (strlen($payload) > self::MAX_WIRE || self::binding($this->config) !== $this->binding) { + return $this->receipt = ['state' => 'blocked', 'reason' => 'gateway-binding-or-budget']; + } + $dir = $this->binding['directory']; + $path = $dir.'/proposal.json'; + $candidate = $dir.'/.proposal-'.bin2hex(random_bytes(16)).'.tmp'; + $stream = @fopen($candidate, 'x+b'); + if ($stream === false) { throw new \RuntimeException('stage-open'); } + $stage = $candidate; + if (fwrite($stream, $payload) !== strlen($payload) || !fflush($stream) || !fsync($stream) || !rewind($stream) + || stream_get_contents($stream, self::MAX_WIRE + 1) !== $payload) { throw new \RuntimeException('stage-verify'); } + if (!fclose($stream)) { throw new \RuntimeException('stage-close'); } + $stream = null; + clearstatcache(true, $path); + if (self::binding($this->config) !== $this->binding || is_link($path) || (file_exists($path) && !is_file($path))) { + return $this->receipt = ['state' => 'blocked', 'reason' => 'gateway-destination-changed']; + } + if ($canCommit() !== true) { return $this->receipt = ['state' => 'superseded']; } + if (!@rename($stage, $path)) { throw new \RuntimeException('replace-refused'); } + $stage = null; + $this->receipt = ['state' => 'submitted']; + return ['state' => 'updated', 'stage' => 'gateway-inbox']; + } catch (\Throwable) { + return $this->receipt = ['state' => 'blocked', 'reason' => 'gateway-proposal-refused']; + } finally { + if (is_resource($stream)) { fclose($stream); } + if ($stage !== null) { @unlink($stage); } + } + } +} From ed25050c9129590c560bba4b46062278f93e07ff Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Tue, 24 Nov 2026 10:27:05 -0500 Subject: [PATCH 7/7] Gateway access: gateway-access-proposal.php --- tests/local/gateway-access-proposal.php | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/local/gateway-access-proposal.php diff --git a/tests/local/gateway-access-proposal.php b/tests/local/gateway-access-proposal.php new file mode 100644 index 00000000..5c0b1e30 --- /dev/null +++ b/tests/local/gateway-access-proposal.php @@ -0,0 +1,54 @@ +['BARRACKS','BATTLE'], 'earth'=>['MAINLAND','CONTINENT']] as $group=>$kinds) { + $dir = "$root/$group/front-door/access-inbox"; + mkdir($dir,0700,true); + $config = [ + 'group'=>$group, 'root'=>"$root/$group", 'relay_port'=>$group==='tdm' ? 2714 : 3714, + 'gate'=>'gateway', 'gateway_verified_access'=>true, + 'regions'=>[['id'=>'gateway','kind'=>'GATEWAY'],['id'=>'first','kind'=>$kinds[0]],['id'=>'second','kind'=>$kinds[1]]], + ]; + $file = "$root/$group.json"; + file_put_contents($file,json_encode($config,JSON_THROW_ON_ERROR)); + $binding = GatewayAccessProposal::binding($file); + $writer = new GatewayAccessProposal($file,$binding,['approved'=>['allow_empty'=>true],'whitelist'=>['allow_empty'=>true]],4096); + $empty = "{\"outputs\":{\"approved\":\"\\n\",\"whitelist\":\"\\n\"},\"unmapped_role_holders\":0}"; + check(($writer($empty,1,static fn()=>true)['state'] ?? null)==='updated', 'complete pair submitted'); + $before = file_get_contents($dir.'/proposal.json'); + check(json_decode($before,true,32,JSON_THROW_ON_ERROR)===['group'=>$group,'approved'=>"\n",'whitelist'=>"\n"], 'exact pair and scope, no version'); + $calls=0; + check(($writer($empty,2,static function () use (&$calls): bool { return ++$calls===1; })['state'] ?? null)==='superseded', 'late generation fence'); + check(file_get_contents($dir.'/proposal.json')===$before && glob($dir.'/.proposal-*')===[], 'late refusal preserves prior pair and deletes stage'); + check(($writer(str_replace('":0','":1',$empty),3,static fn()=>true)['state'] ?? null)==='blocked', 'unmapped role holder refused'); + $wrong = $config; + $wrong['group'] = $group === 'tdm' ? 'earth' : 'tdm'; + // A writer bound earlier does not retarget to another group during a commit. + file_put_contents($file,json_encode($wrong,JSON_THROW_ON_ERROR)); + check(($writer($empty,4,static fn()=>true)['state'] ?? null)==='blocked', 'wrong group refused'); + check(file_get_contents($dir.'/proposal.json')===$before,'prior scoped pair preserved'); + $legacy = ["$root/$group/gateway/SQL/approved.txt","$root/$group/first/SQL/approved.txt"]; + foreach ($legacy as $path) { check(!file_exists($path), 'no direct list write'); } + } + echo "Gateway proposals: synthetic TDM and Persistence group checks passed\n"; +} finally { deleteTree($root); }