From a5f7cdf3027370788f74cb3d15c754c713772071 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 23 Oct 2026 21:51:49 +0100 Subject: [PATCH] feat(web): icons and the web manifest are public for browsers and app installers --- app/tool/preview/server.mjs | 5 +++++ web/serve.mjs | 20 ++++++++++++++++---- web/test/serve.test.mjs | 27 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/tool/preview/server.mjs b/app/tool/preview/server.mjs index 824b6c8c..d86f1bd3 100644 --- a/app/tool/preview/server.mjs +++ b/app/tool/preview/server.mjs @@ -51,6 +51,8 @@ async function staticFile(base, pathname, req, res) { } catch { reply(res, 404, 'Not found'); } } +const PUBLIC_ASSET = /^\/(favicon\.(svg|png|ico)|apple-touch-icon\.png|manifest\.json|icons\/[A-Za-z0-9-]+\.png)$/; + function upstreamRequest(target, options, callback) { return (target.protocol === 'https:' ? httpsRequest : httpRequest)( { protocol: target.protocol, hostname: target.hostname, port: target.port || undefined, @@ -125,6 +127,9 @@ export async function start({ root = resolve('build/web'), upstream = 'http://12 } if (pathname === '/login' || pathname === '/logout' || pathname === '/chat-uploads' || pathname.startsWith('/artifacts/')) return proxy(req, res, target); if (req.method !== 'GET' && req.method !== 'HEAD') return reply(res, 405); + // Icons and the web manifest are public: browsers and app installers + // fetch them without the session cookie. + if (PUBLIC_ASSET.test(pathname)) return staticFile(base, pathname, req, res); authCheck(req, target, (authenticated) => { if (res.destroyed) return; if (authenticated === null) return reply(res, 502, 'Authentication upstream unavailable'); diff --git a/web/serve.mjs b/web/serve.mjs index 4afc9b62..b8ea1b6d 100644 --- a/web/serve.mjs +++ b/web/serve.mjs @@ -304,6 +304,13 @@ function resolvePath(urlPath) { return target; } +/** Icons and the web manifest, which need no sign-in. Fixed names only. */ +const PUBLIC_ASSET = + /^\/(favicon\.(svg|png|ico)|apple-touch-icon\.png|manifest\.json|icons\/[A-Za-z0-9-]+\.png)$/; +function isPublicAsset(pathname) { + return PUBLIC_ASSET.test(pathname); +} + /** One file of the Android app's update feed, or 404. */ async function serveUpdate(req, res, pathname) { let name; @@ -381,11 +388,16 @@ function proxyArtifact(req, res) { const server = createServer(async (req, res) => { const pathname = (req.url ?? "/").split("?")[0]; - // Brand icons are public so the sign-in page itself can show the app icon. - if (req.method === "GET" && (pathname === "/favicon.svg" || pathname === "/apple-touch-icon.png")) { - const body = await readFile(join(HERE, pathname.slice(1))).catch(() => null); + // Icons and the web manifest are public: the sign-in page shows the icon, + // and browsers and app installers fetch them without the session cookie. + if ((req.method === "GET" || req.method === "HEAD") && isPublicAsset(pathname)) { + const name = pathname.slice(1); + const body = + (await readFile(join(ROOT, name)).catch(() => null)) ?? + (await readFile(join(HERE, name)).catch(() => null)); if (body) { - res.writeHead(200, { "Content-Type": MIME[extname(pathname)], "Cache-Control": "no-store" }).end(body); + res.writeHead(200, { "Content-Type": MIME[extname(pathname)], "Cache-Control": "no-cache" }); + res.end(req.method === "HEAD" ? undefined : body); return; } } diff --git a/web/test/serve.test.mjs b/web/test/serve.test.mjs index e06972a0..f5e7ebd3 100644 --- a/web/test/serve.test.mjs +++ b/web/test/serve.test.mjs @@ -62,3 +62,30 @@ test("explains how to build the app when it is missing", async (t) => { assert.equal(page.status, 503); assert.match(await page.text(), /\.\/install --web/); }); + +test("icons and the manifest need no sign-in; the app does", async (t) => { + const dir = await mkdtemp(join(tmpdir(), "serve-")); + const root = join(dir, "app"); + await mkdir(join(root, "icons"), { recursive: true }); + await writeFile(join(root, "index.html"), ""); + await writeFile(join(root, "favicon.png"), "png"); + await writeFile(join(root, "manifest.json"), "{}"); + await writeFile(join(root, "icons", "Icon-192.png"), "png"); + await writeFile(join(root, "main.dart.js"), "app"); + const auth = join(dir, "auth.json"); + await writeFile(auth, JSON.stringify({ password: "password-1", secret: "s".repeat(40) })); + const port = await freePort(); + const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--root", root], { + cwd: here, + env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth }, + stdio: ["ignore", "pipe", "inherit"], + }); + await new Promise((done) => child.stdout.once("data", done)); + t.after(() => child.kill()); + const base = `http://127.0.0.1:${port}`; + for (const path of ["/favicon.png", "/favicon.svg", "/manifest.json", "/icons/Icon-192.png"]) { + assert.equal((await fetch(base + path)).status, 200, path); + } + assert.equal((await fetch(`${base}/main.dart.js`)).status, 401); + assert.equal((await fetch(`${base}/icons/../main.dart.js`)).status, 401); +});