Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(vault): bitwarden logins with no web address list instead of vanishing (#7305)

Santhi Prakashb0862bccf478
diff --git a/server/host/bitwarden-vault.test.ts b/server/host/bitwarden-vault.test.ts
index ed5d7bb7c5..306dff3d71 100644
--- a/server/host/bitwarden-vault.test.ts
+++ b/server/host/bitwarden-vault.test.ts
@@ -34,7 +34,7 @@ function setup(options: { credentials?: typeof CREDENTIALS | null; now?: () => n
describe("Bitwarden through its command line tool", () => {
it("signs in with the API key, unlocks with the master password, and lists names and sites, never a value", async () => {
const { vault, fake } = setup();
- expect(await vault.refresh(CREDENTIALS)).toEqual({ connected: true, server: "vault.bitwarden.com", account: "[email protected]", items: 3 });
+ expect(await vault.refresh(CREDENTIALS)).toEqual({ connected: true, server: "vault.bitwarden.com", account: "[email protected]", items: 5 });
expect(fake.calls).toEqual(["status", "config server https://vault.bitwarden.com", "login --apikey", "unlock --passwordenv BW_PASSWORD --raw", "status [session]",
"sync [session]", "list items [session]"]);
expect(fake.secretsInArgs, "a secret in an argument").toEqual([]);
@@ -42,7 +42,31 @@ describe("Bitwarden through its command line tool", () => {
for (const value of [GITHUB_PASSWORD, STRIPE_KEY, NOTE, OAUTH_SECRET, "JBSWY3DPEHPK3PXP", CREDENTIALS.password]) expect(text).not.toContain(value);
expect(linkedSites(vault, "pepper")).toEqual([{ site: "github.com", hosts: ["github.com"], exactHost: false, username: "deploy-bot" }]);
// A secret by name: a Secure Note's text, and a Login's custom field; never a custom field named like a built-in one.
- expect(linkedSecrets(vault).map((secret) => [secret.name, secret.fields, secret.provider])).toEqual([["Acme OAuth", ["client_secret"], "bitwarden"], ["Stripe", [], "bitwarden"]]);
+ // The two "Deploy" logins have no URI, so no site to fill on; they list as secrets, where a name reads the password.
+ expect(linkedSecrets(vault).map((secret) => [secret.name, secret.fields, secret.provider])).toEqual([["Acme OAuth", ["client_secret"], "bitwarden"], ["Deploy", [], "bitwarden"], ["Deploy", [], "bitwarden"], ["Stripe", [], "bitwarden"]]);
+ });
+
+ it("lists a vault whose logins have no web address: the count and the names still show them, never a bare 0", async () => {
+ // A login fills no site without an http(s) URI — androidapp://, an empty URI list — and a name still reads its
+ // password, so it joins the secrets the way a custom field's item does. Only an item with nothing a name can read
+ // stays unlisted.
+ const items: FakeBwItem[] = [
+ { id: "i-app", name: "Bank app", type: 1, login: { uris: [{ uri: "androidapp://com.acme.bank" }], username: "[email protected]", password: "fake-bank-pass" } },
+ { id: "i-nouri", name: "Door code", type: 1, login: { username: "front", password: "fake-door-pass" } },
+ { id: "i-web", name: "Mail", type: 1, login: { uris: [{ uri: "https://mail.example.com/login" }], username: "me", password: "fake-mail-pass" } },
+ { id: "i-nopass", name: "Passkey only", type: 1, login: { uris: [{ uri: "https://id.example.com" }], username: "me" } },
+ { id: "i-wifi", name: "Wifi", type: 2, notes: "fake-wifi-secret" },
+ ];
+ const fake = fakeBw({ ...CREDENTIALS, email: "[email protected]", items });
+ const vault = new BitwardenLogins({ credentials: () => CREDENTIALS, run: fake.run });
+ expect(await vault.refresh(CREDENTIALS)).toEqual({ connected: true, server: "vault.bitwarden.com", account: "[email protected]", items: 4 });
+ expect(linkedSites(vault, "pepper")).toEqual([{ site: "example.com", hosts: ["mail.example.com"], exactHost: false, username: "me" }]);
+ expect(linkedSecrets(vault).map((secret) => [secret.name, secret.provider])).toEqual([["Bank app", "bitwarden"], ["Door code", "bitwarden"], ["Wifi", "bitwarden"]]);
+ const read = async (label: string) => { const use = findLinkedSecret([vault], label, "pepper"); return use ? readLinkedSecret(use) : null; };
+ expect(await read("Bank app")).toBe("fake-bank-pass");
+ expect(await read("Door code")).toBe("fake-door-pass");
+ expect(await read("bw://i-app")).toBe("fake-bank-pass");
+ expect(findLinkedSecret([vault], "Passkey only", "pepper")).toBeNull();
});
it("fills a login on its site with its password and 2FA key, read from Bitwarden at fill time", async () => {
@@ -72,9 +96,10 @@ describe("Bitwarden through its command line tool", () => {
expect(await read("bw://Acme OAuth/client_secret")).toBe(OAUTH_SECRET);
expect(await read("bw://Stripe/secret key")).toBe(STRIPE_KEY);
expect(await read("bw://Stripe/password"), "the built-in field wins over a custom one of the same name").toBe("fake-stripe-dashboard-pass");
- for (const label of ["bw://Deploy/password", "bw://Nothing/password", "bw://GitHub/nothing", "bw://Acme OAuth/totp", "Deploy", "GitHub"]) {
+ for (const label of ["bw://Deploy/password", "bw://Nothing/password", "bw://GitHub/nothing", "bw://Acme OAuth/totp", "GitHub"]) {
expect(findLinkedSecret([vault], label, "pepper"), label).toBeNull();
}
+ expect(await read("Deploy"), "two of a name: the first's password, like a named custom field's").toBe("fake-deploy-1");
expect(await read("bw://i-dup-2/password"), "two of a name: by id").toBe("fake-deploy-2");
});
diff --git a/server/host/bitwarden-vault.ts b/server/host/bitwarden-vault.ts
index c43fcc24b3..8970de4675 100644
--- a/server/host/bitwarden-vault.ts
+++ b/server/host/bitwarden-vault.ts
@@ -137,7 +137,11 @@ function entryOf(item: BwItem): ItemEntry {
const listedAsSecret = (entry: ItemEntry) => (entry.type === SECURE_NOTE && entry.has.notes) || entry.main !== undefined;
function valueOf(item: BwItem, entry: ItemEntry, field: string | undefined, now: number): string | undefined {
- if (!field) return item.type === SECURE_NOTE && item.notes?.trim() ? item.notes : customValue(item, entry.main);
+ if (!field) {
+ const own = item.type === SECURE_NOTE && item.notes?.trim() ? item.notes : customValue(item, entry.main);
+ // A login has no custom field of its own to read: its name reads the password, like `bw://<name>` does.
+ return own ?? (item.type === LOGIN ? item.login?.password ?? undefined : undefined);
+ }
const builtIn = BUILT_IN.find((one) => sameText(one, field));
if (builtIn === "password") return item.login?.password ?? undefined;
if (builtIn === "username") return item.login?.username ?? undefined;
@@ -168,6 +172,8 @@ export class BitwardenLogins implements VaultProvider {
private account: string | undefined;
private logins: LoginEntry[] = [];
private entries = new Map<string, ItemEntry>();
+ /** Logins that filled no site — an empty or non-http(s) URI list — so they list as secrets, not nowhere. */
+ private unplaced = new Set<string>();
private refreshedAt: number | null = null;
private syncedAt: number | null = null;
private error: BitwardenProblem | undefined;
@@ -186,7 +192,7 @@ export class BitwardenLogins implements VaultProvider {
if (!credentials) return { connected: false };
return {
connected: true, server: hostOf(credentials.serverUrl), ...(this.account ? { account: this.account } : {}),
- items: this.logins.length + [...this.entries.values()].filter(listedAsSecret).length, ...(this.error ? { error: this.error.message } : {}),
+ items: this.logins.length + this.listedSecrets().length, ...(this.error ? { error: this.error.message } : {}),
};
}
@@ -220,6 +226,7 @@ export class BitwardenLogins implements VaultProvider {
this.account = undefined;
this.logins = [];
this.entries = new Map();
+ this.unplaced = new Set();
this.refreshedAt = null;
this.syncedAt = null;
this.error = undefined;
@@ -237,18 +244,23 @@ export class BitwardenLogins implements VaultProvider {
const items = (Array.isArray(listed) ? listed as BwItem[] : []).filter((item) => typeof item?.id === "string" && (item.type === LOGIN || item.type === SECURE_NOTE)).slice(0, MAX_ITEMS);
const logins: LoginEntry[] = [];
const entries = new Map<string, ItemEntry>();
+ const unplaced = new Set<string>();
for (const item of items) {
const entry = entryOf(item);
entries.set(item.id, entry);
if (item.type !== LOGIN || !item.login?.password) continue;
const hosts = new Set((item.login.uris ?? []).flatMap((uri) => normalizeLoginHost(uri.uri) ?? []));
+ let placed = false;
for (const [site, siteHosts] of bySite(hosts)) {
logins.push({ id: `${ID_PREFIX}${item.id}:${site}`, itemId: item.id, site, hosts: siteHosts, exactHost: false,
username: item.login.username ?? "", hasTotp: Boolean(parseTotpKey(item.login.totp)) });
+ placed = true;
}
+ if (!placed) unplaced.add(item.id);
}
this.logins = logins.sort((a, b) => a.site.localeCompare(b.site) || a.username.localeCompare(b.username));
this.entries = entries;
+ this.unplaced = unplaced;
this.refreshedAt = this.now();
this.error = undefined;
return this.status();
@@ -343,10 +355,16 @@ export class BitwardenLogins implements VaultProvider {
return { kind: "login", id: entry.id, site: entry.site, hosts: [...entry.hosts], exactHost: false, username: entry.username, hasTotp: entry.hasTotp,
source: "bitwarden", bots: "all", createdAt: 0, ...(used === undefined ? {} : { lastUsedAt: used }) };
}),
- ...[...this.entries.values()].filter(listedAsSecret).sort((a, b) => a.name.localeCompare(b.name)).map((entry): LinkedItem => ({ kind: "secret", ...this.secretOf(entry) })),
+ ...this.listedSecrets().sort((a, b) => a.name.localeCompare(b.name)).map((entry): LinkedItem => ({ kind: "secret", ...this.secretOf(entry) })),
];
}
+ /** The entries the list shows as secrets: a name reads a Secure Note's text or a custom field — or, for a login
+ * that filled no site, its password. */
+ private listedSecrets(): ItemEntry[] {
+ return [...this.entries.values()].filter((entry) => listedAsSecret(entry) || this.unplaced.has(entry.itemId));
+ }
+
/** The item a `bw://` reference names, by id or by name: null for one that names no item, more than one (two items of
* the same name), or a field the item does not have. */
resolve(reference: string): { secret: LinkedSecret; field: string } | null {