16
16
*/
17
17
function srcEscape ( str ) {
18
18
return JSON . stringify ( {
19
- [ str ] : 1
19
+ [ str ] : 1 ,
20
20
} ) . slice ( 1 , - 3 ) ;
21
21
}
22
22
29
29
const REQUIRE_TERMINATOR = '' ;
30
30
highlightFn = require ( `cardinal${ REQUIRE_TERMINATOR } ` ) . highlight ;
31
31
} catch ( err ) {
32
- highlightFn = text => {
32
+ highlightFn = ( text ) => {
33
33
if ( ! cardinalRecommended ) {
34
34
// eslint-disable-next-line no-console
35
35
console . log ( 'For nicer debug output consider install cardinal@^2.0.0' ) ;
@@ -56,10 +56,44 @@ exports.printDebugWithCode = printDebugWithCode;
56
56
*/
57
57
function typeMatch ( type , list , Types ) {
58
58
if ( Array . isArray ( list ) ) {
59
- return list . some ( t => type === Types [ t ] ) ;
59
+ return list . some ( ( t ) => type === Types [ t ] ) ;
60
60
}
61
61
62
62
return ! ! list ;
63
63
}
64
64
65
65
exports . typeMatch = typeMatch ;
66
+
67
+ function createSafeObject ( ) {
68
+ const nativeProps = [ 'hasOwnProperty' , 'toString' , 'valueOf' ] ;
69
+
70
+ const handler = {
71
+ get ( _ , prop , receiver ) {
72
+ const isNativeProp = nativeProps . includes ( prop ) ;
73
+
74
+ if ( isNativeProp ) {
75
+ return ( ...args ) => Object . prototype [ prop ] . apply ( receiver , args ) ;
76
+ }
77
+
78
+ if ( prop === '__proto__' ) {
79
+ return Object . prototype ;
80
+ }
81
+
82
+ return Reflect . get ( ...arguments ) ;
83
+ } ,
84
+ set ( _ , prop ) {
85
+ const isNativeProp = nativeProps . includes ( prop ) ;
86
+
87
+ if ( isNativeProp || prop === '__proto__' ) {
88
+ return false ;
89
+ }
90
+
91
+ return Reflect . set ( ...arguments ) ;
92
+ } ,
93
+ } ;
94
+
95
+ const safePrototype = Object . create ( Object . prototype ) ;
96
+ return new Proxy ( safePrototype , handler ) ;
97
+ }
98
+
99
+ exports . createSafeObject = createSafeObject ;
0 commit comments