fix(source): refuse to mint a signed url for a job we cannot identify

/api/source-url skipped the ownership check when the job record did not
resolve, and then signed anyway. That made it the one door in the design that
hands out a working capability without asking who is asking, which is precisely
what locking down /api/source was for.

Narrow in practice: a real job resolves, from its metadata or from the manifest
that names its owner while it runs, so the uncovered case is a directory in a
half-written state. It also still takes guessing a UUID, so this was never
worse than the behaviour it replaced. It is fixed because "a hole with a rare
precondition" is still a hole.

Off billing it keeps answering, since there is no owner to check and the
self-host preview would otherwise break for nothing.

The test stubs the minter so that reopening the hole fails on the status
assertion rather than on _cloud_config being None off billing: a test that
passes only because the code under it happens to crash stops guarding the day
it stops crashing. Verified by reopening the hole and reading the failure.

Claude-Session: https://claude.ai/code/session_01WyFE2wXi8quZR6nLRRLDgE
This commit is contained in:
juancarlos.cavero 2026-08-31 00:14:18 +02:00
parent 57e32b9052
commit 56697568f9
2 changed files with 48 additions and 1 deletions

12
app.py
View file

@ -2552,7 +2552,17 @@ async def get_source_url(job_id: str, request: Request):
capability URL here, with the bearer token, and hands the player that. capability URL here, with the bearer token, and hands the player that.
""" """
record = _job_record(job_id) record = _job_record(job_id)
if record is not None: if record is None:
# No record means no owner to check, and minting anyway would hand out
# a working key to whoever asked: the one door in this design that
# gives a capability without asking who you are. A real job resolves
# here (metadata recovers it, an in-flight one has a manifest naming
# its owner), so the only callers this turns away are asking about a
# job that does not exist. Off billing there is nothing to protect and
# the self-host preview must keep working.
if BILLING_ENABLED:
raise HTTPException(status_code=404, detail="Source not found")
else:
await _assert_job_owner(request, record) await _assert_job_owner(request, record)
return {"url": _signed_source_url(job_id)} return {"url": _signed_source_url(job_id)}

View file

@ -176,3 +176,40 @@ def test_every_source_url_the_browser_gets_comes_from_one_place(job, monkeypatch
assert r.status_code == 200, r.text assert r.status_code == 200, r.text
assert r.json()["source"]["available"] is True assert r.json()["source"]["available"] is True
assert r.json()["source"]["url"] == f"/signed/{JOB_ID}" assert r.json()["source"]["url"] == f"/signed/{JOB_ID}"
class TestSourceUrlNeverMintsBlind:
"""The minter must not hand a signed URL to a job it cannot identify."""
def test_self_host_still_answers_for_an_unknown_job(self, tmp_path, monkeypatch):
# Off billing there is no owner and no secret, and the preview has to
# keep working, so an unknown id is not something to refuse.
monkeypatch.setattr(app_module, "OUTPUT_DIR", str(tmp_path))
monkeypatch.setattr(app_module, "UPLOAD_DIR", str(tmp_path))
r = _get("/api/source-url/nobody-home")
assert r.status_code == 200
assert r.json()["url"] == "/api/source/nobody-home"
def test_cloud_refuses_instead_of_signing(self, tmp_path, monkeypatch):
# With billing on, an unresolvable job used to get a valid capability
# for the asking. It must 404 instead.
#
# _signed_source_url is stubbed so that reopening the hole fails on the
# assertion below and not on _cloud_config being None off-billing: a
# test that only passes because the minter happens to crash would stop
# guarding the moment the minter stopped crashing.
monkeypatch.setattr(app_module, "OUTPUT_DIR", str(tmp_path))
monkeypatch.setattr(app_module, "UPLOAD_DIR", str(tmp_path))
monkeypatch.setattr(app_module, "BILLING_ENABLED", True)
monkeypatch.setattr(app_module, "_signed_source_url", lambda j: f"/signed/{j}")
assert _get("/api/source-url/nobody-home").status_code == 404
def test_cloud_still_serves_the_owner(self, job, monkeypatch):
# The job fixture stamps user_id None (self-host style), which
# _assert_job_owner treats as "nothing to check", so this proves the
# refusal above is about the missing record and not a blanket block.
monkeypatch.setattr(app_module, "BILLING_ENABLED", True)
monkeypatch.setattr(app_module, "_signed_source_url", lambda j: f"/signed/{j}")
r = _get(f"/api/source-url/{JOB_ID}")
assert r.status_code == 200
assert r.json()["url"] == f"/signed/{JOB_ID}"