Files
Tom MoorandClaude Fable 5.1 a577d5c2c8 fix: Validate API key and OAuth scopes at the model layer (#13664)
* fix: Validate API key scopes with Sequelize-level validators

* test: Add API regression tests for duplicate OAuth redirect URIs

* fix: Preserve wildcard scope and guard non-string redirect URIs

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-04 23:10:32 -04:00

79 lines
2.2 KiB
TypeScript

import { z } from "zod";
import { ApiKey } from "@server/models";
import { BaseSchema } from "@server/routes/api/schema";
import AuthenticationHelper from "@shared/helpers/AuthenticationHelper";
import { Scope } from "@shared/types";
import { ApiKeyValidation } from "@shared/validations";
const globalScopes = new Set<string>([...Object.values(Scope), "*"]);
/**
* Normalizes a user-supplied scope into its canonical form by prefixing bare
* route scopes with `/api/`.
*/
const normalizeScope = (scope: string) =>
scope.startsWith("/api/") || scope.includes(":") || globalScopes.has(scope)
? scope
: `/api/${scope.replace(/^\//, "")}`;
export const APIKeysCreateSchema = BaseSchema.extend({
body: z.object({
/** API Key name */
name: z
.string()
.trim()
.min(ApiKeyValidation.minNameLength)
.max(ApiKeyValidation.maxNameLength),
/** API Key expiry date */
expiresAt: z.coerce.date().optional(),
/** A list of scopes that this API key has access to */
scope: z
.array(
z
.string()
.trim()
.transform(normalizeScope)
.refine((scope) => AuthenticationHelper.isValidScope(scope), {
error: "Scope must be a valid API scope",
})
)
.optional(),
}),
});
export type APIKeysCreateReq = z.infer<typeof APIKeysCreateSchema>;
export const APIKeysListSchema = BaseSchema.extend({
body: z.object({
/** The owner of the API key */
userId: z.uuid().optional(),
/** Search query to filter API keys by name */
query: z.string().optional(),
/** API keys sorting direction */
direction: z
.string()
.optional()
.transform((val) => (val !== "ASC" ? "DESC" : val)),
/** API keys sorting column */
sort: z
.string()
.refine((val) => Object.keys(ApiKey.getAttributes()).includes(val), {
error: "Invalid sort parameter",
})
.prefault("createdAt"),
}),
});
export type APIKeysListReq = z.infer<typeof APIKeysListSchema>;
export const APIKeysDeleteSchema = BaseSchema.extend({
body: z.object({
/** API Key Id */
id: z.uuid(),
}),
});
export type APIKeysDeleteReq = z.infer<typeof APIKeysDeleteSchema>;