@visulima/cerebro
Version:
A delightful toolkit for building cross-runtime CLIs for Node.js, Deno, and Bun.
59 lines (58 loc) • 2.31 kB
TypeScript
/**
* Security utilities for preventing injection attacks and malicious inputs
*/
/**
* Sanitizes a command argument to prevent injection attacks.
* @param argument The argument string to sanitize.
* @returns The sanitized argument with whitespace trimmed.
* @throws {TypeError} If the argument is not a string.
* @throws {Error} If the argument exceeds maximum length or contains dangerous characters.
*/
export declare const sanitizeArgument: (argument: string) => string;
/**
* Sanitizes an array of arguments.
* @param args The array of arguments to sanitize.
* @returns Array of sanitized arguments.
* @throws {TypeError} If args is not an array or if any argument is not a string.
* @throws {Error} If there are too many arguments or if any argument is invalid.
*/
export declare const sanitizeArguments: (args: ReadonlyArray<string>) => string[];
/**
* Validates that a file path is safe (prevents directory traversal).
* @param path The file path to validate.
* @returns The validated path with whitespace trimmed.
* @throws {TypeError} If the path is not a string.
* @throws {Error} If the path contains traversal sequences, is absolute, or exceeds maximum length.
*/
export declare const validateSafePath: (path: string) => string;
/**
* Rate limiting helper to prevent brute force attacks
* Automatically cleans up expired entries to prevent memory leaks
*/
export declare class RateLimiter {
private attempts;
private readonly maxAttempts;
private readonly windowMs;
/**
* Creates a new RateLimiter instance.
* @param maxAttempts Maximum number of attempts allowed within the time window (default: 5).
* @param windowMs Time window in milliseconds (default: 60000).
*/
constructor(maxAttempts?: number, windowMs?: number);
/**
* Checks if the key has exceeded the rate limit.
* @param key Unique identifier for the rate limit check.
* @returns true if the request is allowed, false if rate limit exceeded.
*/
checkLimit(key: string): boolean;
/**
* Resets the rate limit for a specific key.
* @param key The key to reset.
*/
reset(key: string): void;
/**
* Removes expired entries from the attempts map.
* @param now Current timestamp.
*/
private cleanup;
}