mirror of
https://github.com/outline/outline.git
synced 2026-12-01 09:22:14 +03:00
* fix: Signed file downloads rejected when an API key lacks scope for the route Adds a skip option to the auth middleware so routes authorized by other means can ignore credentials on the request, and applies it to signed file requests. Also maps the get method to the read scope. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Restrict skip option to optional authentication Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
499 lines
13 KiB
TypeScript
499 lines
13 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 { 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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
originalUrl: "/api/auth.info",
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
originalUrl: "/api/documents.create",
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
originalUrl: "/api/users.info",
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
originalUrl: "/api/documents.create",
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
originalUrl: "/api/users.info",
|
|
request: {
|
|
url: "/users.info",
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
request: {
|
|
// @ts-expect-error mock request
|
|
get: vi.fn(() => null),
|
|
},
|
|
// @ts-expect-error mock cookies
|
|
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(
|
|
{
|
|
request: {
|
|
// @ts-expect-error mock 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(
|
|
{
|
|
request: {
|
|
// @ts-expect-error mock 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(
|
|
{
|
|
request: {
|
|
// @ts-expect-error mock 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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
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(
|
|
{
|
|
// @ts-expect-error mock request
|
|
request: {
|
|
get: vi.fn(() => `Bearer ${user.getSessionToken()}`),
|
|
},
|
|
query: {},
|
|
state,
|
|
cache: {},
|
|
},
|
|
next
|
|
);
|
|
expect(next).toHaveBeenCalled();
|
|
expect(state.auth.user.id).toEqual(user.id);
|
|
});
|
|
});
|
|
});
|