Skip to content

Commit 7c536ee

Browse files
committed
fix(googlechat): block webhook startup without valid audience configuration
1 parent 665b478 commit 7c536ee

2 files changed

Lines changed: 79 additions & 4 deletions

File tree

‎extensions/googlechat/src/monitor.lifecycle.test.ts‎

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,30 @@ vi.mock("./runtime.js", () => ({
2727

2828
import { startGoogleChatMonitor } from "./monitor.js";
2929

30+
const configuredAccount = {
31+
accountId: "default",
32+
enabled: true,
33+
credentialSource: "config",
34+
config: {
35+
audienceType: "project-number",
36+
audience: "1234567890",
37+
},
38+
};
39+
3040
describe("Google Chat monitor lifecycle", () => {
3141
beforeEach(() => {
3242
vi.clearAllMocks();
3343
mocks.registerTarget.mockReturnValue(vi.fn());
3444
});
3545

36-
it("publishes ready after the webhook target is registered", async () => {
46+
it.each([
47+
{ audienceType: "app-url", audience: "https://chat.example.test/googlechat" },
48+
{ audienceType: "project-number", audience: "1234567890" },
49+
])("publishes ready after the $audienceType webhook target is registered", async (config) => {
3750
const statusSink = vi.fn();
3851

3952
const stop = await startGoogleChatMonitor({
40-
account: { accountId: "default", enabled: true, credentialSource: "config", config: {} },
53+
account: { ...configuredAccount, config },
4154
config: {},
4255
runtime: {},
4356
abortSignal: new AbortController().signal,
@@ -46,6 +59,7 @@ describe("Google Chat monitor lifecycle", () => {
4659
} as never);
4760

4861
expect(mocks.registerTarget).toHaveBeenCalledOnce();
62+
expect(mocks.registerTarget).toHaveBeenCalledWith(expect.objectContaining(config));
4963
expect(statusSink).toHaveBeenCalledWith({
5064
running: true,
5165
connected: true,
@@ -57,6 +71,54 @@ describe("Google Chat monitor lifecycle", () => {
5771
await stop();
5872
});
5973

74+
it.each([
75+
{ description: "both audience settings are missing", config: {} },
76+
{ description: "the audience is missing", config: { audienceType: "app-url" } },
77+
{
78+
description: "the audience is blank",
79+
config: { audienceType: "app-url", audience: " " },
80+
},
81+
{
82+
description: "the audience type is missing",
83+
config: { audience: "https://chat.example.test/googlechat" },
84+
},
85+
{
86+
description: "the audience type is unsupported",
87+
config: {
88+
audienceType: "unsupported",
89+
audience: "https://chat.example.test/googlechat",
90+
},
91+
},
92+
])("blocks startup when $description", async ({ config }) => {
93+
const statusSink = vi.fn();
94+
const runtime = { error: vi.fn() };
95+
96+
const stop = await startGoogleChatMonitor({
97+
account: { ...configuredAccount, config },
98+
config: {},
99+
runtime,
100+
abortSignal: new AbortController().signal,
101+
webhookPath: "/googlechat",
102+
statusSink,
103+
} as never);
104+
105+
expect(mocks.ingressStart).not.toHaveBeenCalled();
106+
expect(mocks.registerTarget).not.toHaveBeenCalled();
107+
expect(statusSink).toHaveBeenCalledWith({
108+
lifecycle: "blocked",
109+
terminalDisconnect: true,
110+
running: true,
111+
connected: false,
112+
webhookPath: undefined,
113+
lastError: expect.stringContaining("channels.googlechat.audienceType"),
114+
});
115+
expect(runtime.error).toHaveBeenCalledWith(
116+
expect.stringContaining("channels.googlechat.audience"),
117+
);
118+
await expect(stop()).resolves.toBeUndefined();
119+
expect(mocks.ingressStop).not.toHaveBeenCalled();
120+
});
121+
60122
it("stops ingress and rejects startup when the webhook route cannot bind", async () => {
61123
const statusSink = vi.fn();
62124
mocks.registerTarget.mockImplementationOnce(() => {
@@ -65,7 +127,7 @@ describe("Google Chat monitor lifecycle", () => {
65127

66128
await expect(
67129
startGoogleChatMonitor({
68-
account: { accountId: "default", enabled: true, credentialSource: "config", config: {} },
130+
account: configuredAccount,
69131
config: {},
70132
runtime: {},
71133
abortSignal: new AbortController().signal,

‎extensions/googlechat/src/monitor.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type ChannelBotLoopProtectionFacts,
77
type ChannelInboundMediaInput,
88
} from "openclaw/plugin-sdk/channel-inbound";
9-
import { channelReadyPatch } from "openclaw/plugin-sdk/gateway-runtime";
9+
import { channelBlockedPatch, channelReadyPatch } from "openclaw/plugin-sdk/gateway-runtime";
1010
import { mergePairLoopGuardConfig } from "openclaw/plugin-sdk/pair-loop-guard-runtime";
1111
import { normalizeOptionalLowercaseString } from "openclaw/plugin-sdk/string-coerce-runtime";
1212
import type { OpenClawConfig } from "../runtime-api.js";
@@ -497,6 +497,19 @@ async function monitorGoogleChatProvider(
497497

498498
const audienceType = normalizeAudienceType(options.account.config.audienceType);
499499
const audience = options.account.config.audience?.trim();
500+
if (!audienceType || !audience) {
501+
const error =
502+
"Google Chat webhook authentication requires channels.googlechat.audienceType and channels.googlechat.audience.";
503+
options.runtime.error?.(`[${options.account.accountId}] ${error}`);
504+
options.statusSink?.(
505+
channelBlockedPatch(error, {
506+
running: true,
507+
connected: false,
508+
webhookPath: undefined,
509+
}),
510+
);
511+
return async () => {};
512+
}
500513
const mediaMaxMb = options.account.config.mediaMaxMb ?? 20;
501514

502515
warnAppPrincipalMisconfiguration({

0 commit comments

Comments
 (0)