Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apps/web/lib/wallets/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ export function reduceAnnouncements(
return next;
}

const safeIcon = (icon: unknown): string | null =>
typeof icon === "string" && /^(data:image\/|https:\/\/)/.test(icon) ? icon : null;
/**
* Trimmed first: Phantom announces its icon with a leading newline (`"\ndata:image/png;base64,..."`),
* which an anchored check rejects, so its logo fell back to a letter. The allowlist stays strict;
* only the whitespace around the value is ignored.
*/
export const safeIcon = (icon: unknown): string | null => {
if (typeof icon !== "string") return null;
const trimmed = icon.trim();
return /^(data:image\/|https:\/\/)/.test(trimmed) ? trimmed : null;
};

export function evmEntries(
announced: ReadonlyMap<string, Eip6963Detail>,
Expand Down
21 changes: 20 additions & 1 deletion apps/web/lib/wallets/wallets.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { Wallet, WalletAccount } from "@wallet-standard/base";
import { describe, expect, it } from "vitest";
import { applyEvmEvent, connectEvm, evmEntries, parseChainId, reduceAnnouncements } from "./evm";
import {
applyEvmEvent,
connectEvm,
evmEntries,
parseChainId,
reduceAnnouncements,
safeIcon,
} from "./evm";
import { isSolanaWallet, pickSolanaAccount, solanaEntries } from "./solana";
import { keepDetected, stellarEntries } from "./stellar";
import type { Connection, Eip1193Provider, WalletEntry } from "./types";
Expand All @@ -26,6 +33,18 @@ describe("EIP-6963", () => {
m = reduceAnnouncements(m, { info: { rdns: "x", name: "X" } });
expect(m.size).toBe(0);
});
it("keeps an icon that has whitespace around it, as Phantom announces it", () => {
const m = reduceAnnouncements(
new Map(),
detail("app.phantom", "Phantom", "\ndata:image/png;base64,iVBORw0KGgo="),
);
expect(evmEntries(m, null)[0]?.icon).toBe("data:image/png;base64,iVBORw0KGgo=");
expect(safeIcon(" https://x/y.png \n")).toBe("https://x/y.png");
});
it("still drops a script URL that is padded with whitespace", () => {
expect(safeIcon("\n javascript:alert(1)")).toBeNull();
expect(safeIcon(42)).toBeNull();
});
it("drops icons that are not data: images or https", () => {
const m = reduceAnnouncements(new Map(), detail("evil", "Evil", "javascript:alert(1)"));
expect(evmEntries(m, null)[0]?.icon).toBeNull();
Expand Down
Loading