-
-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathencode-test.js
More file actions
341 lines (294 loc) · 9.83 KB
/
encode-test.js
File metadata and controls
341 lines (294 loc) · 9.83 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
jest.dontMock('../encode');
jest.dontMock('../isDangerousKey');
jest.dontMock('../ParseACL');
jest.dontMock('../ParseFile');
jest.dontMock('../ParseGeoPoint');
jest.dontMock('../ParseOp');
jest.dontMock('../ParseUser');
jest.dontMock('../CoreManager');
const mockObject = function (className) {
this.className = className;
};
mockObject.registerSubclass = function () {};
mockObject.prototype = {
_getServerData() {
return this._serverData;
},
toPointer() {
return 'POINTER';
},
toOfflinePointer() {
return 'OFFLINE_POINTER';
},
_getId() {
return 'local1234';
},
dirty() {},
toJSON() {
return this.attributes;
},
_toFullJSON(seen, offline) {
const json = {
__type: 'Object',
className: this.className,
};
for (const attr in this.attributes) {
json[attr] = encode(this.attributes[attr], false, false, seen.concat(this), offline);
}
return json;
},
};
jest.setMock('../ParseObject', {
__esModule: true,
default: mockObject,
});
const encode = require('../encode').default;
const ParseACL = require('../ParseACL').default;
const ParseFile = require('../ParseFile').default;
const ParseGeoPoint = require('../ParseGeoPoint').default;
const ParseObject = require('../ParseObject').default;
const ParseRelation = require('../ParseRelation').default;
const CoreManager = require('../CoreManager').default;
CoreManager.setParseObject(mockObject);
CoreManager.setParseOp(require('../ParseOp'));
CoreManager.setParseUser(require('../ParseUser').default);
describe('encode', () => {
it('ignores primitives', () => {
expect(encode(undefined)).toBe(undefined);
expect(encode(null)).toBe(null);
expect(encode(true)).toBe(true);
expect(encode(12)).toBe(12);
expect(encode('string')).toBe('string');
});
it('encodes dates', () => {
expect(encode(new Date(Date.UTC(2015, 1)))).toEqual({
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z',
});
expect(encode.bind(null, new Date(Date.parse(null)))).toThrow(
'Tried to encode an invalid date.'
);
});
it('encodes regular expressions', () => {
expect(encode(new RegExp('^hello'))).toEqual('^hello');
expect(encode(/a[^b]+c/g)).toEqual('a[^b]+c');
});
it('encodes GeoPoints', () => {
const point = new ParseGeoPoint(40.5, 50.4);
expect(encode(point)).toEqual({
__type: 'GeoPoint',
latitude: 40.5,
longitude: 50.4,
});
});
it('encodes Files', () => {
const file = new ParseFile('parse.txt');
expect(encode.bind(null, file)).toThrow('Tried to encode an unsaved file.');
file._url = 'https://files.parsetfss.com/a/parse.txt';
expect(encode(file)).toEqual({
__type: 'File',
name: 'parse.txt',
url: 'https://files.parsetfss.com/a/parse.txt',
});
});
it('encodes Relations', () => {
const rel = new ParseRelation();
encode(rel);
expect(rel.toJSON.mock.calls.length).toBe(1);
});
it('encodes ACLs', () => {
const acl = new ParseACL({ aUserId: { read: true, write: false } });
expect(encode(acl)).toEqual({
aUserId: {
read: true,
write: false,
},
});
});
it('encodes ParseObjects', () => {
const obj = new ParseObject('Item');
obj._serverData = {};
expect(encode(obj)).toEqual('POINTER');
obj._serverData = obj.attributes = {
str: 'string',
date: new Date(Date.UTC(2015, 1, 1)),
};
expect(encode(obj)).toEqual({
__type: 'Object',
className: 'Item',
str: 'string',
date: {
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z',
},
});
obj.attributes.self = obj;
expect(encode(obj)).toEqual({
__type: 'Object',
className: 'Item',
str: 'string',
date: {
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z',
},
self: 'POINTER',
});
});
it('encodes ParseObjects offline', () => {
const obj = new ParseObject('Item');
obj._serverData = {};
expect(encode(obj, false, false, undefined, true)).toEqual('OFFLINE_POINTER');
obj._serverData = obj.attributes = {
str: 'string',
date: new Date(Date.UTC(2015, 1, 1)),
};
obj.attributes.self = obj;
expect(encode(obj, false, false, undefined, true)).toEqual({
__type: 'Object',
className: 'Item',
str: 'string',
date: {
__type: 'Date',
iso: '2015-02-01T00:00:00.000Z',
},
self: 'OFFLINE_POINTER',
});
});
it('does not encode ParseObjects when they are disallowed', () => {
const obj = new ParseObject('Item');
expect(encode.bind(null, obj, true)).toThrow('Parse Objects not allowed here');
});
it('iterates over arrays', () => {
let arr = [12, new Date(Date.UTC(2015, 1)), 'str'];
expect(encode(arr)).toEqual([12, { __type: 'Date', iso: '2015-02-01T00:00:00.000Z' }, 'str']);
arr = [arr];
expect(encode(arr)).toEqual([[12, { __type: 'Date', iso: '2015-02-01T00:00:00.000Z' }, 'str']]);
});
it('iterates over objects', () => {
const obj = {
num: 12,
date: new Date(Date.UTC(2015, 1)),
str: 'abc',
};
expect(encode(obj)).toEqual({
num: 12,
date: { __type: 'Date', iso: '2015-02-01T00:00:00.000Z' },
str: 'abc',
});
});
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 encoding object with __proto__ key', () => {
const testObj = {};
const maliciousInput = {
normalKey: 'value',
__proto__: { polluted: 'yes' },
};
const result = encode(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 encoding object with constructor key', () => {
const testObj = {};
const maliciousInput = {
normalKey: 'value',
constructor: { polluted: 'yes' },
};
const result = encode(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 encoding object with prototype key', () => {
const testObj = {};
const maliciousInput = {
normalKey: 'value',
prototype: { polluted: 'yes' },
};
const result = encode(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 encoding nested objects with dangerous keys', () => {
const testObj = {};
const maliciousInput = {
nested: {
__proto__: { polluted: 'nested' },
data: 'value',
},
normal: 'key',
};
const result = encode(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 encoding arrays with objects containing dangerous keys', () => {
const testObj = {};
const maliciousInput = [
{ __proto__: { polluted: 'array1' } },
{ constructor: { malicious: 'array2' } },
{ normalKey: 'value' },
];
const result = encode(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 encode own properties, not inherited ones', () => {
const parent = { inherited: 'parent' };
const child = Object.create(parent);
child.own = 'child';
const result = encode(child);
// Should only include own property
expect(result.own).toBe('child');
expect(result.inherited).toBeUndefined();
});
it('should not encode properties from prototype chain', () => {
Object.prototype.exploit = 'malicious';
const obj = { normalKey: 'value' };
const result = encode(obj);
// Should not include prototype property
expect(result.normalKey).toBe('value');
expect(Object.prototype.hasOwnProperty.call(result, 'exploit')).toBe(false);
delete Object.prototype.exploit;
});
});
});