Synology Chat: reject duplicate webhook path ownership

This commit is contained in:
Vincent Koc 2026-03-14 20:02:27 -07:00
parent db20141993
commit c52cccdc40
3 changed files with 76 additions and 2 deletions

View File

@ -94,3 +94,20 @@ export function resolveAccount(cfg: any, accountId?: string | null): ResolvedSyn
allowInsecureSsl: accountOverride.allowInsecureSsl ?? channelCfg.allowInsecureSsl ?? false,
};
}
export function findConflictingWebhookPathAccountIds(cfg: any, accountId: string): string[] {
const current = resolveAccount(cfg, accountId);
const currentPath = current.webhookPath.trim();
if (!current.enabled || !current.token.trim() || !currentPath) {
return [];
}
return listAccountIds(cfg)
.filter((candidateId) => candidateId !== accountId)
.map((candidateId) => resolveAccount(cfg, candidateId))
.filter(
(candidate) =>
candidate.enabled && candidate.token.trim() && candidate.webhookPath.trim() === currentPath,
)
.map((candidate) => candidate.accountId);
}

View File

@ -334,5 +334,46 @@ describe("createSynologyChatPlugin", () => {
abortSecond.abort();
await Promise.allSettled([firstPromise, secondPromise]);
});
it("refuses to register when another enabled account resolves to the same webhook path", async () => {
const registerMock = registerPluginHttpRouteMock;
registerMock.mockClear();
const plugin = createSynologyChatPlugin();
const abortController = new AbortController();
const log = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const result = plugin.gateway.startAccount({
cfg: {
channels: {
"synology-chat": {
enabled: true,
token: "base-token",
incomingUrl: "https://nas/incoming",
webhookPath: "/webhook/synology/shared",
dmPolicy: "allowlist",
allowedUserIds: ["owner-a"],
accounts: {
alerts: {
enabled: true,
token: "alerts-token",
incomingUrl: "https://nas/incoming-alerts",
webhookPath: "/webhook/synology/shared",
dmPolicy: "open",
},
},
},
},
},
accountId: "default",
log,
abortSignal: abortController.signal,
});
await expectPendingStartAccountPromise(result, abortController);
expect(registerMock).not.toHaveBeenCalled();
expect(log.error).toHaveBeenCalledWith(
expect.stringContaining("Each enabled account must use a unique webhookPath."),
);
});
});
});

View File

@ -11,7 +11,11 @@ import {
buildChannelConfigSchema,
} from "openclaw/plugin-sdk/synology-chat";
import { z } from "zod";
import { listAccountIds, resolveAccount } from "./accounts.js";
import {
findConflictingWebhookPathAccountIds,
listAccountIds,
resolveAccount,
} from "./accounts.js";
import { sendMessage, sendFileUrl } from "./client.js";
import { getSynologyRuntime } from "./runtime.js";
import type { ResolvedSynologyChatAccount } from "./types.js";
@ -249,6 +253,19 @@ export function createSynologyChatPlugin() {
);
return waitUntilAbort(ctx.abortSignal);
}
const conflictingAccounts = findConflictingWebhookPathAccountIds(
ctx.cfg,
account.accountId,
);
if (conflictingAccounts.length > 0) {
log?.error?.(
`Conflicting webhookPath ${account.webhookPath} for Synology Chat accounts: ${[
account.accountId,
...conflictingAccounts,
].join(", ")}. Each enabled account must use a unique webhookPath.`,
);
return waitUntilAbort(ctx.abortSignal);
}
log?.info?.(
`Starting Synology Chat channel (account: ${accountId}, path: ${account.webhookPath})`,
@ -325,7 +342,6 @@ export function createSynologyChatPlugin() {
const unregister = registerPluginHttpRoute({
path: account.webhookPath,
auth: "plugin",
replaceExisting: true,
pluginId: CHANNEL_ID,
accountId: account.accountId,
log: (msg: string) => log?.info?.(msg),