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

Failed to init in the Web Worker #76

Open
Jack-Works opened this issue Apr 10, 2020 · 1 comment
Open

Failed to init in the Web Worker #76

Jack-Works opened this issue Apr 10, 2020 · 1 comment

Comments

@Jack-Works
Copy link

https://github.com/PeculiarVentures/webcrypto-liner/blob/faae2d4b127c1830ceb2fad4736d6177feab3544/src/shim.ts#L11-18

Source code:

try {
  // Replace original crypto by liner
  delete (self as any).crypto;
  window.crypto = new Crypto();
  Object.freeze(window.crypto);
} catch (e) {
  Debug.error(e);
}

In the mainframe, crypto.* is defined on the Window interface and self or window has the prototype of Window therefore everything is fine.

In Web Worker, crypto.* is defined on the WorkerGlobalScope, but self is not direct instance of WorkerGlobalScope:

The prototype chain is:

self => DedicatedWorkerGlobalScope => WorkerGlobalScope

Therefore delete self.crypto doesn't work since crypto is not defined on the DedicatedWorkerGlobalScope but WorkerGlobalScope.

After the deletion implicitly failed (delete op return true for non-existence property, in this case, crypto)

Line 13 window.crypto = new Crypto(); failed to run because WorkerGlobalScope.crypto is a configurable getter but no setter.

@Jack-Works
Copy link
Author

Workaround:

Run the following code before loading webcrypto-liner

const MaybeWorkerGlobalScope = Object.getPrototypeOf(Object.getPrototypeOf(globalThis))
const crypto = Object.getOwnPropertyDescriptor(MaybeWorkerGlobalScope, 'crypto')
// The crypto is defined in [WorkerGlobalScope], let's move it to [DedicatedWorkerGlobalScope]
if (crypto) {
    delete MaybeWorkerGlobalScope.crypto
    Object.defineProperty(globalThis, 'crypto', crypto)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant