All files / src/lib security-utils.ts

65.47% Statements 256/391
70.58% Branches 60/85
60% Functions 12/20
65.47% Lines 256/391

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730                                          1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 3x     3x 3x               1x                         1x 1x 1x 1x 1x         1x 1x 1x                   1x 4x 4x       4x 4x 4x       4x 4x     4x 4x     4x   7x 1x 1x 1x     7x           6x 7x 2x     2x 1x 1x 1x     2x 1x 1x 1x 1x 1x 1x     2x                 2x     7x                   6x 7x     4x   4x 4x                 1x 1x 1x                 1x 5x 5x         5x 5x 5x       5x 5x       5x 5x     5x 5x 2x 2x     5x 2x 1x 2x 1x 1x 2x     2x 2x 2x     2x   2x 5x     5x                 1x                                                                         1x 4x 4x           4x 4x 4x       4x 4x         4x     4x 1x 1x     3x 3x 1x 1x 3x     2x 2x 1x 1x 2x     4x               4x                               1x 1x                       1x 5x         5x 5x 5x                 1x                                                           1x 3x 3x             3x         3x 3x 3x 3x 3x   3x       3x 1x 3x 2x 2x   3x       3x 2x 3x 1x 1x   3x   3x 3x 3x   3x 1x 3x 2x 2x   3x 2x 3x 1x 1x     3x 3x 3x 3x 3x 3x 3x 3x   3x 13x 1x 1x 1x 1x 13x   3x 3x 3x 3x 3x 3x               1x 3x         3x     3x     3x     3x     3x     3x           3x 3x                 1x 3x       3x 3x     3x 2x 2x 6x 4x 4x 6x 2x   2x 3x 1x 1x 3x                     1x                   1x 14x 14x 14x 14x 14x 14x   14x 2x 2x 2x 2x 2x 2x   14x 8x 8x   4x 4x 4x             1x                     1x                                 1x               1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x                               1x                                                                   1x 5x 5x 5x 5x 5x               1x 2x 2x 2x 2x  
/**
 * Security Utilities for Input Validation and XSS Prevention
 *
 * Provides comprehensive input validation, sanitization, and XSS prevention
 * for the ZCLAW application.
 *
 * Security features:
 * - HTML sanitization
 * - URL validation
 * - Path traversal prevention
 * - Input validation helpers
 * - Content Security Policy helpers
 */
 
// ============================================================================
// HTML Sanitization
// ============================================================================
 
/**
 * HTML entity encoding map
 */
const HTML_ENTITIES: Record<string, string> = {
  '&': '&amp;',
  '<': '&lt;',
  '>': '&gt;',
  '"': '&quot;',
  "'": '&#x27;',
  '/': '&#x2F;',
  '`': '&#x60;',
  '=': '&#x3D;',
};
 
/**
 * Escape HTML entities in a string
 * Prevents XSS attacks by encoding dangerous characters
 *
 * @param input - The string to escape
 * @returns The escaped string
 */
