Skip to content
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions aw_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,8 @@ def query2(self, name, query, timeperiods, cache: bool = True):
qc = self.query_cache if cache else None
result = []
for timeperiod in timeperiods:
period = timeperiod.split("/")[
:2
] # iso8601 timeperiods are separated by a slash
# iso8601 timeperiods are separated by a slash
period = timeperiod.split("/")
if len(period) != 2:
raise QueryException(
f"Invalid timeperiod '{timeperiod}': expected two ISO8601 "
Expand All @@ -691,6 +690,10 @@ def query2(self, name, query, timeperiods, cache: bool = True):
endtime = iso8601.parse_date(period[1])
except iso8601.ParseError as e:
raise QueryException(f"Invalid timeperiod '{timeperiod}': {e}")
if starttime > endtime:
Comment thread
TimeToBuildBob marked this conversation as resolved.
Resolved
raise QueryException(
f"Invalid timeperiod '{timeperiod}': start must not be after end"
)
period = (starttime, endtime)
db: Any = (
_EventBudget(self.db, self.query_max_events, period)
Expand Down
85 changes: 85 additions & 0 deletions tests/test_input_validation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""Malformed client input must be rejected with 400 and a JSON message, not 500."""

import aw_datastore
import pytest
from aw_datastore import Datastore
from aw_query.exceptions import QueryException

from aw_server.api import ServerAPI

BUCKET = "test-input-validation"
TS = "2026-01-01T00:00:00Z"
Expand Down Expand Up @@ -157,3 +162,83 @@ def test_heartbeat_nonfinite_pulsetime(flask_client, bucket, pulsetime, seeded):
)
assert followup.status_code == 200
assert followup.json["duration"] == (2 if seeded else 0)


QUERY_START = "2024-01-01T00:00:00Z"
QUERY_END = "2024-01-02T00:00:00Z"
INVALID_QUERY_PERIODS = [
f"{QUERY_START}/{QUERY_END}/ignored",
f"{QUERY_START}/{QUERY_END}/",
f"{QUERY_END}/{QUERY_START}",
# Lexically ordered, but the start instant is later than the end instant.
"2024-01-01T00:00:00-02:00/2024-01-01T01:00:00Z",
QUERY_START,
f"/{QUERY_END}",
f"{QUERY_START}/",
f"bad-date/{QUERY_END}",
f"{QUERY_START}/bad-date",
]
VALID_QUERY_PERIODS = [
f"{QUERY_START}/{QUERY_END}",
f"{QUERY_START}/{QUERY_START}",
# Equal instants in different offsets; comparison must use parsed datetimes.
"2024-01-01T01:00:00+01:00/2024-01-01T00:00:00Z",
# Lexically reversed, but start is before end in UTC.
"2024-01-01T01:00:00+02:00/2024-01-01T00:00:00Z",
]


@pytest.fixture()
def query_api():
db = Datastore(aw_datastore.get_storage_methods()["memory"], testing=True)
return ServerAPI(db=db, testing=True)


@pytest.mark.parametrize("period", INVALID_QUERY_PERIODS)
@pytest.mark.parametrize("cache", [False, True])
def test_query_timeperiod_validation_api(query_api, monkeypatch, period, cache):
def unexpected_call(*args, **kwargs):
pytest.fail("Invalid period reached query evaluation or cache access")

monkeypatch.setattr("aw_server.api.query2.query", unexpected_call)
monkeypatch.setattr(query_api.query_cache, "cacheable", unexpected_call)
with pytest.raises(QueryException) as exc:
query_api.query2("validation", ["RETURN = 1;"], [period], cache=cache)
assert period in str(exc.value)


@pytest.mark.parametrize("period", INVALID_QUERY_PERIODS)
@pytest.mark.parametrize("cache", [False, True])
def test_query_timeperiod_validation_http(flask_client, period, cache):
response = flask_client.post(
"/api/0/query/",
json={"query": ["RETURN = 1;"], "timeperiods": [period], "cache": cache},
)
assert_bad_request(response, period)
assert response.json["type"] == "QueryException"


@pytest.mark.parametrize("period", VALID_QUERY_PERIODS)
@pytest.mark.parametrize("cache", [False, True])
def test_query_timeperiod_validation_valid(query_api, flask_client, period, cache):
for _ in range(2):
assert query_api.query2("validation", ["RETURN = 1;"], [period], cache) == [1]
response = flask_client.post(
"/api/0/query/",
json={"query": ["RETURN = 1;"], "timeperiods": [period], "cache": cache},
)
assert response.status_code == 200, response.data
assert response.json == [1]
assert query_api.query_cache.stats()["hits"] == (1 if cache else 0)


@pytest.mark.parametrize("cache", [False, True])
def test_query_timeperiod_validation_mixed_list(query_api, flask_client, cache):
periods = [VALID_QUERY_PERIODS[0], INVALID_QUERY_PERIODS[0]]
with pytest.raises(QueryException):
query_api.query2("validation", ["RETURN = 1;"], periods, cache)
response = flask_client.post(
"/api/0/query/",
json={"query": ["RETURN = 1;"], "timeperiods": periods, "cache": cache},
)
assert_bad_request(response, periods[1])
Loading