mirror of
https://github.com/outline/outline.git
synced 2026-11-28 00:09:58 +03:00
* chore: Prune redundant comments and normalize lint directives Removes comments that restate the following line and stale TODOs, drops the passive event listener feature check now that all supported browsers support it, and renames eslint directives to oxlint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH * fix: Evaluate restricted team domains at validation time The restricted email provider list was captured when the model loaded, so it could not follow the hosting environment and the tests that covered it never reached the model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH * chore: Replace type suppressions with accurate types Adds a Concrete helper type for registries of abstract classes, declares missing Sequelize and refractor typings, uses public ProseMirror APIs, a typed mock context helper in middleware tests, and a StaticLink component for sidebar rows being edited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH --------- Co-authored-by: Claude <noreply@anthropic.com>
480 lines
12 KiB
TypeScript
480 lines
12 KiB
TypeScript
import type { DefaultState } from "koa";
|
|
import { errToString } from "@shared/utils/error";
|
|
import { randomString } from "@shared/random";
|
|
import { Scope } from "@shared/types";
|
|
import {
|
|
buildUser,
|
|
buildTeam,
|
|
buildAdmin,
|
|
buildApiKey,
|
|
buildOAuthAuthentication,
|
|
} from "@server/test/factories";
|
|
import { buildMockContext } from "@server/test/support";
|
|
import { AuthenticationType } from "@server/types";
|
|
import auth from "./authentication";
|
|
|
|
describe("Authentication middleware", () => {
|
|
describe("with session JWT", () => {
|
|
it("should authenticate with correct session token", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${user.getSessionToken()}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
expect(state.auth.user.id).toEqual(user.id);
|
|
});
|
|
|
|
it("should return error with invalid session token", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
|
|
try {
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${user.getSessionToken()}error`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
} catch (e) {
|
|
expect(errToString(e)).toBe("Invalid token");
|
|
}
|
|
});
|
|
|
|
it("should return error if AuthenticationType mismatches", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth({
|
|
type: AuthenticationType.API,
|
|
});
|
|
|
|
try {
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${user.getSessionToken()}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
} catch (e) {
|
|
expect(errToString(e)).toBe("Invalid authentication type");
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("with API key", () => {
|
|
it("should authenticate user with valid API key", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
const key = await buildApiKey({ userId: user.id });
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${key.value}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
expect(state.auth.user.id).toEqual(user.id);
|
|
});
|
|
it("should authenticate with global read scope on read endpoints", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
const key = await buildApiKey({
|
|
userId: user.id,
|
|
scope: [Scope.Read],
|
|
});
|
|
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
originalUrl: "/api/auth.info",
|
|
request: {
|
|
url: "/auth.info",
|
|
get: vi.fn(() => `Bearer ${key.value}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
expect(state.auth.user.id).toEqual(user.id);
|
|
});
|
|
|
|
it("should return 403 authorization error when scope does not match", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
const key = await buildApiKey({
|
|
userId: user.id,
|
|
scope: [Scope.Read],
|
|
});
|
|
|
|
try {
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
originalUrl: "/api/documents.create",
|
|
request: {
|
|
url: "/documents.create",
|
|
get: vi.fn(() => `Bearer ${key.value}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
throw new Error("Expected error to be thrown");
|
|
} catch (e) {
|
|
expect(e).toHaveProperty("status", 403);
|
|
expect(e).toHaveProperty("id", "authorization_error");
|
|
expect(errToString(e)).toContain(
|
|
"does not have access to this resource"
|
|
);
|
|
}
|
|
});
|
|
|
|
it("should return error with invalid API key", async () => {
|
|
const state = {} as DefaultState;
|
|
const authMiddleware = auth();
|
|
|
|
try {
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${randomString(38)}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
} catch (e) {
|
|
expect(errToString(e)).toBe("Invalid API key");
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("with OAuth access token", () => {
|
|
it("should authenticate user with valid OAuth access token", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
const authentication = await buildOAuthAuthentication({
|
|
user,
|
|
scope: [Scope.Read],
|
|
});
|
|
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
originalUrl: "/api/users.info",
|
|
request: {
|
|
url: "/users.info",
|
|
get: vi.fn(() => `Bearer ${authentication.accessToken}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
expect(state.auth.user.id).toEqual(user.id);
|
|
});
|
|
|
|
it("should return error with invalid scope", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
const authentication = await buildOAuthAuthentication({
|
|
user,
|
|
scope: [Scope.Read],
|
|
});
|
|
|
|
try {
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
originalUrl: "/api/documents.create",
|
|
request: {
|
|
url: "/documents.create",
|
|
get: vi.fn(() => `Bearer ${authentication.accessToken}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
} catch (e) {
|
|
expect(errToString(e)).toContain(
|
|
"does not have access to this resource"
|
|
);
|
|
}
|
|
});
|
|
|
|
it("should return error with OAuth access token in body", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
const authentication = await buildOAuthAuthentication({
|
|
user,
|
|
scope: [Scope.Read],
|
|
});
|
|
try {
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
originalUrl: "/api/users.info",
|
|
request: {
|
|
url: "/users.info",
|
|
get: vi.fn(() => null),
|
|
body: {
|
|
token: authentication.accessToken,
|
|
},
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
} catch (e) {
|
|
expect(errToString(e)).toContain(
|
|
"must be passed in the Authorization header"
|
|
);
|
|
}
|
|
});
|
|
});
|
|
|
|
it("should return error message if no auth token is available", async () => {
|
|
const state = {} as DefaultState;
|
|
const authMiddleware = auth();
|
|
|
|
try {
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => "error"),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
} catch (e) {
|
|
expect(errToString(e)).toBe(
|
|
'Bad Authorization header format. Format is "Authorization: Bearer <token>"'
|
|
);
|
|
}
|
|
});
|
|
|
|
it("should allow passing session token in a cookie", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => null),
|
|
},
|
|
cookies: {
|
|
get: vi.fn(() => user.getSessionToken()),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
expect(state.auth.user.id).toEqual(user.id);
|
|
});
|
|
|
|
it("should return error with session token as a GET param", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
|
|
await expect(
|
|
authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => null),
|
|
query: {
|
|
token: user.getSessionToken(),
|
|
},
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
)
|
|
).rejects.toThrow(
|
|
"Session token must be passed in the cookie or Authorization header"
|
|
);
|
|
});
|
|
|
|
it("should return error with session token in body params", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
|
|
await expect(
|
|
authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => null),
|
|
body: {
|
|
token: user.getSessionToken(),
|
|
},
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
)
|
|
).rejects.toThrow(
|
|
"Session token must be passed in the cookie or Authorization header"
|
|
);
|
|
});
|
|
|
|
it("should allow passing transfer token as a GET param", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const authMiddleware = auth();
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => null),
|
|
query: {
|
|
token: user.getTransferToken(),
|
|
},
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
expect(state.auth.user.id).toEqual(user.id);
|
|
});
|
|
|
|
it("should return an error for suspended users", async () => {
|
|
const state = {} as DefaultState;
|
|
const admin = await buildAdmin();
|
|
const user = await buildUser({
|
|
suspendedAt: new Date(),
|
|
suspendedById: admin.id,
|
|
});
|
|
const authMiddleware = auth();
|
|
let error;
|
|
|
|
try {
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${user.getSessionToken()}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
|
|
expect(errToString(error)).toEqual(
|
|
"Your access has been suspended by a workspace admin"
|
|
);
|
|
expect(error).toHaveProperty("errorData.adminEmail", admin.email);
|
|
});
|
|
|
|
it("should return an error for deleted team", async () => {
|
|
const state = {} as DefaultState;
|
|
const team = await buildTeam();
|
|
const user = await buildUser({ teamId: team.id });
|
|
await team.destroy();
|
|
const authMiddleware = auth();
|
|
let error;
|
|
|
|
try {
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${user.getSessionToken()}`),
|
|
},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
vi.fn()
|
|
);
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
|
|
expect(errToString(error)).toEqual("Invalid token");
|
|
});
|
|
describe("with skip", () => {
|
|
it("should ignore credentials when skip returns true", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const apiKey = await buildApiKey({
|
|
userId: user.id,
|
|
scope: ["/api/documents.info"],
|
|
});
|
|
const next = vi.fn();
|
|
const authMiddleware = auth({
|
|
optional: true,
|
|
skip: (ctx) => !!ctx.query.sig,
|
|
});
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${apiKey.value}`),
|
|
},
|
|
query: { sig: "signature" },
|
|
originalUrl: "/api/files.get?sig=signature",
|
|
state,
|
|
cache: {},
|
|
}),
|
|
next
|
|
);
|
|
expect(next).toHaveBeenCalled();
|
|
expect(state.auth).toEqual({});
|
|
});
|
|
|
|
it("should authenticate when skip returns false", async () => {
|
|
const state = {} as DefaultState;
|
|
const user = await buildUser();
|
|
const next = vi.fn();
|
|
const authMiddleware = auth({
|
|
optional: true,
|
|
skip: (ctx) => !!ctx.query.sig,
|
|
});
|
|
await authMiddleware(
|
|
buildMockContext({
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${user.getSessionToken()}`),
|
|
},
|
|
query: {},
|
|
state,
|
|
cache: {},
|
|
}),
|
|
next
|
|
);
|
|
expect(next).toHaveBeenCalled();
|
|
expect(state.auth.user.id).toEqual(user.id);
|
|
});
|
|
});
|
|
});
|