Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
57 changes: 48 additions & 9 deletions routes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4968,24 +4968,57 @@ 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())


@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()
Expand All @@ -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.
Expand Down
74 changes: 74 additions & 0 deletions tests/test_admin_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading