Skip to content

Commit

Permalink
🐛 proxy sandboxshould not delete untouched globals (#2310)
Browse files Browse the repository at this point in the history
  • Loading branch information
shijistar authored Oct 20, 2022
1 parent cec27b6 commit 6544b17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/sandbox/__tests__/proxySandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,24 @@ describe('variables in whitelist', () => {
sandbox.inactive();
expect('__REACT_ERROR_OVERLAY_GLOBAL_HOOK__' in window).toBeFalsy();
});

it('should not modify those global context that have not been touched during the app running', () => {
const original = {
iframeReady: function t1() {},
};
window.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__ = original;
const sandbox = new ProxySandbox('whitelist-variables');
const { proxy } = sandbox;
sandbox.active();
// globals in whitelist
proxy.System = {};
expect(window.System).toEqual({});
// regular globals
proxy.someOther = {};
expect('someOther' in window).toBeFalsy();
sandbox.inactive();
expect(window.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__).toBe(original);
});
});

describe('should work with nest sandbox', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/sandbox/proxySandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default class ProxySandbox implements SandBox {

if (process.env.NODE_ENV === 'test' || --activeSandboxCount === 0) {
// reset the global value to the prev value
globalVariableWhiteList.forEach((p) => {
Object.keys(this.globalWhitelistPrevDescriptor).forEach((p) => {
const descriptor = this.globalWhitelistPrevDescriptor[p];
if (descriptor) {
Object.defineProperty(this.globalContext, p, descriptor);
Expand Down

1 comment on commit 6544b17

@vercel
Copy link

@vercel vercel bot commented on 6544b17 Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.