Skip to content

Commit 88fcebd

Browse files
committed
fix(agent_api_keys): validate webhook path before create
1 parent de85c65 commit 88fcebd

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

agentex/openapi.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4615,7 +4615,9 @@ components:
46154615
- type: string
46164616
- type: 'null'
46174617
title: Secret
4618-
description: Optional signing secret; if unset, one is generated and returned.
4618+
description: Signing secret. For GitHub, omit to generate one, or provide
4619+
an existing webhook secret. For Slack, this is required and must be the
4620+
Slack app's Signing Secret.
46194621
base_url:
46204622
anyOf:
46214623
- type: string

agentex/src/api/routes/agent_api_keys.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ async def create_webhook_trigger(
160160
),
161161
)
162162

163+
forward_path = request.forward_path.lstrip("/")
164+
if _has_control_chars(forward_path):
165+
raise HTTPException(
166+
status_code=400,
167+
detail="forward_path must not contain control characters.",
168+
)
169+
163170
secret = request.secret if request.secret is not None else secrets.token_hex(32)
164171
agent_api_key_entity = await agent_api_key_use_case.create(
165172
agent_id=agent.id,
@@ -168,12 +175,6 @@ async def create_webhook_trigger(
168175
api_key_type=request.source,
169176
)
170177

171-
forward_path = request.forward_path.lstrip("/")
172-
if _has_control_chars(forward_path):
173-
raise HTTPException(
174-
status_code=400,
175-
detail="forward_path must not contain control characters.",
176-
)
177178
encoded_agent_name = quote(request.agent_name, safe="")
178179
encoded_forward_path = quote(forward_path, safe="/")
179180
webhook_path = f"/agents/forward/name/{encoded_agent_name}/{encoded_forward_path}"

agentex/tests/unit/api/test_webhook_trigger.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,30 @@ async def test_forward_path_reserved_chars_are_url_encoded(self, monkeypatch):
104104
assert resp.webhook_url == f"https://sgp.example.com{resp.webhook_path}"
105105

106106
async def test_forward_path_control_chars_rejected(self, monkeypatch):
107+
monkeypatch.setattr(mod, "_check_agent_or_collapse_to_404", AsyncMock())
108+
107109
req = CreateWebhookTriggerRequest(
108110
agent_name="a",
109111
source=AgentAPIKeyType.GITHUB,
110112
name="o/r",
111113
forward_path="github-pr/cfg-9\nnext",
112114
)
115+
agent_use_case = MagicMock()
116+
agent_use_case.get = AsyncMock(return_value=MagicMock(id="agent-1"))
117+
118+
akuc = MagicMock()
119+
akuc.get_by_agent_id_and_name = AsyncMock(return_value=None)
120+
akuc.create = AsyncMock()
121+
113122
with pytest.raises(HTTPException) as exc:
114-
await self._call(monkeypatch, req)
123+
await create_webhook_trigger(
124+
request=req,
125+
agent_api_key_use_case=akuc,
126+
agent_use_case=agent_use_case,
127+
authorization_service=MagicMock(),
128+
)
115129
assert exc.value.status_code == 400
130+
akuc.create.assert_not_awaited()
116131

117132
async def test_conflict_when_key_exists(self, monkeypatch):
118133
req = CreateWebhookTriggerRequest(

0 commit comments

Comments
 (0)