~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
/**
* Settings accordion (collapse/expand) behavior in components/settings.js
*
* Context: "All Settings" rendered fully expanded on desktop at ~77,000px
* tall — every one of ~57 sections (search engine integrations, each with
* 10-15 fields, JSON textareas, etc.) rendered open at once. Mobile already
* fixed this by starting every section collapsed via `initAccordions()`;
* desktop kept the old "all expanded" default. This fix makes both
* viewports default to collapsed by reusing the same mechanism (no new
* parallel accordion implementation), and adds:
* - `role="button"`/`tabindex="0"`/`aria-expanded`/`aria-controls` on the
* section header (keyboard operable, matches the existing
* `role="button"` + `aria-expanded` convention already used by
* services/help.js's collapsible panels).
* - Per-section collapsed-state persistence via localStorage, matching
* the established `ldr_panel_collapsed_<id>` pattern in help.js.
* - An explicit-override escape hatch (`{ defaultCollapsed: false }`) that
* the search filter rebuild already relied on to force every surviving
* section open — this must keep working and must NOT read/write the
* persisted per-section preference (a forced-open search result must
* not permanently overwrite what the user chose while browsing).
*
* Strategy: settings.js is an unexported IIFE, so (like
* settings-fetch-error-handling.test.js) we dynamically import the real
* module into a happy-dom document, dispatch DOMContentLoaded to run its
* real bootstrapping, and assert against the real rendered DOM. Each test
* gets a fresh module instance via `vi.resetModules()` so localStorage
* seeded before import faithfully simulates "returning after a page load".
*/
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import '@js/config/urls.js';
import '@js/services/api.js';
import '@js/utils/alert-helpers.js';
import '@js/utils/provider-options.js';
import '@js/utils/value-helpers.js';
const SETTINGS_PATH = resolve(
__dirname,
'../../../src/local_deep_research/web/static/js/components/settings.js',
);
const SETTINGS_SOURCE = readFileSync(SETTINGS_PATH, 'utf8');
const SETTINGS_PAYLOAD = {
'app.setting_one': {
name: 'Setting One', description: 'first setting', category: 'group_alpha',
value: 'a', ui_element: 'text', editable: true, visible: true,
},
'app.setting_two': {
name: 'Setting Two', description: 'second setting', category: 'group_beta',
value: 'b', ui_element: 'text', editable: true, visible: true,
},
'search.setting_three': {
name: 'Setting Three', description: 'iterations control', category: 'group_gamma',
value: 'c', ui_element: 'text', editable: true, visible: true,
},
};
function buildDom() {
document.head.innerHTML = '<meta name="csrf-token" content="test-csrf">';
document.body.innerHTML = `
<form id="settings-form">
<div class="ldr-settings-tabs">
<div class="ldr-settings-tab active" data-tab="all">All Settings</div>
<div class="ldr-settings-tab" data-tab="app">Application</div>
</div>
<input type="text" id="settings-search">
<div id="settings-alert"></div>
<div id="settings-content"></div>
</form>
`;
}
/**
* The escaper the page really runs with: security/xss-protection.js installs
* this on `window.escapeHtml`, and settings.js captures whatever is on window
* at import time (`const escapeHtml = window.escapeHtml || escapeHtmlFallback;`).
* Kept byte-equivalent to that implementation, including the `/` escape.
*/
const HTML_ESCAPE_MAP = {
'&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/',
};
const realEscapeHtml = (text) => (
text === null || text === undefined
? ''
: String(text).replace(/[&<>"'/]/g, (match) => HTML_ESCAPE_MAP[match])
);
function stubFetch(payload = SETTINGS_PAYLOAD) {
return vi.fn((url) => {
if (url === URLS.SETTINGS_API.BASE) {
return Promise.resolve(new Response(
JSON.stringify({ status: 'success', settings: payload }),
{ status: 200 },
));
}
if (url === '/settings/api/data-location') {
return Promise.resolve(new Response(JSON.stringify({
data_directory: '/tmp/ldr',
security_notice: { encrypted: false },
}), { status: 200 }));
}
if (url === URLS.SETTINGS_API.BACKUP_STATUS) {
return Promise.resolve(new Response(JSON.stringify({
enabled: false, count: 0, backups: [],
}), { status: 200 }));
}
if (url === URLS.SETTINGS_API.AVAILABLE_MODELS) {
return Promise.resolve(new Response(
'{"providers":{},"provider_options":[]}', { status: 200 },
));
}
if (url === URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES) {
return Promise.resolve(new Response(
'{"engine_options":[]}', { status: 200 },
));
}
throw new Error(`Unexpected request: ${url}`);
});
}
/**
* Dynamically (re)import settings.js into a fresh DOM and drive its real
* bootstrapping until the initial "All Settings" render (and therefore
* `initAccordions()`) has run.
*
* @param {Function} [prepareDom] - Optional hook run after the base fixture
* is built but *before* settings.js is imported, for tests that need extra
* markup present at bootstrap time.
* @param {Object} [options]
* @param {Object} [options.payload] - Settings payload /settings/api returns,
* defaulting to SETTINGS_PAYLOAD.
* @param {Function} [options.escapeHtml] - What to install on
* `window.escapeHtml` *before* the import, since settings.js captures it
* once at import time. Defaults to the identity stub the DOM-shape tests
* want; the escaping test passes the real escaper.
*/
async function loadSettingsPage(prepareDom, options = {}) {
vi.resetModules();
vi.useFakeTimers();
buildDom();
if (prepareDom) prepareDom();
window.escapeHtml = options.escapeHtml || (value => String(value));
window.ui = null;
window.modelProvidersRequestInProgress = null;
window.searchEnginesRequestInProgress = null;
const fetchMock = stubFetch(options.payload);
vi.stubGlobal('fetch', fetchMock);
await import('@js/components/settings.js');
document.dispatchEvent(new Event('DOMContentLoaded'));
await Promise.resolve();
await Promise.resolve();
await vi.advanceTimersByTimeAsync(151);
return fetchMock;
}
afterEach(() => {
vi.unstubAllGlobals();
vi.useRealTimers();
localStorage.clear();
// The deep-link test below sets a fragment; clear it so it cannot leak
// into another test's render. A no-op when the hash is already empty.
window.location.hash = '';
});
describe('settings.js source: accordion markup carries aria wiring', () => {
// Cheap source-level guard: every one of the four places that build a
// `.ldr-settings-section-header` string must carry the aria triad,
// independent of the heavier DOM tests below.
it('every section header template includes role/tabindex/aria-expanded/aria-controls', () => {
const headerOpenTags = SETTINGS_SOURCE.match(
/<div class="ldr-settings-section-header"[^>]*>/g,
) || [];
expect(headerOpenTags.length).toBeGreaterThanOrEqual(4);
headerOpenTags.forEach(tag => {
expect(tag).toContain('role="button"');
expect(tag).toContain('tabindex="0"');
expect(tag).toContain('aria-expanded=');
// Regex (not a plain string) so eslint's no-template-curly-in-string
// rule doesn't mistake this literal-source-text assertion for an
// accidentally unwrapped template literal.
expect(tag).toMatch(/aria-controls="\$\{sectionId(?:Attribute)?\}"/);
});
});
});
describe('settings accordion — default collapsed state (desktop and mobile alike)', () => {
it('renders multiple sections, all collapsed by default, with aria-expanded=false', async () => {
await loadSettingsPage();
const headers = Array.from(document.querySelectorAll('.ldr-settings-section-header'));
expect(headers.length).toBeGreaterThanOrEqual(4); // 3 setting groups + data-location + backup-status
headers.forEach(header => {
expect(header.classList.contains('collapsed')).toBe(true);
expect(header.getAttribute('aria-expanded')).toBe('false');
const body = document.getElementById(header.dataset.target);
expect(body).not.toBeNull();
expect(body.classList.contains('collapsed')).toBe(true);
});
});
it('clicking a header expands only that section and flips its aria-expanded', async () => {
await loadSettingsPage();
const headers = Array.from(document.querySelectorAll('.ldr-settings-section-header'));
const [first, second] = headers;
first.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(first.classList.contains('collapsed')).toBe(false);
expect(first.getAttribute('aria-expanded')).toBe('true');
expect(document.getElementById(first.dataset.target).classList.contains('collapsed')).toBe(false);
// Unrelated sections are untouched.
expect(second.classList.contains('collapsed')).toBe(true);
expect(second.getAttribute('aria-expanded')).toBe('false');
// Clicking again re-collapses.
first.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(first.classList.contains('collapsed')).toBe(true);
expect(first.getAttribute('aria-expanded')).toBe('false');
});
it('Enter and Space toggle a focused header (keyboard accessible)', async () => {
await loadSettingsPage();
const header = document.querySelector('.ldr-settings-section-header');
expect(header.getAttribute('tabindex')).toBe('0');
expect(header.getAttribute('role')).toBe('button');
const enterEvent = new KeyboardEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true });
header.dispatchEvent(enterEvent);
expect(header.classList.contains('collapsed')).toBe(false);
expect(enterEvent.defaultPrevented).toBe(true);
const spaceEvent = new KeyboardEvent('keydown', { key: ' ', bubbles: true, cancelable: true });
header.dispatchEvent(spaceEvent);
expect(header.classList.contains('collapsed')).toBe(true);
expect(spaceEvent.defaultPrevented).toBe(true);
// A non-activation key does nothing.
const tabEvent = new KeyboardEvent('keydown', { key: 'Tab', bubbles: true, cancelable: true });
header.dispatchEvent(tabEvent);
expect(header.classList.contains('collapsed')).toBe(true);
expect(tabEvent.defaultPrevented).toBe(false);
});
it('ignores auto-repeat keydowns so a held key does not flap the section', async () => {
await loadSettingsPage();
const header = document.querySelector('.ldr-settings-section-header');
expect(header.classList.contains('collapsed')).toBe(true);
// First keydown of a held Space activates once...
const first = new KeyboardEvent('keydown', { key: ' ', bubbles: true, cancelable: true });
header.dispatchEvent(first);
expect(header.classList.contains('collapsed')).toBe(false);
// ...and the OS auto-repeats that follow must not toggle again,
// while still swallowing the key so the page doesn't scroll.
for (let i = 0; i < 5; i++) {
const repeat = new KeyboardEvent('keydown', {
key: ' ', repeat: true, bubbles: true, cancelable: true,
});
header.dispatchEvent(repeat);
expect(repeat.defaultPrevented).toBe(true);
}
expect(header.classList.contains('collapsed')).toBe(false);
expect(header.getAttribute('aria-expanded')).toBe('true');
});
});
describe('settings accordion — persistence across page loads', () => {
it('persists a manual expand/collapse choice to localStorage under a stable per-section key', async () => {
await loadSettingsPage();
const header = document.querySelector('.ldr-settings-section-header');
const sectionId = header.dataset.target;
header.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(localStorage.getItem(`ldr_settings_section_collapsed_${sectionId}`)).toBe('false');
header.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(localStorage.getItem(`ldr_settings_section_collapsed_${sectionId}`)).toBe('true');
});
it('honors a previously persisted "expanded" preference on the next page load', async () => {
// First load: discover the section id and its initial (collapsed)
// state, matching a real user's first visit.
await loadSettingsPage();
const targetId = document.querySelector('.ldr-settings-section-header').dataset.target;
// Simulate the user having expanded that one section previously —
// seed localStorage exactly the way a real click would have, then
// reload the page (fresh module import, fresh DOM).
localStorage.setItem(`ldr_settings_section_collapsed_${targetId}`, 'false');
await loadSettingsPage();
const headers = Array.from(document.querySelectorAll('.ldr-settings-section-header'));
const remembered = headers.find(h => h.dataset.target === targetId);
const others = headers.filter(h => h.dataset.target !== targetId);
expect(remembered.classList.contains('collapsed')).toBe(false);
expect(remembered.getAttribute('aria-expanded')).toBe('true');
// Sections with no stored preference still default to collapsed.
others.forEach(h => {
expect(h.classList.contains('collapsed')).toBe(true);
});
});
});
describe('settings accordion — search interaction', () => {
it('search expands the matching section and preserves its saved preference while toggling', async () => {
await loadSettingsPage();
const matchingInput = document.getElementById('setting-search-setting_three');
expect(matchingInput).not.toBeNull();
const matchingHeader = matchingInput.closest('.ldr-settings-section')
.querySelector('.ldr-settings-section-header');
const targetId = matchingHeader.dataset.target;
const storageKey = `ldr_settings_section_collapsed_${targetId}`;
localStorage.setItem(storageKey, 'true');
const searchInput = document.getElementById('settings-search');
searchInput.value = 'iterations'; // matches search.setting_three's description
searchInput.dispatchEvent(new Event('input', { bubbles: true }));
// handleSearchInput is debounced 250ms.
await vi.advanceTimersByTimeAsync(260);
const headers = Array.from(document.querySelectorAll('.ldr-settings-section-header'));
expect(headers.length).toBeGreaterThan(0);
headers.forEach(header => {
expect(header.classList.contains('collapsed')).toBe(false);
expect(header.getAttribute('aria-expanded')).toBe('true');
const body = document.getElementById(header.dataset.target);
expect(body.classList.contains('collapsed')).toBe(false);
});
const matchedInput = document.getElementById('setting-search-setting_three');
expect(matchedInput).not.toBeNull();
expect(matchedInput.closest('.ldr-settings-section-body').id).toBe(targetId);
expect(localStorage.getItem(storageKey)).toBe('true');
const searchHeader = matchedInput.closest('.ldr-settings-section')
.querySelector('.ldr-settings-section-header');
for (const collapsed of [true, false]) {
searchHeader.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(searchHeader.classList.contains('collapsed')).toBe(collapsed);
expect(searchHeader.getAttribute('aria-expanded')).toBe(String(!collapsed));
expect(localStorage.getItem(storageKey)).toBe('true');
}
});
it('clearing search restores a saved expanded section and defaults the others to collapsed', async () => {
await loadSettingsPage();
const expandedHeader = document.querySelector('.ldr-settings-section-header');
const expandedId = expandedHeader.dataset.target;
expandedHeader.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(localStorage.getItem(`ldr_settings_section_collapsed_${expandedId}`)).toBe('false');
const searchInput = document.getElementById('settings-search');
searchInput.value = 'iterations';
searchInput.dispatchEvent(new Event('input', { bubbles: true }));
await vi.advanceTimersByTimeAsync(260);
searchInput.value = '';
searchInput.dispatchEvent(new Event('input', { bubbles: true }));
await vi.advanceTimersByTimeAsync(260); // handleSearchInput is debounced 250ms
const headers = Array.from(document.querySelectorAll('.ldr-settings-section-header'));
expect(headers.length).toBeGreaterThan(0);
expect(headers.some(header => header.dataset.target === expandedId)).toBe(true);
headers.forEach(header => {
const collapsed = header.dataset.target !== expandedId;
expect(header.classList.contains('collapsed')).toBe(collapsed);
expect(header.getAttribute('aria-expanded')).toBe(String(!collapsed));
});
expect(localStorage.getItem(`ldr_settings_section_collapsed_${expandedId}`)).toBe('false');
});
});
describe('settings accordion — programmatic reveals and repeated init', () => {
it('expands the section a #hash deep link points into, without persisting a preference', async () => {
// /settings#setting-<key> links are emitted by
// security/egress/warnings.py and the library/download-manager pages.
// With every section collapsed by default the target renders inside
// `display: none`, so the browser's own fragment scroll (and
// find-in-page) land nowhere. On the unedited head nothing reads
// location.hash at any revision, so the deep-linked section stays
// collapsed and the first expect below fails.
window.location.hash = '#setting-search-setting_three';
await loadSettingsPage();
const deepLinked = document.getElementById('setting-search-setting_three');
expect(deepLinked).not.toBeNull();
const body = deepLinked.closest('.ldr-settings-section-body');
const header = deepLinked.closest('.ldr-settings-section')
.querySelector('.ldr-settings-section-header');
expect(body.classList.contains('collapsed')).toBe(false);
expect(header.classList.contains('collapsed')).toBe(false);
expect(header.getAttribute('aria-expanded')).toBe('true');
// Transient: following a deep link must not silently rewrite which
// sections the user finds open on their next visit.
expect(localStorage.getItem(`ldr_settings_section_collapsed_${body.id}`)).toBeNull();
// ...and it expands only that one section.
Array.from(document.querySelectorAll('.ldr-settings-section-header'))
.filter(other => other.dataset.target !== body.id)
.forEach(other => {
expect(other.classList.contains('collapsed')).toBe(true);
});
});
it('resolves a deep link to a dropdown-rendered setting through its label id', async () => {
// `llm.model`, `llm.provider` and `search.tool` are rendered by
// renderCustomDropdownHTML, not as a plain input: NO element ends up
// carrying the bare `setting-llm-model` id the /settings#setting-<key>
// links are built from. The ids that do exist are
// `setting-llm-model-label` (the <label>) and
// `setting-llm-model-dropdown` (the container), so revealHashTarget
// falls back to those. Without the fallback getElementById returns
// null and the function returns before expanding anything.
window.location.hash = '#setting-llm-model';
await loadSettingsPage(undefined, {
payload: {
'llm.model': {
name: 'Model', description: 'The model to use', category: 'llm_general',
value: 'gpt-4', ui_element: 'select', editable: true, visible: true,
},
},
});
// The premise: the id the link names is genuinely absent...
expect(document.getElementById('setting-llm-model')).toBeNull();
const label = document.getElementById('setting-llm-model-label');
expect(label).not.toBeNull();
// ...and the section it lives in was opened anyway.
const body = label.closest('.ldr-settings-section-body');
const header = body.previousElementSibling;
expect(header.classList.contains('ldr-settings-section-header')).toBe(true);
expect(body.classList.contains('collapsed')).toBe(false);
expect(header.classList.contains('collapsed')).toBe(false);
expect(header.getAttribute('aria-expanded')).toBe('true');
// Still transient: no preference was stored for it.
expect(localStorage.getItem(`ldr_settings_section_collapsed_${body.id}`)).toBeNull();
});
it('reveals a given #hash once, not again on every later render', async () => {
// renderSettingsByTab re-runs on a tab click and whenever the search
// box is cleared, with the fragment still sitting in the URL. Revealing
// again there would undo a collapse the user made after following the
// link (and scroll them back to it).
window.location.hash = '#setting-search-setting_three';
await loadSettingsPage();
const deepLinked = document.getElementById('setting-search-setting_three');
expect(deepLinked).not.toBeNull();
const revealedId = deepLinked.closest('.ldr-settings-section-body').id;
expect(document.getElementById(revealedId).classList.contains('collapsed')).toBe(false);
// The user closes the section they were sent to. That persists 'true'.
const header = document.getElementById(revealedId).previousElementSibling;
header.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(document.getElementById(revealedId).classList.contains('collapsed')).toBe(true);
expect(localStorage.getItem(`ldr_settings_section_collapsed_${revealedId}`)).toBe('true');
// Search, then clear it — clearing goes through renderSettingsByTab,
// which calls revealHashTarget again. handleSearchInput is debounced
// 250ms. Every node below is rebuilt, so re-query rather than reusing
// the handles above.
const searchInput = document.getElementById('settings-search');
searchInput.value = 'iterations';
searchInput.dispatchEvent(new Event('input', { bubbles: true }));
await vi.advanceTimersByTimeAsync(260);
searchInput.value = '';
searchInput.dispatchEvent(new Event('input', { bubbles: true }));
await vi.advanceTimersByTimeAsync(260);
const reRenderedBody = document.getElementById(revealedId);
expect(reRenderedBody).not.toBeNull();
expect(reRenderedBody.classList.contains('collapsed')).toBe(true);
expect(reRenderedBody.previousElementSibling.getAttribute('aria-expanded')).toBe('false');
});
it('marking an input invalid opens its collapsed section, writing no preference', async () => {
// An inline validation message appended into a collapsed body is
// invisible: the user sees the banner and never the field-level
// reason. markInvalidInput therefore reveals the owning section.
await loadSettingsPage(undefined, {
payload: {
'search.iterations': {
name: 'Iterations', description: 'search iterations',
category: 'search_parameters', value: 3, ui_element: 'number',
min_value: 1, max_value: 10, step: 1, editable: true, visible: true,
},
},
});
// The auto-save listeners are attached synchronously by
// setupCustomDropdowns() at the end of the render; the 300ms timer is
// only a second, idempotent pass. Advance past it so the test does not
// depend on which of the two got there first.
await vi.advanceTimersByTimeAsync(300);
const input = document.getElementById('setting-search-iterations');
expect(input).not.toBeNull();
expect(input.getAttribute('max')).toBe('10');
const body = input.closest('.ldr-settings-section-body');
const header = body.previousElementSibling;
expect(body.classList.contains('collapsed')).toBe(true);
// Out of the rendered range -> handleInputChange -> markInvalidInput.
input.value = '999';
input.dispatchEvent(new Event('input', { bubbles: true }));
const message = input.closest('.ldr-settings-item')
.querySelector('.ldr-settings-error-message');
expect(message).not.toBeNull();
expect(message.textContent).toContain('Value must be between');
expect(body.classList.contains('collapsed')).toBe(false);
expect(header.classList.contains('collapsed')).toBe(false);
expect(header.getAttribute('aria-expanded')).toBe('true');
expect(localStorage.getItem(`ldr_settings_section_collapsed_${body.id}`)).toBeNull();
});
it('initializes a header exactly once even when initAccordions runs over it twice', async () => {
// initAccordions() queries the whole document, but only
// #settings-content is replaced by a render. A section header living
// outside that container is therefore seen by BOTH the bootstrap call
// in initializeSettings() and the post-render call in
// renderSettingsByTab() — the same nodes, initialized twice.
//
// On the unedited head that attaches two click listeners, so one user
// click toggles twice: the section snaps shut again (a visible no-op)
// and localStorage ends up holding the stale second value ('true').
const STATIC_SECTION_ID = 'section-static-outside-content';
await loadSettingsPage(() => {
const section = document.createElement('div');
section.className = 'ldr-settings-section';
const header = document.createElement('div');
header.className = 'ldr-settings-section-header';
header.dataset.target = STATIC_SECTION_ID;
header.setAttribute('role', 'button');
header.setAttribute('tabindex', '0');
header.setAttribute('aria-expanded', 'true');
header.setAttribute('aria-controls', STATIC_SECTION_ID);
const sectionBody = document.createElement('div');
sectionBody.id = STATIC_SECTION_ID;
sectionBody.className = 'ldr-settings-section-body';
section.appendChild(header);
section.appendChild(sectionBody);
document.getElementById('settings-form').appendChild(section);
});
const staticHeader = document.querySelector(
`.ldr-settings-section-header[data-target="${STATIC_SECTION_ID}"]`,
);
const staticBody = document.getElementById(STATIC_SECTION_ID);
expect(staticHeader).not.toBeNull();
// Both init passes have run over this node by now.
expect(staticHeader.classList.contains('collapsed')).toBe(true);
staticHeader.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(staticHeader.classList.contains('collapsed')).toBe(false);
expect(staticHeader.getAttribute('aria-expanded')).toBe('true');
expect(staticBody.classList.contains('collapsed')).toBe(false);
expect(
localStorage.getItem(`ldr_settings_section_collapsed_${STATIC_SECTION_ID}`),
).toBe('false');
});
});
describe('settings accordion — the section header escapes its category', () => {
// The tests above install `value => String(value)` as window.escapeHtml,
// which cannot tell an escaped render from an unescaped one — and
// eslint's no-unsanitized reports nothing either way, because the value is
// interpolated into a variable that is assigned to innerHTML later. This
// test installs the production escaper BEFORE the import (settings.js
// captures window.escapeHtml exactly once, at import time), so reverting
// `escapeHtml(category)` to a bare `${category}` fails it.
//
// `category` reaches renderSettingsByTab from /settings/api, i.e. from the
// server, via organizeSettings.
const HOSTILE_CATEGORY = '<img src=x onerror=alert(1)> & "quoted"';
// organizeSettings runs every category through formatCategoryName first,
// which upper-cases the first letter of each space-separated word. That
// (not the raw payload value) is what the header has to round-trip.
const RENDERED_CATEGORY = '<img Src=x Onerror=alert(1)> & "quoted"';
// `sectionId` (settings.js) is built from the *same* category string and
// spliced into two unquoted-looking-but-quoted attributes,
// `data-target="${sectionId}"` and `aria-controls="${sectionId}"` (and
// the body's `id="${sectionId}"`). A category carrying both a `"` and a
// `/` breaks out of `data-target`'s value early (the `"` closes the
// attribute) and turns the remainder into a *new* attribute on the
// header (the `/` is swallowed as an inter-attribute separator, not
// glued onto data-target). HOSTILE_CATEGORY above has no `/`, so it
// cannot exercise this — it only ever lands inside an attribute value it
// can't escape. This one is a single word (no spaces), so
// formatCategoryName only capitalizes its first letter.
const HOSTILE_CATEGORY_ATTR_BREAKOUT = 'x"/onmouseover="alert(1)';
const RENDERED_CATEGORY_ATTR_BREAKOUT = 'X"/onmouseover="alert(1)';
it('renders the category as text and keeps data-target === body.id === aria-controls', async () => {
await loadSettingsPage(undefined, {
escapeHtml: realEscapeHtml,
payload: {
'app.hostile_category_setting': {
name: 'Hostile', description: 'a setting in a hostile category',
category: HOSTILE_CATEGORY, value: 'a', ui_element: 'text',
editable: true, visible: true,
},
'app.hostile_attr_breakout_setting': {
name: 'Hostile Attr Breakout', description: 'a category that breaks out of data-target',
category: HOSTILE_CATEGORY_ATTR_BREAKOUT, value: 'a', ui_element: 'text',
editable: true, visible: true,
},
},
});
const titles = Array.from(document.querySelectorAll('.ldr-settings-section-title'));
expect(titles.map(node => node.textContent.trim())).toContain(RENDERED_CATEGORY);
const title = titles.find(node => node.textContent.trim() === RENDERED_CATEGORY);
// Round-trips in both contexts, and is not double-escaped (the `&`
// would show up as a literal `&` if it were).
expect(title.getAttribute('title')).toBe(RENDERED_CATEGORY);
// Nothing was parsed as markup.
expect(title.children.length).toBe(0);
expect(document.querySelector('#settings-content img')).toBeNull();
expect(document.querySelector('#settings-content script')).toBeNull();
// The id derived from the same string still agrees in all three
// places, so the accordion (and the localStorage key built from
// data-target) is wired to the body it claims.
const header = title.closest('.ldr-settings-section-header');
const body = header.nextElementSibling;
expect(body.classList.contains('ldr-settings-section-body')).toBe(true);
expect(header.dataset.target).toBe(body.id);
expect(header.getAttribute('aria-controls')).toBe(body.id);
expect(document.getElementById(header.dataset.target)).toBe(body);
// ...and it is still an operable accordion.
expect(body.classList.contains('collapsed')).toBe(true);
header.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(body.classList.contains('collapsed')).toBe(false);
expect(localStorage.getItem(`ldr_settings_section_collapsed_${body.id}`)).toBe('false');
// The `"`/`/` category: same text round-trip and id triple...
const breakoutTitles = titles.filter(
node => node.textContent.trim() === RENDERED_CATEGORY_ATTR_BREAKOUT,
);
expect(breakoutTitles.length).toBe(1);
const breakoutTitle = breakoutTitles[0];
expect(breakoutTitle.getAttribute('title')).toBe(RENDERED_CATEGORY_ATTR_BREAKOUT);
expect(breakoutTitle.children.length).toBe(0);
const breakoutHeader = breakoutTitle.closest('.ldr-settings-section-header');
const breakoutBody = breakoutHeader.nextElementSibling;
expect(breakoutBody.classList.contains('ldr-settings-section-body')).toBe(true);
expect(breakoutHeader.dataset.target).toBe(breakoutBody.id);
expect(breakoutHeader.getAttribute('aria-controls')).toBe(breakoutBody.id);
expect(document.getElementById(breakoutHeader.dataset.target)).toBe(breakoutBody);
// ...and, the point of this case: the category's `"` did not close
// data-target early and hand its `/onmouseover="alert(1)` tail to
// the parser as a live event-handler attribute on the header. This
// is the assertion `escapeHtml(sectionId)` alone is responsible
// for — it still passes if only `escapeHtml(category)` is reverted.
expect(breakoutHeader.hasAttribute('onmouseover')).toBe(false);
});
});