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

fix: @W-15147937 do not remap Blob, File, FileReader and URL when remapTypedArrays = false #454

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions packages/near-membrane-dom/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export function filterWindowKeys(keys: PropertyKey[], remapTypedArrays: boolean)
excludedKeys.add('crypto');
excludedKeys.add('Crypto');
excludedKeys.add('SubtleCrypto');
excludedKeys.add('Blob');
excludedKeys.add('File');
excludedKeys.add('FileReader');
excludedKeys.add('URL');
}
const result: PropertyKey[] = [];
let resultOffset = 0;
Expand Down Expand Up @@ -128,6 +132,10 @@ export function removeWindowDescriptors<T extends PropertyDescriptorMap>(
ReflectDeleteProperty(unsafeDescs, 'crypto');
ReflectDeleteProperty(unsafeDescs, 'Crypto');
ReflectDeleteProperty(unsafeDescs, 'SubtleCrypto');
ReflectDeleteProperty(unsafeDescs, 'Blob');
ReflectDeleteProperty(unsafeDescs, 'File');
ReflectDeleteProperty(unsafeDescs, 'FileReader');
ReflectDeleteProperty(unsafeDescs, 'URL');
}
return unsafeDescs;
}
Expand Down
209 changes: 205 additions & 4 deletions test/membrane/binary-data.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import createVirtualEnvironment from '@locker/near-membrane-dom';

function createEnvironmentThatAlwaysRemapsTypedArray() {
const alwayRemapping = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect, OuterUint8Array: Uint8Array }),
});

alwayRemapping.evaluate(`
const u8a = new Uint8Array();
expect(u8a instanceof OuterUint8Array).toBe(true);
`);
}
// Safari Technology Preview may not have support for Atomics enabled.
if (typeof Atomics !== 'undefined') {
describe('Atomics', () => {
beforeEach(createEnvironmentThatAlwaysRemapsTypedArray);

it('operates on atomic-friendly typed arrays', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
Expand Down Expand Up @@ -49,6 +61,8 @@
}

describe('Blob', () => {
beforeEach(createEnvironmentThatAlwaysRemapsTypedArray);

it('encodes blobs from typed arrays', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect }),
Expand All @@ -68,16 +82,16 @@
remapTypedArrays: false,
});

expect(() =>
env.evaluate(`
env.evaluate(`
const a = new Uint8Array([97, 98, 99]);
const b = new Blob([a], { type: 'application/octet-stream' });
`)
).toThrow();
`);
});
});

describe('Crypto', () => {
beforeEach(createEnvironmentThatAlwaysRemapsTypedArray);

it('creates random values from typed arrays', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect }),
Expand Down Expand Up @@ -200,6 +214,8 @@
});

describe('DataView', () => {
beforeEach(createEnvironmentThatAlwaysRemapsTypedArray);

it('should not support index access', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
Expand All @@ -225,7 +241,51 @@
});
});

describe('FileReader', () => {
beforeEach(createEnvironmentThatAlwaysRemapsTypedArray);

it('reads from blobs created from typed arrays', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect }),
});

env.evaluate(`
const source = new Uint8Array([97, 98, 99]);
const blob = new Blob([source]);
const reader = new FileReader();

reader.onload = (event) => {
expect(reader.result.byteLength).toBe(source.length);
expect(reader.result).toBeInstanceOf(ArrayBuffer);
done();
};
reader.readAsArrayBuffer(blob);
`);
});
it('reads from blobs created from typed arrays, when typed arrays are not remapped', (done) => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ done, expect }),
remapTypedArrays: false,
});

env.evaluate(`
const source = new Uint8Array([97, 98, 99]);
const blob = new Blob([source]);
const reader = new FileReader();

reader.onload = (event) => {
expect(reader.result.byteLength).toBe(source.length);
expect(reader.result).toBeInstanceOf(ArrayBuffer);
done();
};
reader.readAsArrayBuffer(blob);
`);
});
});

describe('TypedArray', () => {
beforeEach(createEnvironmentThatAlwaysRemapsTypedArray);

it('should support in bound index access', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
Expand Down Expand Up @@ -510,3 +570,144 @@
`);
});
});

describe('URL', () => {
beforeEach(createEnvironmentThatAlwaysRemapsTypedArray);

it('can create an svg blob url', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
});

env.evaluate(`
const createUrl = (type, content) => {
const blob = new Blob([content], { type });
return URL.createObjectURL(blob);
};

const content = \`
<svg id="rectangle" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="blue" />
</svg>\`;

expect(() => {
createUrl('image/svg+xml', content);
}).not.toThrow();
`);
});
it('can create an svg blob url, when typed arrays are not remapped', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
remapTypedArrays: false,
});
env.evaluate(`
const createUrl = (type, content) => {
const blob = new Blob([content], { type });
return URL.createObjectURL(blob);
};

const content = \`
<svg id="rectangle" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="blue" />
</svg>\`;

expect(() => {
createUrl('image/svg+xml', content);
}).not.toThrow();
`);
});

it('can create an html blob url', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
});

env.evaluate(`
const createUrl = (type, content) => {
const blob = new Blob([content], { type });
return URL.createObjectURL(blob);
};

expect(() => {
createUrl('text/html', '<h1>Hello World</h1>')
}).not.toThrow();
`);
});
it('can create an html blob url, when typed arrays are not remapped', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
remapTypedArrays: false,
});
env.evaluate(`
const createUrl = (type, content) => {
const blob = new Blob([content], { type });
return URL.createObjectURL(blob);
};

expect(() => {
createUrl('text/html', '<h1>Hello World</h1>')
}).not.toThrow();
`);
});

it('can create an xml blob url', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
});

env.evaluate(`
const createUrl = (type, content) => {
const blob = new Blob([content], { type });
return URL.createObjectURL(blob);
};

expect(() => {
expect(() => createUrl('text/xml', '<div><span>foo</span></div>')).not.toThrow();
}).not.toThrow();
`);
});
it('can create an xml blob url, when typed arrays are not remapped', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
remapTypedArrays: false,
});
env.evaluate(`
const createUrl = (type, content) => {
const blob = new Blob([content], { type });
return URL.createObjectURL(blob);
};

expect(() => {
expect(() => createUrl('text/xml', '<div><span>foo</span></div>')).not.toThrow();
}).not.toThrow();
`);
});

it('can create a File blob url', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
});

env.evaluate(`
const f = new File(
['<p>PEW</p><script src="http://localhost:9876/resource/test"></script>'],
'foo.txt'
);
URL.createObjectURL(f);
`);
});
it('can create a File blob url, when typed arrays are not remapped', () => {
const env = createVirtualEnvironment(window, {
endowments: Object.getOwnPropertyDescriptors({ expect }),
remapTypedArrays: false,
});
env.evaluate(`
const f = new File(
['<p>PEW</p><script src="http://localhost:9876/resource/test"></script>'],
'foo.txt'
);
URL.createObjectURL(f);
`);
});
});

Check failure on line 713 in test/membrane/binary-data.spec.js

View workflow job for this annotation

GitHub Actions / Run-linter

Delete `⏎`
Loading