Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(theme): reject an import whose colors are null, an array, or empty\n\n`"colors": null` passed validation and produced an activatable theme with zero\ncolour tokens.\n\nThe shape check was:\n\n if (!parsed || typeof parsed !== 'object' || typeof parsed.colors !== 'object')\n\nand `typeof null === 'object'`, so null slipped through. Execution reached\n`colors: { ...(parsed.colors) }`, and spreading null yields `{}` rather than\nthrowing — so the theme was written to the store and could be activated from\nthe gallery, applying nothing.\n\n`typeof [] === 'object'` too, so an array was the same hole by a second route.\nBoth are covered now.\n\n**Empty `{}` is refused as well, and that is a judgement call worth stating.**\nIts user-visible outcome is identical to null: `applyTheme` writes one CSS\ncustom property per token, and `withDerivedChrome` derives only FROM existing\ntokens (`lib/theme/chrome.ts:67-79`), so a zero-token theme applies nothing at\nall — a named entry in the gallery that silently does nothing when clicked.\nAccepting `{}` while rejecting `null` would fix the shape and leave the defect\nthat motivated the report.\n\nThe cost, stated plainly: a fonts-or-backdrop-only theme can no longer be\nimported without at least one colour. That was never really supported — `colors`\nhas always been a required field on this path — and it is the narrower trade\nagainst shipping a theme that does nothing. A single-token theme still imports,\nand there is a test pinning that so the guard cannot drift into over-rejecting.\n\nReplaces the inline `typeof` test with a `hasColorTokens` type predicate at\nmodule scope, so the intent reads at the call site and the narrowing is explicit.\n\nVerified by reading `ThemeStudioPanel.tsx:178-196`, `providers/ThemeProvider.tsx:38-60`\nand `lib/theme/chrome.ts:67-79`; NOT executed — a fleet-wide no-local-builds rule\nis in force, so CI is the check.\n\n## Related\nCloses #5901\n

M3gA-Mindf0638a6987e6
diff --git a/app/src/components/settings/panels/ThemeStudioPanel.test.tsx b/app/src/components/settings/panels/ThemeStudioPanel.test.tsx
index 9bf5074fd2..46a84719a3 100644
--- a/app/src/components/settings/panels/ThemeStudioPanel.test.tsx
+++ b/app/src/components/settings/panels/ThemeStudioPanel.test.tsx
@@ -79,4 +79,50 @@ describe('<ThemeStudioPanel />', () => {
backdrop: { kind: 'image', imageUrl: 'https://example.com/bg.jpg' },
});
});
+
+ // #5901: `typeof null === 'object'` and `typeof [] === 'object'`, so the old
+ // shape check let both through; `{ ...null }` and `{ ...[] }` each yield `{}`,
+ // and a zero-token theme was stored and could be activated from the gallery.
+ // `{}` is refused for the same reason — `applyTheme` writes one custom
+ // property per token, so a colourless theme applies nothing at all.
+ it.each([
+ ['null colors', null],
+ ['an array of colors', []],
+ ['an empty colors object', {}],
+ ['a string colors value', 'surface'],
+ ['a numeric colors value', 42],
+ ])('refuses to import a theme with %s', (_label, colors) => {
+ const { store } = renderWithProviders(<ThemeStudioPanel />, {
+ preloadedState: { theme: themeState },
+ initialEntries: ['/settings/theme'],
+ });
+
+ fireEvent.change(screen.getByLabelText('Import theme'), {
+ target: { value: JSON.stringify({ name: 'Colourless', isDark: false, colors }) },
+ });
+ fireEvent.click(screen.getByRole('button', { name: 'Import' }));
+
+ expect(screen.getByText('Could not parse that theme JSON.')).toBeInTheDocument();
+ expect(store.getState().theme.customThemes).toHaveLength(0);
+ });
+
+ it('still imports a theme carrying a single colour token', () => {
+ const { store } = renderWithProviders(<ThemeStudioPanel />, {
+ preloadedState: { theme: themeState },
+ initialEntries: ['/settings/theme'],
+ });
+
+ fireEvent.change(screen.getByLabelText('Import theme'), {
+ target: {
+ value: JSON.stringify({ name: 'Minimal', isDark: false, colors: { surface: '1 2 3' } }),
+ },
+ });
+ fireEvent.click(screen.getByRole('button', { name: 'Import' }));
+
+ expect(store.getState().theme.customThemes).toHaveLength(1);
+ expect(store.getState().theme.customThemes[0]).toMatchObject({
+ name: 'Minimal',
+ colors: { surface: '1 2 3' },
+ });
+ });
});
diff --git a/app/src/components/settings/panels/ThemeStudioPanel.tsx b/app/src/components/settings/panels/ThemeStudioPanel.tsx
index 9404dc3837..bd1b64c757 100644
--- a/app/src/components/settings/panels/ThemeStudioPanel.tsx
+++ b/app/src/components/settings/panels/ThemeStudioPanel.tsx
@@ -88,6 +88,37 @@ function tileCanvas(theme: Theme): string {
return theme.gradient?.canvas ?? channelsToCss(swatchChannels(theme, 'surface-canvas'));
}
+/**
+ * Does this value carry at least one colour token?
+ *
+ * The old check was `typeof parsed.colors !== 'object'`, which passes for
+ * `null` and for an array — `typeof null` and `typeof []` are both `'object'`.
+ * Execution then reached `colors: { ...(parsed.colors) }`, and spreading either
+ * yields `{}`, so a theme with ZERO colour tokens was written to the store and
+ * could be activated from the gallery (#5901).
+ *
+ * An empty object is refused for the same reason, not merely for tidiness: its
+ * user-visible outcome is identical to `null`. `applyTheme` writes one custom
+ * property per token and `withDerivedChrome` derives only FROM existing tokens
+ * (`lib/theme/chrome.ts:67-79`), so a zero-token theme applies nothing at all —
+ * a named entry in the gallery that silently does nothing when clicked.
+ * Accepting `{}` while rejecting `null` would fix the shape and leave the
+ * defect.
+ *
+ * The cost, stated plainly: a fonts-or-backdrop-only theme can no longer be
+ * imported without at least one colour. That was never really supported —
+ * `colors` has always been a required field here — and it is the narrower
+ * trade against shipping a theme that does nothing.
+ */
+function hasColorTokens(colors: unknown): colors is Record<string, string> {
+ return (
+ typeof colors === 'object' &&
+ colors !== null &&
+ !Array.isArray(colors) &&
+ Object.keys(colors).length > 0
+ );
+}
+
function importedGradient(parsed: Partial<Theme>): Theme['gradient'] {
if (!parsed.gradient || typeof parsed.gradient !== 'object') return undefined;
return typeof parsed.gradient.canvas === 'string' ? { canvas: parsed.gradient.canvas } : {};
@@ -148,7 +179,7 @@ const ThemeStudioPanel = ({ embedded = false }: ThemeStudioPanelProps = {}) => {
setImportError('');
try {
const parsed = JSON.parse(importText) as Partial<Theme>;
- if (!parsed || typeof parsed !== 'object' || typeof parsed.colors !== 'object') {
+ if (!parsed || typeof parsed !== 'object' || !hasColorTokens(parsed.colors)) {
throw new Error('shape');
}
const theme: Theme = {