All files / lib/core/security escapeHtml.js

97.36% Statements 111/114
91.66% Branches 22/24
100% Functions 6/6
97.36% Lines 111/114

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 115778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 21716x 21716x     21716x 21716x 21716x 21716x 21716x 21716x 21716x 21716x 778x 778x 778x 778x 778x 778x 778x 1x 1x 778x 778x 778x 778x 778x 778x 778x 778x 46x 2x 2x 44x 44x 44x 44x 44x 44x 44x 46x 778x 778x 778x 778x 778x 778x 778x 778x 778x 22x 22x 22x 778x 778x 778x 778x 778x 14x 14x 778x 778x 778x 778x 778x 778x 21924x 21924x 21924x 20x 20x 20x 21924x 21924x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 20x 8x 8x 8x 11x 11x 3x 3x 1x 3x   2x 2x 2x 3x 11x 8x 8x 12x 20x  
/**
 * Provides utility for escaping HTML characters to prevent XSS.
 */
export class HtmlEscaper {
  /**
   * Escapes special HTML characters in a string.
   * @param {any} value - The value to escape.
   * @returns {string} The escaped string.
   */
  escape(value) {
    // Explicitly return an empty string for null or undefined
    if (value === null || value === undefined) {
      return '';
    }
 
    return String(value)
      .replace(/&/g, '&')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&#39;');
  }
 
  /**
   * Unescapes HTML entities in a string back to original characters.
   * @param {any} value - The value to unescape.
   * @returns {string} The unescaped string.
   */
  unescape(value) {
    return unescapeHtml(value);
  }
}
 
/**
 * Reverses HTML entity encoding for strings containing entities like &amp;, &lt;, &gt;, &quot;, and &#39;.
 * @param {any} value - The value to unescape.
 * @returns {string} The unescaped string.
 */
export function unescapeHtml(value) {
  if (value === null || value === undefined) {
    return '';
  }
 
  return String(value)
    .replace(/&amp;/g, '&')
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&quot;/g, '"')
    .replace(/&#39;/g, "'");
}
 
/**
 * A wrapper class to designate that a string is safe HTML and should not be escaped.
 */
export class SafeHtml {
  /**
   * @param {any} value
   */
  constructor(value) {
    this.value = String(value);
    this.__isSafeHtml = true;
  }
 
  /**
   * @returns {string}
   */
  toString() {
    return this.value;
  }
 
  /**
   * @param {any} instance
   * @returns {boolean}
   */
  static [Symbol.hasInstance](instance) {
    return Boolean(
      instance &&
      typeof instance === 'object' &&
      (instance.__isSafeHtml === true ||
        instance.constructor === SafeHtml ||
        instance.constructor?.name === 'SafeHtml')
    );
  }
}
 
/**
 * Creates a SafeHtml wrapper for raw HTML insertion.
 * Can be used as a standard function: html('<p>unsafe</p>')
 * or as a tagged template literal: html`<p>${unsafe}</p>`
 * @param {string|TemplateStringsArray} strings
 * @param {...any} values
 * @returns {SafeHtml}
 */
export function html(strings, ...values) {
  if (Array.isArray(strings) && strings.raw) {
    const escaper = new HtmlEscaper();
    let result = '';
    for (let i = 0; i < strings.length; i++) {
      result += strings[i];
      if (i < values.length) {
        const val = values[i];
        if (val instanceof SafeHtml) {
          result += val.toString();
        } else if (val == null) {
          result += '';
        } else {
          result += escaper.escape(val);
        }
      }
    }
    return new SafeHtml(result);
  }
  return new SafeHtml(strings);
}