/** * postgres-mcp - Identifier Sanitization Utilities * * Provides safe handling of PostgreSQL identifiers (table names, column names, schema names) * to prevent SQL injection attacks via identifier interpolation. * * PostgreSQL identifier rules: * - Must start with a letter (a-z) or underscore (_) * - Can contain letters, digits (0-9), underscores, and dollar signs ($) * - Maximum length: 63 bytes (NAMEDATALEN - 1) * - Case-insensitive unless quoted */ /** * Error thrown when an identifier is invalid */ export declare class InvalidIdentifierError extends Error { readonly identifier: string; readonly reason: string; constructor(identifier: string, reason: string); } /** * Validate a PostgreSQL identifier * * @param name - The identifier to validate * @throws InvalidIdentifierError if the identifier is invalid */ export declare function validateIdentifier(name: string): void; /** * Sanitize and quote a PostgreSQL identifier for safe use in SQL queries * * This function: * 1. Validates the identifier against PostgreSQL naming rules * 2. Escapes any embedded double quotes * 3. Wraps the identifier in double quotes for safe interpolation * * @param name - The identifier to sanitize * @returns The sanitized, double-quoted identifier * @throws InvalidIdentifierError if the identifier is invalid * * @example * sanitizeIdentifier('users') // Returns: "users" * sanitizeIdentifier('my_table') // Returns: "my_table" * sanitizeIdentifier('User"Data') // Throws: InvalidIdentifierError */ export declare function sanitizeIdentifier(name: string): string; /** * Check if an identifier needs quoting (is a reserved keyword or has special characters) * * @param name - The identifier to check * @returns True if the identifier needs quoting */ export declare function needsQuoting(name: string): boolean; /** * Sanitize a schema-qualified table name * * @param table - The table name * @param schema - Optional schema name (defaults to no schema prefix) * @returns The sanitized, fully-qualified table reference * * @example * sanitizeTableName('users') // Returns: "users" * sanitizeTableName('users', 'public') // Returns: "public"."users" */ export declare function sanitizeTableName(table: string, schema?: string): string; /** * Sanitize a column reference with optional table qualifier * * @param column - The column name * @param table - Optional table name or alias * @returns The sanitized column reference * * @example * sanitizeColumnRef('id') // Returns: "id" * sanitizeColumnRef('id', 'users') // Returns: "users"."id" */ export declare function sanitizeColumnRef(column: string, table?: string): string; /** * Sanitize an array of identifiers * * @param names - Array of identifier names * @returns Array of sanitized identifiers */ export declare function sanitizeIdentifiers(names: string[]): string[]; /** * Create a safe column list for SELECT statements * * @param columns - Array of column names * @returns Comma-separated list of sanitized column names * * @example * createColumnList(['id', 'name', 'email']) // Returns: "id", "name", "email" */ export declare function createColumnList(columns: string[]): string; /** * Sanitize an index name * PostgreSQL index names follow the same rules as identifiers * * @param name - The index name * @returns The sanitized index name */ export declare function sanitizeIndexName(name: string): string; /** * Generate a safe default index name from table and column names * * @param table - The table name * @param columns - The column name(s) * @param prefix - Optional prefix (default: 'idx') * @returns A sanitized index name */ export declare function generateIndexName(table: string, columns: string | string[], prefix?: string): string; /** * Quote an identifier for safe use in SQL without strict validation. * * Unlike sanitizeIdentifier(), this function: * - Allows reserved keywords (they become valid when quoted) * - Allows any valid PostgreSQL identifier characters * - Only validates basic safety (length, no dangerous characters) * * Use this for user-provided names like savepoints where reserved keywords * are perfectly valid PostgreSQL identifiers when properly quoted. * * @param name - The identifier to quote * @returns The double-quoted identifier safe for SQL interpolation * @throws InvalidIdentifierError if the identifier is genuinely invalid * * @example * quoteIdentifier('outer') // Returns: "outer" (reserved keyword, but valid) * quoteIdentifier('my_savepoint') // Returns: "my_savepoint" * quoteIdentifier('sp1') // Returns: "sp1" */ export declare function quoteIdentifier(name: string): string; //# sourceMappingURL=identifiers.d.ts.map