Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hardening for Request, Headers and Response endowments #1695

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ describe('endowments', () => {
clearInterval: clearIntervalAttenuated,
} = interval.factory();
const { Math: mathAttenuated } = math.factory();
const { fetch: fetchAttenuated } = network.factory();
const {
fetch: fetchAttenuated,
Request: RequestHardened,
Headers: HeadersHardened,
Response: ResponseHardened,
} = network.factory();
const { Date: DateAttenuated } = date.factory();
const { console: consoleAttenuated } = consoleEndowment.factory({
snapId: MOCK_SNAP_ID,
Expand Down Expand Up @@ -172,6 +177,18 @@ describe('endowments', () => {
endowments: { btoa },
factory: () => btoa('Snaps'),
},
RequestHardened: {
endowments: { RequestHardened },
factory: () => new RequestHardened('https://metamask.io'),
},
HeadersHardened: {
endowments: { HeadersHardened },
factory: () => new HeadersHardened(),
},
ResponseHardened: {
endowments: { ResponseHardened },
factory: () => new ResponseHardened(),
},
setTimeoutAttenuated: {
endowments: { setTimeoutAttenuated },
factory: () => setTimeoutAttenuated((param: unknown) => param, 1),
Expand Down Expand Up @@ -214,7 +231,7 @@ describe('endowments', () => {
});

if (factory()) {
it('does not leak `globalThis`', () => {
it(`${name} does not leak 'globalThis'`, () => {
const instance = factory();
expect(walkAndSearch(instance, globalThis)).toBe(false);
});
Expand All @@ -241,7 +258,7 @@ describe('endowments', () => {
},
{
factory: expect.any(Function),
names: ['fetch'],
names: ['fetch', 'Request', 'Headers', 'Response'],
},
{
factory: expect.any(Function),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,19 @@ const createNetwork = () => {

return {
fetch: harden(_fetch),
// Request, Headers and Response are the endowments injected alongside fetch
// only when 'endowment:network-access' permission is requested,
// therefore these are hardened as part of fetch dependency injection within its factory.
// These endowments are not (and should never be) available by default.
Request: harden(Request),
Headers: harden(Headers),
Response: harden(Response),
teardownFunction,
};
};

const endowmentModule = {
names: ['fetch'] as const,
names: ['fetch', 'Request', 'Headers', 'Response'] as const,
factory: createNetwork,
};
export default endowmentModule;