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..4148161 100644 --- a/aw_server/api.py +++ b/aw_server/api.py @@ -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 " @@ -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: + 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) diff --git a/tests/test_input_validation.py b/tests/test_input_validation.py index 8a70241..181f53f 100644 --- a/tests/test_input_validation.py +++ b/tests/test_input_validation.py @@ -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" @@ -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])