fix(security): escape quotes in markdown renderer to block attribute-injection XSS — daily-sweep 2026-06-01
This commit is contained in:
@@ -5,7 +5,12 @@
|
||||
// markdown.
|
||||
|
||||
function escape(s) {
|
||||
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
// Escape quotes too: inline() interpolates link hrefs/titles into double-quoted
|
||||
// attributes, so an unescaped " in a [label](href) lets crafted markdown break
|
||||
// out of the attribute and inject event handlers (DOM-XSS). Matters because the
|
||||
// Lab renders LLM output through this — a prompt-injection surface.
|
||||
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
||||
.replace(/"/g, '"').replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function inline(s) {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// Regression coverage for the tiny markdown renderer's HTML-attribute escaping.
|
||||
// The renderer output is injected via v-html (Lab LLM output, Nox docs), so a
|
||||
// crafted link must not break out of an attribute and inject an event handler.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { renderMarkdown } from '../src/views/nox/markdown.js';
|
||||
|
||||
test('link href cannot break out of the href attribute (DOM-XSS)', () => {
|
||||
const html = renderMarkdown('[click](https://a" onmouseover="alert(1))');
|
||||
// No raw-quote attribute injection: the only " must be the renderer's own
|
||||
// attribute delimiters, never one closed by the payload mid-value.
|
||||
assert.ok(!/onmouseover="/.test(html), 'injected event-handler attribute must not appear');
|
||||
assert.ok(html.includes('"'), 'the embedded quote must be escaped');
|
||||
});
|
||||
|
||||
test('inert-link title cannot inject an attribute', () => {
|
||||
const html = renderMarkdown('[x](foo" onmouseover="alert(1))');
|
||||
assert.ok(!/onmouseover="/.test(html), 'injected event-handler attribute must not appear');
|
||||
assert.ok(html.includes('"'), 'the embedded quote must be escaped');
|
||||
});
|
||||
|
||||
test('plain text with a quote renders the escaped entity', () => {
|
||||
const html = renderMarkdown('she said "hi"');
|
||||
assert.ok(html.includes('"hi"'));
|
||||
});
|
||||
|
||||
test('a normal https link still renders as an anchor', () => {
|
||||
const html = renderMarkdown('[docs](https://caelum.network/nox/docs)');
|
||||
assert.ok(html.includes('href="https://caelum.network/nox/docs"'));
|
||||
assert.ok(html.includes('>docs</a>'));
|
||||
});
|
||||
Reference in New Issue
Block a user