diff --git a/Makefile b/Makefile index e5fe2f3..bdb8d50 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ test: @# Note that extensive integration tests are also run in the bundle repo, @# for both aw-server and aw-server-rust, but without code coverage. python -c 'import aw_server' - python -m pytest tests/test_server.py tests/test_profile.py tests/test_profile_config.py tests/test_query_cache.py tests/test_import.py tests/test_host_header.py tests/test_database_concurrency.py tests/test_settings.py tests/test_csv_export.py tests/test_performance.py + python -m pytest tests/test_server.py tests/test_profile.py tests/test_profile_config.py tests/test_query_cache.py tests/test_import.py tests/test_host_header.py tests/test_database_concurrency.py tests/test_settings.py tests/test_csv_export.py tests/test_performance.py tests/test_input_validation.py typecheck: python -m mypy aw_server tests --ignore-missing-imports diff --git a/aw_server/api.py b/aw_server/api.py index b46ef20..79bf8fc 100644 --- a/aw_server/api.py +++ b/aw_server/api.py @@ -307,8 +307,16 @@ def import_bucket(self, bucket_data: Any): # scrub IDs from events # (otherwise causes weird bugs with no events seemingly imported when importing events exported from aw-server-rust, which contains IDs) for event in bucket_data["events"]: - if "id" in event: - del event["id"] + if isinstance(event, dict): + if "id" in event: + del event["id"] + ts = event.get("timestamp") + if ts is not None and not isinstance(ts, str): + raise ValueError( + f"Event timestamp must be a string or null, not {type(ts).__name__!r}" + ) + else: + raise TypeError("Event must be an object") self.create_events( bucket_id, diff --git a/aw_server/rest.py b/aw_server/rest.py index c1fbe66..3459764 100644 --- a/aw_server/rest.py +++ b/aw_server/rest.py @@ -154,6 +154,9 @@ def _parse_event(data) -> Event: """Construct an Event from client JSON, raising BadRequest on malformed input.""" if not isinstance(data, dict): raise BadRequest("InvalidEvent", "Event must be a JSON object") + timestamp = data.get("timestamp") + if timestamp is not None and not isinstance(timestamp, str): + raise BadRequest("InvalidEvent", "Event timestamp must be a string or null") try: return Event(**data) except (ValueError, TypeError) as e: diff --git a/tests/test_import.py b/tests/test_import.py index 0598f1d..ff91f53 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -132,6 +132,14 @@ def test_import_duplicate_ids_within_export(flask_client, cleanup): assert "test-import-twice" not in _buckets(flask_client) +@pytest.mark.parametrize("buckets", [[], None, "invalid", 1]) +def test_import_all_invalid_buckets_preserves_value_error(app, buckets): + before = app.api.get_buckets() + with pytest.raises(ValueError, match="'buckets' must be an object"): + app.api.import_all(buckets) + assert app.api.get_buckets() == before + + def test_import_buckets_not_an_object(flask_client): r = flask_client.post("/api/0/import", json={"buckets": []}) assert r.status_code == 400 @@ -263,3 +271,40 @@ def flaky_delete(bucket_id): assert "test-import-rb-c" in buckets # ...and it is named in the response so the client can clean it up assert "test-import-rb-c" in r.get_json()["message"] + + +@pytest.mark.parametrize("event", [123, 1.5, True, None, [], "invalid"]) +def test_import_non_object_event_rejected(flask_client, cleanup, event): + """Malformed event entries must return 400 and roll back the import.""" + bucket_id = "test-import-non-object" + cleanup.append(bucket_id) + r = flask_client.post( + "/api/0/import", + json={"buckets": {bucket_id: _bucket(bucket_id, events=[event])}}, + ) + assert r.status_code == 400, r.data + assert r.is_json + assert "event" in r.json["message"].lower() + assert bucket_id not in _buckets(flask_client) + + +@pytest.mark.parametrize("timestamp", [5, 5.0, True, [], {}]) +def test_import_numeric_event_timestamp_rejected(flask_client, timestamp): + """Non-string timestamps in imported events must return 400, not 500.""" + bucket_id = "test-import-numeric-ts" + r = flask_client.post( + "/api/0/import", + json={ + "buckets": { + bucket_id: _bucket( + bucket_id, + events=[{"timestamp": timestamp, "duration": 0, "data": {}}], + ) + } + }, + ) + assert r.status_code == 400, r.data + assert r.is_json + assert "timestamp" in r.json["message"] + # Rollback must have removed the partially-created bucket + assert bucket_id not in _buckets(flask_client) diff --git a/tests/test_input_validation.py b/tests/test_input_validation.py index 8a70241..5bad7b0 100644 --- a/tests/test_input_validation.py +++ b/tests/test_input_validation.py @@ -157,3 +157,53 @@ def test_heartbeat_nonfinite_pulsetime(flask_client, bucket, pulsetime, seeded): ) assert followup.status_code == 200 assert followup.json["duration"] == (2 if seeded else 0) + + +@pytest.mark.parametrize("timestamp", [5, 5.0, True, False, [], {}]) +@pytest.mark.parametrize("batch", [False, True]) +def test_numeric_event_timestamp_rejected_without_storage_mutation( + flask_client, bucket, timestamp, batch +): + url = f"/api/0/buckets/{bucket}/events" + valid = {"timestamp": TS, "duration": 1, "data": {"control": True}} + assert flask_client.post(url, json=valid).status_code == 200 + before = flask_client.get(url).json + invalid = {"timestamp": timestamp, "duration": 0, "data": {}} + # A valid event before the bad item must not make a batch partially commit. + body = [valid, invalid] if batch else invalid + assert_bad_request(flask_client.post(url, json=body), "timestamp") + assert flask_client.get(url).json == before + assert flask_client.post(url, json=valid).status_code == 200 + assert len(flask_client.get(url).json) == 2 + + +@pytest.mark.parametrize("timestamp", [5, 5.0, True, [], {}]) +def test_numeric_event_timestamp_heartbeat_rejected(flask_client, bucket, timestamp): + url = f"/api/0/buckets/{bucket}" + # Heartbeats already reject these values through schema validation. + response = flask_client.post( + f"{url}/heartbeat?pulsetime=1", + json={"timestamp": timestamp, "duration": 0, "data": {}}, + ) + assert response.status_code == 400 + assert response.is_json + assert "timestamp" in response.json["errors"] + assert flask_client.get(f"{url}/events").json == [] + assert ( + flask_client.post( + f"{url}/heartbeat?pulsetime=1", + json={"timestamp": TS, "duration": 0, "data": {}}, + ).status_code + == 200 + ) + + +@pytest.mark.parametrize("body", [{"timestamp": None}, {}]) +def test_numeric_event_timestamp_absent_and_null_compatibility( + flask_client, bucket, body +): + url = f"/api/0/buckets/{bucket}/events" + r = flask_client.post(url, json={**body, "duration": 0, "data": {}}) + assert r.status_code == 200, r.data + assert isinstance(r.json[0]["timestamp"], str) + assert len(flask_client.get(url).json) == 1 diff --git a/tests/test_performance.py b/tests/test_performance.py index c68b480..8a66f8d 100644 --- a/tests/test_performance.py +++ b/tests/test_performance.py @@ -142,7 +142,7 @@ def __str__(self): monkeypatch.setattr( rest.request._get_current_object(), "get_json", - lambda: Payload(timestamp=START, data={"app": "a"}), + lambda: Payload(timestamp=START.isoformat(), data={"app": "a"}), ) monkeypatch.setattr(app.api, "create_events", lambda *args: []) assert rest.EventsResource().post("test") == ([], 200)