@nanggo/social-preview
Version:
Generate beautiful social media preview images from any URL
66 lines (65 loc) • 2.57 kB
TypeScript
/**
* Image security utilities and Sharp configuration
* Prevents pixel bomb attacks and validates image dimensions
*/
import sharp from 'sharp';
/**
* Initialize Sharp with security settings
* Should be called once at application startup
*/
export declare function initializeSharpSecurity(): void;
/**
* Validate image buffer before processing
*/
export declare function validateImageBuffer(imageBuffer: Buffer, allowSvg?: boolean): Promise<void>;
/**
* Validate SVG content for security risks using DOMPurify
*/
export declare function validateSvgContent(svgBuffer: Buffer): Promise<void>;
/**
* Sanitize SVG content and return cleaned result for testing
*/
export declare function sanitizeSvgContent(svgContent: string): string;
/**
* Create a secure Sharp instance with safety checks
*/
export declare function createSecureSharpInstance(imageBuffer: Buffer): sharp.Sharp;
/**
* Execute a Sharp operation with direct instance creation
* Use this for one-shot operations
*/
export declare function withSecureSharp<T>(imageBuffer: Buffer, operation: (sharp: sharp.Sharp) => Promise<T>): Promise<T>;
/**
* Process image with timeout protection to prevent DoS attacks
*/
export declare function processImageWithTimeout<T>(operation: () => Promise<T>, timeoutMs?: number): Promise<T>;
/**
* Safely resize image with dimension validation
*/
export declare function secureResize(sharpInstance: sharp.Sharp, width: number, height: number, options?: sharp.ResizeOptions): sharp.Sharp;
/**
* Create a Sharp instance with metadata removal for privacy and security
*/
export declare function createSecureSharpWithCleanMetadata(imageBuffer: Buffer): sharp.Sharp;
/**
* Execute a Sharp operation with direct instance creation and clean metadata
* Use this for one-shot operations that need automatic cleanup
*/
export declare function withSecureSharpCleanMetadata<T>(imageBuffer: Buffer, operation: (sharp: sharp.Sharp) => Promise<T>): Promise<T>;
/**
* Validate Sharp processing limits before operations
*/
export declare function validateSharpLimits(width: number, height: number): void;
/**
* Export security constants for use in other modules
*/
export declare const IMAGE_SECURITY_LIMITS: {
readonly MAX_INPUT_PIXELS: number;
readonly MAX_IMAGE_WIDTH: 8192;
readonly MAX_IMAGE_HEIGHT: 8192;
readonly MAX_FILE_SIZE: number;
readonly MAX_SVG_SIZE: number;
readonly MAX_DPI: 600;
readonly PROCESSING_TIMEOUT: 30000;
readonly ALLOWED_IMAGE_FORMATS: ("png" | "jpeg" | "webp" | "jpg" | "gif" | "bmp" | "tiff")[];
};