Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace tldjs with @browserpass/url, check port #179

Merged
merged 13 commits into from
Oct 12, 2019
10 changes: 5 additions & 5 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async function updateMatchingPasswordsCount(tabId, forceRefresh = false) {

try {
const tab = await chrome.tabs.get(tabId);
badgeCache.settings.host = new URL(tab.url).hostname;
badgeCache.settings.host = new URL(tab.url).host;
} catch (e) {
throw new Error(`Unable to determine domain of the tab with id ${tabId}`);
}
Expand All @@ -146,7 +146,7 @@ async function updateMatchingPasswordsCount(tabId, forceRefresh = false) {
const files = helpers.ignoreFiles(badgeCache.files, badgeCache.settings);
const logins = helpers.prepareLogins(files, badgeCache.settings);
const matchedPasswordsCount = logins.reduce(
(acc, login) => acc + (login.recent.count || login.inCurrentDomain ? 1 : 0),
(acc, login) => acc + (login.recent.count || login.inCurrentHost ? 1 : 0),
0
);

Expand Down Expand Up @@ -234,7 +234,7 @@ async function saveRecent(settings, login, remove = false) {
localStorage.setItem("recent", JSON.stringify(settings.recent));

// a new entry was added to the popup matching list, need to refresh the count
if (!login.inCurrentDomain && login.recent.count === 1) {
if (!login.inCurrentHost && login.recent.count === 1) {
updateMatchingPasswordsCount(settings.tab.id, true);
}

Expand Down Expand Up @@ -581,7 +581,7 @@ async function getFullSettings() {
// Fill current tab info
try {
settings.tab = (await chrome.tabs.query({ active: true, currentWindow: true }))[0];
settings.host = new URL(settings.tab.url).hostname;
settings.host = new URL(settings.tab.url).host;
} catch (e) {}

return settings;
Expand Down Expand Up @@ -772,7 +772,7 @@ async function handleMessage(settings, message, sendResponse) {
case "launch":
case "launchInNewTab":
try {
var url = message.login.fields.url || message.login.domain;
var url = message.login.fields.url || message.login.host;
if (!url) {
throw new Error("No URL is defined for this entry");
}
Expand Down
58 changes: 42 additions & 16 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
"use strict";

const FuzzySort = require("fuzzysort");
const TldJS = require("tldjs");
const sha1 = require("sha1");
const ignore = require("ignore");
const BrowserpassURL = require("@browserpass/url");

module.exports = {
pathToDomain,
prepareLogins,
filterSortLogins,
ignoreFiles
Expand All @@ -18,30 +17,30 @@ module.exports = {
/**
* Get the deepest available domain component of a path
*
* @since 3.0.0
* @since 3.2.3
*
* @param string path Path to parse
* @param string currentHost Current hostname for the active tab
* @return string|null Extracted domain
* @return string|null Extracted domain info
*/
function pathToDomain(path, currentHost) {
function pathToInfo(path, currentHost) {
var parts = path.split(/\//).reverse();
for (var key in parts) {
if (parts[key].indexOf("@") >= 0) {
continue;
}
var t = TldJS.parse(parts[key]);
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 a valid domain with unknown TLD but the current host is it's subdomain (login.pi.hole)
// - it is or isnt a valid domain but the current host matches it EXACTLY (localhost, pi.hole)
// - it is or isn't a valid domain with unknown TLD but the current host is its subdomain (login.pi.hole)
// - it is or isn't a valid domain but the current host matches it EXACTLY (localhost, pi.hole)
if (
t.isValid &&
((t.domain !== null && (t.tldExists || currentHost.endsWith(`.${t.hostname}`))) ||
currentHost === t.hostname)
info.validDomain ||
currentHost.hostname.endsWith(`.${info.hostname}`) ||
currentHost.hostname === info.hostname
) {
return t.hostname;
return info;
}
}

Expand All @@ -60,6 +59,7 @@ function pathToDomain(path, currentHost) {
function prepareLogins(files, settings) {
const logins = [];
let index = 0;
let host = BrowserpassURL.parseHost(settings.host);

for (let storeId in files) {
for (let key in files[storeId]) {
Expand All @@ -70,9 +70,35 @@ function prepareLogins(files, settings) {
login: files[storeId][key].replace(/\.gpg$/i, ""),
allowFill: true
};
login.domain = pathToDomain(storeId + "/" + login.login, settings.host);
login.inCurrentDomain =
settings.host == login.domain || settings.host.endsWith("." + login.domain);

// extract url info from path
let pathInfo = pathToInfo(storeId + "/" + login.login, host);
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 = host.hostname === pathInfo.hostname;

// check whether the current origin is subordinate to extracted path info, meaning:
// - that the path info is not a single level (e.g. com, net, local)
// - and that the host ends with that path info
if (pathInfo.hostname.includes(".") && host.hostname.endsWith(pathInfo.hostname)) {
login.inCurrentHost = true;
}

// filter out entries with a non-matching port
if (pathInfo.port && pathInfo.port !== host.port) {
login.inCurrentHost = false;
}
} else {
login.host = null;
login.inCurrentHost = false;
}

// update recent counter
login.recent =
settings.recent[sha1(settings.host + sha1(login.store.id + sha1(login.login)))];
if (!login.recent) {
Expand Down Expand Up @@ -124,7 +150,7 @@ function filterSortLogins(logins, searchQuery, currentDomainOnly) {
return false;
});
var remainingInCurrentDomain = candidates.filter(
login => login.inCurrentDomain && !login.recent.count
login => login.inCurrentHost && !login.recent.count
);
candidates = recent.concat(remainingInCurrentDomain);
}
Expand Down
4 changes: 2 additions & 2 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
}
],
"dependencies": {
"@browserpass/url": "^1.1.2",
"chrome-extension-async": "^3.3.2",
"fuzzysort": "^1.1.4",
"idb": "^4.0.3",
"ignore": "^5.1.4",
"mithril": "^1.1.0",
"moment": "^2.24.0",
"sha1": "^1.1.1",
"tldjs": "^2.3.0"
"sha1": "^1.1.1"
},
"devDependencies": {
"browserify": "^16.2.3",
Expand Down
16 changes: 8 additions & 8 deletions src/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
# yarn lockfile v1


"@browserpass/url@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@browserpass/url/-/url-1.1.2.tgz#f12b67861ab728011a40e52902e86c67bfeacae4"
integrity sha512-H0WA6nfd3CnlLXa0cExMb7iOoJYqfjLkitXkK4EMMiy/DD73PO8TONGQFmAIqSaoJ+X5SowOOZwGbm0nol/NKA==
dependencies:
punycode "^2.1.1"

JSONStream@^1.0.3:
version "1.3.5"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
Expand Down Expand Up @@ -1089,7 +1096,7 @@ punycode@^1.3.2, punycode@^1.4.1:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"
integrity sha1-wNWmOycYgArY4esPpSachN1BhF4=

punycode@^2.1.0:
punycode@^2.1.0, punycode@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
Expand Down Expand Up @@ -1347,13 +1354,6 @@ timers-browserify@^1.0.1:
dependencies:
process "~0.11.0"

tldjs@^2.3.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/tldjs/-/tldjs-2.3.1.tgz#cf09c3eb5d7403a9e214b7d65f3cf9651c0ab039"
integrity sha512-W/YVH/QczLUxVjnQhFC61Iq232NWu3TqDdO0S/MtXVz4xybejBov4ud+CIwN9aYqjOecEqIy0PscGkwpG9ZyTw==
dependencies:
punycode "^1.4.1"

to-arraybuffer@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/to-arraybuffer/-/to-arraybuffer-1.0.1.tgz#7d229b1fcc637e466ca081180836a7aabff83f43"
Expand Down