-
-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathdecode-test.js
More file actions
312 lines (264 loc) · 9.11 KB
/
decode-test.js
File metadata and controls
312 lines (264 loc) · 9.11 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
jest.dontMock('../decode');
jest.dontMock('../CoreManager');
jest.dontMock('../isDangerousKey');
jest.dontMock('../ParseFile');
jest.dontMock('../ParseGeoPoint');
jest.dontMock('../ParseObject');
jest.dontMock('../ParsePolygon');
const decode = require('../decode').default;
const ParseFile = require('../ParseFile').default;
const ParseGeoPoint = require('../ParseGeoPoint').default;
const ParseObject = require('../ParseObject').default;
const ParsePolygon = require('../ParsePolygon').default;
describe('decode', () => {
it('ignores primitives', () => {
expect(decode(undefined)).toBe(undefined);
expect(decode(null)).toBe(null);
expect(decode(true)).toBe(true);
expect(decode(12)).toBe(12);
expect(decode('string')).toBe('string');
});
it('decodes dates', () => {
expect(
decode({
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z',
})
).toEqual(new Date(Date.UTC(2015, 1)));
});
it('decodes GeoPoints', () => {
const point = decode({
__type: 'GeoPoint',
latitude: 40.5,
longitude: 50.4,
});
expect(point instanceof ParseGeoPoint).toBe(true);
expect(point.latitude).toBe(40.5);
expect(point.longitude).toBe(50.4);
});
it('decodes Polygons', () => {
const points = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
];
const polygon = decode({
__type: 'Polygon',
coordinates: points,
});
expect(polygon instanceof ParsePolygon).toBe(true);
expect(polygon.coordinates).toEqual(points);
});
it('decodes Files', () => {
const file = decode({
__type: 'File',
name: 'parse.txt',
url: 'https://files.parsetfss.com/a/parse.txt',
});
expect(file instanceof ParseFile).toBe(true);
expect(file.name()).toBe('parse.txt');
expect(file.url()).toBe('https://files.parsetfss.com/a/parse.txt');
});
it('decodes Relations', () => {
const obj = decode({
__type: 'Relation',
className: 'Delivery',
});
expect(obj.constructor.mock.calls[0]).toEqual([null, null]);
expect(obj.targetClassName).toBe('Delivery');
});
it('decodes Pointers', () => {
const spy = jest.spyOn(ParseObject, 'fromJSON');
const data = {
__type: 'Pointer',
className: 'Item',
objectId: '1001',
};
decode(data);
expect(spy.mock.calls[0][0]).toEqual(data);
});
it('decodes ParseObjects', () => {
const spy = jest.spyOn(ParseObject, 'fromJSON');
const data = {
__type: 'Object',
className: 'Item',
objectId: '1001',
};
decode(data);
expect(spy.mock.calls[1][0]).toEqual(data);
});
it('iterates over arrays', () => {
expect(decode([{ __type: 'Date', iso: '2015-02-01T00:00:00.000Z' }, 12, 'string'])).toEqual([
new Date(Date.UTC(2015, 1)),
12,
'string',
]);
});
it('iterates over objects', () => {
expect(
decode({
empty: null,
when: { __type: 'Date', iso: '2015-04-01T00:00:00.000Z' },
count: 15,
})
).toEqual({
empty: null,
when: new Date(Date.UTC(2015, 3)),
count: 15,
});
});
describe('Prototype Pollution Protection', () => {
beforeEach(() => {
// Clear any pollution before each test
delete Object.prototype.polluted;
delete Object.prototype.malicious;
delete Object.prototype.exploit;
});
afterEach(() => {
// Clean up after tests
delete Object.prototype.polluted;
delete Object.prototype.malicious;
delete Object.prototype.exploit;
});
it('should not pollute Object.prototype when decoding object with __proto__ key', () => {
const testObj = {};
const maliciousInput = {
normalKey: 'value',
__proto__: { polluted: 'yes' },
};
const result = decode(maliciousInput);
// Verify Object.prototype was not polluted
expect(testObj.polluted).toBeUndefined();
expect({}.polluted).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
// Verify result only has own property
expect(Object.prototype.hasOwnProperty.call(result, '__proto__')).toBe(false);
expect(result.normalKey).toBe('value');
});
it('should not pollute Object.prototype when decoding object with constructor key', () => {
const testObj = {};
const maliciousInput = {
normalKey: 'value',
constructor: { polluted: 'yes' },
};
const result = decode(maliciousInput);
// Verify Object.prototype was not polluted
expect(testObj.polluted).toBeUndefined();
expect({}.polluted).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
// Verify result doesn't contain constructor from prototype chain
expect(result.normalKey).toBe('value');
});
it('should not pollute Object.prototype when decoding object with prototype key', () => {
const testObj = {};
const maliciousInput = {
normalKey: 'value',
prototype: { polluted: 'yes' },
};
const result = decode(maliciousInput);
// Verify Object.prototype was not polluted
expect(testObj.polluted).toBeUndefined();
expect({}.polluted).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
// Verify result contains only own properties
expect(result.normalKey).toBe('value');
});
it('should not pollute Object.prototype when decoding nested objects with dangerous keys', () => {
const testObj = {};
const maliciousInput = {
nested: {
__proto__: { polluted: 'nested' },
data: 'value',
},
normal: 'key',
};
const result = decode(maliciousInput);
// Verify Object.prototype was not polluted
expect(testObj.polluted).toBeUndefined();
expect({}.polluted).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
// Verify result structure
expect(result.normal).toBe('key');
expect(result.nested).toBeDefined();
expect(result.nested.data).toBe('value');
expect(Object.prototype.hasOwnProperty.call(result.nested, '__proto__')).toBe(false);
});
it('should not pollute Object.prototype when decoding arrays with objects containing dangerous keys', () => {
const testObj = {};
const maliciousInput = [
{ __proto__: { polluted: 'array1' } },
{ constructor: { malicious: 'array2' } },
{ normalKey: 'value' },
];
const result = decode(maliciousInput);
// Verify Object.prototype was not polluted
expect(testObj.polluted).toBeUndefined();
expect(testObj.malicious).toBeUndefined();
expect({}.polluted).toBeUndefined();
expect({}.malicious).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
expect(Object.prototype.malicious).toBeUndefined();
// Verify result array
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(3);
expect(result[2].normalKey).toBe('value');
});
it('should only decode own properties, not inherited ones', () => {
const parent = { inherited: 'parent' };
const child = Object.create(parent);
child.own = 'child';
const result = decode(child);
// Should only include own property
expect(result.own).toBe('child');
expect(result.inherited).toBeUndefined();
});
it('should not decode properties from prototype chain', () => {
Object.prototype.exploit = 'malicious';
const obj = { normalKey: 'value' };
const result = decode(obj);
// Should not include prototype property
expect(result.normalKey).toBe('value');
expect(Object.prototype.hasOwnProperty.call(result, 'exploit')).toBe(false);
delete Object.prototype.exploit;
});
it('should not pollute Object.prototype when decoding Parse type with dangerous className', () => {
const testObj = {};
const maliciousInput = {
__type: 'Pointer',
className: '__proto__',
objectId: 'test123',
};
// This should be handled by ParseObject.fromJSON
decode(maliciousInput);
// Verify Object.prototype was not polluted
expect(testObj.polluted).toBeUndefined();
expect({}.polluted).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
});
it('should not pollute Object.prototype when decoding deeply nested dangerous keys', () => {
const testObj = {};
const maliciousInput = {
level1: {
level2: {
level3: {
__proto__: { polluted: 'deep' },
normalData: 'value',
},
},
},
};
const result = decode(maliciousInput);
// Verify Object.prototype was not polluted
expect(testObj.polluted).toBeUndefined();
expect({}.polluted).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
// Verify result structure is preserved (without dangerous keys)
expect(result.level1.level2.level3.normalData).toBe('value');
expect(Object.prototype.hasOwnProperty.call(result.level1.level2.level3, '__proto__')).toBe(
false
);
});
});
});