diff --git a/evidence/http-initial-environment.json b/evidence/http-initial-environment.json new file mode 100644 index 00000000..26160da2 --- /dev/null +++ b/evidence/http-initial-environment.json @@ -0,0 +1 @@ +{"result":"INVALID_ENVIRONMENT","cause":"Inherited student fixture default pointed to stopped lab4 port8084, not lab5 port8085; all connections refused; no API correctness conclusion.","failed":40,"errors":72,"seconds":8.77} diff --git a/src/pipeline.py b/src/pipeline.py index 2b152a75..5439384d 100644 --- a/src/pipeline.py +++ b/src/pipeline.py @@ -3,6 +3,7 @@ import json import os import random +import re import time from datetime import UTC, datetime from uuid import UUID @@ -68,14 +69,20 @@ def validate_job(data): ): raise ValueError("Invalid job type") for k in ["job_id", "attachment_id"]: + if not isinstance(data[k], str): + raise ValueError("Invalid job UUID") UUID(data[k]) for k in ["pipeline_version", "attempt"]: if type(data[k]) is not int or data[k] < 1: raise ValueError("Invalid job number") - datetime.fromisoformat(data["created_at"].replace("Z", "+00:00")) + timestamp = data["created_at"] + if not isinstance(timestamp, str) or not re.fullmatch( + r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})", + timestamp, + ): + raise ValueError("Invalid job timestamp") + datetime.fromisoformat(timestamp.replace("Z", "+00:00")) if "traceparent" in data: - import re - if not isinstance(data["traceparent"], str) or not re.fullmatch( r"00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}", data["traceparent"] ): diff --git a/src/reconcile.py b/src/reconcile.py index 5a4e9c15..e48d77b0 100644 --- a/src/reconcile.py +++ b/src/reconcile.py @@ -69,6 +69,19 @@ def reconcile(db, store, *, apply=False, grace=None): (reservation["id"],), ) store.delete(key=obj.object_name) + for job in jobs: + if ( + job["state"] == "failed" + and job["source_key"] == obj.object_name + ): + conn.execute( + "UPDATE jobs SET error_code='source_expired' WHERE id=%s", + (job["id"],), + ) + conn.execute( + "UPDATE attachments SET error_code='source_expired' WHERE id=%s AND pipeline_version=%s AND state='failed'", + (job["attachment_id"], job["pipeline_version"]), + ) actions.append(action) return {"apply": apply, "retention_seconds": grace, "actions": actions} diff --git a/student-tests/conftest.py b/student-tests/conftest.py index 7c1f2dfb..c672d63a 100644 --- a/student-tests/conftest.py +++ b/student-tests/conftest.py @@ -10,7 +10,7 @@ from jsonschema import Draft202012Validator, FormatChecker CONTRACT = json.loads( (Path(__file__).parents[1] / "contracts/openapi.json").read_text() ) -BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8084") +BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8085") PASSWORD = "Synthetic-Password-🌍-123" TOKENS = {} diff --git a/student-tests/test_attachments.py b/student-tests/test_attachments.py index b1ce48c5..6aacd2e9 100644 --- a/student-tests/test_attachments.py +++ b/student-tests/test_attachments.py @@ -210,3 +210,68 @@ def test_chunked_body_limit_without_content_length(client, actors, chat): assert r.status_code == 413 assert r.json()["error"]["code"] == "payload_too_large" assert r.json()["error"]["request_id"] == r.headers["x-request-id"] + + +@pytest.mark.parametrize("size", [(1, 17), (320, 200), (1600, 900)]) +def test_variant_geometry_and_alpha(client, actors, chat, size): + stream = io.BytesIO() + Image.new("RGBA", size, (0, 0, 0, 0)).save(stream, format="PNG") + a = check( + upload(client, actors[0], chat, data=stream.getvalue()), + 202, + "Attachment", + ) + a = wait_ready(client, actors[0], a) + for v in a["variants"]: + r = client.get( + f"/api/v1/attachments/{a['id']}/content", + headers=headers(actors[1]), + params={"variant": v["name"]}, + ) + assert len(r.content) == v["size_bytes"] + assert hashlib.sha256(r.content).hexdigest() == v["sha256"] + with Image.open(io.BytesIO(r.content)) as image: + assert image.size == (v["width"], v["height"]) + assert not image.getexif() + assert min(image.convert("RGB").getpixel((0, 0))) >= 250 + if v["name"] == "crop": + assert image.size == (128, 128) + else: + bound = 256 if v["name"] == "thumbnail" else 1280 + assert max(image.size) <= bound + assert image.width <= size[0] and image.height <= size[1] + + +def test_exif_orientation_applied_but_original_preserved(client, actors, chat): + stream = io.BytesIO() + exif = Image.Exif() + exif[274] = 6 + exif[270] = "Synthetic metadata must be removed" + Image.new("RGB", (320, 200), "red").save(stream, format="JPEG", exif=exif) + raw = stream.getvalue() + a = check( + upload( + client, + actors[0], + chat, + data=raw, + mime="image/jpeg", + filename="exif.jpg", + ), + 202, + "Attachment", + ) + a = wait_ready(client, actors[0], a) + r = client.get( + f"/api/v1/attachments/{a['id']}/content", headers=headers(actors[1]) + ) + assert r.content == raw + v = next(v for v in a["variants"] if v["name"] == "thumbnail") + assert (v["width"], v["height"]) == (160, 256) + r = client.get( + f"/api/v1/attachments/{a['id']}/content", + headers=headers(actors[1]), + params={"variant": "thumbnail"}, + ) + with Image.open(io.BytesIO(r.content)) as image: + assert image.size == (160, 256) and not image.getexif()