export function escapeHtml(input: string): string {
  if (typeof input !== 'string') {
    return '';
  }
  return input.replace(/[&<>"'`=\/]/g, char => HTML_ENTITIES[char] || char);
}
 
/**
 * Unescape HTML entities in a string
 *
 * @param input - The string to unescape
 * @returns The unescaped string
 */
export function unescapeHtml(input: string): string {
  if (typeof input !== 'string') {
    return '';
  }
 
  const textarea = document.createElement('textarea');
  textarea.innerHTML = input;
  return textarea.value;
}
 
/**
 * Allowed HTML tags for safe rendering
 */
const ALLOWED_TAGS = new Set([
  'p', 'br', 'b', 'i', 'u', 'strong', 'em',
  'ul', 'ol', 'li', 'blockquote', 'code', 'pre',
  'a', 'span', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
]);
 
/**
 * Allowed HTML attributes
 */
const ALLOWED_ATTRIBUTES = new Set([
  'href', 'title', 'class', 'id', 'target', 'rel',
]);
 
/**
 * Sanitize HTML content for safe rendering
 * Removes dangerous tags and attributes while preserving safe content
 *
 * @param html - The HTML string to sanitize
 * @param options - Sanitization options
 * @returns The sanitized HTML
 */
export function sanitizeHtml(
  html: string,
  options: {
    allowedTags?: string[];
    allowedAttributes?: string[];
    allowDataAttributes?: boolean;
  } = {}
): string {
  if (typeof html !== 'string') {
    return '';
  }
 
  const allowedTags = new Set(options.allowedTags || ALLOWED_TAGS);
  const allowedAttributes = new Set(options.allowedAttributes || ALLOWED_ATTRIBUTES);
 
  // Create a temporary container
  const container = document.createElement('div');
  container.innerHTML = html;
 
  // Recursively clean elements
  function cleanElement(element: Element): void {
    // Remove script tags entirely
    if (element.tagName.toLowerCase() === 'script') {
      element.remove();
      return;
    }
 
    // Remove style tags entirely
    if (element.tagName.toLowerCase() === 'style') {
      element.remove();
      return;
    }
 
    // Remove event handlers and dangerous attributes
    const attributes = Array.from(element.attributes);
    for (const attr of attributes) {
      const attrName = attr.name.toLowerCase();
 
      // Remove event handlers (onclick, onload, etc.)
      if (attrName.startsWith('on')) {
        element.removeAttribute(attr.name);
        continue;
      }
 
      // Remove javascript: URLs
      if (attrName === 'href' || attrName === 'src') {
        const value = attr.value.toLowerCase().trim();
        if (value.startsWith('javascript:') || value.startsWith('data:text/html')) {
          element.removeAttribute(attr.name);
          continue;
        }
      }
 
      // Remove data attributes if not allowed
      if (attrName.startsWith('data-') && !options.allowDataAttributes) {
        element.removeAttribute(attr.name);
        continue;
      }
 
      // Remove non-allowed attributes
      if (!allowedAttributes.has(attrName)) {
        element.removeAttribute(attr.name);
      }
    }
 
    // Remove non-allowed tags (but keep their content)
    if (!allowedTags.has(element.tagName.toLowerCase())) {
      const parent = element.parentNode;
      while (element.firstChild) {
        parent?.insertBefore(element.firstChild, element);
      }
      parent?.removeChild(element);
      return;
    }
 
    // Recursively clean child elements
    Array.from(element.children).forEach(cleanElement);
  }
 
  // Clean all elements
  Array.from(container.children).forEach(cleanElement);
 
  return container.innerHTML;
}
 
// ============================================================================
// URL Validation
// ============================================================================
 
/**
 * Allowed URL schemes
 */
const ALLOWED_SCHEMES = new Set([
  'http', 'https', 'mailto', 'tel', 'ftp', 'file',
]);
 
/**
 * Validate and sanitize a URL
 *
 * @param url - The URL to validate
 * @param options - Validation options
 * @returns The validated URL or null if invalid
 */
export function validateUrl(
  url: string,
  options: {
    allowedSchemes?: string[];
    allowLocalhost?: boolean;
    allowPrivateIp?: boolean;
    maxLength?: number;
  } = {}
): string | null {
  if (typeof url !== 'string' || url.length === 0) {
    return null;
  }
 
  const maxLength = options.maxLength || 2048;
  if (url.length > maxLength) {
    return null;
  }
 
  try {
    const parsed = new URL(url);
 
    // Check scheme
    const allowedSchemes = new Set(options.allowedSchemes || ALLOWED_SCHEMES);
    if (!allowedSchemes.has(parsed.protocol.replace(':', ''))) {
      return null;
    }
 
    // Check for localhost
    if (!options.allowLocalhost) {
      if (parsed.hostname === 'localhost' ||
          parsed.hostname === '127.0.0.1' ||
          parsed.hostname === '[::1]') {
        return null;
      }
    }
 
    // Check for private IP ranges
    if (!options.allowPrivateIp) {
      const privateIpRegex = /^(10\.|172\.(1[6-9]|2[0-9]|3[01])\.|192\.168\.)/;
      if (privateIpRegex.test(parsed.hostname)) {
        return null;
      }
    }
 
    return parsed.toString();
  } catch {
    return null;
  }
}
 
/**
 * Check if a URL is safe for redirect
 * Prevents open redirect vulnerabilities
 *
 * @param url - The URL to check
 * @returns True if the URL is safe for redirect
 */
export function isSafeRedirectUrl(url: string): boolean {
  if (typeof url !== 'string' || url.length === 0) {
    return false;
  }
 
  // Relative URLs are generally safe
  if (url.startsWith('/') && !url.startsWith('//')) {
    return true;
  }
 
  // Check for javascript: protocol
  const lowerUrl = url.toLowerCase().trim();
  if (lowerUrl.startsWith('javascript:')) {
    return false;
  }
 
  // Check for data: protocol
  if (lowerUrl.startsWith('data:')) {
    return false;
  }
 
  // Validate as absolute URL
  const validated = validateUrl(url, { allowLocalhost: false });
  return validated !== null;
}
 
// ============================================================================
// Path Validation
// ============================================================================
 
/**
 * Validate a file path to prevent path traversal attacks
 *
 * @param path - The path to validate
 * @param options - Validation options
 * @returns The validated path or null if invalid
 */
export function validatePath(
  path: string,
  options: {
    allowAbsolute?: boolean;
    allowParentDir?: boolean;
    maxLength?: number;
    allowedExtensions?: string[];
    baseDir?: string;
  } = {}
): string | null {
  if (typeof path !== 'string' || path.length === 0) {
    return null;
  }
 
  const maxLength = options.maxLength || 4096;
  if (path.length > maxLength) {
    return null;
  }
 
  // Normalize path separators
  let normalized = path.replace(/\\/g, '/');
 
  // Check for null bytes
  if (normalized.includes('\0')) {
    return null;
  }
 
  // Check for path traversal
  if (!options.allowParentDir) {
    if (normalized.includes('..') || normalized.includes('./')) {
      return null;
    }
  }
 
  // Check for absolute paths
  if (!options.allowAbsolute) {
    if (normalized.startsWith('/') || /^[a-zA-Z]:/.test(normalized)) {
      return null;
    }
  }
 
  // Check extensions
  if (options.allowedExtensions && options.allowedExtensions.length > 0) {
    const ext = normalized.split('.').pop()?.toLowerCase();
    if (!ext || !options.allowedExtensions.includes(ext)) {
      return null;
    }
  }
 
  // If baseDir is specified, ensure path is within it
  if (options.baseDir) {
    const baseDir = options.baseDir.replace(/\\/g, '/').replace(/\/$/, '');
    if (!normalized.startsWith(baseDir)) {
      // Try to resolve relative to baseDir
      try {
        const resolved = new URL(normalized, `file://${baseDir}/`).pathname;
        if (!resolved.startsWith(baseDir)) {
          return null;
        }
        normalized = resolved;
      } catch {
        return null;
      }
    }
  }
 
  return normalized;
}
 
// ============================================================================
// Input Validation Helpers
// ============================================================================
 
/**
 * Validate an email address
 *
 * @param email - The email to validate
 * @returns True if valid
 */
export function isValidEmail(email: string): boolean {
  if (typeof email !== 'string' || email.length === 0 || email.length > 254) {
    return false;
  }
 
  // RFC 5322 compliant regex (simplified)
  const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
  return emailRegex.test(email);
}
 
/**
 * Validate a username
 *
 * @param username - The username to validate
 * @param options - Validation options
 * @returns True if valid
 */
export function isValidUsername(
  username: string,
  options: {
    minLength?: number;
    maxLength?: number;
    allowedChars?: RegExp;
  } = {}
): boolean {
  const minLength = options.minLength || 3;
  const maxLength = options.maxLength || 30;
  const allowedChars = options.allowedChars || /^[a-zA-Z0-9_-]+$/;
 
  if (typeof username !== 'string') {
    return false;
  }
 
  if (username.length < minLength || username.length > maxLength) {
    return false;
  }
 
  return allowedChars.test(username);
}
 
/**
 * Validate a password strength
 *
 * @param password - The password to validate
 * @param options - Validation options
 * @returns Validation result with strength score
 */
export function validatePasswordStrength(
  password: string,
  options: {
    minLength?: number;
    requireUppercase?: boolean;
    requireLowercase?: boolean;
    requireNumber?: boolean;
    requireSpecial?: boolean;
    maxLength?: number;
  } = {}
): {
  valid: boolean;
  score: number;
  issues: string[];
} {
  const minLength = options.minLength || 8;
  const maxLength = options.maxLength || 128;
  const issues: string[] = [];
  let score = 0;
 
  if (typeof password !== 'string') {
    return { valid: false, score: 0, issues: ['Password must be a string'] };
  }
 
  if (password.length < minLength) {
    issues.push(`Password must be at least ${minLength} characters`);
  } else {
    score += Math.min(password.length / 8, 3) * 10;
  }
 
  if (password.length > maxLength) {
    issues.push(`Password must be at most ${maxLength} characters`);
  }
 
  if (options.requireUppercase !== false && !/[A-Z]/.test(password)) {
    issues.push('Password must contain an uppercase letter');
  } else if (/[A-Z]/.test(password)) {
    score += 10;
  }
 
  if (options.requireLowercase !== false && !/[a-z]/.test(password)) {
    issues.push('Password must contain a lowercase letter');
  } else if (/[a-z]/.test(password)) {
    score += 10;
  }
 
  if (options.requireNumber !== false && !/[0-9]/.test(password)) {
    issues.push('Password must contain a number');
  } else if (/[0-9]/.test(password)) {
    score += 10;
  }
 
  if (options.requireSpecial !== false && !/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
    issues.push('Password must contain a special character');
  } else if (/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
    score += 15;
  }
 
  // Check for common patterns
  const commonPatterns = [
    /123/,
    /abc/i,
    /qwe/i,
    /password/i,
    /admin/i,
    /letmein/i,
  ];
 
  for (const pattern of commonPatterns) {
    if (pattern.test(password)) {
      issues.push('Password contains a common pattern');
      score -= 10;
      break;
    }
  }
 
  return {
    valid: issues.length === 0,
    score: Math.max(0, Math.min(100, score)),
    issues,
  };
}
 
/**
 * Sanitize a filename
 *
 * @param filename - The filename to sanitize
 * @returns The sanitized filename
 */
export function sanitizeFilename(filename: string): string {
  if (typeof filename !== 'string') {
    return '';
  }
 
  // Remove path separators
  let sanitized = filename.replace(/[\/\\]/g, '_');
 
  // Remove null bytes
  sanitized = sanitized.replace(/\0/g, '');
 
  // Remove control characters
  sanitized = sanitized.replace(/[\x00-\x1f\x7f]/g, '');
 
  // Remove dangerous characters
  sanitized = sanitized.replace(/[<>:"|?*]/g, '_');
 
  // Trim whitespace and dots
  sanitized = sanitized.trim().replace(/^\.+|\.+$/g, '');
 
  // Limit length
  if (sanitized.length > 255) {
    const ext = sanitized.split('.').pop();
    const name = sanitized.slice(0, -(ext?.length || 0) - 1);
    sanitized = name.slice(0, 250 - (ext?.length || 0)) + (ext ? `.${ext}` : '');
  }
 
  return sanitized;
}
 
/**
 * Sanitize JSON input
 * Prevents prototype pollution and other JSON-based attacks
 *
 * @param json - The JSON string to sanitize
 * @returns The parsed and sanitized object or null if invalid
 */
export function sanitizeJson<T = unknown>(json: string): T | null {
  if (typeof json !== 'string') {
    return null;
  }
 
  try {
    const parsed = JSON.parse(json);
 
    // Check for prototype pollution
    if (typeof parsed === 'object' && parsed !== null) {
      const dangerousKeys = ['__proto__', 'constructor', 'prototype'];
      for (const key of dangerousKeys) {
        if (key in parsed) {
          delete (parsed as Record<string, unknown>)[key];
        }
      }
    }
 
    return parsed as T;
  } catch {
    return null;
  }
}
 
// ============================================================================
// Rate Limiting
// ============================================================================
 
interface RateLimitEntry {
  count: number;
  resetAt: number;
}
 
const rateLimitStore = new Map<string, RateLimitEntry>();
 
/**
 * Check if an action is rate limited
 *
 * @param key - The rate limit key (e.g., 'api:username')
 * @param maxAttempts - Maximum attempts allowed
 * @param windowMs - Time window in milliseconds
 * @returns True if rate limited (should block), false otherwise
 */
export function isRateLimited(
  key: string,
  maxAttempts: number,
  windowMs: number
): boolean {
  const now = Date.now();
  const entry = rateLimitStore.get(key);
 
  if (!entry || now > entry.resetAt) {
    rateLimitStore.set(key, {
      count: 1,
      resetAt: now + windowMs,
    });
    return false;
  }
 
  if (entry.count >= maxAttempts) {
    return true;
  }
 
  entry.count++;
  return false;
}
 
/**
 * Reset rate limit for a key
 *
 * @param key - The rate limit key to reset
 */
export function resetRateLimit(key: string): void {
  rateLimitStore.delete(key);
}
 
/**
 * Get remaining attempts for a rate-limited action
 *
 * @param key - The rate limit key
 * @param maxAttempts - Maximum attempts allowed
 * @returns Number of remaining attempts
 */
export function getRemainingAttempts(key: string, maxAttempts: number): number {
  const entry = rateLimitStore.get(key);
  if (!entry || Date.now() > entry.resetAt) {
    return maxAttempts;
  }
  return Math.max(0, maxAttempts - entry.count);
}
 
// ============================================================================
// Content Security Policy Helpers
// ============================================================================
 
/**
 * Generate a nonce for CSP
 *
 * @returns A base64-encoded nonce
 */
export function generateCspNonce(): string {
  const array = crypto.getRandomValues(new Uint8Array(16));
  return btoa(String.fromCharCode(...array));
}
 
/**
 * CSP directives for secure applications
 */
export const DEFAULT_CSP_DIRECTIVES = {
  'default-src': "'self'",
  'script-src': "'self' 'unsafe-inline'", // Note: unsafe-inline should be avoided in production
  'style-src': "'self' 'unsafe-inline'",
  'img-src': "'self' data: https:",
  'font-src': "'self'",
  'connect-src': "'self' ws: wss:",
  'frame-ancestors': "'none'",
  'base-uri': "'self'",
  'form-action': "'self'",
};
 
/**
 * Build a Content Security Policy header value
 *
 * @param directives - CSP directives
 * @returns The CSP header value
 */
export function buildCspHeader(
  directives: Partial<typeof DEFAULT_CSP_DIRECTIVES> = DEFAULT_CSP_DIRECTIVES
): string {
  const merged = { ...DEFAULT_CSP_DIRECTIVES, ...directives };
  return Object.entries(merged)
    .map(([key, value]) => `${key} ${value}`)
    .join('; ');
}
 
// ============================================================================
// Security Headers Validation
// ============================================================================
 
/**
 * Check if security headers are properly set (for browser environments)
 */
export function checkSecurityHeaders(): {
  secure: boolean;
  issues: string[];
} {
  const issues: string[] = [];
 
  // Check if running over HTTPS
  if (typeof window !== 'undefined') {
    if (window.location.protocol !== 'https:' && window.location.hostname !== 'localhost') {
      issues.push('Application is not running over HTTPS');
    }
 
    // Check for mixed content
    if (window.location.protocol === 'https:') {
      // This would require DOM inspection to detect mixed content
    }
  }
 
  return {
    secure: issues.length === 0,
    issues,
  };
}
 
// ============================================================================
// Secure Random Generation
// ============================================================================
 
/**
 * Generate a secure random token
 *
 * @param length - Token length in bytes
 * @returns Hex-encoded random token
 */
export function generateSecureToken(length: number = 32): string {
  const array = crypto.getRandomValues(new Uint8Array(length));
  return Array.from(array)
    .map(b => b.toString(16).padStart(2, '0'))
    .join('');
}
 
/**
 * Generate a secure random ID
 *
 * @param prefix - Optional prefix
 * @returns A secure random ID
 */
export function generateSecureId(prefix: string = ''): string {
  const timestamp = Date.now().toString(36);
  const random = generateSecureToken(8);
  return prefix ? `${prefix}_${timestamp}_${random}` : `${timestamp}_${random}`;
}