Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix(apps): auto-recover broken stored icon SVG (PortOS shows blank icon)
Adam Eivy8e7f9fe2b860
diff --git a/server/routes/apps.js b/server/routes/apps.jsindex 14d51bca52..5db4793e07 100644--- a/server/routes/apps.js+++ b/server/routes/apps.js@@ -1,7 +1,7 @@import { Router } from 'express';import { spawn } from 'child_process';import { readFile, writeFile, stat, access, mkdir } from 'fs/promises';-import { join, resolve } from 'path';+import { join, resolve, extname } from 'path';import { PATHS } from '../lib/fileUtils.js';import * as appsService from '../services/apps.js';import { notifyAppsChanged, PORTOS_APP_ID } from '../services/apps.js';@@ -16,7 +16,7 @@ import { parseCronToNextRun } from '../services/eventScheduler.js';import { asyncHandler, ServerError } from '../lib/errorHandler.js';import { safeJSONParse } from '../lib/fileUtils.js';import { parseEcosystemFromPath, usesPm2 } from '../services/streamingDetect.js';-import { detectAppIcon, getIconContentType } from '../services/appIconDetect.js';+import { detectAppIcon, getIconContentType, isUsableSvg } from '../services/appIconDetect.js';import { hasDeployScript } from '../services/appDeployer.js';import { checkScripts, installScripts, XCODE_SCRIPT_NAMES } from '../services/xcodeScripts.js';@@ -277,11 +277,19 @@ router.post('/:id/upgrade-tls', loadApp, asyncHandler(async (req, res) => {router.get('/:id/icon', loadApp, asyncHandler(async (req, res) => {const app = req.loadedApp;- // Use stored appIconPath, or detect on-the-fly+ // Use stored appIconPath, or detect on-the-fly. Stored SVGs that embed an+ // external <image href="..."> render blank under the route's `default-src+ // 'none'` CSP, so re-detect when the cached path is an unusable SVG (e.g.+ // PortOS's own favicon.svg, which wraps a /portos-logo.png reference) —+ // otherwise installs that resolved an icon BEFORE the detector learned to+ // skip these stay broken until the user manually re-detects.let iconPath = app.appIconPath;- if (!iconPath || !await pathExists(iconPath)) {+ const stale =+ !iconPath ||+ !await pathExists(iconPath) ||+ (extname(iconPath).toLowerCase() === '.svg' && !await isUsableSvg(iconPath));+ if (stale) {iconPath = await detectAppIcon(app.repoPath, app.type);- // Persist the detected path for future requestsif (iconPath && iconPath !== app.appIconPath) {await appsService.updateApp(app.id, { appIconPath: iconPath });}@@ -992,8 +1000,13 @@ router.post('/:id/refresh-config', loadApp, asyncHandler(async (req, res) => {updates.uiPort = deriveUiPort(updates.uiPort, updates.apiPort, updates.devUiPort || app.devUiPort);}- // Detect app icon if not already set- if (!app.appIconPath || !await pathExists(app.appIconPath)) {+ // Detect app icon if not already set, missing on disk, or stored as an+ // unusable external-image SVG (won't render under the icon route's CSP).+ const iconStale =+ !app.appIconPath ||+ !await pathExists(app.appIconPath) ||+ (extname(app.appIconPath).toLowerCase() === '.svg' && !await isUsableSvg(app.appIconPath));+ if (iconStale) {const detectedIcon = await detectAppIcon(app.repoPath, app.type);if (detectedIcon) updates.appIconPath = detectedIcon;}diff --git a/server/routes/apps.test.js b/server/routes/apps.test.jsindex 37c78d3afb..72d5a6cff3 100644--- a/server/routes/apps.test.js+++ b/server/routes/apps.test.js@@ -43,7 +43,8 @@ vi.mock('../services/appUpdater.js', () => ({vi.mock('../services/appIconDetect.js', () => ({detectAppIcon: vi.fn(),- getIconContentType: vi.fn()+ getIconContentType: vi.fn(),+ isUsableSvg: vi.fn().mockResolvedValue(true)}));vi.mock('../services/cos.js', () => ({@@ -76,7 +77,7 @@ import * as appsService from '../services/apps.js';import * as pm2Service from '../services/pm2.js';import * as history from '../services/history.js';import * as streamingDetect from '../services/streamingDetect.js';-import { detectAppIcon, getIconContentType } from '../services/appIconDetect.js';+import { detectAppIcon, getIconContentType, isUsableSvg } from '../services/appIconDetect.js';import { installScripts } from '../services/xcodeScripts.js';import { writeFileSync, mkdirSync, rmSync } from 'fs';import { join } from 'path';@@ -631,6 +632,28 @@ describe('Apps Routes', () => {expect(second.status).toBe(304);});++ it('redetects when stored path is an unusable SVG (external <image href>) so PortOS-style icons recover', async () => {+ // Simulate the bad-state PortOS install: appIconPath stored as an SVG+ // that exists on disk but embeds <image href="/portos-logo.png"> — CSP+ // blocks the embed, so it renders blank. The route must re-detect.+ const badSvgPath = join(iconDir, 'favicon.svg');+ const goodPngPath = join(iconDir, 'redetected.png');+ writeFileSync(badSvgPath, '<svg><image href="/logo.png"/></svg>');+ writeFileSync(goodPngPath, 'fake-png-data');+ appsService.getAppById.mockResolvedValue({+ ...mockApp,+ appIconPath: badSvgPath,+ });+ isUsableSvg.mockResolvedValueOnce(false);+ detectAppIcon.mockResolvedValueOnce(goodPngPath);++ const response = await request(app).get('/api/apps/app-001/icon');++ expect(response.status).toBe(200);+ expect(detectAppIcon).toHaveBeenCalledWith('/tmp/test', undefined);+ expect(appsService.updateApp).toHaveBeenCalledWith('app-001', { appIconPath: goodPngPath });+ });});describe('PUT /api/apps/:id/task-types/all', () => {diff --git a/server/services/appIconDetect.js b/server/services/appIconDetect.jsindex fa2e73090e..fb89cda330 100644--- a/server/services/appIconDetect.js+++ b/server/services/appIconDetect.js@@ -60,7 +60,7 @@ const CONTENT_TYPES = {// endpoint's strict `default-src 'none'` CSP, so they are unusable as app icons// even though the file exists. Skip them and let detection fall through to a// sibling PNG/raster file. Inline data: URIs are fine.-async function isUsableSvg(filePath) {+export async function isUsableSvg(filePath) {const content = await readFile(filePath, 'utf-8').catch(() => null);if (content === null) return false;const externalImage = /<image\b[^>]*\b(?:xlink:)?href\s*=\s*['"](?!data:)/i;