Skip to content

Commit 7875c64

Browse files
committed
fix(feishu): require canonical webhook paths before runtime startup
1 parent 3715e70 commit 7875c64

4 files changed

Lines changed: 57 additions & 29 deletions

File tree

‎extensions/feishu/src/config-schema.test.ts‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,27 @@ describe("FeishuConfigSchema webhook validation", () => {
7474
["/legacy%ZZ", "/legacy%ZZ"],
7575
["", "/feishu/events"],
7676
[" ", "/feishu/events"],
77-
])("canonicalizes root and account webhook path %j to %j", (webhookPath, expectedPath) => {
78-
const result = FeishuConfigSchema.parse({
79-
webhookPath,
80-
accounts: { main: { webhookPath } },
81-
});
77+
])("accepts only canonical root and account webhook path %j", (webhookPath, canonicalPath) => {
78+
if (webhookPath === canonicalPath) {
79+
const result = FeishuConfigSchema.parse({
80+
webhookPath,
81+
accounts: { main: { webhookPath } },
82+
});
83+
expect(result.webhookPath).toBe(webhookPath);
84+
expect(result.accounts?.main?.webhookPath).toBe(webhookPath);
85+
return;
86+
}
8287

83-
expect(result.webhookPath).toBe(expectedPath);
84-
expect(result.accounts?.main?.webhookPath).toBe(expectedPath);
88+
for (const [input, issuePath] of [
89+
[{ webhookPath }, "webhookPath"],
90+
[{ accounts: { main: { webhookPath } } }, "accounts.main.webhookPath"],
91+
] as const) {
92+
const result = FeishuConfigSchema.safeParse(input);
93+
expectSchemaIssue(result, issuePath);
94+
if (!result.success) {
95+
expect(result.error.issues[0]?.message).toContain("openclaw doctor --fix");
96+
}
97+
}
8598
});
8699

