Skip to content

Commit 1d80e66

Browse files
committed
Use a stronger random number generator for Vimium's session secret
Fixes philc#3832
1 parent 85726b0 commit 1d80e66

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

background_scripts/main.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ global.urlForTab = {};
3232
// This is exported for use by "marks.js".
3333
global.tabLoadedHandlers = {}; // tabId -> function()
3434

35-
// A secret, available only within the current instantiation of Vimium. The secret is big, likely unguessable
36-
// in practice, but less than 2^31.
37-
chrome.storage.local.set({vimiumSecret: Math.floor(Math.random() * 2000000000)});
35+
// A secret, available only within the current instantiation of Vimium, for the duration of the browser
36+
// session. The secret is a generated strong random string.
37+
const randomArray = window.crypto.getRandomValues(new Uint8Array(32)); // 32-byte random token.
38+
const secretToken = randomArray.reduce((a,b) => a.toString(16) + b.toString(16));
39+
chrome.storage.local.set({vimiumSecret: secretToken});
3840

3941
const completionSources = {
4042
bookmarks: new BookmarkCompleter,

tests/unit_tests/test_chrome_stubs.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
//
2-
// This is a stub for chrome.strorage.sync for testing.
3-
// It does what chrome.storage.sync should do (roughly), but does so synchronously.
4-
// It also provides stubs for a number of other chrome APIs.
2+
// This file contains stubs for a number of browser and chrome APIs which are missing in
3+
// Node.js.
4+
// The chrome.storage.sync stub does roughly what chrome.storage.sync should do, but does so synchronously.
55
//
66

7-
let XMLHttpRequest;
7+
const nodeCrypto = require("crypto");
8+
89
global.window = {};
910
global.localStorage = {};
1011

12+
window.crypto = {
13+
// This polyfill was taken from
14+
// https://github.com/KenanY/get-random-values
15+
getRandomValues: (buffer) => {
16+
if (!(buffer instanceof Uint8Array))
17+
throw new TypeError('expected Uint8Array');
18+
if (buffer.length > 65536)
19+
throw new Error("Buffer length cannot be larger than 65536; this API doesn't support that much entropy.");
20+
var bytes = nodeCrypto.randomBytes(buffer.length);
21+
buffer.set(bytes);
22+
return buffer;
23+
}
24+
}
25+
26+
let XMLHttpRequest;
27+
1128
global.navigator =
1229
{appVersion: "5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36"};
1330

0 commit comments

Comments
 (0)