From eda077d646c35eaa6a4e6b9facf08120d66b894b Mon Sep 17 00:00:00 2001 From: Pariot <278551738+arcwiser@users.noreply.github.com> Date: Mon, 2 Nov 2026 14:43:13 +1100 Subject: [PATCH 1/3] fix(portal): enforce live build entitlements --- routes/admin.py | 57 ++++++++++++++++++++++----- tests/test_admin_security.py | 74 ++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 9 deletions(-) diff --git a/routes/admin.py b/routes/admin.py index 89f8c6f..b560ba8 100644 --- a/routes/admin.py +++ b/routes/admin.py @@ -4968,14 +4968,47 @@ async def review_reset_request(request_id: str, decision: str, user=Depends(requ # ─── End-User Portal Files ────────────────────────────────────────────────── +_PORTAL_FILE_ACCESS_SQL = """ + f.app_id = ? AND f.portal_visible = 1 + AND f.is_active = 1 AND f.is_archived = 0 AND f.is_revoked = 0 + AND (f.available_from IS NULL OR f.available_from <= CURRENT_TIMESTAMP) + AND (f.available_until IS NULL OR f.available_until > CURRENT_TIMESTAMP) + AND ( + ( + f.product_id IS NULL + AND NOT EXISTS ( + SELECT 1 FROM app_file_products unrestricted_afp + WHERE unrestricted_afp.file_id = f.id + ) + ) + OR EXISTS ( + SELECT 1 + FROM license_products portal_lp + JOIN products portal_p ON portal_p.id = portal_lp.product_id + WHERE portal_lp.license_id = ? + AND portal_p.app_id = f.app_id + AND portal_p.is_active = 1 AND portal_p.is_paused = 0 + AND portal_lp.is_paused = 0 + AND (portal_lp.expires_at IS NULL OR portal_lp.expires_at > CURRENT_TIMESTAMP) + AND ( + portal_lp.product_id = f.product_id + OR EXISTS ( + SELECT 1 FROM app_file_products allowed_afp + WHERE allowed_afp.file_id = f.id + AND allowed_afp.product_id = portal_lp.product_id + ) + ) + ) + ) +""" + + @router.get("/portal/files") async def portal_list_files(lic=Depends(require_portal_user), db: aiosqlite.Connection = Depends(get_db)): - sql = """SELECT id, name, created_at, product_id - FROM app_files - WHERE app_id = ? AND portal_visible = 1 - AND (product_id IS NULL OR product_id IN - (SELECT product_id FROM license_products WHERE license_id=?)) - ORDER BY created_at DESC""" + sql = f"""SELECT f.id, f.name, f.created_at, f.product_id + FROM app_files f + WHERE {_PORTAL_FILE_ACCESS_SQL} + ORDER BY f.created_at DESC""" async with db.execute(sql, (lic["app_id"], lic["id"])) as cur: return rows_to_list(await cur.fetchall()) @@ -4983,9 +5016,9 @@ async def portal_list_files(lic=Depends(require_portal_user), db: aiosqlite.Conn @router.get("/portal/files/{file_id}/download") async def portal_download_file(file_id: str, lic=Depends(require_portal_user), db: aiosqlite.Connection = Depends(get_db)): async with db.execute( - """SELECT name, content FROM app_files WHERE id = ? AND app_id = ? AND portal_visible=1 - AND (product_id IS NULL OR product_id IN - (SELECT product_id FROM license_products WHERE license_id=?))""", + f"""SELECT f.id, f.name, f.content + FROM app_files f + WHERE f.id = ? AND {_PORTAL_FILE_ACCESS_SQL}""", (file_id, lic["app_id"], lic["id"]), ) as cur: row = await cur.fetchone() @@ -4996,6 +5029,12 @@ async def portal_download_file(file_id: str, lic=Depends(require_portal_user), d content = row["content"] filename = row["name"] + await db.execute( + "INSERT INTO file_download_events(file_id,license_id,source) VALUES(?,?,?)", + (row["id"], lic["id"], "portal"), + ) + await db.commit() + # Security: sanitize filename to prevent HTTP header injection via # Content-Disposition. Strip any characters outside safe ASCII printable # range, then use RFC 5987 percent-encoding for the filename parameter. diff --git a/tests/test_admin_security.py b/tests/test_admin_security.py index 759b905..929c354 100644 --- a/tests/test_admin_security.py +++ b/tests/test_admin_security.py @@ -364,6 +364,80 @@ async def test_portal_downloads_are_opt_in_and_product_scoped(self): await db.commit() response = await self.admin.portal_download_file("f-1", lic=lic, db=db) self.assertEqual(response.headers["content-disposition"].split(";")[0], 'attachment') + async with db.execute( + "SELECT source FROM file_download_events WHERE file_id='f-1' AND license_id='l-1'" + ) as cur: + self.assertEqual((await cur.fetchone())["source"], "portal") + + async def test_portal_files_require_live_entitlements_and_live_releases(self): + async with aiosqlite.connect(self.db_path) as db: + db.row_factory = aiosqlite.Row + await db.execute( + "INSERT INTO applications(id,name,secret_key) VALUES(?,?,?)", + ("app-portal", "Portal App", "s" * 64), + ) + for product_id in ("product-owned", "product-restricted"): + await db.execute( + "INSERT INTO products(id,app_id,name,level) VALUES(?,?,?,?)", + (product_id, "app-portal", product_id, product_id), + ) + await db.execute( + "INSERT INTO licenses(id,key,key_hash,app_id) VALUES(?,?,?,?)", + ("license-portal", "masked", "p" * 64, "app-portal"), + ) + await db.execute( + """INSERT INTO license_products(id,license_id,product_id,expires_at) + VALUES(?,?,?,datetime('now','+1 hour'))""", + ("entitlement-owned", "license-portal", "product-owned"), + ) + await db.execute( + """INSERT INTO license_products(id,license_id,product_id,expires_at) + VALUES(?,?,?,datetime('now','-1 hour'))""", + ("entitlement-restricted", "license-portal", "product-restricted"), + ) + await db.execute( + """INSERT INTO app_files(id,app_id,name,content,portal_visible) + VALUES(?,?,?,?,1)""", + ("mapped-file", "app-portal", "mapped.bin", b"restricted"), + ) + await db.execute( + "INSERT INTO app_file_products(file_id,product_id) VALUES(?,?)", + ("mapped-file", "product-restricted"), + ) + await db.execute( + """INSERT INTO app_files + (id,app_id,name,content,portal_visible,product_id,is_revoked,is_active) + VALUES(?,?,?,?,?,?,?,?)""", + ("revoked-file", "app-portal", "revoked.bin", b"revoked", 1, + "product-owned", 1, 0), + ) + await db.commit() + lic = {"id": "license-portal", "app_id": "app-portal"} + + self.assertEqual(await self.admin.portal_list_files(lic=lic, db=db), []) + for file_id in ("mapped-file", "revoked-file"): + with self.assertRaises(HTTPException) as denied: + await self.admin.portal_download_file(file_id, lic=lic, db=db) + self.assertEqual(denied.exception.status_code, 404) + + await db.execute( + """UPDATE license_products SET expires_at=datetime('now','+1 hour') + WHERE id='entitlement-restricted'""" + ) + await db.commit() + visible = await self.admin.portal_list_files(lic=lic, db=db) + self.assertEqual([item["id"] for item in visible], ["mapped-file"]) + response = await self.admin.portal_download_file("mapped-file", lic=lic, db=db) + self.assertEqual(response.headers["content-disposition"].split(";")[0], "attachment") + + await db.execute( + "UPDATE license_products SET is_paused=1 WHERE id='entitlement-restricted'" + ) + await db.commit() + self.assertEqual(await self.admin.portal_list_files(lic=lic, db=db), []) + with self.assertRaises(HTTPException) as paused: + await self.admin.portal_download_file("mapped-file", lic=lic, db=db) + self.assertEqual(paused.exception.status_code, 404) async def test_public_status_does_not_expose_application_secrets(self): async with aiosqlite.connect(self.db_path) as db: From 180d2f00be06035c11a24b24ca6ee8ee737cead2 Mon Sep 17 00:00:00 2001 From: Pariot <278551738+arcwiser@users.noreply.github.com> Date: Mon, 2 Nov 2026 14:43:13 +1100 Subject: [PATCH 2/3] fix(deps): update vulnerable auth stack packages --- requirements.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index b5c4b33..ae4b223 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,10 @@ -fastapi==0.115.6 -uvicorn[standard]==0.33.0 -cryptography==44.0.0 +fastapi==0.162.0 +uvicorn[standard]==0.54.0 +cryptography==50.0.2 bcrypt==4.2.1 -python-multipart==0.0.20 +python-multipart==0.0.33 slowapi==0.1.9 -python-dotenv==1.0.1 +python-dotenv==1.2.4 aiofiles==24.1.0 aiosqlite==0.20.0 pyotp==2.9.0 From b4883f59853d41a0a6f2e468dddd6d7f21fdd8f4 Mon Sep 17 00:00:00 2001 From: Pariot <278551738+arcwiser@users.noreply.github.com> Date: Mon, 2 Nov 2026 14:44:41 +1100 Subject: [PATCH 3/3] fix(ci): accept clean merge-marker scan --- .github/workflows/ci.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c925017..dcb3bc8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,8 +24,13 @@ jobs: shell: pwsh run: | $matches = git grep -n -E '^(<<<<<<<|=======|>>>>>>>)' -- ':!sdk/json.hpp' - if ($LASTEXITCODE -eq 0) { Write-Error "Unresolved merge conflict markers found:`n$matches" } - if ($LASTEXITCODE -gt 1) { exit $LASTEXITCODE } + $grepExitCode = $LASTEXITCODE + if ($grepExitCode -eq 0) { + Write-Error "Unresolved merge conflict markers found:`n$matches" + exit 1 + } + if ($grepExitCode -gt 1) { exit $grepExitCode } + exit 0 - name: Compile Python sources run: python -m compileall -q main.py database.py routes utils migrations