Skip to content

Commit 1fb4857

Browse files
zyw02steipeteampagent
authored
fix(cron): reject blank/invalid --webhook before delivery.mode flip (#121533)
* fix(cron): reject blank/invalid --webhook before delivery.mode flip Presence-only typeof checks treated empty or non-http --webhook as a delivery edit, forging mode=webhook with no URL and clearing the prior chat destination on merge. Validate with normalizeHttpWebhookUrl first. Co-authored-by: Peter Steinberger <[email protected]> * test(cron): cover webhook validation boundaries Amp-Thread-ID: https://ampcode.com/threads/T-01a0220d-eaa0-76b4-adb9-68841f015b75 --------- Co-authored-by: zyw02 <[email protected]> Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: Amp <[email protected]>
1 parent 4ba2c46 commit 1fb4857

5 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎src/cli/cron-cli.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,23 @@ describe("cron cli", () => {
771771
});
772772
});
773773

774+
it.each(["", "not-a-url"])("rejects invalid cron add --webhook %j", async (value) => {
775+
await expectCronCommandExit([
776+
"cron",
777+
"add",
778+
"Webhook reminder",
779+
"--at",
780+
"20m",
781+
"--system-event",
782+
"Summarize the latest status",
783+
"--webhook",
784+
value,
785+
]);
786+
787+
expectRuntimeErrorContaining("--webhook must be a valid http(s) URL");
788+
expect(callGatewayFromCli).not.toHaveBeenCalled();
789+
});
790+
774791
it("accepts Hermes-style positional cron schedule and message on cron create", async () => {
775792
const params = await runCronAddAndGetParams([
776793
"0 2 * * *",

‎src/cli/cron-cli/register.cron-add.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Command } from "commander";
88
import { theme } from "../../../packages/terminal-core/src/theme.js";
99
import { THINKING_LEVELS_HELP } from "../../auto-reply/thinking.shared.js";
1010
import type { CronJob } from "../../cron/types.js";
11+
import { normalizeHttpWebhookUrl } from "../../cron/webhook-url.js";
1112
import { sanitizeAgentId } from "../../routing/session-key.js";
1213
import { defaultRuntime } from "../../runtime.js";
1314
import type { GatewayRpcOpts } from "../gateway-rpc.js";
@@ -203,8 +204,12 @@ export function registerCronAddCommand(cron: Command) {
203204

204205
const hasAnnounce = Boolean(opts.announce) || opts.deliver === true;
205206
const hasNoDeliver = opts.deliver === false;
206-
const webhookUrl = normalizeOptionalString(opts.webhook);
207-
const hasWebhook = typeof opts.webhook === "string";
207+
const webhookUrl =
208+
typeof opts.webhook === "string" ? normalizeHttpWebhookUrl(opts.webhook) : null;
209+
if (typeof opts.webhook === "string" && !webhookUrl) {
210+
throw new Error("--webhook must be a valid http(s) URL");
211+
}
212+
const hasWebhook = Boolean(webhookUrl);
208213
const deliveryFlagCount = [hasAnnounce, hasNoDeliver, hasWebhook].filter(
209214
Boolean,
210215
).length;

‎src/cli/cron-cli/register.cron-edit-options.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const assignIf = (
2424
export async function resolveCronEditPayloadDeliveryPatch(
2525
opts: Record<string, unknown>,
2626
loadExistingJob: () => Promise<CronJob>,
27+
webhookUrl: string | undefined,
2728
): Promise<Record<string, unknown>> {
2829
const patch: Record<string, unknown> = {};
2930
const hasSystemEventPatch = typeof opts.systemEvent === "string";
@@ -89,7 +90,7 @@ export async function resolveCronEditPayloadDeliveryPatch(
8990
throw new Error("Invalid --script-tool-budget (must be a positive integer).");
9091
}
9192

92-
const hasWebhookDelivery = typeof opts.webhook === "string";
93+
const hasWebhookDelivery = Boolean(webhookUrl);
9394
const hasDeliveryModeFlag =
9495
opts.announce || typeof opts.deliver === "boolean" || hasWebhookDelivery;
9596
const threadId = parseCronThreadIdOption(opts.threadId);
@@ -275,8 +276,7 @@ export async function resolveCronEditPayloadDeliveryPatch(
275276
delivery.channel = channel ? channel : undefined;
276277
}
277278
if (hasWebhookDelivery) {
278-
const webhook = normalizeOptionalString(opts.webhook) ?? "";
279-
delivery.to = webhook ? webhook : undefined;
279+
delivery.to = webhookUrl;
280280
} else if (opts.clearTo) {
281281
delivery.to = null;
282282
} else if (typeof opts.to === "string") {

‎src/cli/cron-cli/register.cron-edit.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,10 @@ describe("cron edit command", () => {
10521052
exitSpy.mockRestore();
10531053
});
10541054

1055+
it.each(["", "not-a-url"])("rejects invalid --webhook %j before gateway RPC", async (value) => {
1056+
await expectCronEditRejection(["--webhook", value], "--webhook must be a valid http(s) URL");
1057+
});
1058+
10551059
it("documents the delivery clear flags alongside the sibling --clear-model", () => {
10561060
const editCommand = createCronProgram().commands.find((command) => command.name() === "edit");
10571061
const help = editCommand?.helpInformation() ?? "";

‎src/cli/cron-cli/register.cron-edit.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import type { Command } from "commander";
88
import { THINKING_LEVELS_HELP } from "../../auto-reply/thinking.shared.js";
99
import type { CronJob } from "../../cron/types.js";
10+
import { normalizeHttpWebhookUrl } from "../../cron/webhook-url.js";
1011
import { danger } from "../../globals.js";
1112
import { formatErrorMessage } from "../../infra/errors.js";
1213
import { sanitizeAgentId } from "../../routing/session-key.js";
@@ -221,7 +222,14 @@ export function registerCronEditCommand(cron: Command) {
221222
"--channel, --to, --account, and --thread-id require a non-main agentTurn or command job with delivery.",
222223
);
223224
}
224-
const hasWebhookDelivery = typeof opts.webhook === "string";
225+
const webhookUrl =
226+
typeof opts.webhook === "string"
227+
? (normalizeHttpWebhookUrl(opts.webhook) ?? undefined)
228+
: undefined;
229+
if (typeof opts.webhook === "string" && !webhookUrl) {
230+
throw new Error("--webhook must be a valid http(s) URL");
231+
}
232+
const hasWebhookDelivery = Boolean(webhookUrl);
225233
const deliveryModeFlagCount = [
226234
Boolean(opts.announce),
227235
typeof opts.deliver === "boolean",
@@ -413,7 +421,7 @@ export function registerCronEditCommand(cron: Command) {
413421

414422
Object.assign(
415423
patch,
416-
await resolveCronEditPayloadDeliveryPatch(opts, readExistingCronJob),
424+
await resolveCronEditPayloadDeliveryPatch(opts, readExistingCronJob, webhookUrl),
417425
);
418426

419427
const hasFailureAlertAfter = typeof opts.failureAlertAfter === "string";

0 commit comments

Comments
 (0)