diff --git a/apps/web/src/components/settings/ThemeImportDialog.tsx b/apps/web/src/components/settings/ThemeImportDialog.tsx index d39b27b38c..a914749141 100644 --- a/apps/web/src/components/settings/ThemeImportDialog.tsx +++ b/apps/web/src/components/settings/ThemeImportDialog.tsx @@ -361,9 +361,16 @@ export function ThemeImportDialog({ : null; for (const theme of conflicts) { try { + const existingTheme = + mode === "update" + ? getCustomThemes().find((candidate) => candidate.id === theme.id) + : undefined; + const themeToUpdate = existingTheme?.collection + ? { ...theme, collection: existingTheme.collection } + : theme; resolved.push( mode === "update" - ? updateCustomTheme(theme) + ? updateCustomTheme(themeToUpdate) : installCustomTheme(versionedCopy(theme, preferredName)), ); } catch (cause) { diff --git a/apps/web/src/openVsxThemes.test.ts b/apps/web/src/openVsxThemes.test.ts index 3e707a27a9..b4e5cd54fd 100644 --- a/apps/web/src/openVsxThemes.test.ts +++ b/apps/web/src/openVsxThemes.test.ts @@ -231,6 +231,7 @@ describe("Open VSX themes", () => { publisher: "demo", name: "theme", version: "1.0.0", + license: "MIT", contributes: { themes: [ { label: "Demo Dark", uiTheme: "vs-dark", path: "./themes/demo-dark.json" }, @@ -239,14 +240,19 @@ describe("Open VSX themes", () => { ], }, }; - zip.file("extension/package.json", JSON.stringify(packagedManifest)); - const packageBytes = await zip.generateAsync({ - type: "arraybuffer", - comment: `PK\u0005\u0006${"x".repeat(26)}`, - }); - const checksum = [...sha256(new Uint8Array(packageBytes))] - .map((byte) => byte.toString(16).padStart(2, "0")) - .join(""); + let packageBytes = new ArrayBuffer(0); + let checksum = ""; + const rebuildPackage = async () => { + zip.file("extension/package.json", JSON.stringify(packagedManifest)); + packageBytes = await zip.generateAsync({ + type: "arraybuffer", + comment: `PK\u0005\u0006${"x".repeat(26)}`, + }); + checksum = [...sha256(new Uint8Array(packageBytes))] + .map((byte) => byte.toString(16).padStart(2, "0")) + .join(""); + }; + await rebuildPackage(); // The public manifest is only a preflight hint. Imports must use the manifest // inside the checksummed VSIX rather than this inconsistent contribution. const manifest = { @@ -283,6 +289,7 @@ describe("Open VSX themes", () => { expect(themes).toHaveLength(2); expect(new Set(themes.map((theme) => theme.id)).size).toBe(2); + expect(themes.every((theme) => /^ovx-[a-z0-9-]+-[0-9a-f]{12}$/.test(theme.id))).toBe(true); expect(themes.every((theme) => theme.collection?.id === "open-vsx:demo.theme")).toBe(true); const paired = themes.find( (theme) => @@ -293,6 +300,19 @@ describe("Open VSX themes", () => { expect(paired.colors.canvas).toBe("#fafafa"); expect(getThemeColorsForMode(paired, "dark")!.canvas).toBe("#111111"); expect(getThemeColorsForMode(paired, "dark")!.text).toBe("#eeeeee"); + + packagedManifest.contributes.themes[0]!.label = "Renamed Dark"; + packagedManifest.contributes.themes[1]!.label = "Renamed Light"; + packagedManifest.contributes.themes[2]!.label = "Renamed Solo"; + await rebuildPackage(); + const renamedThemes = await importOpenVsxThemeExtension(extension); + expect(renamedThemes.map((theme) => theme.id)).toEqual(themes.map((theme) => theme.id)); + + packagedManifest.license = "Proprietary"; + await rebuildPackage(); + await expect(importOpenVsxThemeExtension(extension)).rejects.toThrow( + "does not match its advertised license", + ); }); it("stops import work when the request is cancelled", async () => { diff --git a/apps/web/src/openVsxThemes.ts b/apps/web/src/openVsxThemes.ts index f61e6177a0..458ff42fed 100644 --- a/apps/web/src/openVsxThemes.ts +++ b/apps/web/src/openVsxThemes.ts @@ -100,6 +100,24 @@ function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } +function shortHash(value: string): string { + return [...sha256(new TextEncoder().encode(value))] + .slice(0, 6) + .map((byte) => byte.toString(16).padStart(2, "0")) + .join(""); +} + +function openVsxThemeId(extensionId: string, source: string): string { + return `ovx-theme-${shortHash(`${extensionId}:${source}`)}`; +} + +function openVsxCollectionId(extensionId: string): string { + const normalized = `open-vsx:${extensionId.toLowerCase()}`; + return /^[a-z0-9][a-z0-9.:-]{0,127}$/.test(normalized) + ? normalized + : `open-vsx:${shortHash(extensionId)}`; +} + function trustedOpenVsxUrl(value: unknown): string | null { if (typeof value !== "string") return null; try { @@ -632,13 +650,19 @@ export async function importOpenVsxThemeExtension( ) { throw new Error("That extension package does not match the selected Open VSX theme."); } + if ( + typeof packagedManifest.license === "string" && + packagedManifest.license.trim() !== extension.license + ) { + throw new Error("That extension package does not match its advertised license."); + } const contributions = themeContributions(packagedManifest); if (contributions.length === 0) throw new Error("That extension does not contain color themes."); if (contributions.length > MAX_THEMES_PER_EXTENSION) { throw new Error("That extension contains too many color themes to import safely."); } - const parsed: Array<{ theme: ThemeDefinition; sourceName: string }> = []; + const parsed: Array<{ theme: ThemeDefinition; sourceName: string; sourcePath: string }> = []; const failures: string[] = []; const themeCache = new Map>(); const themeBudget = { files: 0 }; @@ -666,7 +690,11 @@ export async function importOpenVsxThemeExtension( ...(type ? { type } : {}), }; if (!isVsCodeThemeFile(decorated)) throw new Error("not a VS Code color theme"); - parsed.push({ theme: parseVsCodeThemeFile(decorated), sourceName: path.split("/").at(-1)! }); + parsed.push({ + theme: parseVsCodeThemeFile(decorated), + sourceName: path.split("/").at(-1)!, + sourcePath: path, + }); } catch (cause) { signal?.throwIfAborted(); failures.push(cause instanceof Error ? cause.message : "theme could not be read"); @@ -675,10 +703,17 @@ export async function importOpenVsxThemeExtension( if (parsed.length === 0) { throw new Error(failures[0] ?? "That extension has no compatible color themes."); } - const paired = pairVsCodeThemes(resolveThemeLabelCollisions(parsed)); + const extensionId = extension.id.toLowerCase(); + const resolved = resolveThemeLabelCollisions(parsed).map((theme, index) => ({ + ...theme, + id: openVsxThemeId(extensionId, parsed[index]!.sourcePath), + })); + const paired = pairVsCodeThemes(resolved, { + pairedId: (light, dark) => openVsxThemeId(extensionId, [light.id, dark.id].sort().join(":")), + }); const themes = resolveThemeLabelCollisions(paired.map((theme) => ({ theme }))); const collection = { - id: `open-vsx:${extension.id.toLowerCase()}`, + id: openVsxCollectionId(extension.id), label: extension.name.slice(0, 48), }; return themes.map((theme) => ({ ...theme, collection })); diff --git a/apps/web/src/vscodeThemeImport.ts b/apps/web/src/vscodeThemeImport.ts index 6432e68869..ad0cbb2231 100644 --- a/apps/web/src/vscodeThemeImport.ts +++ b/apps/web/src/vscodeThemeImport.ts @@ -336,6 +336,9 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition { */ export function pairVsCodeThemes( themes: ReadonlyArray, + options?: { + pairedId?: (light: ThemeDefinition, dark: ThemeDefinition) => string; + }, ): ReadonlyArray { const stripAppearance = (label: string) => label @@ -371,6 +374,7 @@ export function pairVsCodeThemes( order: group.order, theme: parseThemeFile({ version: THEME_FILE_VERSION, + ...(options?.pairedId ? { id: options.pairedId(group.light[0]!, group.dark[0]!) } : {}), name: key, appearance: "light", colors: group.light[0]!.colors,