Skip to content

Commit 0bd78fc

Browse files
authored
fix(splicer): only stub preview1 random_get when random is disabled (#367)
The preview1 random_get import was always replaced with an unreachable stub, so OpenSSL entropy (RSA/EC public key validation during crypto.subtle.importKey) trapped the instance. Leave it to the preview1 adapter, which maps it to wasi:random, unless 'random' is disabled. Fixes #334
1 parent e9a72ee commit 0bd78fc

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎crates/spidermonkey-embedding-splicer/src/stub_wasi.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,11 @@ pub fn stub_wasi(
136136

137137
let mut module = Module::parse(wasm.as_slice(), false, false).unwrap();
138138

139-
stub_preview1(&mut module)?;
140-
141139
stub_filesystem(&mut module, &target_world_imports)?;
142140
stub_cli(&mut module, &target_world_imports)?;
143141

144142
if !features.contains(&Feature::Random) {
143+
stub_preview1(&mut module)?;
145144
stub_random(&mut module)?;
146145
}
147146

@@ -208,7 +207,6 @@ fn target_world_requires_io(target_world_imports: &HashSet<String>) -> bool {
208207

209208
const PREVIEW1: &str = "wasi_snapshot_preview1";
210209
fn stub_preview1(module: &mut Module) -> Result<()> {
211-
// random comes from prevew2 only in StarlingMonkey
212210
stub_import(module, PREVIEW1, "random_get", unreachable_stub)?;
213211
Ok(())
214212
}

‎test/builtins/crypto-import-key.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { strictEqual } from 'node:assert';
2+
import { generateKeyPairSync, sign } from 'node:crypto';
3+
4+
const data = 'componentize-js';
5+
const rsa = generateKeyPairSync('rsa', { modulusLength: 2048 });
6+
const ec = generateKeyPairSync('ec', { namedCurve: 'P-256' });
7+
const rsaSig = sign('sha256', Buffer.from(data), rsa.privateKey);
8+
const ecSig = sign('sha256', Buffer.from(data), {
9+
key: ec.privateKey,
10+
dsaEncoding: 'ieee-p1363',
11+
});
12+
13+
export const source = `
14+
const b64 = (s) => Uint8Array.from(atob(s), (c) => c.charCodeAt(0));
15+
const data = new TextEncoder().encode('${data}');
16+
async function verify(jwk, sig, importAlg, verifyAlg) {
17+
const key = await crypto.subtle.importKey('jwk', jwk, importAlg, false, ['verify']);
18+
return key.type + ' ' + await crypto.subtle.verify(verifyAlg, key, b64(sig), data);
19+
}
20+
export async function run () {
21+
const rsaAlg = { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' };
22+
console.log(await verify(${JSON.stringify(rsa.publicKey.export({ format: 'jwk' }))}, '${rsaSig.toString('base64')}', rsaAlg, rsaAlg));
23+
console.log(await verify(${JSON.stringify(ec.publicKey.export({ format: 'jwk' }))}, '${ecSig.toString('base64')}', { name: 'ECDSA', namedCurve: 'P-256' }, { name: 'ECDSA', hash: 'SHA-256' }));
24+
}
25+
`;
26+
27+
export async function test(run) {
28+
const { stdout, stderr } = await run();
29+
strictEqual(stderr, '');
30+
strictEqual(stdout, 'public true\npublic true\n');
31+
}

0 commit comments

Comments
 (0)