87100
it.each([
@@ -98,11 +111,14 @@ describe("FeishuConfigSchema webhook validation", () => {
98111
);
99112
});
100113

101-
it("canonicalizes accepted webhook input through the exported runtime parser", () => {
114+
it("rejects legacy webhook input while preserving the exported canonical default", () => {
102115
expect(
103116
FeishuChannelConfigSchema.runtime?.safeParse({
104117
webhookPath: "https://example.com/hook/?tenant=alpha#fragment",
105118
}),
119+
).toMatchObject({ success: false });
120+
expect(
121+
FeishuChannelConfigSchema.runtime?.safeParse({ webhookPath: "/hook/?tenant=alpha" }),
106122
).toMatchObject({ success: true, data: { webhookPath: "/hook/?tenant=alpha" } });
107123
expect(FeishuChannelConfigSchema.schema).toMatchObject({
108124
properties: {

‎extensions/feishu/src/config-schema.ts‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,12 @@ const FeishuDomainSchema = z.union([
2929
z.string().url().startsWith("https://"),
3030
]);
3131
const FeishuConnectionModeSchema = z.enum(["websocket", "webhook"]);
32-
const FeishuWebhookPathSchema = z.string().transform((value, ctx) => {
33-
const path = normalizeFeishuWebhookPath(value);
34-
if (path !== null) {
35-
return path;
36-
}
37-
ctx.addIssue({
38-
code: z.ZodIssueCode.custom,
32+
const FeishuWebhookPathSchema = z
33+
.string()
34+
.refine((value) => normalizeFeishuWebhookPath(value) === value, {
3935
message:
40-
'webhookPath must be a valid HTTP(S) URL or route path; run "openclaw doctor --fix" to repair it',
36+
'webhookPath must be a canonical HTTP request path; run "openclaw doctor --fix" to repair it',
4137
});
42-
return z.NEVER;
43-
});
4438
const TtsOverrideSchema = z
4539
.object({
4640
auto: z.enum(["off", "always", "inbound", "tagged"]).optional(),
@@ -269,7 +263,7 @@ const FeishuConfigSchemaBase = z
269263
verificationToken: buildSecretInputSchema().optional(),
270264
domain: FeishuDomainSchema.optional().default("feishu"),
271265
connectionMode: FeishuConnectionModeSchema.optional().default("websocket"),
272-
webhookPath: FeishuWebhookPathSchema.optional().prefault(DEFAULT_FEISHU_WEBHOOK_PATH),
266+
webhookPath: FeishuWebhookPathSchema.optional().default(DEFAULT_FEISHU_WEBHOOK_PATH),
273267
...FeishuSharedConfigShape,
274268
dmPolicy: DmPolicySchema.optional().default("pairing"),
275269
reactionNotifications: ReactionNotificationModeSchema.optional().default("own"),

‎extensions/feishu/src/monitor.transport.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
wsClients,
2828
} from "./monitor.state.js";
2929
import type { ResolvedFeishuAccount } from "./types.js";
30-
import { normalizeFeishuWebhookPath } from "./webhook-path.js";
30+
import { DEFAULT_FEISHU_WEBHOOK_PATH, normalizeFeishuWebhookPath } from "./webhook-path.js";
3131

3232
type MonitorTransportParams = {
3333
account: ResolvedFeishuAccount;
@@ -373,10 +373,10 @@ export async function monitorWebhook({
373373
}
374374

375375
const port = account.config.webhookPort ?? 3000;
376-
const path = normalizeFeishuWebhookPath(account.config.webhookPath);
377-
if (path === null) {
376+
const path = account.config.webhookPath ?? DEFAULT_FEISHU_WEBHOOK_PATH;
377+
if (normalizeFeishuWebhookPath(path) !== path) {
378378
throw new Error(
379-
`Feishu account "${accountId}" webhookPath must be a valid HTTP(S) URL or route path; ` +
379+
`Feishu account "${accountId}" webhookPath must be a canonical HTTP request path; ` +
380380
'run "openclaw doctor --fix" to repair it',
381381
);
382382
}

‎extensions/feishu/src/monitor.webhook-e2e.test.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Lark from "@larksuiteoapi/node-sdk";
66
import { expectDefined } from "@openclaw/normalization-core";
77
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
88
import { resolveFeishuRuntimeAccount } from "./accounts.js";
9+
import { normalizeCompatibilityConfig } from "./doctor-contract.js";
910
import { createFeishuRuntimeMockModule } from "./monitor.test-mocks.js";
1011
import {
1112
buildWebhookConfig,
@@ -596,7 +597,7 @@ describe("Feishu webhook signed-request e2e", () => {
596597
["empty root", "root", "", "/feishu/events"],
597598
["whitespace account", "account", " ", "/feishu/events"],
598599
])(
599-
"canonicalizes the configured %s before raw webhook admission",
600+
"requires Doctor to canonicalize the configured %s before raw webhook admission",
600601
async (_label, scope, configuredPath, acceptedTarget) => {
601602
const accountId = `legacy-route-${scope}`;
602603
const port = await getFreePort();
@@ -619,11 +620,11 @@ describe("Feishu webhook signed-request e2e", () => {
619620
},
620621
},
621622
};
622-
const account = resolveFeishuRuntimeAccount(
623+
const unmigratedAccount = resolveFeishuRuntimeAccount(
623624
{ cfg: config, accountId },
624625
{ requireEventSecrets: true },
625626
);
626-
expect(account.config.webhookPath).toBe(configuredPath);
627+
expect(unmigratedAccount.config.webhookPath).toBe(configuredPath);
627628

628629
const handler = vi.fn(async () => ({ accepted: true }));
629630
const eventDispatcher = new Lark.EventDispatcher({
@@ -633,14 +634,31 @@ describe("Feishu webhook signed-request e2e", () => {
633634
eventDispatcher.register({ "test.legacy_route_boundary": handler });
634635
const statusSink = vi.fn();
635636
const abortController = new AbortController();
636-
const monitorPromise = monitorWebhook({
637-
account,
637+
const monitorParams = {
638+
account: unmigratedAccount,
638639
accountId,
639640
runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() },
640641
abortSignal: abortController.signal,
641642
eventDispatcher,
642643
statusSink,
643-
});
644+
};
645+
const needsMigration = configuredPath !== acceptedTarget;
646+
if (needsMigration) {
647+
await expect(monitorWebhook(monitorParams)).rejects.toThrow("openclaw doctor --fix");
648+
expect(httpServers.has(accountId)).toBe(false);
649+
expect(handler).not.toHaveBeenCalled();
650+
expect(statusSink).not.toHaveBeenCalled();
651+
}
652+
const migrated = normalizeCompatibilityConfig({ cfg: config });
653+
expect(migrated.changes.some((change) => change.includes(".webhookPath"))).toBe(
654+
needsMigration,
655+
);
656+
const account = resolveFeishuRuntimeAccount(
657+
{ cfg: migrated.config, accountId },
658+
{ requireEventSecrets: true },
659+
);
660+
expect(account.config.webhookPath).toBe(acceptedTarget);
661+
const monitorPromise = monitorWebhook({ ...monitorParams, account });
644662
const rawBody = JSON.stringify({
645663
schema: "2.0",
646664
header: { event_type: "test.legacy_route_boundary" },

0 commit comments

Comments
 (0)