-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathprototypeLies.js
647 lines (616 loc) · 16.8 KB
/
prototypeLies.js
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/* Prototype lies */
const getIframe = () => {
try {
const numberOfIframes = window.length
const frag = new DocumentFragment()
const div = document.createElement('div')
frag.appendChild(div)
const ghost = () => `
height: 100vh;
width: 100vw;
position: absolute;
left:-10000px;
visibility: hidden;
`
div.innerHTML = `<div style="${ghost()}"><iframe></iframe></div>`
document.body.appendChild(frag)
const iframeWindow = window[numberOfIframes]
return {
iframeWindow,
div
}
} catch (error) {
return {
iframeWindow: window,
div: undefined
}
}
}
const {
iframeWindow,
div: iframeContainerDiv
} = getIframe()
const getPrototypeLies = iframeWindow => {
// Lie Tests
// object constructor descriptor should return undefined properties
const getUndefinedValueLie = (obj, name) => {
const objName = obj.name
const objNameUncapitalized = window[objName.charAt(0).toLowerCase() + objName.slice(1)]
const hasInvalidValue = !!objNameUncapitalized && (
typeof Object.getOwnPropertyDescriptor(objNameUncapitalized, name) != 'undefined' ||
typeof Reflect.getOwnPropertyDescriptor(objNameUncapitalized, name) != 'undefined'
)
return hasInvalidValue ? true : false
}
// accessing the property from the prototype should throw a TypeError
const getIllegalTypeErrorLie = (obj, name) => {
const proto = obj.prototype
try {
proto[name]
//console.log(obj.name, name)
return true
} catch (error) {
return error.constructor.name != 'TypeError' ? true : false
}
const illegal = [
'',
'is',
'call',
'seal',
'keys',
'bind',
'apply',
'assign',
'freeze',
'values',
'entries',
'toString',
'isFrozen',
'isSealed',
'constructor',
'isExtensible',
'getPrototypeOf',
'preventExtensions',
'propertyIsEnumerable',
'getOwnPropertySymbols',
'getOwnPropertyDescriptors'
]
const lied = !!illegal.find(prop => {
try {
prop == '' ? Object(proto[name]) : Object[prop](proto[name])
return true
} catch (error) {
return error.constructor.name != 'TypeError' ? true : false
}
})
return lied
}
// calling the interface prototype on the function should throw a TypeError
const getCallInterfaceTypeErrorLie = (apiFunction, proto) => {
try {
new apiFunction()
apiFunction.call(proto)
return true
} catch (error) {
return error.constructor.name != 'TypeError' ? true : false
}
}
// applying the interface prototype on the function should throw a TypeError
const getApplyInterfaceTypeErrorLie = (apiFunction, proto) => {
try {
new apiFunction()
apiFunction.apply(proto)
return true
} catch (error) {
return error.constructor.name != 'TypeError' ? true : false
}
}
// creating a new instance of the function should throw a TypeError
const getNewInstanceTypeErrorLie = apiFunction => {
try {
new apiFunction()
return true
} catch (error) {
return error.constructor.name != 'TypeError' ? true : false
}
}
// extending the function on a fake class should throw a TypeError and message "not a constructor"
const getClassExtendsTypeErrorLie = apiFunction => {
try {
class Fake extends apiFunction { }
return true
} catch (error) {
// Native has TypeError and 'not a constructor' message in FF & Chrome
return error.constructor.name != 'TypeError' ? true :
!/not a constructor/i.test(error.message) ? true : false
}
}
// setting prototype to null and converting to a string should throw a TypeError
const getNullConversionTypeErrorLie = apiFunction => {
const nativeProto = Object.getPrototypeOf(apiFunction)
try {
Object.setPrototypeOf(apiFunction, null) + ''
return true
} catch (error) {
return error.constructor.name != 'TypeError' ? true : false
} finally {
// restore proto
Object.setPrototypeOf(apiFunction, nativeProto)
}
}
// toString() and toString.toString() should return a native string in all frames
const getToStringLie = (apiFunction, name, iframeWindow) => {
let iframeToString, iframeToStringToString
try {
iframeToString = iframeWindow.Function.prototype.toString.call(apiFunction)
} catch (e) { }
try {
iframeToStringToString = iframeWindow.Function.prototype.toString.call(apiFunction.toString)
} catch (e) { }
const apiFunctionToString = (
iframeToString ?
iframeToString :
apiFunction.toString()
)
const apiFunctionToStringToString = (
iframeToStringToString ?
iframeToStringToString :
apiFunction.toString.toString()
)
/*
Accepted strings:
'function name() { [native code] }'
'function name() {\n [native code]\n}'
'function get name() { [native code] }'
'function get name() {\n [native code]\n}'
'function () { [native code] }'
`function () {\n [native code]\n}`
*/
const trust = name => ({
[`function ${name}() { [native code] }`]: true,
[`function get ${name}() { [native code] }`]: true,
[`function () { [native code] }`]: true,
[`function ${name}() {${'\n'} [native code]${'\n'}}`]: true,
[`function get ${name}() {${'\n'} [native code]${'\n'}}`]: true,
[`function () {${'\n'} [native code]${'\n'}}`]: true
})
return (
!trust(name)[apiFunctionToString] ||
!trust('toString')[apiFunctionToStringToString]
)
}
// "prototype" in function should not exist
const getPrototypeInFunctionLie = apiFunction => 'prototype' in apiFunction ? true : false
// "arguments", "caller", "prototype", "toString" should not exist in descriptor
const getDescriptorLie = apiFunction => {
const hasInvalidDescriptor = (
!!Object.getOwnPropertyDescriptor(apiFunction, 'arguments') ||
!!Reflect.getOwnPropertyDescriptor(apiFunction, 'arguments') ||
!!Object.getOwnPropertyDescriptor(apiFunction, 'caller') ||
!!Reflect.getOwnPropertyDescriptor(apiFunction, 'caller') ||
!!Object.getOwnPropertyDescriptor(apiFunction, 'prototype') ||
!!Reflect.getOwnPropertyDescriptor(apiFunction, 'prototype') ||
!!Object.getOwnPropertyDescriptor(apiFunction, 'toString') ||
!!Reflect.getOwnPropertyDescriptor(apiFunction, 'toString')
)
return hasInvalidDescriptor ? true : false
}
// "arguments", "caller", "prototype", "toString" should not exist as own property
const getOwnPropertyLie = apiFunction => {
const hasInvalidOwnProperty = (
apiFunction.hasOwnProperty('arguments') ||
apiFunction.hasOwnProperty('caller') ||
apiFunction.hasOwnProperty('prototype') ||
apiFunction.hasOwnProperty('toString')
)
return hasInvalidOwnProperty ? true : false
}
// descriptor keys should only contain "name" and "length"
const getDescriptorKeysLie = apiFunction => {
const descriptorKeys = Object.keys(Object.getOwnPropertyDescriptors(apiFunction))
const hasInvalidKeys = '' + descriptorKeys != 'length,name' && '' + descriptorKeys != 'name,length'
return hasInvalidKeys ? true : false
}
// own property names should only contain "name" and "length"
const getOwnPropertyNamesLie = apiFunction => {
const ownPropertyNames = Object.getOwnPropertyNames(apiFunction)
const hasInvalidNames = (
'' + ownPropertyNames != 'length,name' && '' + ownPropertyNames != 'name,length'
)
return hasInvalidNames ? true : false
}
// own keys names should only contain "name" and "length"
const getOwnKeysLie = apiFunction => {
const ownKeys = Reflect.ownKeys(apiFunction)
const hasInvalidKeys = '' + ownKeys != 'length,name' && '' + ownKeys != 'name,length'
return hasInvalidKeys ? true : false
}
// calling toString() on an object created from the function should throw a TypeError
const getNewObjectToStringTypeErrorLie = apiFunction => {
try {
Object.create(apiFunction).toString()
return true
} catch (error) {
return error.constructor.name != 'TypeError' ? true : false
}
}
// API Function Test
const getLies = (apiFunction, proto, obj = null) => {
if (typeof apiFunction != 'function') {
return {
lied: false,
lieTypes: []
}
}
const name = apiFunction.name.replace(/get\s/, '')
const lies = {
// custom lie string names
[`a: accessing the property from the prototype should throw a TypeError`]: obj ? getIllegalTypeErrorLie(obj, name) : false,
[`b: object constructor descriptor should return undefined properties`]: obj ? getUndefinedValueLie(obj, name) : false,
[`c: calling the interface prototype on the function should throw a TypeError`]: getCallInterfaceTypeErrorLie(apiFunction, proto),
[`d: applying the interface prototype on the function should throw a TypeError`]: getApplyInterfaceTypeErrorLie(apiFunction, proto),
[`e: creating a new instance of the function should throw a TypeError`]: getNewInstanceTypeErrorLie(apiFunction),
[`f: extending the function on a fake class should throw a TypeError`]: getClassExtendsTypeErrorLie(apiFunction),
[`g: setting prototype to null and converting to a string should throw a TypeError`]: getNullConversionTypeErrorLie(apiFunction),
[`h: toString() and toString.toString() should return a native string in all frames`]: getToStringLie(apiFunction, name, iframeWindow),
[`i: "prototype" in function should not exist`]: getPrototypeInFunctionLie(apiFunction),
[`j: "arguments", "caller", "prototype", "toString" should not exist in descriptor`]: getDescriptorLie(apiFunction),
[`k: "arguments", "caller", "prototype", "toString" should not exist as own property`]: getOwnPropertyLie(apiFunction),
[`l: descriptor keys should only contain "name" and "length"`]: getDescriptorKeysLie(apiFunction),
[`m: own property names should only contain "name" and "length"`]: getOwnPropertyNamesLie(apiFunction),
[`n: own keys names should only contain "name" and "length"`]: getOwnKeysLie(apiFunction),
[`o: calling toString() on an object created from the function should throw a TypeError`]: getNewObjectToStringTypeErrorLie(apiFunction)
}
const lieTypes = Object.keys(lies).filter(key => !!lies[key])
return {
lied: lieTypes.length,
lieTypes
}
}
// Lie Detector
const createLieDetector = () => {
const isSupported = obj => typeof obj != 'undefined' && !!obj
const props = {} // lie list and detail
let propsSearched = [] // list of properties searched
return {
getProps: () => props,
getPropsSearched: () => propsSearched,
searchLies: (fn, {
target = [],
ignore = []
} = {}) => {
let obj
// check if api is blocked or not supported
try {
obj = fn()
if (!isSupported(obj)) {
return
}
} catch (error) {
return
}
const interfaceObject = !!obj.prototype ? obj.prototype : obj
Object.getOwnPropertyNames(interfaceObject)
.forEach(name => {
const skip = (
name == 'constructor' ||
(target.length && !new Set(target).has(name)) ||
(ignore.length && new Set(ignore).has(name))
)
if (skip) {
return
}
const objectNameString = /\s(.+)\]/
const apiName = `${obj.name ? obj.name : objectNameString.test(obj) ? objectNameString.exec(obj)[1] : undefined
}.${name}`
propsSearched.push(apiName)
try {
const proto = obj.prototype ? obj.prototype : obj
let res // response from getLies
// search if function
try {
const apiFunction = proto[name] // may trigger TypeError
if (typeof apiFunction == 'function') {
res = getLies(proto[name], proto)
if (res.lied) {
return (props[apiName] = res.lieTypes)
}
return
}
} catch (error) { }
// else search getter function
const getterFunction = Object.getOwnPropertyDescriptor(proto, name).get
res = getLies(getterFunction, proto, obj) // send the obj for special tests
if (res.lied) {
return (props[apiName] = res.lieTypes)
}
return
} catch (error) {
const lie = `failed prototype test execution`
return (
props[apiName] = [lie]
)
}
})
}
}
}
const lieDetector = createLieDetector()
const {
searchLies
} = lieDetector
// search for lies: remove target to search all properties
searchLies(() => AnalyserNode)
searchLies(() => AudioBuffer, {
target: [
'copyFromChannel',
'getChannelData'
]
})
searchLies(() => BiquadFilterNode, {
target: [
'getFrequencyResponse'
]
})
searchLies(() => CanvasRenderingContext2D, {
target: [
'getImageData',
'getLineDash',
'isPointInPath',
'isPointInStroke',
'measureText',
'quadraticCurveTo'
]
})
searchLies(() => Date, {
target: [
'getDate',
'getDay',
'getFullYear',
'getHours',
'getMinutes',
'getMonth',
'getTime',
'getTimezoneOffset',
'setDate',
'setFullYear',
'setHours',
'setMilliseconds',
'setMonth',
'setSeconds',
'setTime',
'toDateString',
'toJSON',
'toLocaleDateString',
'toLocaleString',
'toLocaleTimeString',
'toString',
'toTimeString',
'valueOf'
]
})
searchLies(() => Intl.DateTimeFormat, {
target: [
'format',
'formatRange',
'formatToParts',
'resolvedOptions'
]
})
searchLies(() => Document, {
target: [
'createElement',
'createElementNS',
'getElementById',
'getElementsByClassName',
'getElementsByName',
'getElementsByTagName',
'getElementsByTagNameNS',
'referrer',
'write',
'writeln'
],
ignore: [
// Firefox returns undefined on getIllegalTypeErrorLie test
'onreadystatechange',
'onmouseenter',
'onmouseleave'
]
})
searchLies(() => DOMRect)
searchLies(() => DOMRectReadOnly)
searchLies(() => Element, {
target: [
'append',
'appendChild',
'getBoundingClientRect',
'getClientRects',
'insertAdjacentElement',
'insertAdjacentHTML',
'insertAdjacentText',
'insertBefore',
'prepend',
'replaceChild',
'replaceWith',
'setAttribute'
]
})
searchLies(() => Function, {
target: [
'toString',
],
ignore: [
// Chrome Firefox returns false positives
'caller',
'arguments'
]
})
searchLies(() => HTMLCanvasElement)
searchLies(() => HTMLElement, {
target: [
'clientHeight',
'clientWidth',
'offsetHeight',
'offsetWidth',
'scrollHeight',
'scrollWidth'
],
ignore: [
// Firefox returns undefined on getIllegalTypeErrorLie test
'onmouseenter',
'onmouseleave'
]
})
searchLies(() => HTMLIFrameElement, {
target: [
'contentDocument',
'contentWindow',
]
})
searchLies(() => IntersectionObserverEntry, {
target: [
'boundingClientRect',
'intersectionRect',
'rootBounds'
]
})
searchLies(() => Math, {
target: [
'acos',
'acosh',
'asinh',
'atan',
'atan2',
'atanh',
'cbrt',
'cos',
'cosh',
'exp',
'expm1',
'log',
'log10',
'log1p',
'sin',
'sinh',
'sqrt',
'tan',
'tanh'
]
})
searchLies(() => MediaDevices, {
target: [
'enumerateDevices',
'getDisplayMedia',
'getUserMedia'
]
})
searchLies(() => Navigator, {
target: [
'appCodeName',
'appName',
'appVersion',
'buildID',
'connection',
'deviceMemory',
'getBattery',
'getGamepads',
'getVRDisplays',
'hardwareConcurrency',
'language',
'languages',
'maxTouchPoints',
'mimeTypes',
'oscpu',
'platform',
'plugins',
'product',
'productSub',
'sendBeacon',
'serviceWorker',
'userAgent',
'vendor',
'vendorSub'
]
})
searchLies(() => Node, {
target: [
'appendChild',
'insertBefore',
'replaceChild'
]
})
searchLies(() => OffscreenCanvasRenderingContext2D, {
target: [
'getImageData',
'getLineDash',
'isPointInPath',
'isPointInStroke',
'measureText',
'quadraticCurveTo'
]
})
searchLies(() => Range, {
target: [
'getBoundingClientRect',
'getClientRects',
]
})
searchLies(() => Intl.RelativeTimeFormat, {
target: [
'resolvedOptions'
]
})
searchLies(() => Screen)
searchLies(() => SVGRect)
searchLies(() => TextMetrics)
searchLies(() => WebGLRenderingContext, {
target: [
'bufferData',
'getParameter',
'readPixels'
]
})
searchLies(() => WebGL2RenderingContext, {
target: [
'bufferData',
'getParameter',
'readPixels'
]
})
/* potential targets:
RTCPeerConnection
Plugin
PluginArray
MimeType
MimeTypeArray
Worker
History
*/
// return lies list and detail
const props = lieDetector.getProps()
const propsSearched = lieDetector.getPropsSearched()
return {
lieList: Object.keys(props).sort(),
lieDetail: props,
lieCount: Object.keys(props).reduce((acc, key) => acc + props[key].length, 0),
propsSearched
}
}
// start program
const start = performance.now()
const {
lieList,
lieDetail,
lieCount,
propsSearched
} = getPrototypeLies(iframeWindow) // execute and destructure the list and detail
if (iframeContainerDiv) {
iframeContainerDiv.parentNode.removeChild(iframeContainerDiv)
}
const perf = performance.now() - start
// check lies later in any function
lieList.includes('HTMLCanvasElement.toDataURL') // returns true or false
lieDetail['HTMLCanvasElement.toDataURL'] // returns the list of lies
console.log(propsSearched)
console.log(lieList)
console.log(lieDetail)