Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

28.0 KB
Raw Blame
/**
 * XSS Protection utilities for Local Deep Research
 *
 * This module provides secure HTML sanitization functions to prevent
 * cross-site scripting (XSS) attacks when rendering dynamic content.
 * Uses DOMPurify for proven, security-reviewed HTML sanitization.
 *
 * This module intentionally provides HTML sanitization utilities. All innerHTML
 * operations use DOMPurify sanitization or escapeHtml encoding, and each is
 * suppressed by an inline directive at its own call site (a module-level
 * suppression in this block comment would be ignored by the scanner anyway).
 *
 * ARCHITECTURE NOTE: Inline Fallback Pattern
 * ------------------------------------------
 * A single global `escapeHtmlFallback` is defined in services/ui.js (loaded
 * via base.html on all pages). This provides defense-in-depth XSS protection
 * if this xss-protection.js file fails to load.
 *
 * IMPORTANT: Only services/ui.js should define escapeHtmlFallback at top-level
 * scope. Other files must NOT redeclare it — doing so causes SyntaxError
 * ("Identifier has already been declared") since const/var cannot coexist
 * in the same global scope across <script> tags.
 *
 * Files wrapped in IIFEs (components/results.js, components/settings.js,
 * components/details.js, components/fallback/ui.js) may safely define their
 * own scoped fallback.
 *
 * The usage pattern is: `(window.escapeHtml || escapeHtmlFallback)(text)`
 * This ensures the global function is preferred when available, with the
 * inline fallback as a safety net.
 */

