diff --git a/aw_server/rest.py b/aw_server/rest.py index 91acf045..90d8dcf1 100644 --- a/aw_server/rest.py +++ b/aw_server/rest.py @@ -1,4 +1,5 @@ import json +import math import re import traceback from functools import wraps @@ -402,6 +403,8 @@ def post(self, bucket_id): pulsetime = _parse_arg(request.args, "pulsetime", float) if pulsetime is None: raise BadRequest("MissingParameter", "Missing required parameter pulsetime") + if not math.isfinite(pulsetime): + raise BadRequest("InvalidParameter", "Parameter pulsetime must be finite") # This lock is meant to ensure that only one heartbeat is processed at a time, # as the heartbeat function is not thread-safe. diff --git a/tests/test_input_validation.py b/tests/test_input_validation.py index e43884fa..8a702416 100644 --- a/tests/test_input_validation.py +++ b/tests/test_input_validation.py @@ -121,3 +121,39 @@ def test_valid_input_still_accepted(flask_client, bucket): ) assert r.status_code == 200 assert r.json == 1 + + +@pytest.mark.parametrize("pulsetime", ["inf", "-inf", "nan", "1e999"]) +@pytest.mark.parametrize("seeded", [False, True]) +def test_heartbeat_nonfinite_pulsetime(flask_client, bucket, pulsetime, seeded): + endpoint = f"/api/0/buckets/{bucket}/heartbeat" + heartbeat = {"timestamp": TS, "duration": 0, "data": {"app": "test"}} + if seeded: + assert ( + flask_client.post( + endpoint, query_string={"pulsetime": "1"}, json=heartbeat + ).status_code + == 200 + ) + merged = flask_client.post( + endpoint, + query_string={"pulsetime": "1"}, + json=dict(heartbeat, timestamp="2026-01-01T00:00:01Z"), + ) + assert merged.status_code == 200 + assert merged.json["duration"] == 1 + before = flask_client.get(f"/api/0/buckets/{bucket}/events").json + response = flask_client.post( + endpoint, + query_string={"pulsetime": pulsetime}, + json=dict(heartbeat, timestamp="2026-01-01T00:00:02Z"), + ) + assert_bad_request(response, "pulsetime") + assert flask_client.get(f"/api/0/buckets/{bucket}/events").json == before + followup = flask_client.post( + endpoint, + query_string={"pulsetime": "1"}, + json=dict(heartbeat, timestamp="2026-01-01T00:00:02Z"), + ) + assert followup.status_code == 200 + assert followup.json["duration"] == (2 if seeded else 0)