-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add harden events utilities Change lockdown execution order WIP: Experimental testing (for debugging purposes only) Update with version of code that will work after updating post-message-stream Add review refactoring (1) Add review refactoring (2) Add review refactoring (3) Update post-message-stream version and add test to improve coverage Fix coverage after rebase Try to fix coverage Refactor lockdown-events.ts Fix yarn conflicts after rebase Refactor test for events lockdown function Add lockdown events in other environments Revert "Add lockdown events in other environments" This reverts commit 4e89e4e. Add new proposal for implementing lockdown of events Fix coverage issues * Fix after rebase
- Loading branch information
Showing
11 changed files
with
157 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"branches": 77.41, | ||
"functions": 91.26, | ||
"lines": 85.29, | ||
"statements": 85.37 | ||
"branches": 78.62, | ||
"functions": 91.4, | ||
"lines": 87.95, | ||
"statements": 88.02 | ||
} |
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
25 changes: 25 additions & 0 deletions
25
packages/snaps-execution-environments/src/common/lockdown/lockdown-events.test.browser.ts
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,25 @@ | ||
// eslint-disable-next-line import/unambiguous | ||
import { expect } from '@wdio/globals'; | ||
|
||
import { executeLockdownEvents } from './lockdown-events'; | ||
|
||
describe('lockdown events security', () => { | ||
it('should lockdown events and made event properties inaccessible', async () => { | ||
executeLockdownEvents(); | ||
|
||
const eventTarget = new EventTarget(); | ||
|
||
const promise = new Promise((resolve) => { | ||
eventTarget.addEventListener('just-test-event', (eventObject) => { | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
resolve(eventObject.composedPath); | ||
}); | ||
}); | ||
|
||
const testEvent = new Event('just-test-event'); | ||
eventTarget.dispatchEvent(testEvent); | ||
|
||
const result = await promise; | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
packages/snaps-execution-environments/src/common/lockdown/lockdown-events.ts
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,57 @@ | ||
// When creating a sandbox, limitation of the events from leaking | ||
// sensitive objects is required. This is done by overriding own properties | ||
// of prototypes of all existing events. | ||
import { hasProperty } from '@metamask/utils'; | ||
|
||
/** | ||
* Targeted Event objects and properties. | ||
* Note: This is a map of the prototypes that inherit from Events with | ||
* properties that are identified to leak sensitive objects. | ||
* Not all browsers support all event types, so checking its existence is required. | ||
*/ | ||
const targetEvents = new Map(); | ||
if (hasProperty(globalThis, 'UIEvent')) { | ||
targetEvents.set(UIEvent.prototype, ['view']); | ||
} | ||
if (hasProperty(globalThis, 'MutationEvent')) { | ||
targetEvents.set(MutationEvent.prototype, ['relatedNode']); | ||
} | ||
if (hasProperty(globalThis, 'MessageEvent')) { | ||
targetEvents.set(MessageEvent.prototype, ['source']); | ||
} | ||
if (hasProperty(globalThis, 'FocusEvent')) { | ||
targetEvents.set(FocusEvent.prototype, ['relatedTarget']); | ||
} | ||
if (hasProperty(globalThis, 'MouseEvent')) { | ||
targetEvents.set(MouseEvent.prototype, [ | ||
'relatedTarget', | ||
'fromElement', | ||
'toElement', | ||
]); | ||
} | ||
if (hasProperty(globalThis, 'TouchEvent')) { | ||
targetEvents.set(TouchEvent.prototype, ['targetTouches', 'touches']); | ||
} | ||
if (hasProperty(globalThis, 'Event')) { | ||
targetEvents.set(Event.prototype, [ | ||
'target', | ||
'currentTarget', | ||
'srcElement', | ||
'composedPath', | ||
]); | ||
} | ||
|
||
/** | ||
* Attenuate Event objects by replacing its own properties. | ||
*/ | ||
export function executeLockdownEvents() { | ||
targetEvents.forEach((properties, prototype) => { | ||
for (const property of properties) { | ||
Object.defineProperty(prototype, property, { | ||
value: undefined, | ||
configurable: false, | ||
writable: false, | ||
}); | ||
} | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { executeLockdown } from '../common/lockdown/lockdown'; | ||
import { executeLockdownEvents } from '../common/lockdown/lockdown-events'; | ||
import { executeLockdownMore } from '../common/lockdown/lockdown-more'; | ||
import { IFrameSnapExecutor } from './IFrameSnapExecutor'; | ||
|
||
executeLockdown(); | ||
executeLockdownMore(); | ||
executeLockdownEvents(); | ||
|
||
IFrameSnapExecutor.initialize(); |
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