{"version":3,"file":"validate-BpwoDOgt.mjs","names":[],"sources":["../src/database/validate.ts"],"sourcesContent":["/**\n * SQL Identifier Validation\n *\n * Validates identifiers (table names, column names, index names) before\n * they are used in raw SQL expressions. This is the primary defense against\n * SQL injection via dynamic identifier interpolation.\n *\n * @see AGENTS.md § Database: Never Interpolate Into SQL\n */\n\n/**\n * Pattern for safe SQL identifiers.\n * Must start with a lowercase letter, followed by lowercase letters, digits, or underscores.\n */\nconst IDENTIFIER_PATTERN = /^[a-z][a-z0-9_]*$/;\n\n/**\n * Pattern for generic alphanumeric identifiers (case-insensitive).\n * Must start with a letter, followed by letters, digits, or underscores.\n */\nconst GENERIC_IDENTIFIER_PATTERN = /^[a-zA-Z][a-zA-Z0-9_]*$/;\n\n/**\n * Pattern for plugin identifiers.\n * Must start with a lowercase letter, followed by lowercase letters, digits, underscores, or hyphens.\n */\nconst PLUGIN_IDENTIFIER_PATTERN = /^[a-z][a-z0-9_-]*$/;\n\n/**\n * Pattern for plugin storage collection names.\n * Manifests declare these as free-form keys, so both cases and hyphens are\n * allowed; the charset still excludes quotes, whitespace, and punctuation.\n */\nconst STORAGE_COLLECTION_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/;\n\n/**\n * Maximum length for SQL identifiers.\n * SQLite has no formal limit, but we cap at 128 for sanity.\n */\nconst MAX_IDENTIFIER_LENGTH = 128;\n\n/**\n * Error thrown when an identifier fails validation.\n */\nexport class IdentifierError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic identifier: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"IdentifierError\";\n\t}\n}\n\n/**\n * Validate that a string is a safe SQL identifier.\n *\n * Safe identifiers match `/^[a-z][a-z0-9_]*$/` and are at most 128 characters.\n * This prevents SQL injection when identifiers must be interpolated into raw SQL\n * (e.g., dynamic table names, column names in json_extract paths).\n *\n * @param value - The string to validate\n * @param label - Human-readable label for error messages (e.g., \"field name\", \"table name\")\n * @throws {IdentifierError} If the value is not a valid identifier\n *\n * @example\n * ```typescript\n * validateIdentifier(fieldName, \"field name\");\n * // safe to use in: json_extract(data, '$.${fieldName}')\n * ```\n */\nexport function validateIdentifier(value: string, label = \"identifier\"): void {\n\tif (!value || typeof value !== \"string\") {\n\t\tthrow new IdentifierError(`${label} must be a non-empty string`, String(value));\n\t}\n\n\tif (value.length > MAX_IDENTIFIER_LENGTH) {\n\t\tthrow new IdentifierError(\n\t\t\t`${label} must be ${MAX_IDENTIFIER_LENGTH} characters or less, got ${value.length}`,\n\t\t\tvalue,\n\t\t);\n\t}\n\n\tif (!IDENTIFIER_PATTERN.test(value)) {\n\t\tthrow new IdentifierError(`${label} must match /^[a-z][a-z0-9_]*$/ (got \"${value}\")`, value);\n\t}\n}\n\n/**\n * Validate that a string is a safe JSON field name for use in json_extract paths.\n *\n * More permissive than `validateIdentifier` — allows camelCase (mixed case)\n * since JSON keys in plugin storage data blobs commonly use camelCase.\n * Matches `/^[a-zA-Z][a-zA-Z0-9_]*$/`.\n *\n * @param value - The string to validate\n * @param label - Human-readable label for error messages\n * @throws {IdentifierError} If the value is not valid\n */\nexport function validateJsonFieldName(value: string, label = \"JSON field name\"): void {\n\tif (!value || typeof value !== \"string\") {\n\t\tthrow new IdentifierError(`${label} must be a non-empty string`, String(value));\n\t}\n\n\tif (value.length > MAX_IDENTIFIER_LENGTH) {\n\t\tthrow new IdentifierError(\n\t\t\t`${label} must be ${MAX_IDENTIFIER_LENGTH} characters or less, got ${value.length}`,\n\t\t\tvalue,\n\t\t);\n\t}\n\n\tif (!GENERIC_IDENTIFIER_PATTERN.test(value)) {\n\t\tthrow new IdentifierError(\n\t\t\t`${label} must match /^[a-zA-Z][a-zA-Z0-9_]*$/ (got \"${value}\")`,\n\t\t\tvalue,\n\t\t);\n\t}\n}\n\n/**\n * Validate that a string is a safe SQL identifier, allowing hyphens.\n *\n * Like `validateIdentifier` but also permits hyphens, which appear in\n * plugin IDs (e.g., \"my-plugin\"). Matches `/^[a-z][a-z0-9_-]*$/`.\n *\n * @param value - The string to validate\n * @param label - Human-readable label for error messages\n * @throws {IdentifierError} If the value is not valid\n */\nexport function validatePluginIdentifier(value: string, label = \"plugin identifier\"): void {\n\tif (!value || typeof value !== \"string\") {\n\t\tthrow new IdentifierError(`${label} must be a non-empty string`, String(value));\n\t}\n\n\tif (value.length > MAX_IDENTIFIER_LENGTH) {\n\t\tthrow new IdentifierError(\n\t\t\t`${label} must be ${MAX_IDENTIFIER_LENGTH} characters or less, got ${value.length}`,\n\t\t\tvalue,\n\t\t);\n\t}\n\n\tif (!PLUGIN_IDENTIFIER_PATTERN.test(value)) {\n\t\tthrow new IdentifierError(`${label} must match /^[a-z][a-z0-9_-]*$/ (got \"${value}\")`, value);\n\t}\n}\n\n/**\n * Validate a plugin storage collection name.\n *\n * Collections are declared as free-form manifest keys and stored as opaque\n * text, so this is deliberately more permissive than `validateIdentifier`:\n * `form-submissions` and `formSubmissions` are legitimate. The charset still\n * rejects quotes and punctuation, keeping generated index names inert.\n *\n * @param value - The string to validate\n * @param label - Human-readable label for error messages\n * @throws {IdentifierError} If the value is not valid\n */\nexport function validateStorageCollectionName(value: string, label = \"collection name\"): void {\n\tif (!value || typeof value !== \"string\") {\n\t\tthrow new IdentifierError(`${label} must be a non-empty string`, String(value));\n\t}\n\n\tif (value.length > MAX_IDENTIFIER_LENGTH) {\n\t\tthrow new IdentifierError(\n\t\t\t`${label} must be ${MAX_IDENTIFIER_LENGTH} characters or less, got ${value.length}`,\n\t\t\tvalue,\n\t\t);\n\t}\n\n\tif (!STORAGE_COLLECTION_PATTERN.test(value)) {\n\t\tthrow new IdentifierError(\n\t\t\t`${label} must match /^[a-zA-Z][a-zA-Z0-9_-]*$/ (got \"${value}\")`,\n\t\t\tvalue,\n\t\t);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;AAcA,MAAM,qBAAqB;;;;;AAM3B,MAAM,6BAA6B;;;;;AAMnC,MAAM,4BAA4B;;;;;;AAOlC,MAAM,6BAA6B;;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