Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions extensions/googlechat/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export async function verifyGoogleChatRequest(params: {
bearer?: string | null;
audienceType?: GoogleChatAudienceType | null;
audience?: string | null;
expectedAddOnPrincipal?: string | null;
expectedPrincipal?: string | null;
}): Promise<{ ok: boolean; reason?: string }> {
const bearer = params.bearer?.trim();
if (!bearer) {
Expand All @@ -119,23 +119,20 @@ export async function verifyGoogleChatRequest(params: {
if (!payload?.email_verified) {
return { ok: false, reason: "email not verified" };
}
if (email === CHAT_ISSUER) {
return { ok: true };
}
if (!ADDON_ISSUER_PATTERN.test(email)) {
if (email !== CHAT_ISSUER && !ADDON_ISSUER_PATTERN.test(email)) {
return { ok: false, reason: `invalid issuer: ${email}` };
}
const expectedAddOnPrincipal = params.expectedAddOnPrincipal?.trim().toLowerCase();
if (!expectedAddOnPrincipal) {
return { ok: false, reason: "missing add-on principal binding" };
const expectedPrincipal = params.expectedPrincipal?.trim().toLowerCase();
if (!expectedPrincipal) {
return { ok: false, reason: "missing principal binding (appPrincipal config required)" };
}
const tokenPrincipal = String(payload?.sub ?? "")
.trim()
.toLowerCase();
if (!tokenPrincipal || tokenPrincipal !== expectedAddOnPrincipal) {
if (!tokenPrincipal || tokenPrincipal !== expectedPrincipal) {
return {
ok: false,
reason: `unexpected add-on principal: ${tokenPrincipal || "<missing>"}`,
reason: `unexpected principal: ${tokenPrincipal || "<missing>"}`,
};
}
return { ok: true };
Expand Down
2 changes: 1 addition & 1 deletion extensions/googlechat/src/monitor-webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe("googlechat monitor webhook", () => {
expect(verifyGoogleChatRequest).toHaveBeenCalledWith(
expect.objectContaining({
bearer: "addon-token",
expectedAddOnPrincipal: "chat-app",
expectedPrincipal: "chat-app",
}),
);
expect(processEvent).toHaveBeenCalledWith(
Expand Down
4 changes: 2 additions & 2 deletions extensions/googlechat/src/monitor-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function createGoogleChatWebhookRequestHandler(params: {
bearer: headerBearer,
audienceType: target.audienceType,
audience: target.audience,
expectedAddOnPrincipal: target.account.config.appPrincipal,
expectedPrincipal: target.account.config.appPrincipal,
});
return verification.ok;
},
Expand Down Expand Up @@ -176,7 +176,7 @@ export function createGoogleChatWebhookRequestHandler(params: {
bearer: parsed.addOnBearerToken,
audienceType: target.audienceType,
audience: target.audience,
expectedAddOnPrincipal: target.account.config.appPrincipal,
expectedPrincipal: target.account.config.appPrincipal,
});
return verification.ok;
},
Expand Down
53 changes: 48 additions & 5 deletions extensions/googlechat/src/targets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,65 @@ function mockTicket(payload: Record<string, unknown>) {
}

describe("verifyGoogleChatRequest", () => {
it("accepts Google Chat app-url tokens from the Chat issuer", async () => {
it("accepts Chat issuer tokens when principal matches", async () => {
mocks.verifyIdToken.mockReset();
mockTicket({
email: "chat@system.gserviceaccount.com",
email_verified: true,
sub: "12345",
});

await expect(
verifyGoogleChatRequest({
bearer: "token",
audienceType: "app-url",
audience: "https://example.com/googlechat",
expectedPrincipal: "12345",
}),
).resolves.toEqual({ ok: true });
});

it("rejects Chat issuer tokens when no principal binding is configured", async () => {
mocks.verifyIdToken.mockReset();
mockTicket({
email: "chat@system.gserviceaccount.com",
email_verified: true,
sub: "12345",
});

await expect(
verifyGoogleChatRequest({
bearer: "token",
audienceType: "app-url",
audience: "https://example.com/googlechat",
}),
).resolves.toEqual({
ok: false,
reason: "missing principal binding (appPrincipal config required)",
});
});

it("rejects Chat issuer tokens when principal does not match", async () => {
mocks.verifyIdToken.mockReset();
mockTicket({
email: "chat@system.gserviceaccount.com",
email_verified: true,
sub: "99999",
});

await expect(
verifyGoogleChatRequest({
bearer: "token",
audienceType: "app-url",
audience: "https://example.com/googlechat",
expectedPrincipal: "12345",
}),
).resolves.toEqual({
ok: false,
reason: "unexpected principal: 99999",
});
});

it("rejects add-on tokens when no principal binding is configured", async () => {
mocks.verifyIdToken.mockReset();
mockTicket({
Expand All @@ -240,7 +283,7 @@ describe("verifyGoogleChatRequest", () => {
}),
).resolves.toEqual({
ok: false,
reason: "missing add-on principal binding",
reason: "missing principal binding (appPrincipal config required)",
});
});

Expand All @@ -257,7 +300,7 @@ describe("verifyGoogleChatRequest", () => {
bearer: "token",
audienceType: "app-url",
audience: "https://example.com/googlechat",
expectedAddOnPrincipal: "principal-1",
expectedPrincipal: "principal-1",
}),
).resolves.toEqual({ ok: true });
});
Expand All @@ -275,11 +318,11 @@ describe("verifyGoogleChatRequest", () => {
bearer: "token",
audienceType: "app-url",
audience: "https://example.com/googlechat",
expectedAddOnPrincipal: "principal-1",
expectedPrincipal: "principal-1",
}),
).resolves.toEqual({
ok: false,
reason: "unexpected add-on principal: principal-2",
reason: "unexpected principal: principal-2",
});
});
});
Loading