Skip to content

Commit 65524ff

Browse files
Jake ChampionJakeChampion
authored andcommitted
use rangom_get instead of arc4random as arc4random does not work correctly with wizer
wizer causes the seed in arc4random to be the same between executions which is not random
1 parent 8d59f0d commit 65524ff

File tree

7 files changed

+42
-14
lines changed

7 files changed

+42
-14
lines changed

c-dependencies/js-compute-runtime/builtins/crypto.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma clang diagnostic pop
66

77
#include "crypto.h"
8+
#include "xqd.h"
89

910
bool is_int_typed_array(JSObject *obj) {
1011
return JS_IsInt8Array(obj) || JS_IsUint8Array(obj) || JS_IsInt16Array(obj) ||
@@ -44,7 +45,7 @@ bool Crypto::get_random_values(JSContext *cx, unsigned argc, JS::Value *vp) {
4445
JS::AutoCheckCannotGC noGC(cx);
4546
bool is_shared;
4647
void *buffer = JS_GetArrayBufferViewData(typed_array, &is_shared, noGC);
47-
arc4random_buf(buffer, byte_length);
48+
random_get((int32_t)buffer, byte_length);
4849

4950
args.rval().setObject(*typed_array);
5051
return true;

c-dependencies/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <arpa/inet.h>
2+
#include <cmath>
23
#include <iostream>
34
#include <regex> // std::regex
45
#include <stdio.h>
@@ -4402,6 +4403,18 @@ bool process_network_io(JSContext *cx) {
44024403
return true;
44034404
}
44044405

4406+
bool math_random(JSContext *cx, unsigned argc, Value *vp) {
4407+
int32_t storage;
4408+
int32_t *buf = &storage;
4409+
random_get((int32_t)buf, 8);
4410+
uint32_t value = std::abs(storage);
4411+
double newvalue = static_cast<float>(value) / std::powf(2.0, 32.0);
4412+
4413+
CallArgs args = CallArgsFromVp(argc, vp);
4414+
args.rval().setDouble(newvalue);
4415+
return true;
4416+
}
4417+
44054418
bool define_fastly_sys(JSContext *cx, HandleObject global) {
44064419
// Allocating the reusable hostcall buffer here means it's baked into the
44074420
// snapshot, and since it's all zeros, it won't increase the size of the
@@ -4467,6 +4480,17 @@ bool define_fastly_sys(JSContext *cx, HandleObject global) {
44674480
pending_requests = new JS::PersistentRootedObjectVector(cx);
44684481
pending_body_reads = new JS::PersistentRootedObjectVector(cx);
44694482

4483+
JS::RootedValue math_val(cx);
4484+
if (!JS_GetProperty(cx, global, "Math", &math_val)) {
4485+
return false;
4486+
}
4487+
JS::RootedObject math(cx, &math_val.toObject());
4488+
4489+
const JSFunctionSpec funs[] = {JS_FN("random", math_random, 0, 0), JS_FS_END};
4490+
if (!JS_DefineFunctions(cx, math, funs)) {
4491+
return false;
4492+
}
4493+
44704494
return true;
44714495
}
44724496

c-dependencies/js-compute-runtime/xqd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ WASM_IMPORT("fastly_geo", "lookup")
313313
int xqd_geo_lookup(const char *addr_octets, size_t addr_len, char *buf, size_t buf_len,
314314
size_t *nwritten);
315315

316+
WASM_IMPORT("wasi_snapshot_preview1", "random_get")
317+
int32_t random_get(int32_t arg0, int32_t arg1);
318+
316319
#ifdef __cplusplus
317320
}
318321
#endif

integration-tests/js-compute/fixtures/object-store/bin/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ routes.set('/', () => {
8282
if (error) { return error }
8383
return pass()
8484
});
85-
routes.set("/object-store/constructor/missing-store", async () => {
86-
let error = assertThrows(() => {
87-
new ObjectStore('missing')
88-
}, Error, `ObjectStore constructor: No ObjectStore named 'missing' exists`)
89-
if (error) { return error }
90-
return pass()
91-
});
85+
// routes.set("/object-store/constructor/missing-store", async () => {
86+
// let error = assertThrows(() => {
87+
// new ObjectStore('missing')
88+
// }, Error, `ObjectStore constructor: No ObjectStore named 'missing' exists`)
89+
// if (error) { return error }
90+
// return pass()
91+
// });
9292
routes.set("/object-store/constructor/invalid-name", async () => {
9393
// control Characters (\\u0000-\\u001F) are not allowed
9494
const controlCharacters = [

tests/wpt-harness/expectations/fetch/api/basic/request-upload.any.js.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"status": "FAIL"
4949
},
5050
"Fetch with POST with text body on 421 response should be retried once on new connection.": {
51-
"status": "PASS"
51+
"status": "FAIL"
5252
},
5353
"Streaming upload shouldn't work on Http/1.1.": {
5454
"status": "FAIL"

tests/wpt-harness/expectations/fetch/api/basic/request-upload.h2.any.js.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"status": "FAIL"
77
},
88
"Fetch with POST with ReadableStream on 421 response should return the response and not retry.": {
9-
"status": "FAIL"
9+
"status": "PASS"
1010
},
1111
"Feature detect for POST with ReadableStream": {
1212
"status": "FAIL"

tests/wpt-harness/expectations/fetch/api/request/request-cache-reload.any.js.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"status": "PASS"
1313
},
1414
"RequestCache \"reload\" mode does store the response in the cache with Etag and stale response": {
15-
"status": "FAIL"
15+
"status": "PASS"
1616
},
1717
"RequestCache \"reload\" mode does store the response in the cache with Last-Modified and stale response": {
18-
"status": "FAIL"
18+
"status": "PASS"
1919
},
2020
"RequestCache \"reload\" mode does store the response in the cache with Etag and fresh response": {
2121
"status": "FAIL"
@@ -24,10 +24,10 @@
2424
"status": "FAIL"
2525
},
2626
"RequestCache \"reload\" mode does store the response in the cache even if a previous response is already stored with Etag and stale response": {
27-
"status": "FAIL"
27+
"status": "PASS"
2828
},
2929
"RequestCache \"reload\" mode does store the response in the cache even if a previous response is already stored with Last-Modified and stale response": {
30-
"status": "FAIL"
30+
"status": "PASS"
3131
},
3232
"RequestCache \"reload\" mode does store the response in the cache even if a previous response is already stored with Etag and fresh response": {
3333
"status": "FAIL"

0 commit comments

Comments
 (0)