Skip to content

Commit f073a95

Browse files
evqnewpavlov
authored andcommitted
Fix CSP error for wasm bindgen (#92)
1 parent 73bbbdc commit f073a95

File tree

1 file changed

+23
-39
lines changed

1 file changed

+23
-39
lines changed

src/wasm32_bindgen.rs

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,54 +58,38 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
5858
}
5959

6060
fn getrandom_init() -> Result<RngSource, Error> {
61-
// First up we need to detect if we're running in node.js or a
62-
// browser. To do this we get ahold of the `this` object (in a bit
63-
// of a roundabout fashion).
64-
//
65-
// Once we have `this` we look at its `self` property, which is
66-
// only defined on the web (either a main window or web worker).
67-
let this = Function::new("return this").call(&JsValue::undefined());
68-
assert!(this != JsValue::undefined());
69-
let this = This::from(this);
70-
let is_browser = this.self_() != JsValue::undefined();
71-
72-
if !is_browser {
73-
return Ok(RngSource::Node(node_require("crypto")));
74-
}
61+
if let Ok(self_) = Global::get_self() {
62+
// If `self` is defined then we're in a browser somehow (main window
63+
// or web worker). Here we want to try to use
64+
// `crypto.getRandomValues`, but if `crypto` isn't defined we assume
65+
// we're in an older web browser and the OS RNG isn't available.
66+
67+
let crypto = self_.crypto();
68+
if crypto.is_undefined() {
69+
return Err(BINDGEN_CRYPTO_UNDEF);
70+
}
7571

76-
// If `self` is defined then we're in a browser somehow (main window
77-
// or web worker). Here we want to try to use
78-
// `crypto.getRandomValues`, but if `crypto` isn't defined we assume
79-
// we're in an older web browser and the OS RNG isn't available.
80-
let crypto = this.crypto();
81-
if crypto.is_undefined() {
82-
return Err(BINDGEN_CRYPTO_UNDEF);
83-
}
72+
// Test if `crypto.getRandomValues` is undefined as well
73+
let crypto: BrowserCrypto = crypto.into();
74+
if crypto.get_random_values_fn().is_undefined() {
75+
return Err(BINDGEN_GRV_UNDEF);
76+
}
8477

85-
// Test if `crypto.getRandomValues` is undefined as well
86-
let crypto: BrowserCrypto = crypto.into();
87-
if crypto.get_random_values_fn().is_undefined() {
88-
return Err(BINDGEN_GRV_UNDEF);
78+
return Ok(RngSource::Browser(crypto));
8979
}
9080

91-
// Ok! `self.crypto.getRandomValues` is a defined value, so let's
92-
// assume we can do browser crypto.
93-
Ok(RngSource::Browser(crypto))
81+
return Ok(RngSource::Node(node_require("crypto")));
9482
}
9583

9684
#[wasm_bindgen]
9785
extern "C" {
98-
type Function;
99-
#[wasm_bindgen(constructor)]
100-
fn new(s: &str) -> Function;
101-
#[wasm_bindgen(method)]
102-
fn call(this: &Function, self_: &JsValue) -> JsValue;
103-
104-
type This;
105-
#[wasm_bindgen(method, getter, structural, js_name = self)]
106-
fn self_(me: &This) -> JsValue;
86+
type Global;
87+
#[wasm_bindgen(getter, catch, static_method_of = Global, js_class = self, js_name = self)]
88+
fn get_self() -> Result<Self_, JsValue>;
89+
90+
type Self_;
10791
#[wasm_bindgen(method, getter, structural)]
108-
fn crypto(me: &This) -> JsValue;
92+
fn crypto(me: &Self_) -> JsValue;
10993

11094
#[derive(Clone, Debug)]
11195
type BrowserCrypto;

0 commit comments

Comments
 (0)