Skip to content

Automate browser based workflows with AI

Cap and dedupe parsed run-tag filter terms (SKY-12327 follow-up) (#7413)

Aaron Perez4d73df9164a5
diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.py
index 82cee8814..47863384d 100644
--- a/skyvern/forge/sdk/routes/agent_protocol.py
+++ b/skyvern/forge/sdk/routes/agent_protocol.py
@@ -3896,12 +3896,19 @@ async def get_runs(
return ORJSONResponse([run.model_dump() for run in runs])
+_MAX_TAG_FILTER_TERMS = 20
+
+
def _parse_tag_filter_terms(tags: list[str] | None) -> list[tuple[str | None, str | None]]:
"""Parse a repeated/comma-separated ``tags`` query param into (key, value) filter terms.
Shared by ``get_runs_v2``, ``get_workflow_runs_by_id``, and ``get_workflows``. Each term is a
label (``production`` -> ``(None, "production")``), a group wildcard (``env:*`` -> ``("env", None)``),
or an exact group:label (``env:prod`` -> ``("env", "prod")``); malformed terms raise a 400.
+
+ Terms are deduplicated, then capped at ``_MAX_TAG_FILTER_TERMS`` (400 beyond it): the
+ ``Query(max_length=20)`` on callers only bounds repeated params, not the comma-split
+ expansion, and each distinct term becomes its own AND'd subquery.
"""
# A lone empty value (?tags= with nothing else) is a no-op for backward
# compat; any blank segment alongside real ones — comma (env:prod,) or
@@ -3940,7 +3947,13 @@ def _parse_tag_filter_terms(tags: list[str] | None) -> list[tuple[str | None, st
)
else:
parsed_tags.append((tag_key, tag_value))
- return parsed_tags
+ deduped_tags = list(dict.fromkeys(parsed_tags))
+ if len(deduped_tags) > _MAX_TAG_FILTER_TERMS:
+ raise HTTPException(
+ status_code=http_status.HTTP_400_BAD_REQUEST,
+ detail=f"Too many tag filter terms; at most {_MAX_TAG_FILTER_TERMS} distinct terms are allowed.",
+ )
+ return deduped_tags
async def _parse_and_gate_tag_filter_terms(
diff --git a/tests/unit/test_runs_list_run_tag_filter.py b/tests/unit/test_runs_list_run_tag_filter.py
index bf39aaf0b..76e0577b9 100644
--- a/tests/unit/test_runs_list_run_tag_filter.py
+++ b/tests/unit/test_runs_list_run_tag_filter.py
@@ -262,6 +262,31 @@ async def test_parse_and_gate_tag_filter_terms_only_gates_nonempty_filters(
gate.assert_awaited_once_with(organization)
+async def test_parse_tag_filter_terms_dedupes_repeated_terms() -> None:
+ from skyvern.forge.sdk.routes import agent_protocol as ap
+
+ assert ap._parse_tag_filter_terms(["env:prod,env:prod", "env:prod", "demo,demo"]) == [
+ ("env", "prod"),
+ (None, "demo"),
+ ]
+
+
+async def test_parse_tag_filter_terms_caps_comma_split_terms() -> None:
+ from fastapi import HTTPException
+
+ from skyvern.forge.sdk.routes import agent_protocol as ap
+
+ at_cap = ",".join(f"k{i}:v" for i in range(ap._MAX_TAG_FILTER_TERMS))
+ assert len(ap._parse_tag_filter_terms([at_cap])) == ap._MAX_TAG_FILTER_TERMS
+
+ over_cap = ",".join(f"k{i}:v" for i in range(ap._MAX_TAG_FILTER_TERMS + 1))
+ with pytest.raises(HTTPException) as exc_info:
+ ap._parse_tag_filter_terms([over_cap])
+ assert exc_info.value.status_code == 400
+
+
@pytest.fixture
def route_client(monkeypatch: pytest.MonkeyPatch) -> TestClient:
from skyvern.forge.sdk.routes.routers import base_router