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
3 changes: 3 additions & 0 deletions aw_server/rest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import math
import re
import traceback
from functools import wraps
Expand Down Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions tests/test_input_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading