askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
16 hours ago
History
vault.test.ts · 5.3 KB · 117 lines
| 1 | import { mkdtempSync, readFileSync, writeFileSync, statSync } from "node:fs"; |
| 2 | import { tmpdir } from "node:os"; |
| 3 | import { join } from "node:path"; |
| 4 | import { describe, expect, it } from "vitest"; |
| 5 | import { Vault, VaultError, generatePassword, normalizeOrigin } from "../src/index.js"; |
| 6 | |
| 7 | const tmp = () => join(mkdtempSync(join(tmpdir(), "vault-")), "v.json"); |
| 8 | const M = "correct horse battery"; |
| 9 | |
| 10 | describe("Vault", () => { |
| 11 | it("round-trips entries through lock/unlock", async () => { |
| 12 | const p = tmp(); |
| 13 | const v = await Vault.create(p, M); |
| 14 | await v.add({ origin: "https://askrobots.com/login", username: "dan", password: "s3cret!" }); |
| 15 | v.lock(); |
| 16 | const v2 = await Vault.unlock(p, M); |
| 17 | const [e] = v2.find("https://askrobots.com/anything"); |
| 18 | expect(e?.username).toBe("dan"); |
| 19 | expect(v2.get(e!.id).password).toBe("s3cret!"); |
| 20 | }); |
| 21 | |
| 22 | it("never writes plaintext to disk, and chmods 600", async () => { |
| 23 | const p = tmp(); |
| 24 | const v = await Vault.create(p, M); |
| 25 | await v.add({ origin: "https://askrobots.com", username: "dan", password: "hunter2-unique" }); |
| 26 | const raw = readFileSync(p, "utf8"); |
| 27 | expect(raw).not.toContain("hunter2-unique"); |
| 28 | expect(raw).not.toContain("askrobots"); |
| 29 | expect(statSync(p).mode & 0o777).toBe(0o600); |
| 30 | }); |
| 31 | |
| 32 | it("rejects a wrong master password and a tampered file", async () => { |
| 33 | const p = tmp(); |
| 34 | await Vault.create(p, M); |
| 35 | await expect(Vault.unlock(p, "wrong password!")).rejects.toMatchObject({ code: "wrong-password" }); |
| 36 | const env = JSON.parse(readFileSync(p, "utf8")); |
| 37 | env.data = Buffer.from("tampered").toString("base64"); |
| 38 | writeFileSync(p, JSON.stringify(env)); |
| 39 | await expect(Vault.unlock(p, M)).rejects.toMatchObject({ code: "wrong-password" }); |
| 40 | }); |
| 41 | |
| 42 | it("matches by EXACT origin only", async () => { |
| 43 | const v = await Vault.create(tmp(), M); |
| 44 | await v.add({ origin: "https://askrobots.com", username: "a", password: "p" }); |
| 45 | expect(v.find("https://askrobots.com/x")).toHaveLength(1); |
| 46 | expect(v.find("https://askrobots.com:443/")).toHaveLength(1); // default port normalizes |
| 47 | expect(v.find("http://askrobots.com")).toHaveLength(0); // scheme differs |
| 48 | expect(v.find("https://www.askrobots.com")).toHaveLength(0); // subdomain differs |
| 49 | expect(v.find("https://askrobots.com.evil.test")).toHaveLength(0); // lookalike |
| 50 | expect(v.find("https://askrobots.com:8443")).toHaveLength(0); // port differs |
| 51 | expect(v.find("file:///etc/passwd")).toEqual([]); |
| 52 | }); |
| 53 | |
| 54 | it("getFor refuses a credential from another origin", async () => { |
| 55 | const v = await Vault.create(tmp(), M); |
| 56 | const s = await v.add({ origin: "https://askrobots.com", username: "a", password: "p" }); |
| 57 | expect(v.getFor("https://askrobots.com/login", s.id).password).toBe("p"); |
| 58 | expect(() => v.getFor("https://evil.test", s.id)).toThrow(VaultError); |
| 59 | }); |
| 60 | |
| 61 | it("add updates in place for the same origin+username", async () => { |
| 62 | const v = await Vault.create(tmp(), M); |
| 63 | await v.add({ origin: "https://a.test", username: "u", password: "one" }); |
| 64 | await v.add({ origin: "https://a.test", username: "u", password: "two" }); |
| 65 | expect(v.list()).toHaveLength(1); |
| 66 | expect(v.get(v.list()[0]!.id).password).toBe("two"); |
| 67 | }); |
| 68 | |
| 69 | it("list/find never expose passwords", async () => { |
| 70 | const v = await Vault.create(tmp(), M); |
| 71 | await v.add({ origin: "https://a.test", username: "u", password: "topsecret" }); |
| 72 | expect(JSON.stringify(v.list())).not.toContain("topsecret"); |
| 73 | expect(JSON.stringify(v.find("https://a.test"))).not.toContain("topsecret"); |
| 74 | }); |
| 75 | |
| 76 | it("remove and changeMaster", async () => { |
| 77 | const p = tmp(); |
| 78 | const v = await Vault.create(p, M); |
| 79 | const s = await v.add({ origin: "https://a.test", username: "u", password: "p" }); |
| 80 | await v.changeMaster("a brand new master"); |
| 81 | await expect(Vault.unlock(p, M)).rejects.toMatchObject({ code: "wrong-password" }); |
| 82 | const v2 = await Vault.unlock(p, "a brand new master"); |
| 83 | await v2.remove(s.id); |
| 84 | expect(v2.list()).toHaveLength(0); |
| 85 | await expect(v2.remove(s.id)).rejects.toMatchObject({ code: "not-found" }); |
| 86 | }); |
| 87 | |
| 88 | it("a locked vault refuses everything", async () => { |
| 89 | const v = await Vault.create(tmp(), M); |
| 90 | v.lock(); |
| 91 | expect(() => v.list()).toThrow(/locked/); |
| 92 | await expect(v.add({ origin: "https://a.test", username: "u", password: "p" })).rejects.toMatchObject({ code: "locked" }); |
| 93 | }); |
| 94 | |
| 95 | it("won't overwrite an existing vault, and enforces a minimum master length", async () => { |
| 96 | const p = tmp(); |
| 97 | await Vault.create(p, M); |
| 98 | await expect(Vault.create(p, M)).rejects.toMatchObject({ code: "exists" }); |
| 99 | await expect(Vault.create(tmp(), "short")).rejects.toMatchObject({ code: "weak-password" }); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe("helpers", () => { |
| 104 | it("normalizeOrigin", () => { |
| 105 | expect(normalizeOrigin("Askrobots.com")).toBe("https://askrobots.com"); |
| 106 | expect(normalizeOrigin("http://127.0.0.1:8899/x?y")).toBe("http://127.0.0.1:8899"); |
| 107 | expect(() => normalizeOrigin("ftp://x.test")).toThrow(VaultError); |
| 108 | }); |
| 109 | |
| 110 | it("generatePassword has every class, right length, and varies", () => { |
| 111 | const p = generatePassword(24); |
| 112 | expect(p).toHaveLength(24); |
| 113 | expect(p).toMatch(/[a-z]/); expect(p).toMatch(/[A-Z]/); expect(p).toMatch(/[0-9]/); expect(p).toMatch(/[^a-zA-Z0-9]/); |
| 114 | expect(generatePassword(24)).not.toBe(p); |
| 115 | expect(generatePassword(12, { symbols: false })).toMatch(/^[a-zA-Z0-9]+$/); |
| 116 | }); |
| 117 | }); |