-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathbase.js
More file actions
516 lines (460 loc) · 15.1 KB
/
base.js
File metadata and controls
516 lines (460 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//------------------------------------- Initialisation --------------------------------------//
"use strict";
const FuzzySort = require("fuzzysort");
const sha1 = require("sha1");
const ignore = require("ignore");
const hash = require("hash.js");
const Authenticator = require("otplib").authenticator.Authenticator;
const BrowserpassURL = require("@browserpass/url");
const fieldsPrefix = {
secret: ["secret", "password", "pass"],
login: ["login", "username", "user"],
openid: ["openid"],
otp: ["otp", "totp"],
url: ["url", "uri", "website", "site", "link", "launch"],
};
const containsSymbolsRegEx = RegExp(/[\p{P}\p{S}]/, "u");
const LATEST_NATIVE_APP_VERSION = 3001000;
const AUTH_URL_QUERY_PARAM = "authUrl";
const LAUNCH_URL_DEPRECATION_MESSAGE = `<h3>Deprecation</h3><p>"Ctrl+G" and "Ctrl+Shift+G" shortcuts are deprecated and will be removed in a future version.</p> <p>It is no longer necessary to open websites which require basic auth using these shortcuts. Navigate websites normally and Browserpass will automatically open a popup for you to choose the credentials.</p>`;
module.exports = {
containsSymbolsRegEx,
fieldsPrefix,
AUTH_URL_QUERY_PARAM,
LATEST_NATIVE_APP_VERSION,
LAUNCH_URL_DEPRECATION_MESSAGE,
deepCopy,
filterSortLogins,
getPopupUrl,
getSetting,
ignoreFiles,
isChrome,
isThunderbird,
makeTOTP,
parseAuthUrl,
prepareLogin,
prepareLogins,
unsecureRequestWarning,
};
//----------------------------------- Function definitions ----------------------------------//
/**
* Deep copy an object
*
* Firefox requires data to be serializable,
* this removes everything offending such as functions
*
* @since 3.0.0 moved to helpers.js 3.8.0
*
* @param object obj an object to copy
* @return object a new deep copy
*/
function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* Returns url string of the html popup page
* @since 3.10.0
*
* @returns string
*/
function getPopupUrl() {
const base = chrome || browser;
return base.runtime.getURL("popup/popup.html");
}
/*
* Get most relevant setting value
*
* @param string key Setting key
* @param object login Login object
* @param object settings Settings object
* @return object Setting value
*/
function getSetting(key, login, settings) {
if (typeof login.settings[key] !== "undefined") {
return login.settings[key];
}
if (typeof settings.stores[login.store.id].settings[key] !== "undefined") {
return settings.stores[login.store.id].settings[key];
}
return settings[key];
}
/**
* returns true if agent string is Chrome / Chromium
*
* @since 3.10.0
*/
function isChrome() {
return chrome.runtime.getURL("/").startsWith("chrom");
}
/**
* Returns true if running in Thunderbird.
* Checks for the browser.credentials API which is only available in Thunderbird.
*
* @return boolean
*/
function isThunderbird() {
return typeof browser !== "undefined" && browser.credentials !== undefined;
}
/**
* Get the deepest available domain component of a path
*
* @since 3.2.3
*
* @param string path Path to parse
* @param object currentHost Current host info for the active tab
* @return object|null Extracted domain info
*/
function pathToInfo(path, currentHost) {
var parts = path.split(/\//).reverse();
for (var key in parts) {
if (parts[key].indexOf("@") >= 0) {
continue;
}
var info = BrowserpassURL.parseHost(parts[key]);
// Part is considered to be a domain component in one of the following cases:
// - it is a valid domain with well-known TLD (github.com, login.github.com)
// - it is or isn't a valid domain with any TLD but the current host matches it EXACTLY (localhost, pi.hole)
// - it is or isn't a valid domain with any TLD but the current host is its subdomain (login.pi.hole)
if (
info.validDomain ||
currentHost.hostname === info.hostname ||
currentHost.hostname.endsWith(`.${info.hostname}`)
) {
return info;
}
}
return null;
}
/**
* Returns decoded url param for "authUrl" if present
* @since 3.10.0
* @param string url string to parse and compare against extension popup url
* @returns string | null
*/
function parseAuthUrl(url) {
const currentUrl = url || null;
// query url not exact match when includes fragments, so must start with extension url
if (currentUrl && `${currentUrl}`.startsWith(getPopupUrl())) {
const encodedUrl = new URL(currentUrl).searchParams.get(AUTH_URL_QUERY_PARAM);
if (encodedUrl) {
return decodeURIComponent(encodedUrl);
}
}
return null;
}
/**
* Prepare list of logins based on provided files
*
* @since 3.1.0
*
* @param string array List of password files
* @param string object Settings object
* @return array List of logins
*/
function prepareLogins(files, settings) {
const logins = [];
let index = 0;
let origin = new BrowserpassURL(
settings.authRequested ? parseAuthUrl(settings.tab.url) : settings.origin
);
for (let storeId in files) {
for (let key in files[storeId]) {
// set login fields
const login = prepareLogin(settings, storeId, files[storeId][key], index++, origin);
logins.push(login);
}
}
return logins;
}
/**
* Prepare a single login based settings, storeId, and path
*
* @since 3.8.0
*
* @param string settings Settings object
* @param string storeId Store ID alphanumeric ID
* @param string file Relative path in store to password
* @param number index An array index for login, if building an array of logins (optional, default: 0)
* @param object origin Instance of BrowserpassURL (optional, default: new BrowserpassURL(settings.origin))
* @return object of login
*/
function prepareLogin(settings, storeId, file, index = 0, origin = undefined) {
const login = {
index: index > -1 ? parseInt(index) : 0,
store: settings.stores[storeId],
// remove the file-type extension
login: file.replace(/\.[^.]+$/u, ""),
loginPath: file,
allowFill: true,
};
origin = BrowserpassURL.prototype.isPrototypeOf(origin)
? origin
: new BrowserpassURL(settings.origin);
// extract url info from path
let pathInfo = pathToInfo(storeId + "/" + login.login, origin);
if (pathInfo) {
// set assumed host
login.host = pathInfo.port ? `${pathInfo.hostname}:${pathInfo.port}` : pathInfo.hostname;
// check whether extracted path info matches the current origin
login.inCurrentHost = origin.hostname === pathInfo.hostname;
// check whether the current origin is subordinate to extracted path info, meaning:
// - that the path info is not a single level domain (e.g. com, net, local)
// - and that the current origin is a subdomain of that path info
if (pathInfo.hostname.includes(".") && origin.hostname.endsWith(`.${pathInfo.hostname}`)) {
login.inCurrentHost = true;
}
// filter out entries with a non-matching port
if (pathInfo.port && pathInfo.port !== origin.port) {
login.inCurrentHost = false;
}
} else {
login.host = null;
login.inCurrentHost = false;
}
// update recent counter
login.recent =
settings.recent[sha1(settings.origin + sha1(login.store.id + sha1(login.login)))];
if (!login.recent) {
login.recent = {
when: 0,
count: 0,
};
}
return login;
}
/**
* Filter and sort logins
*
* @since 3.1.0
*
* @param string array List of logins
* @param string object Settings object
* @return array Filtered and sorted list of logins
*/
function filterSortLogins(logins, searchQuery, currentDomainOnly) {
var fuzzyFirstWord = searchQuery.substr(0, 1) !== " ";
searchQuery = searchQuery.trim();
// get candidate list
var candidates = logins.map((candidate) => {
let lastSlashIndex = candidate.login.lastIndexOf("/") + 1;
return Object.assign(candidate, {
path: candidate.login.substr(0, lastSlashIndex),
display: candidate.login.substr(lastSlashIndex),
});
});
var mostRecent = null;
if (currentDomainOnly) {
var recent = candidates.filter(function (login) {
if (login.recent.count > 0) {
// find most recently used login
if (!mostRecent || login.recent.when > mostRecent.recent.when) {
mostRecent = login;
}
return true;
}
return false;
});
var remainingInCurrentDomain = candidates.filter(
(login) => login.inCurrentHost && !login.recent.count
);
candidates = recent.concat(remainingInCurrentDomain);
}
candidates.sort((a, b) => {
// show most recent first
if (a === mostRecent) {
return -1;
}
if (b === mostRecent) {
return 1;
}
// sort by frequency
var countDiff = b.recent.count - a.recent.count;
if (countDiff) {
return countDiff;
}
// sort by specificity, only if filtering for one domain
if (currentDomainOnly) {
var domainLevelsDiff =
(b.login.match(/\./g) || []).length - (a.login.match(/\./g) || []).length;
if (domainLevelsDiff) {
return domainLevelsDiff;
}
}
// sort alphabetically
return a.login.localeCompare(b.login);
});
if (searchQuery.length) {
let filter = searchQuery.split(/\s+/);
let fuzzyFilter = fuzzyFirstWord ? filter[0] : "";
let substringFilters = filter.slice(fuzzyFirstWord ? 1 : 0).map((w) => w.toLowerCase());
// First reduce the list by running the substring search
substringFilters.forEach(function (word) {
candidates = candidates.filter((c) => c.login.toLowerCase().indexOf(word) >= 0);
});
// Then run the fuzzy filter
let fuzzyResults = {};
if (fuzzyFilter) {
candidates = FuzzySort.go(fuzzyFilter, candidates, {
keys: ["login", "store.name"],
allowTypo: false,
}).map((result) => {
fuzzyResults[result.obj.login] = result;
return result.obj;
});
}
// Finally highlight all matches
candidates = candidates.map((c) => highlightMatches(c, fuzzyResults, substringFilters));
}
// Prefix root entries with slash to let them have some visible path
candidates.forEach((c) => {
c.path = c.path || "/";
});
return candidates;
}
/**
* Generate TOTP token
*
* @since 3.6.0
*
* @param object params OTP generation params
* @return string Generated OTP code
*/
function makeTOTP(params) {
switch (params.algorithm) {
case "sha1":
case "sha256":
case "sha512":
break;
default:
throw new Error(`Unsupported TOTP algorithm: ${params.algorithm}`);
}
var generator = new Authenticator();
generator.options = {
crypto: {
createHmac: (a, k) => hash.hmac(hash[a], k),
},
algorithm: params.algorithm,
digits: params.digits,
step: params.period,
};
return generator.generate(params.secret);
}
//----------------------------------- Private functions ----------------------------------//
/**
* Highlight filter matches
*
* @since 3.0.0
*
* @param object entry password entry
* @param object fuzzyResults positions of fuzzy filter matches
* @param array substringFilters list of substring filters applied
* @return object entry with highlighted matches
*/
function highlightMatches(entry, fuzzyResults, substringFilters) {
// Add all positions of the fuzzy search to the array
let matches = (
fuzzyResults[entry.login] && fuzzyResults[entry.login][0]
? fuzzyResults[entry.login][0].indexes
: []
).slice();
// Add all positions of substring searches to the array
let login = entry.login.toLowerCase();
for (let word of substringFilters) {
let startIndex = login.indexOf(word);
for (let i = 0; i < word.length; i++) {
matches.push(startIndex + i);
}
}
// Prepare the final array of matches before
matches = sortUnique(matches, (a, b) => a - b);
const OPEN = "<em>";
const CLOSE = "</em>";
let highlighted = "";
var matchesIndex = 0;
var opened = false;
for (var i = 0; i < entry.login.length; ++i) {
var char = entry.login[i];
if (i == entry.path.length) {
if (opened) {
highlighted += CLOSE;
}
var path = highlighted;
highlighted = "";
if (opened) {
highlighted += OPEN;
}
}
if (matches[matchesIndex] === i) {
matchesIndex++;
if (!opened) {
opened = true;
highlighted += OPEN;
}
} else {
if (opened) {
opened = false;
highlighted += CLOSE;
}
}
highlighted += char;
}
if (opened) {
opened = false;
highlighted += CLOSE;
}
let display = highlighted;
return Object.assign(entry, {
path: path,
display: display,
});
}
/**
* Filter out ignored files according to .browserpass.json rules
*
* @since 3.2.0
*
* @param object files Arrays of files, grouped by store
* @param object settings Settings object
* @return object Filtered arrays of files, grouped by store
*/
function ignoreFiles(files, settings) {
let filteredFiles = {};
for (let store in files) {
let storeSettings = settings.stores[store].settings;
if (storeSettings.hasOwnProperty("ignore")) {
if (typeof storeSettings.ignore === "string") {
storeSettings.ignore = [storeSettings.ignore];
}
filteredFiles[store] = ignore().add(storeSettings.ignore).filter(files[store]);
} else {
filteredFiles[store] = files[store];
}
}
return filteredFiles;
}
/**
* Sort and remove duplicates
*
* @since 3.0.0
*
* @param array array items to sort
* @param function comparator sort comparator
* @return array sorted items without duplicates
*/
function sortUnique(array, comparator) {
return array
.sort(comparator)
.filter((elem, index, arr) => index == !arr.length || arr[index - 1] != elem);
}
/**
* Returns warning html string with unsecure url specified
* @returns html string
*/
function unsecureRequestWarning(url) {
return (
"<h3 >Warning: Are you sure you want to do this?</h3>" +
"<p>You are about to send login credentials via an insecure connection!</p>" +
"<p>If there is an attacker watching your network traffic, they may be able to see your username and password.</p>" +
`<p>URL: <strong>${url}</strong></p>`
);
}