-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: non-security mode for create-react-scripts compat. (KLUDGE) (#642)
* feat: non-security mode for create-react-scripts compat. (KLUDGE) Co-authored-by: Mark S. Miller <erights@gmail.com>
- Loading branch information
Showing
4 changed files
with
129 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import test from 'ava'; | ||
import '../lockdown.js'; | ||
|
||
lockdown({ | ||
__allowUnsafeMonkeyPatching__: 'unsafe', | ||
overrideTaming: 'min', | ||
}); | ||
|
||
test('Unsafe kludge for monkey patching', t => { | ||
t.false(Object.isFrozen(Object.prototype)); | ||
|
||
const x = {}; | ||
x.toString = () => 'foo'; | ||
x.constructor = 'Foo'; | ||
t.is(`${x}`, 'foo'); | ||
t.is(x.constructor, 'Foo'); | ||
|
||
harden(x); | ||
// Because harden is tranisitively contagious up inheritance chain, | ||
// hardening x also hardens Object.prototype and other primordials | ||
// reachable from it. | ||
for (const reachable of [ | ||
Object.prototype, | ||
Object, | ||
Function.prototype, | ||
Function.prototype.constructor, | ||
Function.prototype.apply, | ||
]) { | ||
t.true(Object.isFrozen(reachable)); | ||
} | ||
for (const unreachable of [ | ||
Function, | ||
// eslint-disable-next-line no-eval | ||
eval, | ||
globalThis, | ||
Reflect, | ||
Reflect.apply, | ||
]) { | ||
t.false(Object.isFrozen(unreachable)); | ||
} | ||
|
||
const y = {}; | ||
// Under even the 'min' override taming, we still enable | ||
// Object.prototype.toString to be overridden by assignment. | ||
y.toString = () => 'bar'; | ||
t.throws( | ||
() => { | ||
// At the 'min' override taming, we do not enable | ||
// Object.prototype.constructor to be overridden by | ||
// assignment. This did not matter before hardening | ||
// x because the override mistake only applies to | ||
// non-writable properties and Object.prototype had | ||
// not yet been frozen. | ||
y.constructor = 'Bar'; | ||
}, | ||
undefined, | ||
'Override should not be enabled for "constructor".', | ||
); | ||
t.is(`${y}`, 'bar'); | ||
t.is(y.constructor, Object); | ||
}); |