~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(security): escape settings dashboard metadata (#7050)
LearningCircuit1cfdbd197421
diff --git a/changelog.d/+settings-metadata-rendering.security.md b/changelog.d/+settings-metadata-rendering.security.mdnew file mode 100644index 0000000000..35ba153f92--- /dev/null+++ b/changelog.d/+settings-metadata-rendering.security.md@@ -0,0 +1 @@+Escape stored settings keys and JSON property names when rendering dashboard controls, preventing them from creating executable markup while preserving normal settings edits.diff --git a/src/local_deep_research/web/static/js/components/settings.js b/src/local_deep_research/web/static/js/components/settings.jsindex 1b5af55074..ebc07b0ac4 100644--- a/src/local_deep_research/web/static/js/components/settings.js+++ b/src/local_deep_research/web/static/js/components/settings.js@@ -249,31 +249,75 @@// Use global escapeHtml from xss-protection.js with fallbackconst escapeHtml = window.escapeHtml || escapeHtmlFallback;+ // Preserve existing IDs for registered keys. Encode all code units of+ // custom metadata so quotes cannot break attributes and dot/dash pairs+ // cannot collapse to the same control ID.+ function metadataId(prefix, value) {+ const encoded = String(value).split('')+ .map(codeUnit => codeUnit.charCodeAt(0).toString(16).padStart(4, '0'))+ .join('');+ return `${prefix}--x-${encoded}`;+ }++ function settingIdFor(key) {+ const raw = String(key);+ return /^\w+(?:\.\w+)*$/.test(raw)+ ? `setting-${raw.replace(/\./g, '-')}`+ : metadataId('setting', raw);+ }++ function jsonPropertyIdFor(settingId, property) {+ const raw = String(property);+ return /^\w+$/.test(raw)+ ? `${settingId}_${raw}`+ : metadataId(`${settingId}-property`, raw);+ }++ function sectionIdFor(type, category) {+ const plainType = String(type);+ const plainCategory = String(category);+ if (/^\w+$/.test(plainType) && /^\w+(?: \w+)*$/.test(plainCategory)) {+ return `section-${plainType}-${plainCategory.replace(/ /g, '-').toLowerCase()}`;+ }+ return metadataId('section', JSON.stringify([plainType, plainCategory]));+ }++ function jsonControls(parentKey, property) {+ return Array.from(document.querySelectorAll('.ldr-json-property-control'))+ .filter(control => control.dataset.parentKey === String(parentKey) &&+ (property === undefined || control.dataset.property === String(property)));+ }+/*** Helper function to generate custom dropdown HTML (similar to Jinja macro)* @param {object} params - Parameters for the dropdown* @returns {string} HTML string for the custom dropdown input part*/function renderCustomDropdownHTML(params) {+ const dropdownId = escapeHtml(params.dropdown_id);+ const inputId = escapeHtml(params.input_id);+ const settingKey = escapeHtml(params.data_setting_key || params.input_id);+ const placeholder = escapeHtml(params.placeholder || '');+ const labelId = params.label_id ? escapeHtml(params.label_id) : '';// Basic structure with input and list containerlet dropdownHTML = `- <div class="ldr-custom-dropdown" id="${params.dropdown_id}">+ <div class="ldr-custom-dropdown" id="${dropdownId}"><input type="text"- id="${params.input_id}"- data-key="${params.data_setting_key || params.input_id}"+ id="${inputId}"+ data-key="${settingKey}"class="ldr-custom-dropdown-input"- placeholder="${params.placeholder}"+ placeholder="${placeholder}"autocomplete="off"role="combobox"aria-haspopup="listbox"aria-expanded="false"aria-autocomplete="list"- aria-controls="${params.dropdown_id}-list"- ${params.label_id ? `aria-labelledby="${params.label_id}"` : ''}+ aria-controls="${dropdownId}-list"+ ${labelId ? `aria-labelledby="${labelId}"` : ''}${params.disabled ? "disabled" : ""}><!-- Hidden input that will be included in form submission -->- <input type="hidden" name="${params.input_id}" id="${params.input_id}_hidden" value="">- <div class="ldr-custom-dropdown-list" id="${params.dropdown_id}-list" role="listbox"${params.label_id ? ` aria-labelledby="${params.label_id}"` : ''}></div>+ <input type="hidden" name="${inputId}" id="${inputId}_hidden" value="">+ <div class="ldr-custom-dropdown-list" id="${dropdownId}-list" role="listbox"${labelId ? ` aria-labelledby="${labelId}"` : ''}></div></div>`;@@ -281,8 +325,8 @@const refreshButtonHTML = params.show_refresh ? `<button type="button"class="ldr-custom-dropdown-refresh-btn"- id="${params.input_id}-refresh"- aria-label="${params.refresh_aria_label || 'Refresh options'}">+ id="${inputId}-refresh"+ aria-label="${escapeHtml(params.refresh_aria_label || 'Refresh options')}"><i class="fas fa-sync-alt" aria-hidden="true"></i></button>` : '';@@ -1103,10 +1147,10 @@if (!parentKey || !property) return;// Find all controls for this parent JSON- const controls = document.querySelectorAll(`.ldr-json-property-control[data-parent-key="${parentKey}"]`);+ const controls = jsonControls(parentKey);// Create an object to hold the JSON data- const jsonData = {};+ const jsonData = Object.create(null);// Populate the object with values from all controlscontrols.forEach(control => {@@ -1132,7 +1176,8 @@});// Find the hidden input that stores the original JSON- const originalInput = document.getElementById(`${parentKey.replace(/\./g, '-')}_original`);+ const originalInput = document.getElementById(`${settingIdFor(parentKey)}_original`)+ || document.getElementById(`${parentKey.replace(/\./g, '-')}_original`);let originalJson = {};if (originalInput) {@@ -1174,12 +1219,20 @@// Update the UI even if we're not saving to the serverif (originalInput) {// Update the original JSON with new values- Object.assign(originalJson, jsonData);+ if (!originalJson || typeof originalJson !== 'object') {+ originalJson = {};+ }+ for (const [key, value] of Object.entries(jsonData)) {+ Object.defineProperty(originalJson, key, {+ value, enumerable: true, writable: true, configurable: true,+ });+ }originalInput.value = JSON.stringify(originalJson);}// Also update any textarea that might display this JSON- const jsonTextarea = document.getElementById(parentKey.replace(/\./g, '-'));+ const jsonTextarea = document.getElementById(settingIdFor(parentKey))+ || document.getElementById(parentKey.replace(/\./g, '-'));if (jsonTextarea && jsonTextarea.tagName === 'TEXTAREA') {jsonTextarea.value = JSON.stringify(jsonData, null, 2);}@@ -1269,7 +1322,7 @@// Handle JSON from expanded controlsdocument.querySelectorAll('input[id$="_original"]').forEach(input => {if (input.name.endsWith('_original')) {- const actualName = input.name.replace('_original', '');+ const actualName = input.name.slice(0, -'_original'.length);// Create a hidden input with the actual nameconst hiddenInput = document.createElement('input');@@ -1839,14 +1892,15 @@};// Group by prefix and category- const grouped = {};+ const grouped = Object.create(null);// Filter settings based on current tabconst filteredSettings = settings.filter(setting => {const parts = setting.key.split('.');const prefix = parts[0]; // app, llm, search, etc.const subKey = parts[1]; // The actual key name without prefix- const effectivePrefix = prefixToTab[prefix] || prefix;+ const effectivePrefix = Object.hasOwn(prefixToTab, prefix)+ ? prefixToTab[prefix] : prefix;// Filter out nested settings like app.llm, app.search, app.general, app.web, etc.if (prefix === 'app' && (subKey === 'llm' || subKey === 'search' || subKey === 'general' || subKey === 'web')) {@@ -1928,7 +1982,7 @@// Create namespace if neededif (!grouped[prefix]) {- grouped[prefix] = {};+ grouped[prefix] = Object.create(null);}// Use category or create one based on subkey@@ -1939,7 +1993,10 @@// For duplicate "general" categories, prefix with the typeif (category.toLowerCase() === 'general') {- category = `${typeMap[prefix] || prefix.charAt(0).toUpperCase() + prefix.slice(1)} General`;+ const displayType = Object.hasOwn(typeMap, prefix)+ ? typeMap[prefix]+ : prefix.charAt(0).toUpperCase() + prefix.slice(1);+ category = `${displayType} General`;}// Create category array if needed@@ -1960,7 +2017,8 @@// Identify high-priority categoriesconst highPriorityCategories = [];const otherCategories = [];- const priorityKeysForPrefix = prioritySettings[prefix] || [];+ const priorityKeysForPrefix = Object.hasOwn(prioritySettings, prefix)+ ? prioritySettings[prefix] : [];const highestPriorityKeys = ['provider', 'model', 'tool']; // Keys whose *containing category* should be firstcategories.forEach(category => {@@ -2004,7 +2062,7 @@const sortedCategoryNames = [...highPriorityCategories, ...otherCategories];// Create new object with sorted categories and sorted settings within each- const sortedPrefixedCategories = {};+ const sortedPrefixedCategories = Object.create(null);sortedCategoryNames.forEach(category => {sortedPrefixedCategories[category] = grouped[prefix][category];@@ -2012,7 +2070,8 @@sortedPrefixedCategories[category].sort((a, b) => {const aKey = a.key.split('.')[1];const bKey = b.key.split('.')[1];- const priorities = prioritySettings[prefix] || [];+ const priorities = Object.hasOwn(prioritySettings, prefix)+ ? prioritySettings[prefix] : [];const aIndex = priorities.indexOf(aKey);const bIndex = priorities.indexOf(bKey);if (aIndex !== -1 && bIndex !== -1) return aIndex - bIndex;@@ -2652,7 +2711,7 @@// innerHTML string below, so escape it — and the id derived// from it — before interpolation. Hardening of a pre-existing// template: `data-target`/`id` already carried the raw value.- const sectionId = escapeHtml(`section-${type}-${category.replace(/\s+/g, '-').toLowerCase()}`);+ const sectionId = sectionIdFor(type, category);const safeCategory = escapeHtml(category);html += `@@ -2794,7 +2853,8 @@// Log the setting being processedSafeLogger.log('Processing Setting:', setting.key, 'UI Element:', setting.ui_element);- const settingId = `setting-${setting.key.replace(/\./g, '-')}`;+ const settingId = settingIdFor(setting.key);+ const settingKeyAttribute = escapeHtml(setting.key);let inputElement;// Generate the appropriate input element based on UI element type@@ -2820,7 +2880,7 @@// cannot see it (handleInputChange honours this).const textareaPlaceholder = secrecyPlaceholder(setting);inputElement = `- <textarea id="${settingId}" name="${setting.key}"+ <textarea id="${settingId}" name="${settingKeyAttribute}"class="ldr-settings-textarea"placeholder="${escapeHtml(textareaPlaceholder)}"${isRedacted ? 'data-redacted="true"' : ''}@@ -2852,7 +2912,7 @@}inputElement = `- <textarea id="${settingId}" name="${setting.key}"+ <textarea id="${settingId}" name="${settingKeyAttribute}"class="ldr-settings-textarea${jsonClass}"${!setting.editable ? 'disabled' : ''}>${setting.value !== null ? escapeHtml(String(setting.value)) : ''}</textarea>@@ -2948,7 +3008,7 @@);}inputElement = `- <select id="${settingId}" name="${setting.key}"+ <select id="${settingId}" name="${settingKeyAttribute}"class="ldr-settings-select ldr-form-control"${!setting.editable ? 'disabled' : ''}>@@ -2966,12 +3026,12 @@<label class="ldr-checkbox-label" for="${settingId}"><!-- Hidden input ensures unchecked state is submitted --><input type="hidden"- name="${setting.key}"+ name="${settingKeyAttribute}"id="${hiddenFallbackId}"value="false"class="ldr-checkbox-hidden-fallback"><!-- Actual checkbox overrides hidden input when checked -->- <input type="checkbox" id="${settingId}" name="${setting.key}"+ <input type="checkbox" id="${settingId}" name="${settingKeyAttribute}"class="ldr-settings-checkbox"data-hidden-fallback="${hiddenFallbackId}"${checked}@@ -2992,10 +3052,10 @@inputElement = `<div class="ldr-settings-range-container">- <input type="range" id="${settingId}" name="${setting.key}"+ <input type="range" id="${settingId}" name="${settingKeyAttribute}"class="ldr-settings-range ldr-form-control"value="${escapeHtml(String(setting.value !== null ? setting.value : min))}"- min="${min}" max="${max}" step="${step}"+ min="${escapeHtml(String(min))}" max="${escapeHtml(String(max))}" step="${escapeHtml(String(step))}"${!setting.editable ? 'disabled' : ''}><span class="ldr-settings-range-value">${escapeHtml(String(setting.value !== null ? setting.value : min))}</span>@@ -3010,10 +3070,10 @@const numStep = setting.step !== null ? setting.step : 1;inputElement = `- <input type="number" id="${settingId}" name="${setting.key}"+ <input type="number" id="${settingId}" name="${settingKeyAttribute}"class="ldr-settings-input ldr-form-control"value="${escapeHtml(String(setting.value !== null ? setting.value : ''))}"- min="${numMin}" max="${numMax}" step="${numStep}"+ min="${escapeHtml(String(numMin))}" max="${escapeHtml(String(numMax))}" step="${escapeHtml(String(numStep))}"${!setting.editable ? 'disabled' : ''}>`;@@ -3041,7 +3101,7 @@const placeholder = secrecyPlaceholder(setting);inputElement = `<input type="password"- id="${settingId}" name="${setting.key}"+ id="${settingId}" name="${settingKeyAttribute}"class="ldr-settings-input ldr-form-control"value=""autocomplete="new-password"@@ -3057,7 +3117,7 @@// rejected by the route handler).inputElement = `<input type="text"- id="${settingId}" name="${setting.key}"+ id="${settingId}" name="${settingKeyAttribute}"class="ldr-settings-input ldr-form-control"value=""placeholder="${escapeHtml(secrecyPlaceholder(setting))}"@@ -3069,7 +3129,7 @@// Default to text inputinputElement = `<input type="text"- id="${settingId}" name="${setting.key}"+ id="${settingId}" name="${settingKeyAttribute}"class="ldr-settings-input ldr-form-control"value="${escapeHtml(String(setting.value !== null ? setting.value : ''))}"${!setting.editable ? 'disabled' : ''}@@ -3088,7 +3148,7 @@// For checkboxes, we've already handled the label in the inputElementif (setting.ui_element === 'checkbox') {return `- <div class="ldr-settings-item form-group" data-key="${setting.key}">+ <div class="ldr-settings-item form-group" data-key="${settingKeyAttribute}">${inputElement}${setting.description ? `<div class="ldr-input-help">@@ -3104,7 +3164,7 @@const helpTextHTML = setting.description ? `<div class="ldr-input-help">${escapeHtml(setting.description)}</div>` : '';return `- <div class="ldr-settings-item form-group" data-key="${setting.key}">+ <div class="ldr-settings-item form-group" data-key="${settingKeyAttribute}"><div class="ldr-settings-item-header"><label for="${settingId}" id="${settingId}-label" title="${escapeHtml(settingName)}">${escapeHtml(settingName)}@@ -3124,24 +3184,27 @@* @returns {string} - The HTML for the expanded JSON controls*/function renderExpandedJsonControls(setting, settingId, jsonObj) {+ const settingKeyAttribute = escapeHtml(setting.key);let html = `- <div class="ldr-settings-item form-group" data-key="${setting.key}">+ <div class="ldr-settings-item form-group" data-key="${settingKeyAttribute}"><div class="ldr-settings-item-header"><label for="${settingId}" title="${escapeHtml(setting.name)}">${escapeHtml(setting.name)}</label></div><div class="ldr-json-expanded-controls">- <input type="hidden" id="${settingId}_original" name="${setting.key}_original"+ <input type="hidden" id="${settingId}_original" name="${escapeHtml(`${setting.key}_original`)}"value="${escapeHtml(JSON.stringify(jsonObj))}"><div class="ldr-json-property-controls">`;// Create individual form controls for each JSON property- for (const key in jsonObj) {+ for (const key of Object.keys(jsonObj)) {const value = jsonObj[key];- const controlId = `${settingId}_${key}`;+ const controlId = jsonPropertyIdFor(settingId, key);+ const propertyKeyAttribute = escapeHtml(key);+ const compoundNameAttribute = escapeHtml(`${setting.key}_${key}`);const formattedName = formatPropertyName(key);let controlHtml;@@ -3154,21 +3217,21 @@<label class="ldr-checkbox-label" for="${controlId}"><!-- Hidden input ensures unchecked state is submitted --><input type="hidden"- name="${setting.key}_${key}"+ name="${compoundNameAttribute}"id="${hiddenFallbackId}"value="false"class="ldr-checkbox-hidden-fallback"><!-- Actual checkbox overrides hidden input when checked --><input type="checkbox"id="${controlId}"- name="${setting.key}_${key}"+ name="${compoundNameAttribute}"class="ldr-json-property-control ldr-settings-checkbox"- data-property="${key}"- data-parent-key="${setting.key}"+ data-property="${propertyKeyAttribute}"+ data-parent-key="${settingKeyAttribute}"data-hidden-fallback="${hiddenFallbackId}"${value ? 'checked' : ''}${!setting.editable ? 'disabled' : ''}>- <span class="ldr-checkbox-text">${formattedName}</span>+ <span class="ldr-checkbox-text">${escapeHtml(formattedName)}</span></label></div></div>@@ -3176,13 +3239,13 @@} else if (typeof value === 'number') {controlHtml = `<div class="ldr-json-property-item">- <label for="${controlId}" class="ldr-property-label" title="${formattedName}">${formattedName}</label>+ <label for="${controlId}" class="ldr-property-label" title="${escapeHtml(formattedName)}">${escapeHtml(formattedName)}</label><input type="number"id="${controlId}"- name="${setting.key}_${key}"+ name="${compoundNameAttribute}"class="ldr-settings-input ldr-form-control ldr-json-property-control"- data-property="${key}"- data-parent-key="${setting.key}"+ data-property="${propertyKeyAttribute}"+ data-parent-key="${settingKeyAttribute}"value="${value}"${!setting.editable ? 'disabled' : ''}></div>@@ -3190,12 +3253,12 @@} else if (typeof value === 'string' && (value === 'ITERATION' || value === 'NONE')) {controlHtml = `<div class="ldr-json-property-item">- <label for="${controlId}" class="ldr-property-label" title="${formattedName}">${formattedName}</label>+ <label for="${controlId}" class="ldr-property-label" title="${escapeHtml(formattedName)}">${escapeHtml(formattedName)}</label><select id="${controlId}"- name="${setting.key}_${key}"+ name="${compoundNameAttribute}"class="ldr-settings-select ldr-form-control ldr-json-property-control"- data-property="${key}"- data-parent-key="${setting.key}"+ data-property="${propertyKeyAttribute}"+ data-parent-key="${settingKeyAttribute}"${!setting.editable ? 'disabled' : ''}><option value="ITERATION" ${value === 'ITERATION' ? 'selected' : ''}>Iteration</option><option value="NONE" ${value === 'NONE' ? 'selected' : ''}>None</option>@@ -3205,13 +3268,13 @@} else {controlHtml = `<div class="ldr-json-property-item">- <label for="${controlId}" class="ldr-property-label" title="${formattedName}">${formattedName}</label>+ <label for="${controlId}" class="ldr-property-label" title="${escapeHtml(formattedName)}">${escapeHtml(formattedName)}</label><input type="text"id="${controlId}"- name="${setting.key}_${key}"+ name="${compoundNameAttribute}"class="ldr-settings-input ldr-form-control ldr-json-property-control"- data-property="${key}"- data-parent-key="${setting.key}"+ data-property="${propertyKeyAttribute}"+ data-parent-key="${settingKeyAttribute}"value="${escapeHtml(String(value))}"${!setting.editable ? 'disabled' : ''}></div>@@ -3296,11 +3359,11 @@// Process expanded JSON controlsdocument.querySelectorAll('input[id$="_original"]').forEach(input => {if (input.name && input.name.endsWith('_original')) {- const actualName = input.name.replace('_original', '');+ const actualName = input.name.slice(0, -'_original'.length);// Get all controls for this setting- const jsonData = {};- const controls = document.querySelectorAll(`.ldr-json-property-control[data-parent-key="${actualName}"]`);+ const jsonData = Object.create(null);+ const controls = jsonControls(actualName);controls.forEach(control => {const propName = control.dataset.property;@@ -3877,7 +3940,7 @@// innerHTML string below, so escape it — and the id derived// from it — before interpolation. Hardening of a pre-existing// template: `data-target`/`id` already carried the raw value.- const sectionId = escapeHtml(`section-${type}-${category.replace(/\s+/g, '-').toLowerCase()}`);+ const sectionId = sectionIdFor(type, category);const safeCategory = escapeHtml(category);html += `@@ -3977,7 +4040,7 @@// Reset expanded JSON controlsdocument.querySelectorAll('input[id$="_original"]').forEach(input => {if (input.name.endsWith('_original')) {- const actualName = input.name.replace('_original', '');+ const actualName = input.name.slice(0, -'_original'.length);const originalValue = originalSettings[actualName];if (originalValue) {@@ -4002,7 +4065,7 @@// Update individual controlsfor (const prop in jsonData) {- const control = document.querySelector(`.ldr-json-property-control[data-parent-key="${actualName}"][data-property="${prop}"]`);+ const control = jsonControls(actualName, prop)[0];if (control) {if (control.type === 'checkbox') {control.checked = !!jsonData[prop];diff --git a/tests/js/security/settings-render-xss.test.js b/tests/js/security/settings-render-xss.test.jsnew file mode 100644index 0000000000..a1b149ca8b--- /dev/null+++ b/tests/js/security/settings-render-xss.test.js@@ -0,0 +1,250 @@+/**+ * Exercise the settings dashboard's real API-to-DOM render path with hostile+ * stored metadata. Adapted from the settings regression suite in PR #5632.+ */++const WINDOW_KEYS = [+ 'escapeHtml', 'escapeHtmlAttribute', 'ui', 'modelProvidersRequestInProgress',+ 'searchEnginesRequestInProgress', 'modelDropdownsInitialized',+ 'searchEngineDropdownInitialized', '__LDR_MOBILE_BREAKPOINT_PX',+ 'setupSettingsDropdowns', 'LdrValueHelpers', 'LdrProviderOptions', 'URLS',+];+const ORIGINAL_WINDOW_STATE = new Map(+ WINDOW_KEYS.map(key => [key, { owned: Object.hasOwn(window, key), value: window[key] }])+);+const ORIGINAL_HEAD_NODES = [...document.head.childNodes].map(node => node.cloneNode(true));+const ORIGINAL_BODY_NODES = [...document.body.childNodes].map(node => node.cloneNode(true));++const CATEGORY = 'X"onmouseover="alert(1)';+const SETTING_KEY = 'app.x"onfocus="alert(2)';+const JSON_SETTING_KEY = 'app.json"onfocus="alert(3)';+const JSON_PROPERTY = 'name"][onclick="alert(4)';+const SPECIAL_JSON_SETTING_KEY = 'app.json_original.extra';+const SPECIAL_JSON_PROPERTY = '__proto__';+const REDACTED_KEY = 'app.x"onfocus="alert(5).api_key';+const COLLIDING_DOTTED_KEY = 'app.collision.probe';+const COLLIDING_DASHED_KEY = 'app.collision-probe';++function setting(name, value, uiElement = 'text', extra = {}) {+ return {+ name, value, ui_element: uiElement, category: 'app_parameters',+ description: 'A setting', editable: true, visible: true, ...extra,+ };+}++function payload() {+ return {+ status: 'success',+ settings: {+ 'app.benign_control': setting('Benign Control', 'benign'),+ 'app.hostile_category': setting('Hostile Category Surface', 'safe', 'text', {+ category: CATEGORY,+ }),+ [SETTING_KEY]: setting('Hostile Key', 1, 'number', {+ min_value: CATEGORY, max_value: CATEGORY, step: CATEGORY,+ }),+ [REDACTED_KEY]: setting('Redacted Key', '[REDACTED]'),+ [JSON_SETTING_KEY]: setting('JSON Key', JSON.stringify({+ [JSON_PROPERTY]: 'old value',+ '<img src=x onerror=alert(6)>': true,+ }), 'json'),+ [SPECIAL_JSON_SETTING_KEY]: setting('Special JSON Key', JSON.stringify({+ [SPECIAL_JSON_PROPERTY]: 'old value',+ keep: 'unchanged',+ }), 'json'),+ [COLLIDING_DOTTED_KEY]: setting('Dotted Key', 'dotted'),+ [COLLIDING_DASHED_KEY]: setting('Dashed Key', 'dashed'),+ 'constructor.alpha': setting('Prototype Prefix A', 'a', 'text', {+ category: 'general',+ }),+ 'constructor.beta': setting('Prototype Prefix B', 'b', 'text', {+ category: 'general',+ }),+ },+ };+}++function onAttributes(root) {+ const found = [];+ root.querySelectorAll('*').forEach(element => {+ for (const attribute of element.attributes) {+ if (/^on/i.test(attribute.name)) found.push(attribute.name);+ }+ });+ return found;+}++async function renderSettings({ searchValue } = {}) {+ document.body.innerHTML = `+ <form id="settings-form">+ <input id="settings-search" type="text">+ <button type="button" class="ldr-settings-tab" data-tab="all">All</button>+ <button type="button" class="ldr-settings-tab" data-tab="app">App</button>+ <div id="settings-alert"></div>+ <div id="settings-content"></div>+ <div id="raw-config" style="display: none">+ <textarea id="raw-config-editor"></textarea>+ </div>+ </form>+ `;+ await import('@js/config/urls.js');+ await import('@js/services/api.js');+ await import('@js/utils/alert-helpers.js');+ await import('@js/utils/provider-options.js');+ await import('@js/utils/value-helpers.js');+ await import('@js/security/xss-protection.js');+ expect(typeof window.escapeHtml).toBe('function');+ window.api.fetchWithErrorHandling = vi.fn(async () => payload());+ vi.stubGlobal('fetch', vi.fn(async () => new Response('{}', { status: 200 })));++ vi.useFakeTimers();+ try {+ vi.resetModules();+ await import('@js/components/settings.js');+ document.dispatchEvent(new Event('DOMContentLoaded'));+ await vi.advanceTimersByTimeAsync(400);+ if (searchValue !== undefined) {+ const search = document.getElementById('settings-search');+ search.value = searchValue;+ search.dispatchEvent(new Event('input'));+ await vi.advanceTimersByTimeAsync(300);+ }+ } finally {+ vi.useRealTimers();+ }+ const content = document.getElementById('settings-content');+ expect(content.innerHTML).not.toBe('');+ return content;+}++afterEach(() => {+ vi.restoreAllMocks();+ vi.unstubAllGlobals();+ vi.useRealTimers();+ document.head.replaceChildren(...ORIGINAL_HEAD_NODES.map(node => node.cloneNode(true)));+ document.body.replaceChildren(...ORIGINAL_BODY_NODES.map(node => node.cloneNode(true)));+ ORIGINAL_WINDOW_STATE.forEach((state, key) => {+ if (state.owned) window[key] = state.value;+ else delete window[key];+ });+});++it.each([+ ['initial', undefined],+ ['search', 'hostile category surface'],+])('keeps stored metadata inert in the %s render', async (_label, searchValue) => {+ const content = await renderSettings({ searchValue });+ const title = Array.from(content.querySelectorAll('.ldr-settings-section-title'))+ .find(element => element.textContent.trim() === CATEGORY);+ expect(title).toBeDefined();+ expect(title.getAttribute('title')).toBe(CATEGORY);+ expect(onAttributes(content)).toEqual([]);+ expect(content.querySelector('img, svg, script')).toBeNull();+ const header = title.closest('.ldr-settings-section-header');+ expect(header.dataset.target).toMatch(/^[a-z][\w-]*$/i);+ expect(document.getElementById(header.dataset.target)).not.toBeNull();+});++it('round-trips hostile keys as data with distinct selector-safe IDs', async () => {+ const content = await renderSettings();+ const inputs = Array.from(content.querySelectorAll('input.ldr-settings-input'));+ const hostile = inputs.find(input => input.name === SETTING_KEY);+ const redacted = inputs.find(input => input.name === REDACTED_KEY);+ const dotted = inputs.find(input => input.name === COLLIDING_DOTTED_KEY);+ const dashed = inputs.find(input => input.name === COLLIDING_DASHED_KEY);+ const benign = document.getElementById('setting-app-benign_control');+ expect(hostile).toBeDefined();+ expect(hostile.closest('.ldr-settings-item').dataset.key).toBe(SETTING_KEY);+ for (const attribute of ['min', 'max', 'step']) {+ expect(hostile.getAttribute(attribute)).toBe(CATEGORY);+ }+ expect(redacted?.value).toBe('');+ expect(redacted?.dataset.redacted).toBe('true');+ expect(dotted?.id).not.toBe(dashed?.id);+ expect(benign?.value).toBe('benign');+ for (const input of [hostile, redacted, dotted, dashed]) {+ expect(input.id).toMatch(/^[a-z][\w-]*$/i);+ }+ expect(new Set(Array.from(content.querySelectorAll('[id]'), element => element.id)).size)+ .toBe(content.querySelectorAll('[id]').length);+ expect(onAttributes(content)).toEqual([]);+});++it('edits JSON properties under quote-bearing setting keys without selector injection', async () => {+ const content = await renderSettings();+ const control = Array.from(content.querySelectorAll('.ldr-json-property-control'))+ .find(input => input.dataset.parentKey === JSON_SETTING_KEY+ && input.dataset.property === JSON_PROPERTY);+ expect(control).toBeDefined();+ expect(control.name).toBe(`${JSON_SETTING_KEY}_${JSON_PROPERTY}`);+ const original = control.closest('.ldr-settings-item')+ .querySelector('input[id$="_original"]');+ expect(original).not.toBeNull();+ expect(onAttributes(content)).toEqual([]);++ vi.useFakeTimers();+ control.value = 'new value';+ control.dispatchEvent(new Event('input', { bubbles: true }));+ expect(JSON.parse(original.value)[JSON_PROPERTY]).toBe('new value');++ const saves = [];+ window.api.fetchWithErrorHandling = vi.fn(async (url, options) => {+ saves.push({ url, options });+ return { status: 'success', settings: {} };+ });+ document.getElementById('settings-form')+ .dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));+ await Promise.resolve();+ await Promise.resolve();+ const save = saves.find(request => request.url === URLS.SETTINGS_API.SAVE_ALL_SETTINGS);+ expect(save).toBeDefined();+ expect(JSON.parse(save.options.body)[JSON_SETTING_KEY][JSON_PROPERTY])+ .toBe('new value');+});++it('preserves prototype-shaped JSON properties under suffix-bearing setting keys', async () => {+ const content = await renderSettings();+ const control = Array.from(content.querySelectorAll('.ldr-json-property-control'))+ .find(input => input.dataset.parentKey === SPECIAL_JSON_SETTING_KEY+ && input.dataset.property === SPECIAL_JSON_PROPERTY);+ expect(control).toBeDefined();+ const original = control.closest('.ldr-settings-item')+ .querySelector('input[id$="_original"]');+ expect(original).not.toBeNull();++ vi.useFakeTimers();+ control.value = 'new value';+ control.dispatchEvent(new Event('input', { bubbles: true }));+ const updated = JSON.parse(original.value);+ expect(Object.hasOwn(updated, SPECIAL_JSON_PROPERTY)).toBe(true);+ expect(updated[SPECIAL_JSON_PROPERTY]).toBe('new value');+ expect(updated.keep).toBe('unchanged');++ const saves = [];+ window.api.fetchWithErrorHandling = vi.fn(async (url, options) => {+ saves.push({ url, options });+ return { status: 'success', settings: {} };+ });+ document.getElementById('settings-form')+ .dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));+ await Promise.resolve();+ await Promise.resolve();+ const save = saves.find(request => request.url === URLS.SETTINGS_API.SAVE_ALL_SETTINGS);+ expect(save).toBeDefined();+ const saved = JSON.parse(save.options.body)[SPECIAL_JSON_SETTING_KEY];+ expect(Object.hasOwn(saved, SPECIAL_JSON_PROPERTY)).toBe(true);+ expect(saved[SPECIAL_JSON_PROPERTY]).toBe('new value');+ expect(saved.keep).toBe('unchanged');+});++it('treats prototype-like legacy prefixes as setting metadata', async () => {+ const content = await renderSettings();+ const first = content.querySelector('input[name="constructor.alpha"]');+ const second = content.querySelector('input[name="constructor.beta"]');+ expect(first).not.toBeNull();+ expect(second).not.toBeNull();+ expect(first.closest('.ldr-settings-section').contains(second)).toBe(true);+ expect(first.closest('.ldr-settings-section')+ .querySelector('.ldr-settings-section-title').textContent.trim())+ .toBe('Constructor General');+});