-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathsanitization.test.ts
More file actions
113 lines (99 loc) · 3.12 KB
/
Copy pathsanitization.test.ts
File metadata and controls
113 lines (99 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import {
sanitizeHtml,
sanitizeMentionAttributes,
checkMentionAttributes,
} from '../sanitization/htmlSanitizer';
describe('sanitizeMentionAttributes', () => {
it('returns an empty object when given no attributes', () => {
expect(sanitizeMentionAttributes()).toEqual({});
expect(sanitizeMentionAttributes({})).toEqual({});
});
it('keeps data-* and commonly-allowed attributes', () => {
expect(
sanitizeMentionAttributes({
'data-user-id': '42',
'data-team': 'core',
'id': 'm1',
'class': 'highlight',
})
).toEqual({
'data-user-id': '42',
'data-team': 'core',
'id': 'm1',
'class': 'highlight',
});
});
it('strips event handlers and unsafe attributes', () => {
const result = sanitizeMentionAttributes({
'onclick': 'alert(1)',
'onmouseover': 'steal()',
// eslint-disable-next-line no-script-url
'href': 'javascript:alert(1)',
'data-user-id': '42',
});
expect(result).toEqual({ 'data-user-id': '42' });
});
it('does not return the reserved text/indicator attributes', () => {
const result = sanitizeMentionAttributes({
'text': 'Joe',
'indicator': '@',
'data-user-id': '42',
});
expect(result).toEqual({ 'data-user-id': '42' });
});
});
describe('checkMentionAttributes', () => {
let warnSpy: jest.SpyInstance;
beforeEach(() => {
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
warnSpy.mockRestore();
});
it('does not warn for data-*, text, indicator, or commonly-allowed attributes', () => {
checkMentionAttributes({
'data-user-id': '42',
'text': 'Joe',
'indicator': '@',
'id': 'm1',
'class': 'x',
'style': 'color: red',
});
expect(warnSpy).not.toHaveBeenCalled();
});
it('warns for custom attributes without a recognized prefix', () => {
checkMentionAttributes({ foo: 'bar' });
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toContain('foo');
});
it('does nothing when given no attributes', () => {
checkMentionAttributes();
expect(warnSpy).not.toHaveBeenCalled();
});
});
describe('sanitizeHtmlMention', () => {
it('keeps <mention> tags with text/indicator/data-* attributes', () => {
const out = sanitizeHtml(
'<mention text="Joe" indicator="@" data-user-id="42">@Joe</mention>'
);
expect(out).toContain('text="Joe"');
expect(out).toContain('indicator="@"');
expect(out).toContain('data-user-id="42"');
});
it('strips <mention> event handlers', () => {
expect(
sanitizeHtml('<mention onclick="alert(1)">x</mention>')
).not.toContain('onclick');
});
});
describe('sanitizeLinkAttributes', () => {
it('strips javascript: URLs from links', () => {
const out = sanitizeHtml('<a href="javascript:alert(1)">x</a>');
// eslint-disable-next-line no-script-url
expect(out).not.toContain('javascript:');
});
it('strips unknown protocol URLs from links', () => {
const out = sanitizeHtml('<a href="custom://link">x</a>');
expect(out).not.toContain('custom');
});
});