@csrf-armor/core
Version:
Framework-agnostic CSRF protection core functionality
1 lines • 4.5 kB
Source Map (JSON)
{"version":3,"file":"errors.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/**\n * Base error class for all CSRF-related errors.\n *\n * Provides structured error information including error codes and HTTP status codes\n * for proper error handling and logging in applications using CSRF protection.\n *\n * @public\n * @example\n * ```typescript\n * import { CsrfError } from '@csrf-armor/core';\n *\n * try {\n * await csrfProtection.protect(req, res);\n * } catch (error) {\n * if (error instanceof CsrfError) {\n * console.log(`CSRF Error [${error.code}]: ${error.message}`);\n * res.status(error.statusCode).json({ error: error.message });\n * }\n * }\n * ```\n */\nexport class CsrfError extends Error {\n /**\n * Creates a new CSRF error.\n *\n * @param message - Human-readable error description\n * @param code - Machine-readable error code for programmatic handling\n * @param statusCode - HTTP status code to return (defaults to 403 Forbidden)\n */\n constructor(\n message: string,\n public readonly code: string,\n public readonly statusCode: number = 403\n ) {\n super(message);\n this.name = 'CsrfError';\n }\n}\n\n/**\n * Error thrown when a CSRF token has expired.\n *\n * This typically occurs when a user has a page open for longer than the\n * configured token expiry time, or when server time has drifted significantly\n * from the time the token was generated.\n *\n * @public\n * @example\n * ```typescript\n * import { TokenExpiredError } from '@csrf-armor/core';\n *\n * try {\n * await csrfProtection.protect(req, res);\n * } catch (error) {\n * if (error instanceof TokenExpiredError) {\n * // Redirect to refresh the page and get a new token\n * res.redirect(req.url);\n * }\n * }\n * ```\n */\nexport class TokenExpiredError extends CsrfError {\n constructor() {\n super('CSRF token has expired', 'TOKEN_EXPIRED');\n }\n}\n\n/**\n * Error thrown when a CSRF token is malformed or invalid.\n *\n * This can occur due to:\n * - Corrupted token data during transmission\n * - Invalid token format or structure\n * - Failed cryptographic signature verification\n * - Tampered token content\n *\n * @public\n * @example\n * ```typescript\n * import { TokenInvalidError } from '@csrf-armor/core';\n *\n * try {\n * await csrfProtection.protect(req, res);\n * } catch (error) {\n * if (error instanceof TokenInvalidError) {\n * console.log('Invalid token received:', error.message);\n * // Generate and provide a new valid token\n * const newToken = await csrfProtection.generateToken();\n * res.status(400).json({ error: error.message, newToken });\n * }\n * }\n * ```\n */\nexport class TokenInvalidError extends CsrfError {\n /**\n * Creates a new token invalid error.\n *\n * @param reason - Specific reason why the token is invalid\n */\n constructor(reason = 'Invalid token format') {\n super(`CSRF token is invalid: ${reason}`, 'TOKEN_INVALID');\n }\n}\n\n/**\n * Error thrown when request origin doesn't match allowed origins.\n *\n * Used primarily by the `origin-check` strategy to validate that requests\n * are coming from authorized domains. This helps prevent CSRF attacks from\n * malicious websites.\n *\n * @public\n * @example\n * ```typescript\n * import { OriginMismatchError } from '@csrf-armor/core';\n *\n * try {\n * await csrfProtection.protect(req, res);\n * } catch (error) {\n * if (error instanceof OriginMismatchError) {\n * console.log('Blocked request from unauthorized origin:', error.message);\n * res.status(403).json({\n * error: 'Request from unauthorized origin',\n * origin: req.headers.origin\n * });\n * }\n * }\n * ```\n */\nexport class OriginMismatchError extends CsrfError {\n /**\n * Creates a new origin mismatch error.\n *\n * @param origin - The unauthorized origin that was detected\n */\n constructor(origin: string) {\n super(`Origin \"${origin}\" is not allowed`, 'ORIGIN_MISMATCH');\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAqBA,IAAa,YAAb,cAA+B,MAAM;;;;;;;;CAQnC,YACE,SACA,AAAgB,MAChB,AAAgB,aAAqB,KACrC;AACA,QAAM,QAAQ;EAHE;EACA;AAGhB,OAAK,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;AA0BhB,IAAa,oBAAb,cAAuC,UAAU;CAC/C,cAAc;AACZ,QAAM,0BAA0B,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BpD,IAAa,oBAAb,cAAuC,UAAU;;;;;;CAM/C,YAAY,SAAS,wBAAwB;AAC3C,QAAM,0BAA0B,UAAU,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6B9D,IAAa,sBAAb,cAAyC,UAAU;;;;;;CAMjD,YAAY,QAAgB;AAC1B,QAAM,WAAW,OAAO,mBAAmB,kBAAkB"}