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.

test: Add tests for XSS protection and dropdown highlighting

LearningCircuit8d82964af221
diff --git a/tests/infrastructure_tests/test_xss_protection.test.js b/tests/infrastructure_tests/test_xss_protection.test.js
new file mode 100644
index 0000000000..90091df45c
--- /dev/null
+++ b/tests/infrastructure_tests/test_xss_protection.test.js
@@ -0,0 +1,273 @@
+/**
+ * JavaScript tests for XSS Protection and Dropdown Highlighting
+ * Run with: npm test tests/infrastructure_tests/test_xss_protection.test.js
+ *
+ * Tests the safeSetInnerHTML function and dropdown search highlighting
+ * which requires DOMPurify to be loaded.
+ */
+
+// Mock DOMPurify for testing
+const mockDOMPurify = {
+ sanitize: jest.fn((content, config) => {
+ // Simple mock that allows span tags with class attribute
+ // but removes script tags
+ let result = content;
+ // Remove script tags
+ result = result.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
+ // Keep allowed tags like span with class
+ return result;
+ })
+};
+
+// Setup global mocks
+global.DOMPurify = mockDOMPurify;
+global.window = {
+ DOMPurify: mockDOMPurify
+};
+global.document = {
+ createElement: jest.fn(() => ({
+ className: '',
+ textContent: '',
+ innerHTML: '',
+ style: {},
+ setAttribute: jest.fn(),
+ appendChild: jest.fn(),
+ removeChild: jest.fn(),
+ addEventListener: jest.fn(),
+ querySelectorAll: jest.fn(() => []),
+ firstChild: null
+ }))
+};
+
+// Load the XSS protection module
+require('../../src/local_deep_research/web/static/js/security/xss-protection.js');
+
+describe('XSS Protection Module', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ describe('DOMPurify Integration', () => {
+ test('DOMPurify should be available globally', () => {
+ expect(global.DOMPurify).toBeDefined();
+ expect(global.DOMPurify.sanitize).toBeDefined();
+ });
+
+ test('window.DOMPurify should be set', () => {
+ expect(global.window.DOMPurify).toBeDefined();
+ });
+ });
+
+ describe('escapeHtml', () => {
+ test('should escape HTML special characters', () => {
+ const { escapeHtml } = global.window;
+ expect(escapeHtml('<script>')).toBe('&lt;script&gt;');
+ expect(escapeHtml('a & b')).toBe('a &amp; b');
+ expect(escapeHtml('"quoted"')).toBe('&quot;quoted&quot;');
+ expect(escapeHtml("'single'")).toBe('&#39;single&#39;');
+ });
+
+ test('should handle non-string input', () => {
+ const { escapeHtml } = global.window;
+ expect(escapeHtml(123)).toBe('123');
+ expect(escapeHtml(null)).toBe('null');
+ });
+ });
+
+ describe('safeSetInnerHTML', () => {
+ test('should use DOMPurify when allowHtmlTags is true', () => {
+ const { safeSetInnerHTML } = global.window;
+ const element = {
+ innerHTML: '',
+ textContent: ''
+ };
+
+ const htmlContent = '<span class="ldr-highlight">test</span>';
+ safeSetInnerHTML(element, htmlContent, true);
+
+ expect(mockDOMPurify.sanitize).toHaveBeenCalled();
+ expect(element.innerHTML).toBe(htmlContent);
+ });
+
+ test('should use textContent when allowHtmlTags is false', () => {
+ const { safeSetInnerHTML } = global.window;
+ const element = {
+ innerHTML: '',
+ textContent: ''
+ };
+
+ const content = 'plain text';
+ safeSetInnerHTML(element, content, false);
+
+ expect(element.textContent).toBe(content);
+ });
+
+ test('should handle null element gracefully', () => {
+ const { safeSetInnerHTML } = global.window;
+ expect(() => safeSetInnerHTML(null, 'content')).not.toThrow();
+ });
+
+ test('should handle null content gracefully', () => {
+ const { safeSetInnerHTML } = global.window;
+ const element = { innerHTML: '', textContent: '' };
+ expect(() => safeSetInnerHTML(element, null)).not.toThrow();
+ });
+ });
+
+ describe('safeSetTextContent', () => {
+ test('should set textContent safely', () => {
+ const { safeSetTextContent } = global.window;
+ const element = { textContent: '' };
+
+ safeSetTextContent(element, '<script>alert("xss")</script>');
+ expect(element.textContent).toBe('<script>alert("xss")</script>');
+ });
+
+ test('should handle null element gracefully', () => {
+ const { safeSetTextContent } = global.window;
+ expect(() => safeSetTextContent(null, 'content')).not.toThrow();
+ });
+ });
+
+ describe('sanitizeHtml', () => {
+ test('should use DOMPurify to sanitize HTML', () => {
+ const { sanitizeHtml } = global.window;
+ const dirty = '<script>alert("xss")</script><b>safe</b>';
+ const result = sanitizeHtml(dirty);
+
+ expect(mockDOMPurify.sanitize).toHaveBeenCalledWith(
+ dirty,
+ expect.any(Object)
+ );
+ });
+
+ test('should return empty string for empty input', () => {
+ const { sanitizeHtml } = global.window;
+ expect(sanitizeHtml('')).toBe('');
+ expect(sanitizeHtml(null)).toBe('');
+ expect(sanitizeHtml(undefined)).toBe('');
+ });
+ });
+});
+
+describe('Dropdown Highlight Rendering', () => {
+ describe('Highlight HTML Structure', () => {
+ test('highlight span should have correct class', () => {
+ const highlightHtml = '<span class="ldr-highlight">search</span>';
+ expect(highlightHtml).toContain('class="ldr-highlight"');
+ });
+
+ test('highlight pattern should match search terms', () => {
+ // Simulate the highlightText function from custom_dropdown.js
+ function highlightText(text, search) {
+ if (!search.trim()) return text;
+ const regex = new RegExp(`(${search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
+ return text.replace(regex, '<span class="ldr-highlight">$1</span>');
+ }
+
+ const result = highlightText('bytedance-seed/seed-1.6-flash-h', 'flas');
+ expect(result).toBe('bytedance-seed/seed-1.6-<span class="ldr-highlight">flas</span>h-h');
+ });
+
+ test('highlight should be case-insensitive', () => {
+ function highlightText(text, search) {
+ if (!search.trim()) return text;
+ const regex = new RegExp(`(${search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
+ return text.replace(regex, '<span class="ldr-highlight">$1</span>');
+ }
+
+ const result = highlightText('FLASH Model', 'flash');
+ expect(result).toContain('ldr-highlight');
+ expect(result).toContain('FLASH');
+ });
+
+ test('highlight should handle multiple matches', () => {
+ function highlightText(text, search) {
+ if (!search.trim()) return text;
+ const regex = new RegExp(`(${search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
+ return text.replace(regex, '<span class="ldr-highlight">$1</span>');
+ }
+
+ const result = highlightText('test test test', 'test');
+ const matches = result.match(/ldr-highlight/g);
+ expect(matches).toHaveLength(3);
+ });
+
+ test('highlight should escape regex special characters in search', () => {
+ function highlightText(text, search) {
+ if (!search.trim()) return text;
+ const regex = new RegExp(`(${search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
+ return text.replace(regex, '<span class="ldr-highlight">$1</span>');
+ }
+
+ // Should not throw on special regex characters
+ expect(() => highlightText('test (parentheses)', '(paren')).not.toThrow();
+ expect(() => highlightText('test [brackets]', '[brack')).not.toThrow();
+ expect(() => highlightText('test.dot', '.')).not.toThrow();
+ });
+ });
+
+ describe('DOMPurify Sanitization Config', () => {
+ test('should allow span tags', () => {
+ const { sanitizeHtml } = global.window;
+ const input = '<span class="ldr-highlight">text</span>';
+ sanitizeHtml(input);
+
+ // Verify DOMPurify was called with config that allows span
+ expect(mockDOMPurify.sanitize).toHaveBeenCalledWith(
+ input,
+ expect.objectContaining({
+ ALLOWED_TAGS: expect.arrayContaining(['span'])
+ })
+ );
+ });
+
+ test('should allow class attribute', () => {
+ const { sanitizeHtml } = global.window;
+ const input = '<span class="ldr-highlight">text</span>';
+ sanitizeHtml(input);
+
+ expect(mockDOMPurify.sanitize).toHaveBeenCalledWith(
+ input,
+ expect.objectContaining({
+ ALLOWED_ATTR: expect.arrayContaining(['class'])
+ })
+ );
+ });
+
+ test('should forbid script tags', () => {
+ const { sanitizeHtml } = global.window;
+ const input = '<script>alert("xss")</script>';
+ sanitizeHtml(input);
+
+ expect(mockDOMPurify.sanitize).toHaveBeenCalledWith(
+ input,
+ expect.objectContaining({
+ FORBID_TAGS: expect.arrayContaining(['script'])
+ })
+ );
+ });
+
+ test('should forbid event handlers', () => {
+ const { sanitizeHtml } = global.window;
+ const input = '<div onclick="alert()">test</div>';
+ sanitizeHtml(input);
+
+ expect(mockDOMPurify.sanitize).toHaveBeenCalledWith(
+ input,
+ expect.objectContaining({
+ FORBID_ATTR: expect.arrayContaining(['onclick'])
+ })
+ );
+ });
+ });
+});
+
+describe('App.js DOMPurify Export', () => {
+ test('DOMPurify should be exported to window object', () => {
+ // This test verifies the fix - DOMPurify must be on window
+ // for safeSetInnerHTML to use it instead of falling back to textContent
+ expect(global.window.DOMPurify).toBeDefined();
+ expect(typeof global.window.DOMPurify.sanitize).toBe('function');
+ });
+});