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

sodium.ready never resolves in webworker if this webworker is created as a blob #320

Open
rwasef1830 opened this issue Jun 22, 2023 · 0 comments

Comments

@rwasef1830
Copy link

rwasef1830 commented Jun 22, 2023

Hello,

Using the technique described here:
https://stackoverflow.com/a/73621351/111830

If I importScripts("sodium.js") in such a blob created worker, sodium.ready inside the worker never fires.
If the web worker is created "normally" with an external js file, it works correctly.

Complete test case of issue:

<!DOCTYPE html>
<html>
  <body>
	<script>
		class Threadable extends Function {		  
			constructor(f) {
				super("...as",`return (${f.toString()}).apply(this, as)`);
			}
		  
			spawn(...as) {
				var code = `self.onmessage = m => self.postMessage((${this.toString()}).apply(self,m.data));`,
					blob = new Blob([code], {type: "text/javascript"}),
					wrkr = new Worker(window.URL.createObjectURL(blob));
				
				return new Promise((v, x) => (wrkr.onmessage = m => (v(m.data), wrkr.terminate()), 
											  wrkr.onerror   = e => (x(e.message), wrkr.terminate()), 
											  wrkr.postMessage(as)));
			}
		};
		
		const generateSecurityHashChunkWorker = new Threadable((salt, secret) => {
			const sodiumOnload = async sodium => {
                                // Never enters here
				console.log("Sodium ready fired");
				
				let saltSodium = sodium.from_string(salt);
				saltSodium = await sodium.crypto_generichash(16, saltSodium, null);
				const secretSodium = sodium.from_string(iterationSecret);

				const hash = await sodium.crypto_pwhash(
					64,
					secretSodium,
					saltSodium,
					sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, 
					sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
					sodium.crypto_pwhash_ALG_ARGON2ID13);

				console.log("sodium result", hash);
				postMessage(hash);
			};
			importScripts('https://fastly.jsdelivr.net/gh/jedisct1/libsodium.js@master/dist/browsers-sumo/sodium.js');
			sodium.ready.then(async () => await sodiumOnload(sodium));
			console.log("sodium.ready", sodium.ready);
		});
		
		const generateSecurityHashAsync = async (salt, secret) => {
			if (!salt) {
				throw "salt cannot be empty";
			}

			if (!secret) {
				throw "secret cannot be empty";
			}
			
			const start = new Date().getTime();
			const worker = generateSecurityHashChunkWorker.spawn(
				salt, 
				secret);
			const hash = await worker;
			console.log("Hash result from awaiting worker", hash);
			
			const timeMilliseconds = new Date().getTime() - start;
			console.log("Security hash time (ms)", timeMilliseconds, "size", hash?.length);

			return hash;
		};
	
		document.addEventListener("DOMContentLoaded", async event => {
			const salt = "Test";
			const password = "Password";
			const hash = await generateSecurityHashAsync(salt, password);
			console.log(hash);
		});
	</script>
  </body>
</html>
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