Initial import of BugPass
This commit is contained in:
commit
df3554542c
20 files changed
+1588
No files matched your search
@@ -0,0 +1,79 @@
|
||||
// All password generation happens here. The master password lives only in `master` (RAM):
|
||||
// never stored, hashed, or sent anywhere except into generatePassword/buildFingerprint.
|
||||
import { generatePassword, buildFingerprint } from "./lesspass.js";
|
||||
import { getDomain } from "./domain.js";
|
||||
|
||||
const DEFAULTS = { lowercase: true, uppercase: true, digits: true, symbols: true, length: 16, counter: 1 };
|
||||
const IDLE_MS = 30 * 60 * 1000;
|
||||
const LOCKED = { locked: true };
|
||||
|
||||
let master = null, idleTimer, clipHash = null;
|
||||
const lock = () => { master = null; clearTimeout(idleTimer); };
|
||||
const touch = () => { clearTimeout(idleTimer); idleTimer = setTimeout(lock, IDLE_MS); };
|
||||
const gen = (p) => generatePassword({ ...DEFAULTS, ...p }, master);
|
||||
const sha = async (s) => [...new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(s)))].join();
|
||||
|
||||
const native = async (msg) => {
|
||||
const r = await browser.runtime.sendNativeMessage("bugpass", msg);
|
||||
if (r.error) throw new Error(r.error);
|
||||
return r;
|
||||
};
|
||||
|
||||
// Only our own pages (popup, manager, autofill iframe) may talk to us; content scripts have page URLs.
|
||||
const isExt = (s) => s.id === browser.runtime.id && (s.url ?? "").startsWith(browser.runtime.getURL(""));
|
||||
|
||||
const handlers = {
|
||||
status: () => ({ unlocked: master !== null }),
|
||||
async unlock({ master: m }) {
|
||||
if (typeof m !== "string" || !m) throw new Error("Master password required");
|
||||
master = m;
|
||||
return { unlocked: true, fingerprint: await buildFingerprint(m) };
|
||||
},
|
||||
lock: () => { lock(); return { unlocked: false }; },
|
||||
list: async () => ({ profiles: (await native({ cmd: "list" })).profiles }),
|
||||
save: ({ profile }) => native({ cmd: "save", profile }),
|
||||
delete: ({ site, login }) => native({ cmd: "delete", site, login }),
|
||||
openManager: () => browser.tabs.create({ url: browser.runtime.getURL("manager.html") }).then(() => ({ ok: true })),
|
||||
async fill({ tabId, profile, toast }, sender) {
|
||||
if (!master) return LOCKED;
|
||||
tabId ??= sender.tab?.id; // autofill iframe: its own tab
|
||||
// never hand a password to a page that isn't on the profile's site (tab may have navigated)
|
||||
const tab = await browser.tabs.get(tabId);
|
||||
let host = "";
|
||||
try { host = new URL(tab.url).hostname; } catch {}
|
||||
if (!profile?.site || !host || getDomain(host) !== profile.site) throw new Error(`Tab is not on ${profile?.site}`);
|
||||
const password = await gen(profile);
|
||||
await browser.tabs.sendMessage(tabId, { type: "fill", username: profile.login, password, toast: !!toast }, { frameId: 0 });
|
||||
return { ok: true };
|
||||
},
|
||||
async copy({ profile }) {
|
||||
if (!master) return LOCKED;
|
||||
const pw = await gen(profile);
|
||||
await navigator.clipboard.writeText(pw);
|
||||
const h = clipHash = await sha(pw);
|
||||
// clear after 30s, only if the clipboard still holds this password
|
||||
setTimeout(async () => {
|
||||
if (clipHash === h && await sha(await navigator.clipboard.readText()) === h) await navigator.clipboard.writeText("");
|
||||
}, 30000);
|
||||
return { ok: true };
|
||||
},
|
||||
async reveal({ profile }) {
|
||||
if (!master) return LOCKED;
|
||||
return { password: await gen(profile) };
|
||||
},
|
||||
// autofill iframe -> its tab's top-frame content script (UI-only messages, no secrets)
|
||||
relay({ msg }, sender) {
|
||||
if (sender.tab && ["close", "resize", "focus"].includes(msg?.type))
|
||||
return browser.tabs.sendMessage(sender.tab.id, msg, { frameId: 0 });
|
||||
},
|
||||
};
|
||||
|
||||
export async function onMessage(m, sender) {
|
||||
if (!isExt(sender)) throw new Error("BugPass: forbidden");
|
||||
const r = await handlers[m.type](m, sender);
|
||||
if (master) touch(); // auto-lock after 30 min without use
|
||||
return r;
|
||||
}
|
||||
|
||||
// Unknown types (e.g. "key" broadcasts meant for the autofill iframe) are left for other listeners.
|
||||
browser.runtime.onMessage.addListener((m, s) => (m && Object.hasOwn(handlers, m.type) ? onMessage(m, s) : undefined));
|
||||
Reference in new issue
Block a user