From 2408bafc499a8d71405f60ed712c1e7e94f543bb Mon Sep 17 00:00:00 2001 From: Canina Automation Date: Wed, 28 Oct 2026 20:48:21 +0000 Subject: [PATCH] fix(stage): verify existing database alias for bootstrap runtime --- scripts/stage-author-bootstrap/ci.py | 19 +++++++++++---- scripts/stage-author-bootstrap/test_ci.py | 24 +++++++++++++++++++ .../bootstrap-runtime-url.md | 15 ++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 specs/stage-author-restoration/bootstrap-runtime-url.md diff --git a/scripts/stage-author-bootstrap/ci.py b/scripts/stage-author-bootstrap/ci.py index 803c38c6..627d97f1 100644 --- a/scripts/stage-author-bootstrap/ci.py +++ b/scripts/stage-author-bootstrap/ci.py @@ -16,6 +16,18 @@ op = importlib.util.module_from_spec(spec) spec.loader.exec_module(op) +def validate_runtime_url(value, database_info): + from urllib.parse import urlparse + url = urlparse(value) + aliases = {a for n in database_info.get('NetworkSettings', {}).get('Networks', {}).values() + for a in (n.get('Aliases') or [])} + if (url.scheme not in ('postgres', 'postgresql') + or url.hostname not in ({op.STAGE, 'db_stage'} & (aliases | {op.STAGE})) + or url.port not in (None, 5432) or url.path != '/caninodb_stage' + or url.fragment or url.query not in ('', 'schema=public')): + raise ValueError('Existing Stage runtime DB identity mismatch') + + def run(env=os.environ, call=subprocess.run): sha = env.get('GITHUB_SHA', '') refs = {'refs/heads/ops/author-bootstrap-stage-rehearse': 'rehearse', @@ -47,10 +59,9 @@ def run(env=os.environ, call=subprocess.run): if info.get('Name') != '/' + op.BACKEND or not info.get('State', {}).get('Running'): raise ValueError('Existing Stage runtime unavailable') values = dict(x.split('=', 1) for x in info['Config']['Env']) - from urllib.parse import urlparse - url = urlparse(values.get('DATABASE_URL', '')) - if url.hostname != op.STAGE or url.path != '/caninodb_stage' or url.query or url.fragment: - raise ValueError('Existing Stage runtime DB identity mismatch') + database_info = json.loads(command(['docker', 'inspect', op.STAGE], capture_output=True).stdout)[0] + op.check_identity(database_info) + validate_runtime_url(values.get('DATABASE_URL', ''), database_info) if any('\n' in v or '\r' in v for v in values.values()): raise ValueError('Multiline env refused') live_env = evidence / 'stage.env' op.private_write(live_env, '\n'.join(k + '=' + v for k, v in values.items()).encode()) diff --git a/scripts/stage-author-bootstrap/test_ci.py b/scripts/stage-author-bootstrap/test_ci.py index 00be70d2..0761b350 100644 --- a/scripts/stage-author-bootstrap/test_ci.py +++ b/scripts/stage-author-bootstrap/test_ci.py @@ -30,6 +30,13 @@ class WorkflowTests(unittest.TestCase): if args[:2] == ['docker','build']: if build_fail: raise subprocess.CalledProcessError(1,args) Path(args[args.index('--iidfile')+1]).write_text('sha256:'+'b'*64) + if args[:3] == ['docker','inspect', ci.op.STAGE]: + return subprocess.CompletedProcess(args,0,stdout=json.dumps([{ + 'Name':'/'+ci.op.STAGE, 'Image':ci.op.POSTGRES_IMAGE, 'Id':'c'*64, + 'State':{'Running':True}, 'Config':{'Env':['POSTGRES_DB=caninodb_stage']}, + 'Mounts':[{'Destination':'/var/lib/postgresql/data','RW':True,'Type':'volume'}], + 'NetworkSettings':{'Networks':{'stage':{'Aliases':['canino_db_stage','db_stage']}}} + }]).encode()) if args[:2] == ['docker','inspect']: return subprocess.CompletedProcess(args,0,stdout=json.dumps([{ 'Name':'/canino_backend_stage','State':{'Running':True}, @@ -51,6 +58,23 @@ class WorkflowTests(unittest.TestCase): self.assertEqual((root/'evidence/clone.env').stat().st_mode & 0o777,0o600) return calls + def test_stage_runtime_url_identity(self): + network = {'NetworkSettings': {'Networks': {'stage': {'Aliases': ['canino_db_stage', 'db_stage']}}}} + ci.validate_runtime_url('postgresql://u:p@db_stage:5432/caninodb_stage?schema=public', network) + ci.validate_runtime_url('postgresql://u:p@canino_db_stage:5432/caninodb_stage', network) + for url in ['postgresql://u:p@db_stage/caninodb_stage?'+q for q in + ('&&','schema=public&','%73chema=public','schema=%70ublic')] + [ + 'postgresql://u:p@db_prod/caninodb_stage?schema=public', + 'postgresql://u:p@db_stage/caninodb_prod?schema=public', + 'postgresql://u:p@db_stage/caninodb_stage?schema=private', + 'postgresql://u:p@db_stage/caninodb_stage?schema=public&schema=public', + 'postgresql://u:p@db_stage/caninodb_stage?sslmode=disable', + 'postgresql://u:p@db_stage:5433/caninodb_stage', + 'postgresql://u:p@db_stage/caninodb_stage#fragment']: + with self.assertRaises(ValueError): ci.validate_runtime_url(url, network) + with self.assertRaises(ValueError): + ci.validate_runtime_url('postgresql://u:p@db_stage/caninodb_stage', {}) + def test_real_ci_driver_rehearse(self): self.exercise('refs/heads/ops/author-bootstrap-stage-rehearse') def test_real_ci_driver_apply(self): self.exercise('refs/heads/ops/author-bootstrap-stage-apply') def test_missing_token_still_builds(self): diff --git a/specs/stage-author-restoration/bootstrap-runtime-url.md b/specs/stage-author-restoration/bootstrap-runtime-url.md new file mode 100644 index 00000000..dec67155 --- /dev/null +++ b/specs/stage-author-restoration/bootstrap-runtime-url.md @@ -0,0 +1,15 @@ +# Stage bootstrap runtime URL compatibility + +## Scope and evidence +Run 1346 at reviewed source 46da1c5339a9bd4dc2585c9f1912d8a034d927ce built the backend image, then failed before operation evidence or credential copies. Read-only inspection found the existing Stage DATABASE_URL host `db_stage` and literal query `schema=public`. Fixed `canino_db_stage` advertises aliases `canino_db_stage` and `db_stage` on its actual Docker network. No live DDL or business data operation occurred. + +## Acceptance and plan +Keep source/ref/review bindings, original archive recovery and apply gates unchanged. Independently inspect and validate the exact fixed Stage DB container using the existing image/database/durable-mount check before URL acceptance. Accept only fixed canonical hostname or observed `db_stage` alias, database `caninodb_stage`, default/5432 port and no fragment. Query is empty or exactly literal `schema=public`; reject duplicates, alternate schema, malformed/encoded alternatives and any other key. Preserve runtime values privately, without changing Stage configuration. + +## Tests and tasks +- RED: new runtime URL contract test failed because validator was absent. +- GREEN: 16 bootstrap tests and 17 unchanged schema-operation tests passed. +- Independent review flagged parse_qsl normalization; replaced with literal query allowlist and regressions covering all four review probes. Bootstrap 16 tests pass again; diff check clean. +- Review integrated patch, bind repository reviewed-SHA secret to new commit, then repeat dedicated rehearsal and apply only after real full clone recovery passes. + +No frontend/backend files changed, so no new graph build was needed. Existing graph dirty stamp, .specify and unrelated evidence remain untouched. Original source/release evidence is not represented as green live acceptance.