Spaces:
Sleeping
Sleeping
Download packages/ui/src/security.ts from Benaah/MicProtocol: direct link, hf CLI and curl.
- Browser
- Download file 5.14 kB
-
https://huggingface.co/spaces/Benaah/MicProtocol/resolve/main/packages/ui/src/security.ts
- Command line
-
hf download hf://spaces/Benaah/MicProtocol/packages/ui/src/security.ts
-
curl -L -o security.ts https://huggingface.co/spaces/Benaah/MicProtocol/resolve/main/packages/ui/src/security.ts
5.14 kB
| /** | |
| * βββ MicProtocol Input Security & Attack Immunity Suite βββββββββ | |
| * Defense-in-depth sanitization, boundary enforcement, and injection | |
| * prevention for all form and input components across web & mobile. | |
| */ | |
| export const SAFE_MAX_LENGTHS = { | |
| email: 254, // RFC 5321 limit | |
| password: 128, // Prevents bcrypt / argon2 DoS via long inputs | |
| name: 100, | |
| title: 200, | |
| url: 2048, // Maximum safe URL length | |
| search: 120, // Prevents Meilisearch / DB algorithmic complexity DoS | |
| phone: 32, | |
| shortText: 255, | |
| longText: 5000, | |
| code: 64, | |
| } as const; | |
| /** | |
| * Regex to detect dangerous Trojan Source Unicode Bidirectional control characters. | |
| * U+202A to U+202E (LRE, RLE, PDF, LRO, RLO) | |
| * U+2066 to U+2069 (LRI, RLI, FSI, PDI) | |
| * U+200E, U+200F (LRM, RLM) | |
| * U+061C (ALM), U+FEFF (BOM) | |
| */ | |
| const BIDI_REGEX = /[\u202A-\u202E\u2066-\u2069\u200E\u200F\u061C\uFEFF]/g; | |
| /** | |
| * Regex to detect raw ASCII control characters (0x00-0x08, 0x0B, 0x0C, 0x0E-0x1F, 0x7F). | |
| * Null byte \0 is explicitly targeted to prevent null byte injection and path traversal. | |
| */ | |
| const CONTROL_CHARS_REGEX = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g; | |
| /** | |
| * Regex for script tags, javascript: pseudo-protocols, and inline event handlers. | |
| */ | |
| const DANGEROUS_HTML_REGEX = /<\s*script[^>]*>.*?<\s*\/\s*script\s*>|<\s*script[^>]*>|javascript:|vbscript:|data:text\/html|on\w+\s*=/gi; | |
| export interface SanitizeOptions { | |
| maxLength?: number; | |
| allowNewlines?: boolean; | |
| trim?: boolean; | |
| stripHtml?: boolean; | |
| } | |
| /** | |
| * Sanitizes generic user input: | |
| * 1. Strips null bytes (\0) and invisible control characters. | |
| * 2. Neutralizes Trojan Source BiDi override attacks. | |
| * 3. Strips script tags, javascript: pseudo-protocols, and inline events. | |
| * 4. Enforces max length boundaries. | |
| */ | |
| export function sanitizeText(input: unknown, options: SanitizeOptions = {}): string { | |
| if (input == null) return ""; | |
| let str = String(input); | |
| // 1. Strip null bytes & control chars | |
| str = str.replace(CONTROL_CHARS_REGEX, ""); | |
| // 2. Strip BiDi overrides | |
| str = str.replace(BIDI_REGEX, ""); | |
| // 3. Strip dangerous scripts & event handlers | |
| str = str.replace(DANGEROUS_HTML_REGEX, ""); | |
| // 4. Handle newlines | |
| if (!options.allowNewlines) { | |
| str = str.replace(/[\r\n]+/g, " "); | |
| } | |
| // 5. Trim if requested | |
| if (options.trim !== false) { | |
| str = str.trim(); | |
| } | |
| // 6. Enforce max length | |
| const max = options.maxLength ?? SAFE_MAX_LENGTHS.shortText; | |
| if (str.length > max) { | |
| str = str.slice(0, max); | |
| } | |
| return str; | |
| } | |
| /** | |
| * Sanitizes and validates email addresses against RFC limits: | |
| * - Trims whitespace | |
| * - Enforces max length of 254 characters (RFC 5321) | |
| * - Converts to lower-case | |
| * - Strips control characters and quotes | |
| */ | |
| export function sanitizeEmail(email: unknown): string { | |
| if (email == null) return ""; | |
| let str = String(email).replace(CONTROL_CHARS_REGEX, "").replace(BIDI_REGEX, "").trim(); | |
| if (str.length > SAFE_MAX_LENGTHS.email) { | |
| str = str.slice(0, SAFE_MAX_LENGTHS.email); | |
| } | |
| return str.toLowerCase(); | |
| } | |
| /** | |
| * Strict URL sanitization: | |
| * Only permits http:, https:, mailto:, and tel: protocols. | |
| * Explicitly rejects javascript:, data:, blob:, file:, and invalid URI formats. | |
| */ | |
| export function sanitizeUrl(url: unknown): string { | |
| if (url == null) return ""; | |
| let str = String(url).replace(CONTROL_CHARS_REGEX, "").replace(BIDI_REGEX, "").trim(); | |
| if (str.length > SAFE_MAX_LENGTHS.url) { | |
| str = str.slice(0, SAFE_MAX_LENGTHS.url); | |
| } | |
| // Reject javascript:, data:, vbscript: | |
| const lower = str.toLowerCase(); | |
| if ( | |
| lower.startsWith("javascript:") || | |
| lower.startsWith("vbscript:") || | |
| lower.startsWith("data:") || | |
| lower.startsWith("blob:") || | |
| lower.startsWith("file:") | |
| ) { | |
| return ""; | |
| } | |
| return str; | |
| } | |
| /** | |
| * Detects common malicious injection signatures for early warning/blocking: | |
| * - SQL Injection (UNION SELECT, OR 1=1, DROP TABLE, --) | |
| * - Path traversal (../, ..\) | |
| * - Prototype pollution (__proto__, constructor.prototype) | |
| * - XSS patterns | |
| */ | |
| export function detectMaliciousPayload(input: string): { isMalicious: boolean; reason?: string } { | |
| if (!input) return { isMalicious: false }; | |
| // Prototype pollution | |
| if (/__proto__|constructor\s*\[\s*['"]prototype['"]\s*\]/i.test(input)) { | |
| return { isMalicious: true, reason: "Prototype pollution pattern detected" }; | |
| } | |
| // Path traversal | |
| if (/\.\.[\/\\]/.test(input)) { | |
| return { isMalicious: true, reason: "Path traversal pattern detected" }; | |
| } | |
| // Common SQL injection meta-patterns | |
| const sqlPatterns = [ | |
| /\bunion\s+(all\s+)?select\b/i, | |
| /\bdrop\s+table\b/i, | |
| /;\s*select\b/i, | |
| /;\s*delete\s+from\b/i, | |
| /;\s*update\s+\w+\s+set\b/i, | |
| /['"]\s*or\s+['"]?1['"]?\s*=\s*['"]?1/i, | |
| ]; | |
| for (const pattern of sqlPatterns) { | |
| if (pattern.test(input)) { | |
| return { isMalicious: true, reason: "SQL injection pattern detected" }; | |
| } | |
| } | |
| // Script injection | |
| if (/<script|javascript:|onerror\s*=/i.test(input)) { | |
| return { isMalicious: true, reason: "Script injection pattern detected" }; | |
| } | |
| return { isMalicious: false }; | |
| } | |