fix: Avoid issuing collab token to API clients (#13332)

* fix: Avoid issuing collab token to API clients

* Address feedback
This commit is contained in:
Tom Moor authored and GitHub committed 2026-08-07 08:44:20 -04:00
1 parent eb07bfebec
commit a00b64a4e6
2 files changed
+55 -5

No files matched your search

+45 -1
View File
@@ -1,6 +1,13 @@
import { faker } from "@faker-js/faker";
import { randomUUID } from "node:crypto";
import { buildUser, buildTeam, buildUserPasskey } from "@server/test/factories";
import { Scope } from "@shared/types";
import {
buildApiKey,
buildOAuthAuthentication,
buildUser,
buildTeam,
buildUserPasskey,
} from "@server/test/factories";
import { getTestServer, setSelfHosted } from "@server/test/support";
const mockTeamInSessionId = randomUUID();
@@ -44,6 +51,43 @@ describe("#auth.info", () => {
expect(body.data.team.allowedDomains).toEqual([]);
});
it("should return a collaboration token for a session", async () => {
const user = await buildUser();
const res = await server.post("/api/auth.info", user);
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.collaborationToken).toBeTruthy();
});
it("should not return a collaboration token for an API key", async () => {
const user = await buildUser();
const key = await buildApiKey({ userId: user.id });
const res = await server.post("/api/auth.info", {
headers: {
Authorization: `Bearer ${key.value}`,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.collaborationToken).toBeUndefined();
});
it("should not return a collaboration token for an OAuth access token", async () => {
const user = await buildUser();
const authentication = await buildOAuthAuthentication({
user,
scope: [Scope.Read],
});
const res = await server.post("/api/auth.info", {
headers: {
Authorization: `Bearer ${authentication.accessToken}`,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.collaborationToken).toBeUndefined();
});
it("should require the team to not be deleted", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
+10 -4
View File
@@ -19,6 +19,7 @@ import {
} from "@server/presenters";
import ValidateSSOAccessTask from "@server/queues/tasks/ValidateSSOAccessTask";
import type { APIContext } from "@server/types";
import { AuthenticationType } from "@server/types";
import { getSessionsInCookie } from "@server/utils/authentication";
import RateLimiter from "@server/utils/RateLimiter";
import type * as T from "./schema";
@@ -119,7 +120,7 @@ router.post("auth.config", async (ctx: APIContext<T.AuthConfigReq>) => {
const NON_SSO_SERVICES = ["email", "passkeys"];
router.post("auth.info", auth(), async (ctx: APIContext<T.AuthInfoReq>) => {
const { user, service } = ctx.state.auth;
const { user, service, type } = ctx.state.auth;
const sessions = getSessionsInCookie(ctx);
const signedInTeamIds = Object.keys(sessions);
@@ -140,9 +141,9 @@ router.post("auth.info", auth(), async (ctx: APIContext<T.AuthInfoReq>) => {
// to have access to the workspace they are signed into. This only applies
// to SSO sessions - email and passkey logins don't have associated
// UserAuthentication records that need validation.
const isOAuthSession = !service || !NON_SSO_SERVICES.includes(service);
const requiresSSOValidation = !service || !NON_SSO_SERVICES.includes(service);
if (
isOAuthSession &&
requiresSSOValidation &&
user.lastSignedInAt &&
user.lastSignedInAt < subHours(new Date(), 1)
) {
@@ -168,7 +169,12 @@ router.post("auth.info", auth(), async (ctx: APIContext<T.AuthInfoReq>) => {
team: presentTeam(team),
groups: await Promise.all(groups.map(presentGroup)),
groupUsers: groups.map((group) => presentGroupUser(group.groupUsers[0])),
collaborationToken: user.getCollaborationToken(),
// The collaboration token is only for the client and should not be issued
// to API or OAuth consumers
collaborationToken:
type === AuthenticationType.APP
? user.getCollaborationToken()
: undefined,
availableTeams: uniqBy([...signedInTeams, ...availableTeams], "id").map(
(availableTeam) =>
presentAvailableTeam(