mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
fix: arbitrary list starts
This commit is contained in:
1 parent
7170a1d78f
commit
92c270ee2e
5 files changed
+35
-2
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Fixed starting lists at arbitrary numbers.
|
||||
@@ -94,7 +94,6 @@ describe('bidirectional round-trip', () => {
|
||||
const html = markdownToHtml(markdown);
|
||||
const injected = injectDataMd(html);
|
||||
const result = htmlToMarkdown(injected);
|
||||
// Note: marked normalizes ordered lists to start at 1, but we increment for output
|
||||
expect(result).toContain('1. First');
|
||||
expect(result).toContain('2. Second');
|
||||
});
|
||||
|
||||
@@ -216,6 +216,17 @@ describe('markdownToHtml', () => {
|
||||
expect(result).not.toContain('<ul>');
|
||||
});
|
||||
|
||||
it('preserves arbitrary ordered list start numbers', () => {
|
||||
const result = markdownToHtml('23423. hello');
|
||||
expect(result).toContain('start="23423"');
|
||||
});
|
||||
|
||||
it('strips invalid ol start values', () => {
|
||||
const result = markdownToHtml('<ol start="javascript:alert(1)"><li>x</li></ol>');
|
||||
expect(result).toContain('<ol>');
|
||||
expect(result).not.toContain('start=');
|
||||
});
|
||||
|
||||
it('handles text without markdown', () => {
|
||||
const result = markdownToHtml('Plain text without any formatting');
|
||||
expect(result).toContain('Plain text');
|
||||
|
||||
@@ -56,6 +56,8 @@ const decodeHtmlEntities = (text: string): string => {
|
||||
|
||||
const MATRIX_TO_PLACEHOLDER_PREFIX = 'MATRIXTORAWLINKTOKEN';
|
||||
|
||||
const ORDERED_LIST_START_REGEX = /^-?\d+$/;
|
||||
|
||||
const escapeHtml = (text: string): string =>
|
||||
text
|
||||
.replace(/&/g, '&')
|
||||
@@ -152,12 +154,18 @@ export function markdownToHtml(markdown: string, options?: MarkdownToHtmlOptions
|
||||
|
||||
const allowlistedHtml = escapeNonAllowlistedHtmlTags(unescapedInline);
|
||||
|
||||
// Force all links to open in a new tab
|
||||
// Force all links to open in a new tab, validate <ol start>.
|
||||
DOMPurify.addHook('afterSanitizeAttributes', (node) => {
|
||||
if (node.tagName === 'A' && node.getAttribute('href')) {
|
||||
node.setAttribute('target', '_blank');
|
||||
node.setAttribute('rel', 'noreferrer noopener');
|
||||
}
|
||||
if (node.tagName === 'OL') {
|
||||
const start = node.getAttribute('start');
|
||||
if (start !== null && !ORDERED_LIST_START_REGEX.test(start)) {
|
||||
node.removeAttribute('start');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const sanitized = DOMPurify.sanitize(allowlistedHtml, {
|
||||
@@ -185,7 +193,9 @@ export function markdownToHtml(markdown: string, options?: MarkdownToHtmlOptions
|
||||
],
|
||||
// Ensure these safe attrs survive sanitization even when the input HTML
|
||||
// originates from markdown-embedded tags (e.g. custom emoji <img>).
|
||||
// `start` must be URI-safe or DOMPurify drops it when ALLOWED_URI_REGEXP is set.
|
||||
ADD_ATTR: ['target', 'rel', 'height', 'width'],
|
||||
ADD_URI_SAFE_ATTR: ['start'],
|
||||
// Force all links to have safe rel attribute
|
||||
FORCE_BODY: false,
|
||||
ALLOWED_URI_REGEXP: /^(?:https?|ftp|mailto|magnet|mxc):/i,
|
||||
|
||||
@@ -122,6 +122,14 @@ describe('sanitizeCustomHtml', () => {
|
||||
expect(result.match(/\ssrc=/g)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('drops invalid ol start values', () => {
|
||||
expect(sanitizeCustomHtml('<ol start="2"><li>ok</li></ol>')).toContain('start="2"');
|
||||
expect(sanitizeCustomHtml('<ol start="javascript:alert(1)"><li>x</li></ol>')).not.toContain(
|
||||
'start='
|
||||
);
|
||||
expect(sanitizeCustomHtml('<ol start="1.5"><li>x</li></ol>')).not.toContain('start=');
|
||||
});
|
||||
|
||||
it('drops invalid Matrix color attributes instead of translating them to style', () => {
|
||||
const result = sanitizeCustomHtml(
|
||||
'<span data-mx-color="red" data-mx-bg-color="#123">text</span>'
|
||||
|
||||
Reference in new issue
Block a user