mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +03:00
* chore: Rename APIContext to Context and flatten its fields Auth and transaction now live directly on the context instead of in ctx.state and a nested ctx.context, and model helpers read them directly. APIContext remains as a deprecated alias. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Remove deprecated APIContext alias Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * fix: Read auth from context in OAuth provider callbacks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Add withUser and withTransaction context helpers Replaces manual createContext rebuilds that copy fields from an existing context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Remove arbitrary property access from Koa context types AppContext and Context no longer inherit the any index signature from Koa's DefaultContext, custom fields are declared explicitly, and importing Koa's loose Context type is disallowed by lint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf --------- Co-authored-by: Claude <noreply@anthropic.com>
96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
import { randomBytes, createHmac } from "node:crypto";
|
|
import { CSRF } from "@shared/constants";
|
|
import { safeEqual } from "./crypto";
|
|
import type { AppContext } from "@server/types";
|
|
|
|
/**
|
|
* Returns the name of the cookie that carries the CSRF token for a request.
|
|
* Secure requests use the host-bound cookie, which a sibling subdomain cannot
|
|
* write. The name must be resolved the same way when attaching and verifying,
|
|
* otherwise the host-bound cookie can be substituted for the weaker one.
|
|
*
|
|
* @param ctx the request context.
|
|
* @returns the cookie name for this request.
|
|
*/
|
|
export const getCookieName = (ctx: Pick<AppContext, "request">): string =>
|
|
ctx.request.secure ? CSRF.secureCookieName : CSRF.cookieName;
|
|
|
|
/**
|
|
* Reads the CSRF token from the request cookies.
|
|
*
|
|
* @param ctx the request context.
|
|
* @returns the token, or undefined when no CSRF cookie is present.
|
|
*/
|
|
export const getTokenFromCookie = (
|
|
ctx: Pick<AppContext, "cookies" | "request">
|
|
): string | undefined => ctx.cookies.get(getCookieName(ctx));
|
|
|
|
/**
|
|
* Expires the CSRF cookie so that the next safe request mints a fresh token.
|
|
* Call this when the session changes hands, such as on sign in and sign out.
|
|
*
|
|
* @param ctx the request context.
|
|
*/
|
|
export const clearCSRFCookie = (
|
|
ctx: Pick<AppContext, "cookies" | "request">
|
|
): void => {
|
|
ctx.cookies.set(getCookieName(ctx), null, {
|
|
httpOnly: false,
|
|
sameSite: "lax",
|
|
secure: ctx.request.secure,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Generates cryptographically secure random bytes
|
|
*
|
|
* @param size The number of bytes to generate
|
|
* @returns A buffer containing random bytes
|
|
*/
|
|
export const generateRawToken = (size: number): Buffer => randomBytes(size);
|
|
|
|
/**
|
|
* Creates an HMAC-SHA256 signature for a token
|
|
*
|
|
* @param token The token to sign
|
|
* @param secret The secret key for signing
|
|
* @returns The HMAC signature as a hex string
|
|
*/
|
|
export const signToken = (token: Buffer, secret: string): string =>
|
|
createHmac("sha256", secret).update(token).digest("hex");
|
|
|
|
/**
|
|
* Bundles a token with its HMAC signature
|
|
*
|
|
* @param token The raw token
|
|
* @param secret The secret key for signing
|
|
* @returns A string containing the token and signature separated by a dot
|
|
*/
|
|
export const bundleToken = (token: Buffer, secret: string): string => {
|
|
const sig = signToken(token, secret);
|
|
return `${token.toString("hex")}.${sig}`;
|
|
};
|
|
|
|
/**
|
|
* Unbundles and verifies a token with its HMAC signature
|
|
*
|
|
* @param bundled The bundled token string
|
|
* @param secret The secret key for verification
|
|
* @returns An object indicating validity and the raw token if valid
|
|
*/
|
|
export const unbundleToken = (
|
|
bundled: string,
|
|
secret: string
|
|
): { valid: boolean; raw?: Buffer } => {
|
|
const [hex, sig] = bundled.split(".");
|
|
if (!hex || !sig) {
|
|
return { valid: false };
|
|
}
|
|
|
|
const token = Buffer.from(hex, "hex");
|
|
const expected = signToken(token, secret);
|
|
|
|
const valid = safeEqual(sig, expected);
|
|
return { valid, raw: valid ? token : undefined };
|
|
};
|