-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsanitize-blocks.test.ts
More file actions
187 lines (172 loc) · 6.54 KB
/
Copy pathsanitize-blocks.test.ts
File metadata and controls
187 lines (172 loc) · 6.54 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { sanitizeBlock, sanitizeBlocks } from '../src/lib/sanitize-blocks';
import type { SupportedBlock } from '../src/types';
describe('sanitizeBlock', () => {
it('strips javascript: from a section button url', () => {
const block = {
type: 'section',
text: { type: 'mrkdwn', text: 'hi' },
accessory: {
type: 'button',
text: { type: 'plain_text', text: 'Click' },
url: 'javascript:alert(1)',
action_id: 'x'
}
} as unknown as SupportedBlock;
const out = sanitizeBlock(block) as typeof block;
expect((out.accessory as { url: string }).url).toBe('');
});
it('strips javascript: from a rich_text link url', () => {
const block: SupportedBlock = {
type: 'rich_text',
elements: [
{
type: 'rich_text_section',
elements: [{ type: 'link', url: 'javascript:alert(1)', text: 'click me' }]
}
]
} as SupportedBlock;
const out = sanitizeBlock(block) as typeof block;
const link = (out.elements[0] as { elements: { url: string }[] }).elements[0];
expect(link.url).toBe('');
});
it('strips data:image/svg+xml from image_url', () => {
const block = {
type: 'image',
image_url: 'data:image/svg+xml,<svg onload=alert(1)>',
alt_text: 'evil'
} as unknown as SupportedBlock;
const out = sanitizeBlock(block) as typeof block & { image_url: string };
expect(out.image_url).toBe('');
});
it('leaves safe URLs intact', () => {
const block = {
type: 'section',
text: { type: 'mrkdwn', text: 'hi' },
accessory: {
type: 'image',
image_url: 'https://example.com/img.png',
alt_text: 'pic'
}
} as unknown as SupportedBlock;
const out = sanitizeBlock(block);
expect(out).toBe(block); // reference-stable when nothing changed
});
it('returns a new reference only when a URL was rewritten', () => {
const safe = { type: 'divider' } as SupportedBlock;
expect(sanitizeBlock(safe)).toBe(safe);
const unsafe = {
type: 'image',
image_url: 'javascript:alert(1)',
alt_text: 'evil'
} as unknown as SupportedBlock;
expect(sanitizeBlock(unsafe)).not.toBe(unsafe);
});
it('walks into nested arrays (carousel of cards with bad action urls)', () => {
const block = {
type: 'carousel',
elements: [
{
type: 'card',
actions: [
{ type: 'button', text: { type: 'plain_text', text: 'A' }, url: 'javascript:alert(1)' },
{ type: 'button', text: { type: 'plain_text', text: 'B' }, url: 'https://ok.example.com' }
]
}
]
} as unknown as SupportedBlock;
const out = sanitizeBlock(block) as typeof block;
const actions = (out.elements[0] as { actions: { url: string }[] }).actions;
expect(actions[0].url).toBe('');
expect(actions[1].url).toBe('https://ok.example.com');
});
});
describe('sanitizeBlocks', () => {
it('returns the same array reference when nothing needs rewriting', () => {
const blocks: SupportedBlock[] = [{ type: 'divider' }];
expect(sanitizeBlocks(blocks)).toBe(blocks);
});
it('returns a new array when any block was rewritten', () => {
const blocks = [
{
type: 'image',
image_url: 'javascript:alert(1)',
alt_text: 'evil'
}
] as unknown as SupportedBlock[];
expect(sanitizeBlocks(blocks)).not.toBe(blocks);
});
});
describe('retrieval-only image metadata', () => {
it('drops send-invalid fields Slack adds to retrieved image blocks', () => {
const block = {
type: 'image',
image_url: 'https://example.com/a.png',
alt_text: 'ok',
image_width: 800,
image_height: 600,
image_bytes: 12345,
fallback: '800x600px image',
is_animated: false
} as unknown as SupportedBlock;
const out = sanitizeBlock(block) as Record<string, unknown>;
for (const k of ['image_width', 'image_height', 'image_bytes', 'fallback', 'is_animated']) {
expect(Object.hasOwn(out, k)).toBe(false);
}
expect(out.image_url).toBe('https://example.com/a.png');
expect(out.alt_text).toBe('ok');
});
it('strips the same fields from a nested image element (e.g. context block)', () => {
const block = {
type: 'context',
elements: [{ type: 'image', image_url: 'https://x/y.gif', alt_text: 'g', is_animated: true, image_bytes: 9 }]
} as unknown as SupportedBlock;
const out = sanitizeBlock(block) as { elements: Record<string, unknown>[] };
expect(Object.hasOwn(out.elements[0], 'is_animated')).toBe(false);
expect(Object.hasOwn(out.elements[0], 'image_bytes')).toBe(false);
});
it('drops preview_images Slack adds to retrieved data_visualization blocks', () => {
const block = {
type: 'data_visualization',
title: 'Weekly active users',
chart: { type: 'line', series: [] },
preview_images: [{ url: 'https://slack.example/chart.png' }]
} as unknown as SupportedBlock;
const out = sanitizeBlock(block) as Record<string, unknown>;
expect(Object.hasOwn(out, 'preview_images')).toBe(false);
expect(out.title).toBe('Weekly active users');
expect(out.chart).toBeDefined();
});
it('leaves preview_images untouched on non-data_visualization objects', () => {
const block = {
type: 'section',
text: { type: 'mrkdwn', text: 'hi' },
preview_images: ['keep me']
} as unknown as SupportedBlock;
const out = sanitizeBlock(block) as Record<string, unknown>;
expect(out.preview_images).toEqual(['keep me']);
expect(out).toBe(block);
});
it('leaves the same field names untouched on non-image objects', () => {
const block = {
type: 'section',
text: { type: 'mrkdwn', text: 'hi' },
fallback: 'keep me',
is_animated: true
} as unknown as SupportedBlock;
const out = sanitizeBlock(block) as Record<string, unknown>;
expect(out.fallback).toBe('keep me');
expect(out.is_animated).toBe(true);
expect(out).toBe(block);
});
});
describe('prototype pollution shape', () => {
it('a JSON object with __proto__ key does not pollute Object.prototype', () => {
const parsed = JSON.parse('{"__proto__": {"polluted": true}}');
// Modern engines treat `__proto__` in JSON as a plain own property
// (it does NOT mutate the prototype). Our sanitizer should preserve
// that behavior — no merge that walks the prototype chain.
const sanitized = sanitizeBlock(parsed as SupportedBlock);
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
expect(Object.hasOwn(sanitized, '__proto__')).toBe(true);
});
});