(function() {
    'use strict';

    // Check if DOMPurify is available dynamically (bound by app.js/Vite module).
    // Evaluated per call rather than once, so no helper depends on the
    // relative load order of this script and the module that binds it.
    // Usable only when isSupported is exactly true: DOMPurify computes
    // isSupported from an && chain that can yield null/undefined as well
    // as false, and sanitize() returns its input unchanged whenever it is
    // falsy -- treating such an instance as present would fail open.
    function hasDOMPurify() {
        return typeof DOMPurify !== 'undefined' && DOMPurify !== null &&
            DOMPurify.isSupported === true;
    }

    // Browsing-context keywords that never open a new browsing context.
    // Any other non-empty target (_blank, a named window, or an unknown
    // keyword) can, so it gets the forced rel.
    const SAME_CONTEXT_TARGETS = new Set(['_self', '_parent', '_top']);

    // Elements whose `target` can navigate a new browsing context and whose
    // `rel` accepts noopener/noreferrer: <a> (HTML, and SVG <a>, whose
    // lowercase tagName is upper-cased below), <area>, and <form>.
    const NEW_CONTEXT_TAGS = new Set(['A', 'AREA', 'FORM']);

    // Element accessors taken from Element.prototype rather than read off
    // the node. On a <form>, a descendant control named or id'd `tagName`,
    // `getAttribute` or `setAttribute` shadows the same-named instance
    // property (HTMLFormElement's [LegacyOverrideBuiltIns] named
    // properties), and DOMPurify's clobbering check does not cover all of
    // them, so the hook must never trust a property looked up on the node.
    // Captured at load when Element exists, otherwise on first use.
    let _elementApi = null;
    function _getElementApi() {
        if (_elementApi) {
            return _elementApi;
        }
        if (typeof Element !== 'function' || !Element.prototype) {
            return null;
        }
        const proto = Element.prototype;
        const tagNameDesc = Object.getOwnPropertyDescriptor(proto, 'tagName');
        if (!tagNameDesc || typeof tagNameDesc.get !== 'function' ||
            typeof proto.getAttribute !== 'function' ||
            typeof proto.setAttribute !== 'function') {
            return null;
        }
        _elementApi = {
            ElementCtor: Element,
            getTagName: tagNameDesc.get,
            getAttr: proto.getAttribute,
            setAttr: proto.setAttribute,
        };
        return _elementApi;
    }
    _getElementApi();

    // Merge the forced keywords into an existing rel instead of replacing
    // it: keep every other token (e.g. nofollow, ugc) once, drop `opener`
    // (it would re-grant window.opener), and append noopener noreferrer.
    function _mergeRel(existing) {
        const kept = [];
        const seen = new Set();
        String(existing === null || existing === undefined ? '' : existing)
            .split(/[\t\n\f\r ]+/)
            .forEach(function(token) {
                const lower = token.toLowerCase();
                if (token === '' || lower === 'opener' ||
                    lower === 'noopener' || lower === 'noreferrer' ||
                    seen.has(lower)) {
                    return;
                }
                seen.add(lower);
                kept.push(token);
            });
        kept.push('noopener', 'noreferrer');
        return kept.join(' ');
    }

    function hardenLinkTarget(node) {
        // Enforce rel noopener noreferrer on every link or form that
        // can open a new browsing context, so the opened page cannot
        // reach window.opener (tabnabbing) or receive the Referer.
        // Every read and write goes through the prototype accessors
        // above, never through a property looked up on the node.
        const api = _getElementApi();
        if (!api || !(node instanceof api.ElementCtor)) {
            return;
        }
        const tag = String(api.getTagName.call(node)).toUpperCase();
        if (!NEW_CONTEXT_TAGS.has(tag)) {
            return;
        }
        const target = api.getAttr.call(node, 'target');
        if (target === null) {
            return;
        }
        // Only exact keywords are exempt (ASCII case-insensitive).
        // DOMPurify trims attribute values before invoking this hook.
        const normalized = String(target).toLowerCase();
        if (tag === 'A' && normalized !== '') {
            // Named links must not select another existing browsing context.
            api.setAttr.call(node, 'target', SAME_CONTEXT_TARGETS.has(normalized) ? normalized : '_blank');
        }
        if (normalized !== '' && !SAME_CONTEXT_TARGETS.has(normalized)) {
            api.setAttr.call(node, 'rel', _mergeRel(api.getAttr.call(node, 'rel')));
        }
    }

    // Configure DOMPurify hooks to prevent tabnabbing attacks.
    // Registration is lazy and idempotent per DOMPurify instance. The
    // load-time call below registers when DOMPurify is already bound, but
    // correctness does not rest on it: every sanitize path whose config
    // keeps `target` (this module's, and renderMarkdown via the exported
    // ensure) ensures the hook first, so a DOMPurify bound (or replaced)
    // after this script loads is covered too. Direct DOMPurify.sanitize
    // callers elsewhere keep no `target` attribute, so need no hook.
    const _hookedPurifiers = new WeakSet();
    let _warnedNoAddHook = false;
    function _ensureTabnabbingHook() {
        if (!hasDOMPurify()) {
            return;
        }
        const purifier = DOMPurify;
        if (!purifier || _hookedPurifiers.has(purifier)) {
            return;
        }
        if (typeof purifier.addHook !== 'function') {
            // Report once via SafeLogger when it is available: sanitizing
            // still works, but links that open a new browsing context will
            // not get the forced rel.
            if (!_warnedNoAddHook && typeof SafeLogger !== 'undefined' &&
                SafeLogger && typeof SafeLogger.warn === 'function') {
                _warnedNoAddHook = true;
                SafeLogger.warn('DOMPurify has no addHook; tabnabbing rel hook not registered');
            }
            return;
        }
        purifier.addHook('afterSanitizeAttributes', hardenLinkTarget);
        _hookedPurifiers.add(purifier);
    }
    _ensureTabnabbingHook();

    /**
     * HTML entity encoding map for XSS prevention
     */
    const HTML_ESCAPE_MAP = {
        '&': '&amp;',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        "'": '&#39;',
        '/': '&#x2F;'
    };

    /**
     * DOMPurify configuration for secure sanitization
     */
    const SANITIZE_CONFIG = {
        ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'span', 'br', 'p', 'div', 'ul', 'ol', 'li', 'a', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
        ALLOWED_ATTR: ['class', 'id', 'href', 'title', 'alt', 'target', 'rel'],
        ALLOW_DATA_ATTR: false,
        FORBID_TAGS: ['script', 'iframe', 'object', 'embed', 'form', 'input', 'button', 'style', 'meta', 'link'],
        // The real guard against event handlers is ALLOWED_ATTR above,
        // which does not list any on* name. Note the precise reason: it is
        // NOT that DOMPurify only ever keeps an attribute named in
        // ALLOWED_ATTR -- _isValidAttribute returns true for any `data-*`
        // name when ALLOW_DATA_ATTR is on, and for any `aria-*` name when
        // ALLOW_ARIA_ATTR is on, both of them *before* the allowlist is
        // consulted (dompurify/dist/purify.js, the ALLOW_DATA_ATTR /
        // ALLOW_ARIA_ATTR early returns). ALLOW_ARIA_ATTR defaults to true
        // and is deliberately left at its default here. Those two escape
        // hatches are name-shaped, though: they match only /^data-[...]/ and
        // /^aria-[\-\w]+$/, and no on* handler has either prefix. So every
        // on* attribute does fall through to the allowlist, finds no entry,
        // and is dropped -- the conclusion holds, just not for the blanket
        // reason the earlier wording gave.
        //
        // FORBID_ATTR itself takes literal attribute names, not a glob:
        // 'on*' matches nothing DOMPurify would otherwise keep, so it was
        // never doing anything -- listed here only as documentation of
        // intent, not as an active control.
        FORBID_ATTR: ['onerror', 'onload', 'onclick', 'onmouseover', 'onfocus', 'onblur', 'onchange', 'onsubmit'],
        KEEP_CONTENT: true,
        SANITIZE_DOM: true,
        SANITIZE_NAMED_PROPS: true,
        SAFE_FOR_TEMPLATES: true,
        WHOLE_DOCUMENT: false,
        RETURN_DOM: false,
        RETURN_DOM_FRAGMENT: false,
        RETURN_DOM_IMPORT: false,
        CUSTOM_ELEMENT_HANDLING: {
            tagNameCheck: null,
            attributeNameCheck: null,
            allowCustomizedBuiltInElements: false
        }
    };

    /**
     * Maximum sanitize passes before sanitizeStable gives up and falls back
     * to escaped text. Benign markup reaches a fixpoint on the second pass
     * (pass 1 sanitizes, pass 2 observes no change); anything still mutating
     * after this many passes is treated as hostile.
     */
    const MAX_SANITIZE_PASSES = 5;

    /**
     * Sanitize to a reparse fixpoint to prevent mXSS.
     *
     * The mXSS class exists because sanitizers inspect a parse tree but ship a
     * SERIALIZATION of it, and the browser re-parses that serialization into a
     * DIFFERENT tree. Inspecting the result once does not close the gap: the
     * inspected tree is still not the tree the user gets.
     *
     * This applies DOMPurify repeatedly until `sanitize(s) === s`. At that
     * point the string is a fixpoint of parse -> sanitize -> serialize, so the
     * tree the browser builds from it is one DOMPurify has already declared
     * clean under `config` -- a mutation that only materialises on reparse is
     * caught by the following pass instead of shipping unseen.
     *
     * If the string never stabilises it is escaped rather than emitted: the
     * content is still shown (as text), but no markup derived from it reaches
     * the DOM. This is deliberately fail-closed, matching safeSetInnerHTML's
     * DOMPurify-missing branch.
     *
     * Note this is a pure string operation -- it never touches the DOM
     * directly, so it behaves identically in every environment DOMPurify
     * itself works in.
     *
     * @param {string} dirty - The potentially dirty HTML string
     * @param {Object} config - DOMPurify configuration
     * @returns {string} - Sanitized HTML that is stable across reparsing
     */
    function sanitizeStable(dirty, config) {
        if (!hasDOMPurify()) {
            return escapeHtml(dirty);
        }
        _ensureTabnabbingHook();
        let current = String(dirty);

        for (let pass = 0; pass < MAX_SANITIZE_PASSES; pass++) {
            const next = DOMPurify.sanitize(current, config);

            // RETURN_DOM/RETURN_DOM_FRAGMENT configurations (and test doubles
            // that mimic them) return nodes, not strings. String stability is
            // meaningless there, so hand the node back untouched.
            if (typeof next !== 'string') {
                return next;
            }

            if (next === current) {
                return next;
            }

            current = next;
        }

        SafeLogger.warn(
            'sanitizeStable: markup did not reach a sanitization fixpoint after '
            + MAX_SANITIZE_PASSES + ' passes; rendering as escaped text'
        );
        return escapeHtml(current);
    }

    /**
     * Escape HTML entities in a string to prevent XSS
     * @param {string} text - The text to escape
     * @returns {string} - The escaped text safe for HTML content
     */
    function escapeHtml(text) {
        if (text === null || text === undefined) {
            return '';
        }
        if (typeof text !== 'string') {
            text = String(text);
        }

        // bearer:disable javascript_lang_manual_html_sanitization
        return text.replace(/[&<>"'/]/g, (match) => HTML_ESCAPE_MAP[match]);
    }

/**
 * Escape HTML attributes to prevent XSS
 * @param {string} text - The text to escape for attribute context
 * @returns {string} - The escaped text safe for HTML attributes
 */
// This IS the sanitization function
// bearer:disable javascript_lang_manual_html_sanitization
function escapeHtmlAttribute(text) {
    if (typeof text !== 'string') {
        text = String(text);
    }

    // For attributes, we need to escape quotes and ampersands
    // bearer:disable javascript_lang_manual_html_sanitization
    return text.replace(/["&'<>]/g, (match) => HTML_ESCAPE_MAP[match]);
}

/**
 * Safely set innerHTML with content sanitization using DOMPurify
 * @param {Element} element - The DOM element to update
 * @param {string} content - The content to set (will be sanitized)
 * @param {boolean} allowHtmlTags - If true, allows basic HTML tags, otherwise escapes everything
 */
// Content is sanitized by DOMPurify before insertion
// bearer:disable javascript_lang_dangerous_insert_html
function safeSetInnerHTML(element, content, allowHtmlTags = false) {
    if (!element) {
        return;
    }

    if (!content) {
        element.textContent = '';
        return;
    }

    const contentString = String(content);

    if (allowHtmlTags && hasDOMPurify()) {
        // Use DOMPurify for secure HTML sanitization
        const sanitized = sanitizeStable(contentString, SANITIZE_CONFIG);
        // bearer:disable javascript_lang_dangerous_insert_html
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-28: content already sanitized by sanitizeStable() above
        element.innerHTML = sanitized;
    } else if (allowHtmlTags) {
        // DOMPurify not available but HTML requested - escape all HTML for safety
        SafeLogger.warn('DOMPurify not available, escaping HTML instead of sanitizing');
        element.textContent = contentString;
    } else {
        // Escape all HTML - use textContent for maximum security
        element.textContent = contentString;
    }
}

/**
 * Create a safe DOM element with text content
 * @param {string} tagName - The HTML tag name
 * @param {string} text - The text content (will be escaped)
 * @param {Object} attributes - Optional attributes object
 * @param {string[]} classNames - Optional CSS class names
 * @returns {Element} - The created DOM element
 */
function safeCreateElement(tagName, text = '', attributes = {}, classNames = []) {
    // Tag whitelist for programmatic DOM creation (broader than DOMPurify's
    // SANITIZE_CONFIG which applies to untrusted HTML sanitization)
    const SAFE_TAGS = new Set([
        'b', 'i', 'em', 'strong', 'span', 'br', 'p', 'div', 'small',
        'ul', 'ol', 'li', 'a',
        'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
        'hr', 'label', 'nav', 'button',
        'table', 'thead', 'tbody', 'tr', 'th', 'td',
        'select', 'option', 'optgroup', 'canvas'
    ]);
    // Attribute allowlist: only these names (plus aria-*/data-* prefixes)
    // are ever set. Everything else -- including all on* event handlers --
    // is dropped silently.
    const SAFE_ATTRS = new Set([
        'class', 'id', 'title', 'role', 'tabindex',
        'href', 'target', 'rel',
        'type', 'value', 'disabled', 'name', 'alt',
        'for', 'colspan', 'rowspan', 'scope'
    ]);

    // href only permits http/https/mailto/tel schemes plus relative
    // references (#, /path, ./path, query-only). javascript:, data:,
    // vbscript:, file:, about: and protocol-relative //host values are
    // rejected. Control characters and whitespace are stripped before
    // scheme detection because browsers ignore them inside URLs.
    function isSafeHref(raw) {
        // eslint-disable-next-line no-control-regex -- intentional: browsers ignore control characters inside URL schemes, so they must be stripped before scheme validation
        const normalized = String(raw).replace(/[\x00-\x20\x7F]/g, '');
        // Backslashes are normalized to forward slashes for protocol-relative detection only -- browsers accept /\ and \\ as scheme-relative URLs (e.g. /\evil.com == //evil.com). The assigned value itself is never rewritten, so a href like /\path\foo survives verbatim when safe.
        if (normalized.replace(/\\/g, '/').startsWith('//')) {
            return false;
        }
        const schemeMatch = /^[a-z][a-z0-9+.-]*:/i.exec(normalized);
        if (schemeMatch) {
            const scheme = schemeMatch[0].slice(0, -1).toLowerCase();
            return scheme === 'http' || scheme === 'https' ||
                scheme === 'mailto' || scheme === 'tel';
        }
        return true;
    }

    const normalizedTag = String(tagName).toLowerCase();
    if (!SAFE_TAGS.has(normalizedTag)) {
        throw new Error('safeCreateElement: disallowed tag "' + tagName + '"');
    }

    // normalizedTag is validated against the SAFE_TAGS whitelist above (throws
    // otherwise) and createElement does not parse HTML -- Bearer false positive.
    // bearer:disable javascript_lang_dangerous_insert_html
    const element = document.createElement(normalizedTag);

    if (text) {
        element.textContent = text;
    }

    Object.entries(attributes).forEach(([key, value]) => {
        if (!key || value === null || value === undefined) {
            return;
        }
        const name = String(key).toLowerCase();
        const allowed = SAFE_ATTRS.has(name) ||
            name.startsWith('aria-') || name.startsWith('data-');
        if (!allowed) {
            return;
        }
        if (name === 'href' && !isSafeHref(value)) {
            return;
        }
        element.setAttribute(name, String(value));
    });

    _ensureTabnabbingHook();
    // Mirror the DOMPurify hook for programmatically created links.
    hardenLinkTarget(element);

    if (classNames.length > 0) {
        element.className = classNames.join(' ');
    }

    return element;
}

/**
 * Safe text content setter that prevents XSS
 * @param {Element} element - The DOM element to update
 * @param {string} content - The content to set safely
 */
function safeSetTextContent(element, content) {
    if (!element) {
        return;
    }

    element.textContent = String(content || '');
}

/**
 * Create a safe alert message element
 * @param {string} message - The alert message (will be escaped)
 * @param {string} type - Alert type (success, error, warning, info)
 * @param {Object|null} [options] - Alert behavior options
 * @param {boolean} [options.announce=true] - Whether to expose the element
 *     as an assertive live alert
 * @returns {Element} - The created alert element
 */
function createSafeAlertElement(message, type = 'info', options = {}) {
    const alert = document.createElement('div');
    const alertType = window.LdrAlertHelpers.mapAlertType(type);
    alert.className = `alert alert-${alertType}`;
    if (options?.announce !== false) {
        alert.setAttribute('role', 'alert');
        alert.setAttribute('aria-atomic', 'true');
    }

    // Create icon
    const iconMap = {
        'success': 'fa-check-circle',
        'error': 'fa-exclamation-circle',
        'warning': 'fa-exclamation-triangle',
        'info': 'fa-info-circle'
    };

    const icon = document.createElement('i');
    icon.className = `fas ${iconMap[type] || iconMap['info']}`;
    icon.setAttribute('aria-hidden', 'true');

    // Create message text (escaped)
    const messageText = document.createElement('span');
    messageText.textContent = message; // Safe - textContent prevents HTML injection

    // Create close button (use button element for accessibility)
    const closeBtn = document.createElement('button');
    closeBtn.className = 'ldr-alert-close';
    closeBtn.setAttribute('aria-label', 'Dismiss alert');
    closeBtn.type = 'button';
    closeBtn.textContent = '×';
    closeBtn.addEventListener('click', () => {
        alert.remove();
    });

    // Assemble alert
    alert.appendChild(icon);
    alert.appendChild(messageText);
    alert.appendChild(closeBtn);

    return alert;
}

/**
 * Secure HTML sanitization using DOMPurify
 * @param {string} dirty - The potentially dirty HTML string
 * @param {Object} config - Optional DOMPurify configuration overrides
 * @returns {string} - The sanitized HTML string
 */
function sanitizeHtml(dirty, config = {}) {
    if (!dirty) return '';

    if (hasDOMPurify()) {
        const finalConfig = { ...SANITIZE_CONFIG, ...config };
        return sanitizeStable(dirty, finalConfig);
    }
    // Fallback: escape all HTML if DOMPurify is not available
    SafeLogger.warn('DOMPurify not available, falling back to HTML escaping');
    return escapeHtml(String(dirty));
}

/**
 * Validate and sanitize user input for safe display
 * @param {any} input - The input to validate and sanitize
 * @param {Object} options - Sanitization options
 * @returns {string} - The sanitized string
 */
function sanitizeUserInput(input, options = {}) {
    const {
        maxLength = 10000,
        allowLineBreaks = true,
        trimWhitespace = true,
        allowHtml = false
    } = options;

    if (input === null || input === undefined) {
        return '';
    }

    let sanitized = String(input);

    // Trim if requested
    if (trimWhitespace) {
        sanitized = sanitized.trim();
    }

    // Enforce max length
    if (sanitized.length > maxLength) {
        sanitized = sanitized.substring(0, maxLength);
    }

    // Handle line breaks
    if (allowLineBreaks) {
        sanitized = sanitized.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
    }

    // Either escape HTML or sanitize it
    if (allowHtml) {
        return sanitizeHtml(sanitized);
    }
    return escapeHtml(sanitized);
}

  // Export to global scope
    window.XSSProtection = {
        escapeHtml,
        ensureTabnabbingHook: _ensureTabnabbingHook,
        escapeHtmlAttribute,
        safeSetInnerHTML,
        safeCreateElement,
        safeSetTextContent,
        createSafeAlertElement,
        sanitizeUserInput,
        sanitizeHtml,
        sanitizeStable,
        // app.js calls this after publishing its deferred DOMPurify import.
        // Exposing the idempotent installer also covers direct DOMPurify
        // consumers that do not route through sanitizeStable().
        ensureDOMPurifyHooks: _ensureTabnabbingHook
    };

    /**
     * Safely update button content with icon and text
     * @param {HTMLElement} button - The button element to update
     * @param {string} iconClass - Font Awesome icon class (without 'fas')
     * @param {string} text - Button text content
     * @param {boolean} addSpinner - Whether to add spinner animation
     */
    function safeUpdateButton(button, iconClass, text, addSpinner = false) {
        if (!button) return;

        // Clear existing content
        while (button.firstChild) {
            button.removeChild(button.firstChild);
        }

        // Create icon (aria-hidden for accessibility — decorative icons beside text)
        const icon = document.createElement('i');
        icon.className = `fas ${iconClass}`;
        icon.setAttribute('aria-hidden', 'true');
        if (addSpinner) {
            icon.className += ' fa-spin';
        }

        // Add icon and text
        button.appendChild(icon);
        if (text) {
            button.appendChild(document.createTextNode(text));
        }
    }

    /**
     * Create a loading overlay with safe DOM manipulation
     * @param {Object} options - Loading overlay options
     * @returns {HTMLElement} - The created loading overlay element
     */
    function createSafeLoadingOverlay(options = {}) {
        const {
            iconClass = 'fa-spinner fa-spin fa-3x',
            title = 'Loading...',
            description = 'Please wait...',
            iconMarginBottom = '20px',
            titleMargin = '10px 0',
            textOpacity = '0.8'
        } = options;

        const overlay = document.createElement('div');
        overlay.className = 'ldr-loading-overlay';

        const content = document.createElement('div');
        content.className = 'ldr-loading-content';
        content.style.textAlign = 'center';

        // Icon
        const icon = document.createElement('i');
        icon.className = `fas ${iconClass}`;
        icon.style.marginBottom = iconMarginBottom;

        // Title
        const titleElement = document.createElement('h3');
        titleElement.style.margin = titleMargin;
        titleElement.textContent = title;

        // Description
        const descriptionElement = document.createElement('p');
        descriptionElement.style.opacity = textOpacity;
        descriptionElement.textContent = description;

        // Assemble overlay
        content.appendChild(icon);
        content.appendChild(titleElement);
        content.appendChild(descriptionElement);
        overlay.appendChild(content);

        return overlay;
    }

    /**
     * Safely set CSS styles on an element
     * @param {HTMLElement} element - The element to style
     * @param {Object} styles - Style object with CSS properties
     */
    function safeSetStyles(element, styles) {
        if (!element || !styles) return;

        Object.entries(styles).forEach(([property, value]) => {
            element.style[property] = value;
        });
    }

    /**
     * Show a safe alert message
     * @param {string} containerId - ID of the alert container
     * @param {string} message - Alert message (will be sanitized)
     * @param {string} type - Alert type (success, error, warning, info)
     * @param {Object|null} [options] - Alert behavior options passed to
     *     createSafeAlertElement
     */
    function showSafeAlert(containerId, message, type = 'info', options = {}) {
        const alertContainer = document.getElementById(containerId);
        if (!alertContainer) return;

        // Clear existing alerts
        while (alertContainer.firstChild) {
            alertContainer.removeChild(alertContainer.firstChild);
        }

        // Create alert using our secure method
        const alert = createSafeAlertElement(message, type, options);
        alertContainer.appendChild(alert);
        alertContainer.style.display = 'block';
    }

    // Also export individual functions for convenience
    window.escapeHtml = escapeHtml;
    window.escapeHtmlAttribute = escapeHtmlAttribute;
    window.safeSetInnerHTML = safeSetInnerHTML;
    window.safeCreateElement = safeCreateElement;
    window.safeSetTextContent = safeSetTextContent;
    window.createSafeAlertElement = createSafeAlertElement;
    window.sanitizeUserInput = sanitizeUserInput;
    window.sanitizeHtml = sanitizeHtml;

    // Export UI helper functions
    window.safeUpdateButton = safeUpdateButton;
    window.createSafeLoadingOverlay = createSafeLoadingOverlay;
    window.safeSetStyles = safeSetStyles;
    window.showSafeAlert = showSafeAlert;

})();