Skip to content

Commit 33f064b

Browse files
fix(scheduled-tasks): address webhook filter review
- Drop the matches op: an attacker-controlled body against a backtracking regex could block the event loop. - A webhook save without filters keeps the stored filters; [] clears them. The web and mobile forms always send filters. - Filters run after the signature and age checks but before the rate slot, so ignored requests never take a run's slot, and filtered rows keep their own retention so they can't evict real deliveries. - Release the relay claim for a filtered delivery so a redelivery is evaluated again. - Decode delivery outcomes leniently and label unknown ones in the UI. - in values must be non-empty, comma-free and trimmed, so the forms' comma-separated editing round-trips them; in [] is rejected. - Forms report the shared filter error, including the 16-filter limit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 832d29c commit 33f064b

16 files changed

Lines changed: 258 additions & 127 deletions

‎apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ function WebhookFilterRows(props: {
299299
) : null}
300300
{props.filters.length > 0 ? (
301301
<Text className="px-4 pb-3 text-sm text-foreground-muted">
302-
Runs only when every filter matches. Other requests are logged as filtered.
302+
Runs only when every filter matches. Other requests are logged as filtered. Separate “is
303+
one of” values with commas; a value can't contain a comma.
303304
</Text>
304305
) : null}
305306
</>
@@ -712,11 +713,8 @@ function TaskForm({
712713
return;
713714
}
714715
const parsedFilters = parseWebhookFilterDrafts(draft.schedule.filters);
715-
if (draft.schedule.mode === "webhook" && "invalidIndex" in parsedFilters) {
716-
Dialog.alert(
717-
"Invalid filter",
718-
`Filter ${parsedFilters.invalidIndex + 1} needs a path under body, headers or query, and a value its test accepts.`,
719-
);
716+
if (draft.schedule.mode === "webhook" && "error" in parsedFilters) {
717+
Dialog.alert("Invalid filter", parsedFilters.error);
720718
return;
721719
}
722720
if (

‎apps/mobile/src/features/settings/scheduledTaskDraft.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe("scheduleDraftForTask", () => {
3737
type: "webhook",
3838
signature: null,
3939
maxDeliveryAgeMinutes: null,
40+
filters: [],
4041
});
4142
});
4243

@@ -62,14 +63,14 @@ describe("scheduleDraftForTask", () => {
6263
const saved = scheduleFromDraft(
6364
scheduleDraftForTask({ schedule: { type: "webhook", signature } }),
6465
);
65-
expect(saved).toEqual({ type: "webhook", signature, maxDeliveryAgeMinutes: null });
66+
expect(saved).toEqual({ type: "webhook", signature, maxDeliveryAgeMinutes: null, filters: [] });
6667
expect(saved?.type === "webhook" && saved.signature && "secret" in saved.signature).toBe(false);
6768
});
6869

6970
it("round-trips webhook filters and refuses an invalid one", () => {
7071
const filters = [
7172
{ path: "headers.x-github-event", op: "equals", value: "pull_request" },
72-
{ path: "body.label.name", op: "matches", value: "^agent:" },
73+
{ path: "body.label.name", op: "in", value: ["needs review", "agent:fix"] },
7374
] as const;
7475
const draft = scheduleDraftForTask({ schedule: { type: "webhook", signature: null, filters } });
7576
expect(scheduleFromDraft(draft)).toMatchObject({ filters });

‎apps/mobile/src/features/settings/scheduledTaskDraft.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ export function scheduleFromDraft(draft: ScheduleDraft): ScheduledTaskUpsertSche
116116
prefix: draft.signature.prefix,
117117
},
118118
maxDeliveryAgeMinutes,
119-
...(parsedFilters.filters.length > 0 ? { filters: parsedFilters.filters } : {}),
119+
// Always sent: the server keeps stored filters when the key is missing.
120+
filters: parsedFilters.filters,
120121
};
121122
}
122123
if (draft.mode === "interval") {

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const WorkflowStopTool = Tool.make("t3_workflow_stop", {
116116

117117
export const ScheduleTaskTool = Tool.make("schedule_task", {
118118
description:
119-
"Create persistent work in the app scheduler that runs even when no turn is active. Pass schedule as a STRUCTURED OBJECT, never JSON text. Timers: {type:'interval', everyMs:3600000} is hourly; {type:'fixed_time', timeOfDay:'09:00', weekdays:[1,2,3,4,5]} is weekday mornings; report the returned nextRunAt. Webhooks: {type:'webhook'} runs once per request to a generated URL. The run sees the request ONLY through prompt placeholders: {{body.path}} (e.g. {{body.action}}, {{body.release.tag_name}}), {{headers.name}}, {{query.name}}, {{body}}, or {{request}} (method, headers with credentials redacted, and body). Each value arrives framed as untrusted request data, and $skill, @file or /command mentions inside it do not act; without a signature, anyone with the URL can trigger a run. For a sender that signs requests, first call request_secret so the user enters the secret privately (never ask for it in chat or invent one), then set signature with the returned secretRef, e.g. GitHub: {type:'webhook', signature:{header:'x-hub-signature-256', encoding:'hex', prefix:'sha256=', secretRef}}. To run only for some requests, add filters, all of which must match, e.g. filters:[{path:'headers.x-github-event', op:'equals', value:'pull_request'}, {path:'body.action', op:'in', value:['opened','reopened']}]; ops are equals, notEquals, in, startsWith, matches (regex) and exists. The result's webhookUrl is the public URL to give the user; if it is absent, this environment has no T3 Connect managed tunnel, so tell the user to enable T3 Connect remote access rather than sharing a path. Omit projectId for this thread's project. In this thread's project, runs post into THIS thread by default (bindToCurrentThread=true), which suits an orchestrator that sees every trigger, delegates work, and can dedupe against what is in flight; use false only when the user wants a fresh top-level thread per run. Elsewhere each run launches a fresh thread. Provider, model, and runtime settings inherit from this thread, or from the project default when there is no calling thread.",
119+
"Create persistent work in the app scheduler that runs even when no turn is active. Pass schedule as a STRUCTURED OBJECT, never JSON text. Timers: {type:'interval', everyMs:3600000} is hourly; {type:'fixed_time', timeOfDay:'09:00', weekdays:[1,2,3,4,5]} is weekday mornings; report the returned nextRunAt. Webhooks: {type:'webhook'} runs once per request to a generated URL. The run sees the request ONLY through prompt placeholders: {{body.path}} (e.g. {{body.action}}, {{body.release.tag_name}}), {{headers.name}}, {{query.name}}, {{body}}, or {{request}} (method, headers with credentials redacted, and body). Each value arrives framed as untrusted request data, and $skill, @file or /command mentions inside it do not act; without a signature, anyone with the URL can trigger a run. For a sender that signs requests, first call request_secret so the user enters the secret privately (never ask for it in chat or invent one), then set signature with the returned secretRef, e.g. GitHub: {type:'webhook', signature:{header:'x-hub-signature-256', encoding:'hex', prefix:'sha256=', secretRef}}. To run only for some requests, add filters, all of which must match, e.g. filters:[{path:'headers.x-github-event', op:'equals', value:'pull_request'}, {path:'body.action', op:'in', value:['opened','reopened']}]; ops are equals, notEquals, in, startsWith and exists. The result's webhookUrl is the public URL to give the user; if it is absent, this environment has no T3 Connect managed tunnel, so tell the user to enable T3 Connect remote access rather than sharing a path. Omit projectId for this thread's project. In this thread's project, runs post into THIS thread by default (bindToCurrentThread=true), which suits an orchestrator that sees every trigger, delegates work, and can dedupe against what is in flight; use false only when the user wants a fresh top-level thread per run. Elsewhere each run launches a fresh thread. Provider, model, and runtime settings inherit from this thread, or from the project default when there is no calling thread.",
120120
parameters: OrchestratorMcpScheduleTaskInput,
121121
success: OrchestratorMcpScheduleTaskResult,
122122
failure: OrchestratorMcpFailure,
@@ -143,7 +143,7 @@ const ListScheduledTasksTool = Tool.make("list_scheduled_tasks", {
143143

144144
const UpdateScheduledTaskTool = Tool.make("update_scheduled_task", {
145145
description:
146-
"Update an existing scheduled task by scheduledTaskId (from list_scheduled_tasks). Only the provided fields change; omit a field to leave it as-is. Use enabled=false to pause a task without deleting it. Set bindToCurrentThread to move the task between posting into this thread and launching a fresh thread per run. A schedule replaces the whole trigger, so to change a webhook's filters pass its full schedule with the new filters.",
146+
"Update an existing scheduled task by scheduledTaskId (from list_scheduled_tasks). Only the provided fields change; omit a field to leave it as-is. Use enabled=false to pause a task without deleting it. Set bindToCurrentThread to move the task between posting into this thread and launching a fresh thread per run. A webhook schedule without filters keeps the stored filters; pass filters to replace them, or filters:[] to clear them.",
147147
parameters: OrchestratorMcpUpdateScheduledTaskInput,
148148
success: OrchestratorMcpScheduleTaskResult,
149149
failure: OrchestratorMcpFailure,

‎apps/server/src/scheduledTasks/ScheduledTaskService.ts‎

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type ScheduledTaskListWebhookDeliveriesResult,
1616
type ScheduledTaskRotateWebhookTokenInput,
1717
type ScheduledTaskWebhookDeliveryOutcome,
18+
type ScheduledTaskWebhookFilter,
1819
type ScheduledTaskListResult,
1920
type ScheduledTaskMutationResult,
2021
type ScheduledTaskRunNowInput,
@@ -58,6 +59,8 @@ import { constantTimeEquals, verifyWebhookSignature } from "./webhookVerificatio
5859

5960
export const WEBHOOK_ROUTE_PREFIX = "/api/hooks";
6061
const WEBHOOK_DELIVERY_RETENTION = 50;
62+
/** Kept apart from the rest, so a stream of filtered requests can't evict real deliveries. */
63+
const WEBHOOK_FILTERED_DELIVERY_RETENTION = 20;
6164
const WEBHOOK_DELIVERY_LOG_BODY_LIMIT = 64 * 1024;
6265
const WEBHOOK_DELIVERY_LOG_PROMPT_LIMIT = 64 * 1024;
6366
const WEBHOOK_MAX_QUEUED_PER_TASK = 20;
@@ -134,6 +137,9 @@ export type WebhookTriggerResult =
134137
}
135138
| { readonly _tag: "expired" };
136139

140+
const webhookFilters = (filters: ReadonlyArray<ScheduledTaskWebhookFilter> | undefined) =>
141+
filters?.length ? { filters } : {};
142+
137143
const decodeTask = Schema.decodeUnknownEffect(ScheduledTask);
138144
const decodeTaskId = Schema.decodeUnknownOption(ScheduledTaskId);
139145
const decodeScheduleJson = Schema.decodeUnknownEffect(
@@ -997,7 +1003,14 @@ export const layer = Layer.effect(
9971003
prefix: input.schedule.signature.prefix,
9981004
},
9991005
maxDeliveryAgeMinutes: input.schedule.maxDeliveryAgeMinutes ?? null,
1000-
...(input.schedule.filters?.length ? { filters: input.schedule.filters } : {}),
1006+
...webhookFilters(
1007+
// Omitted keeps the stored filters, so a client that predates
1008+
// them can re-save a task without clearing them.
1009+
input.schedule.filters ??
1010+
(existingTask?.schedule.type === "webhook"
1011+
? existingTask.schedule.filters
1012+
: undefined),
1013+
),
10011014
}
10021015
: input.schedule;
10031016
const webhook =
@@ -1301,15 +1314,17 @@ export const layer = Layer.effect(
13011314
rendered_prompt = excluded.rendered_prompt,
13021315
error = excluded.error
13031316
`;
1317+
const filtered = input.outcome === "filtered" ? 1 : 0;
13041318
yield* sql`
13051319
DELETE FROM scheduled_task_webhook_deliveries
13061320
WHERE task_id = ${input.taskId}
1321+
AND (outcome = 'filtered') = ${filtered}
13071322
AND delivery_id NOT IN (
13081323
SELECT delivery_id FROM scheduled_task_webhook_deliveries
1309-
WHERE task_id = ${input.taskId}
1324+
WHERE task_id = ${input.taskId} AND (outcome = 'filtered') = ${filtered}
13101325
-- rowid breaks timestamp ties in arrival order; delivery ids are random.
13111326
ORDER BY received_at DESC, rowid DESC
1312-
LIMIT ${WEBHOOK_DELIVERY_RETENTION}
1327+
LIMIT ${filtered ? WEBHOOK_FILTERED_DELIVERY_RETENTION : WEBHOOK_DELIVERY_RETENTION}
13131328
)
13141329
`;
13151330
}),
@@ -1520,6 +1535,42 @@ export const layer = Layer.effect(
15201535
...(details.error === undefined ? {} : { error: details.error }),
15211536
});
15221537

1538+
const signature = schedule.signature;
1539+
const signatureVerified =
1540+
signature === null ||
1541+
(row.webhook_secret !== null &&
1542+
verifyWebhookSignature({
1543+
signature,
1544+
secret: row.webhook_secret,
1545+
headers: request.headers,
1546+
body: request.body,
1547+
}));
1548+
const maxAgeMinutes = schedule.maxDeliveryAgeMinutes ?? null;
1549+
const tooOld =
1550+
maxAgeMinutes !== null &&
1551+
DateTime.toEpochMillis(now) - DateTime.toEpochMillis(receivedAt) >
1552+
maxAgeMinutes * 60_000;
1553+
1554+
// Filters: checked after the signature and age, and before the rate
1555+
// limit, so requests the task ignores never take a run's slot. The
1556+
// request is accepted so the sender doesn't retry it, and its relay
1557+
// claim is released because nothing ran.
1558+
if (task.enabled && signatureVerified && !tooOld) {
1559+
const failedFilter = findFailedWebhookFilter(schedule.filters ?? [], request);
1560+
if (failedFilter !== null) {
1561+
yield* log("filtered", {
1562+
signatureVerified: signature !== null,
1563+
error: describeFailedWebhookFilter(failedFilter),
1564+
});
1565+
yield* observe("filtered");
1566+
return yield* releaseClaim({
1567+
_tag: "accepted" as const,
1568+
deliveryId,
1569+
outcome: "filtered" as const,
1570+
});
1571+
}
1572+
}
1573+
15231574
// Only the first rejected request in a window is logged, so a flood
15241575
// cannot write rows or push the real deliveries out of the log.
15251576
const slot = yield* takeRateSlot(task.id, DateTime.toEpochMillis(now));
@@ -1536,45 +1587,17 @@ export const layer = Layer.effect(
15361587
yield* observe("disabled");
15371588
return { _tag: "disabled" as const };
15381589
}
1539-
const signature = schedule.signature;
1540-
if (signature !== null) {
1541-
const verified =
1542-
row.webhook_secret !== null &&
1543-
verifyWebhookSignature({
1544-
signature,
1545-
secret: row.webhook_secret,
1546-
headers: request.headers,
1547-
body: request.body,
1548-
});
1549-
if (!verified) {
1550-
yield* log("rejected_signature");
1551-
yield* observe("rejected_signature");
1552-
return { _tag: "rejected_signature" as const };
1553-
}
1590+
if (!signatureVerified) {
1591+
yield* log("rejected_signature");
1592+
yield* observe("rejected_signature");
1593+
return { _tag: "rejected_signature" as const };
15541594
}
1555-
const maxAgeMinutes = schedule.maxDeliveryAgeMinutes ?? null;
1556-
if (
1557-
maxAgeMinutes !== null &&
1558-
DateTime.toEpochMillis(now) - DateTime.toEpochMillis(receivedAt) >
1559-
maxAgeMinutes * 60_000
1560-
) {
1595+
if (tooOld) {
15611596
yield* log("expired", { signatureVerified: signature !== null });
15621597
yield* observe("expired");
15631598
return { _tag: "expired" as const };
15641599
}
15651600

1566-
// Filters: a request the task does not care about is accepted, so
1567-
// the sender does not retry it, and logged without starting a run.
1568-
const failedFilter = findFailedWebhookFilter(schedule.filters ?? [], request);
1569-
if (failedFilter !== null) {
1570-
yield* log("filtered", {
1571-
signatureVerified: signature !== null,
1572-
error: describeFailedWebhookFilter(failedFilter),
1573-
});
1574-
yield* observe("filtered");
1575-
return { _tag: "accepted" as const, deliveryId, outcome: "filtered" as const };
1576-
}
1577-
15781601
const frameId = yield* crypto.randomBytes(6).pipe(
15791602
Effect.map((bytes) => Buffer.from(bytes).toString("hex")),
15801603
Effect.mapError((cause) => taskError("Could not generate frame id.", { cause })),

‎apps/server/src/scheduledTasks/ScheduledTaskService.webhook.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,3 +1030,89 @@ it.effect("checks filters after the signature and max age, and runs only matchin
10301030
}),
10311031
),
10321032
);
1033+
1034+
const pullRequestFilterInput = () =>
1035+
webhookTaskInput({
1036+
schedule: {
1037+
type: "webhook",
1038+
filters: [{ path: "headers.x-github-event", op: "equals", value: "pull_request" }],
1039+
},
1040+
});
1041+
const githubEvent = (event: string) => ({
1042+
headers: { "content-type": "application/json", "x-github-event": event },
1043+
});
1044+
1045+
it.effect("filtered deliveries take no rate slot and cannot evict real deliveries", () =>
1046+
withService(({ service, launches }) =>
1047+
Effect.gen(function* () {
1048+
const { task } = yield* service.upsert(yield* pullRequestFilterInput());
1049+
const ignored = yield* Effect.forEach(Array.from({ length: 70 }), () =>
1050+
service.triggerWebhook(requestFor(task, githubEvent("push"))),
1051+
);
1052+
assert.isTrue(
1053+
ignored.every((result) => result._tag === "accepted" && result.outcome === "filtered"),
1054+
);
1055+
const matched = yield* service.triggerWebhook(requestFor(task, githubEvent("pull_request")));
1056+
assert.deepInclude(matched, { _tag: "accepted", outcome: "accepted" });
1057+
yield* Queue.take(launches);
1058+
yield* Effect.forEach(Array.from({ length: 5 }), () =>
1059+
service.triggerWebhook(requestFor(task, githubEvent("push"))),
1060+
);
1061+
1062+
const outcomes = (yield* service.listWebhookDeliveries({ id: task.id })).deliveries.map(
1063+
(delivery) => delivery.outcome,
1064+
);
1065+
assert.equal(outcomes.filter((outcome) => outcome === "filtered").length, 20);
1066+
assert.equal(outcomes.filter((outcome) => outcome === "accepted").length, 1);
1067+
}),
1068+
),
1069+
);
1070+
1071+
it.effect("a filtered relay delivery releases its claim, so a redelivery is evaluated again", () =>
1072+
withService(({ service, launches }) =>
1073+
Effect.gen(function* () {
1074+
const { task } = yield* service.upsert(yield* pullRequestFilterInput());
1075+
const held = { relayDeliveryId: "relay-filtered" };
1076+
const first = yield* service.triggerWebhook(
1077+
requestFor(task, { ...held, ...githubEvent("push") }),
1078+
);
1079+
const again = yield* service.triggerWebhook(
1080+
requestFor(task, { ...held, ...githubEvent("push") }),
1081+
);
1082+
assert.deepInclude(first, { _tag: "accepted", outcome: "filtered" });
1083+
assert.deepInclude(again, { _tag: "accepted", outcome: "filtered" });
1084+
const ran = yield* service.triggerWebhook(
1085+
requestFor(task, { ...held, ...githubEvent("pull_request") }),
1086+
);
1087+
assert.deepInclude(ran, { _tag: "accepted", outcome: "accepted" });
1088+
yield* Queue.take(launches);
1089+
const replayed = yield* service.triggerWebhook(
1090+
requestFor(task, { ...held, ...githubEvent("pull_request") }),
1091+
);
1092+
assert.deepInclude(replayed, { _tag: "accepted", outcome: "duplicate" });
1093+
}),
1094+
),
1095+
);
1096+
1097+
it.effect("a save without filters keeps the stored ones and an empty list clears them", () =>
1098+
withService(({ service }) =>
1099+
Effect.gen(function* () {
1100+
const { task } = yield* service.upsert(yield* pullRequestFilterInput());
1101+
const filters = task.schedule.type === "webhook" ? task.schedule.filters : undefined;
1102+
assert.lengthOf(filters ?? [], 1);
1103+
1104+
const kept = yield* service.upsert(
1105+
yield* webhookTaskInput({ title: "Renamed", schedule: { type: "webhook" } }),
1106+
);
1107+
assert.deepEqual(
1108+
kept.task.schedule.type === "webhook" && kept.task.schedule.filters,
1109+
filters,
1110+
);
1111+
1112+
const cleared = yield* service.upsert(
1113+
yield* webhookTaskInput({ schedule: { type: "webhook", filters: [] } }),
1114+
);
1115+
assert.notProperty(cleared.task.schedule, "filters");
1116+
}),
1117+
),
1118+
);

0 commit comments

Comments
 (0)