Skip to content

Commit

Permalink
fix: do not remap crypto, Crypto and SubtleCrypto if remapTypedArrays…
Browse files Browse the repository at this point in the history
… is false (#450)
  • Loading branch information
rwaldron authored Feb 12, 2024
1 parent 8255881 commit d9a76c6
Show file tree
Hide file tree
Showing 3 changed files with 319 additions and 26 deletions.
34 changes: 16 additions & 18 deletions packages/near-membrane-dom/src/window.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
ReflectApply,
ReflectDeleteProperty,
ReflectGetPrototypeOf,
ReflectOwnKeys,
toSafeArray,
toSafeWeakMap,
SetCtor,
SetProtoHas,
} from '@locker/near-membrane-shared';
import { IS_CHROMIUM_BROWSER, rootWindow } from '@locker/near-membrane-shared-dom';

Expand Down Expand Up @@ -64,28 +66,22 @@ export function getCachedGlobalObjectReferences(
}

export function filterWindowKeys(keys: PropertyKey[], remapTypedArrays: boolean): PropertyKey[] {
const result: PropertyKey[] = toSafeArray([]);
const excludedKeys = new SetCtor(['document', 'location', 'top', 'window']);
// Crypto and typed arrays must be from the same global object
if (remapTypedArrays === false) {
excludedKeys.add('crypto');
excludedKeys.add('Crypto');
excludedKeys.add('SubtleCrypto');
}
const result: PropertyKey[] = [];
let resultOffset = 0;
for (let i = 0, { length } = keys; i < length; i += 1) {
const key = keys[i];
if (
// Filter out unforgeable property keys that cannot be installed.
key !== 'document' &&
key !== 'location ' &&
key !== 'top' &&
key !== 'window' &&
// Remove other browser specific unforgeables.
key !== 'chrome'
) {
result[resultOffset++] = key;
if (ReflectApply(SetProtoHas, excludedKeys, [key])) {
continue;
}
result[resultOffset++] = key;
}

// Crypto and typed arrays must be from the same global object
if (remapTypedArrays === false) {
result.splice(result.indexOf('Crypto'), 1);
}

return result;
}

Expand Down Expand Up @@ -129,7 +125,9 @@ export function removeWindowDescriptors<T extends PropertyDescriptorMap>(
ReflectDeleteProperty(unsafeDescs, 'chrome');
// Crypto and typed arrays must be from the same global object
if (remapTypedArrays === false) {
ReflectDeleteProperty(unsafeDescs, 'crypto');
ReflectDeleteProperty(unsafeDescs, 'Crypto');
ReflectDeleteProperty(unsafeDescs, 'SubtleCrypto');
}
return unsafeDescs;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/near-membrane-shared/src/Set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export const SetCtor = Set;

const { prototype: SetProto } = SetCtor;

export const { add: SetProtoAdd, values: SetProtoValues } = SetProto;
export const { add: SetProtoAdd, has: SetProtoHas, values: SetProtoValues } = SetProto;

export const SetProtoSizeGetter = ObjectLookupOwnGetter(SetProto, 'size')!;
Loading

0 comments on commit d9a76c6

Please sign in to comment.