tag', () => {
expect(parseMarkup('A full paragraph.')).toEqual([
{ type: 'paragraph', children: [{ type: 'text', content: 'A full paragraph.' }] },
]);
});
it('parses as an alias for paragraph', () => {
expect(parseMarkup('
Short form.
')).toEqual([
{ type: 'paragraph', children: [{ type: 'text', content: 'Short form.' }] },
]);
});
});
describe('accessibilityLabel', () => {
it('extracts accessibilityLabel from link tags', () => {
expect(
parseMarkup('settings'),
).toEqual([
{
type: 'link',
href: '/settings',
accessibilityLabel: 'Go to settings',
children: [{ type: 'text', content: 'settings' }],
},
]);
});
it('extracts accessibilityLabel from emphasis tags', () => {
expect(parseMarkup('critical')).toEqual([
{
type: 'important',
accessibilityLabel: 'Notice',
children: [{ type: 'text', content: 'critical' }],
},
]);
});
it('extracts accessibilityLabel from strong tags', () => {
expect(parseMarkup('bold')).toEqual([
{
type: 'strong',
accessibilityLabel: 'Key point',
children: [{ type: 'text', content: 'bold' }],
},
]);
});
it('extracts accessibilityLabel from strikethrough tags', () => {
expect(
parseMarkup('£50'),
).toEqual([
{
type: 'strikethrough',
accessibilityLabel: 'Was 50 GBP',
children: [{ type: 'text', content: '£50' }],
},
]);
});
it('does not extract accessibilityLabel from paragraph tags', () => {
expect(parseMarkup('text')).toEqual([
{ type: 'paragraph', children: [{ type: 'text', content: 'text' }] },
]);
});
it('does not include accessibilityLabel key when attribute is absent', () => {
const result = parseMarkup('text');
expect(result[0]).not.toHaveProperty('accessibilityLabel');
});
});
describe('stack overflow via deep nesting (DoS)', () => {
it('handles deeply nested tags without crashing', () => {
const depth = 10000;
const input = `${''.repeat(depth)}x${''.repeat(depth)}`;
expect(() => parseMarkup(input)).not.toThrow();
});
it('degrades gracefully at excessive depth', () => {
const depth = 10000;
const input = `${''.repeat(depth)}x${''.repeat(depth)}`;
const result = parseMarkup(input);
expect(result.length).toBeGreaterThan(0);
});
it('respects MAX_NESTING_DEPTH', () => {
const depth = MAX_NESTING_DEPTH + 20;
const input = `${''.repeat(depth)}x${''.repeat(depth)}`;
const result = parseMarkup(input);
const deepNode = JSON.stringify(result);
let nesting = 0;
let maxNesting = 0;
for (const char of deepNode) {
if (char === '{') nesting += 1;
if (char === '}') nesting -= 1;
maxNesting = Math.max(maxNesting, nesting);
}
expect(maxNesting).toBeLessThan(MAX_NESTING_DEPTH * 3);
});
});
describe('prototype pollution via action attribute', () => {
it('does not extract __proto__ as action value', () => {
const result = parseMarkup('click');
const linkNode = result[0];
expect(linkNode).toHaveProperty('type', 'link');
expect(linkNode).not.toHaveProperty('action');
});
it('does not extract constructor as action value', () => {
const result = parseMarkup('click');
const linkNode = result[0];
expect(linkNode).toHaveProperty('type', 'link');
expect(linkNode).not.toHaveProperty('action');
});
it('does not extract toString as action value', () => {
const result = parseMarkup('click');
const linkNode = result[0];
expect(linkNode).toHaveProperty('type', 'link');
expect(linkNode).not.toHaveProperty('action');
});
it('does not extract valueOf as action value', () => {
const result = parseMarkup('click');
const linkNode = result[0];
expect(linkNode).toHaveProperty('type', 'link');
expect(linkNode).not.toHaveProperty('action');
});
it('does not extract hasOwnProperty as action value', () => {
const result = parseMarkup('click');
const linkNode = result[0];
expect(linkNode).toHaveProperty('type', 'link');
expect(linkNode).not.toHaveProperty('action');
});
it('allows legitimate action values', () => {
const result = parseMarkup('click');
const linkNode = result[0] as { type: string; action?: string };
expect(linkNode.action).toBe('save');
});
});
describe('duplicate attribute injection', () => {
it('uses first href value when duplicated', () => {
const result = parseMarkup('click');
const linkNode = result[0] as { type: string; href?: string };
expect(linkNode.href).toBe('/safe');
});
it('uses first action value when duplicated', () => {
const result = parseMarkup('click');
const linkNode = result[0] as { type: string; action?: string };
expect(linkNode.action).toBe('safe');
});
it('uses first target value when duplicated', () => {
const result = parseMarkup('click');
const linkNode = result[0] as { type: string; target?: string };
expect(linkNode.target).toBe('_blank');
});
});
describe('HTML/script in accessibilityLabel', () => {
it('strips HTML from accessibilityLabel', () => {
const result = parseMarkup(
'text',
);
const linkNode = result[0] as { type: string; accessibilityLabel?: string };
expect(linkNode.accessibilityLabel).not.toContain('<');
expect(linkNode.accessibilityLabel).not.toContain('>');
});
it('strips angle brackets from accessibilityLabel (defense-in-depth)', () => {
const result = parseMarkup(
'text',
);
const node = result[0] as { type: string; accessibilityLabel?: string };
expect(node.accessibilityLabel).toBe('testbbold/bend');
expect(node.accessibilityLabel).not.toContain('<');
expect(node.accessibilityLabel).not.toContain('>');
});
it('strips quotes from accessibilityLabel to prevent attribute injection', () => {
const result = parseMarkup(
'text',
);
const node = result[0] as { type: string; accessibilityLabel?: string };
if (node.accessibilityLabel) {
expect(node.accessibilityLabel).not.toContain('"');
}
});
it('preserves safe accessibilityLabel text', () => {
const result = parseMarkup(
'text',
);
const node = result[0] as { type: string; accessibilityLabel?: string };
expect(node.accessibilityLabel).toBe('Navigate to settings page');
});
});
describe('unvalidated target attribute', () => {
it('allows target="_blank"', () => {
const result = parseMarkup('text');
const linkNode = result[0] as { type: string; target?: string };
expect(linkNode.target).toBe('_blank');
});
it('allows target="_self"', () => {
const result = parseMarkup('text');
const linkNode = result[0] as { type: string; target?: string };
expect(linkNode.target).toBe('_self');
});
it('blocks target="_top" (iframe breakout)', () => {
const result = parseMarkup('text');
const linkNode = result[0] as { type: string; target?: string };
expect(linkNode).not.toHaveProperty('target');
});
it('blocks target="_parent" (iframe breakout)', () => {
const result = parseMarkup('text');
const linkNode = result[0] as { type: string; target?: string };
expect(linkNode).not.toHaveProperty('target');
});
it('blocks arbitrary target values', () => {
const result = parseMarkup('text');
const linkNode = result[0] as { type: string; target?: string };
expect(linkNode).not.toHaveProperty('target');
});
});
describe('slash as attribute separator bypass', () => {
it('does not parse attributes after / separator', () => {
const result = parseMarkup('click');
const linkNode = result[0] as { type: string; href?: string };
expect(linkNode).not.toHaveProperty('href');
});
});
describe('control characters in attribute values', () => {
it('strips control characters from href values', () => {
const result = parseMarkup('text');
const linkNode = result[0] as { type: string; href?: string };
if (linkNode.href) {
expect(linkNode.href).not.toContain('\x00');
}
});
it('strips newlines from href values', () => {
const result = parseMarkup('text');
const linkNode = result[0] as { type: string; href?: string };
if (linkNode.href) {
expect(linkNode.href).not.toContain('\n');
}
});
it('strips tabs from href values', () => {
const result = parseMarkup('text');
const linkNode = result[0] as { type: string; href?: string };
if (linkNode.href) {
expect(linkNode.href).not.toContain('\t');
}
});
});
describe('mismatched nesting (graceful degradation)', () => {
it('handles inner tag closed by outer close tag', () => {
const result = parseMarkup('text