Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): 404 a missing asset instead of serving the SPA shell
Yudhi Armyndharis475ee20bc494
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6961f10c1..ffb1bae0a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -58,6 +58,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- **A missing dashboard asset returns 404 instead of the SPA shell.** `ServeStaticModule`'s built-in+ fallback answered *every* unmatched GET with `index.html`, so a mistyped or stale `<script src>`+ came back `200 text/html` and the browser reported a JavaScript syntax error from parsing the HTML+ shell — pointing at the wrong file and hiding a broken build. `main.ts` already serves dashboard+ documents (it injects the per-response CSP nonce, so it must own them) and is correctly narrow:+ it skips `/assets`, and only answers extensionless paths or explicit `text/html` navigations. The+ module's own catch-all is now disabled so that handler is the single owner. Client-side routes are+ unaffected.++ This also repairs a deployment shape that was broken outright: when the install path contains a+ dot-segment (`~/.openwa`, a checkout under `~/.cache`, a `TMPDIR` inside a dotdir), the built-in+ fallback sent the index by **absolute** path and Express's `send` refuses dot-segments under its+ default `dotfiles: 'ignore'` — so every client-side route 404'd while `/` and the hashed assets+ kept working. The e2e lock now runs its whole matrix against both path shapes, with a fixture+ guard asserting the two really differ (`os.tmpdir()` is not reliably dot-free, and using it+ blindly collapsed both cases onto one shape and hid exactly this bug).+- **The non-root smoke test can actually be run.** `scripts/smoke-test-non-root.sh` carried a UTF-8BOM ahead of its `#!/bin/sh`, and neither it nor `scripts/smoke-test-docker-proxy.sh` had theexecutable bit — so the `./scripts/…` invocation both of them document failed outright. The imagediff --git a/src/app.module.ts b/src/app.module.tsindex 5c3f94c5d..c57625eea 100644--- a/src/app.module.ts+++ b/src/app.module.ts@@ -92,6 +92,19 @@ if (dashboardServingEnabled && dashboardBuildPresent) {// Let Nest own these so unknown API/socket routes return real 404s/JSON rather// than the SPA index.html fallback (Express 5 / path-to-regexp v8 wildcard syntax).exclude: ['/api/{*splat}', '/socket.io/{*splat}', '/mcp', '/mcp/{*splat}'],+ // Disable this module's OWN catch-all SPA fallback. main.ts already serves dashboard+ // documents (it injects the per-response CSP nonce, which is why it must own them), and+ // that handler is correctly narrow: it skips /assets and only answers extensionless paths+ // or explicit text/html navigations. The built-in fallback here is not narrow — it answers+ // EVERY unmatched GET with index.html, so a mistyped `<script src>` came back 200 HTML and+ // the browser reported a JavaScript parse error instead of the real 404, hiding broken+ // builds behind a confusing symptom. It is also outright broken when the install path+ // contains a dot-segment (~/.openwa, a checkout under ~/.cache): it sends the index by+ // ABSOLUTE path and Express's `send` refuses dot-segments, 404ing every client-side route.+ // Turning it off fixes both, and makes behaviour identical on either path shape.+ // ServeStaticModule has no explicit off switch, so this is a renderPath literal that no+ // real request can match.+ renderPath: '/__openwa_spa_fallback_owned_by_main_ts__',}),);}diff --git a/test/serve-static.e2e-spec.ts b/test/serve-static.e2e-spec.tsindex 2c7464ab4..b29f80b80 100644--- a/test/serve-static.e2e-spec.ts+++ b/test/serve-static.e2e-spec.ts@@ -44,15 +44,19 @@ class PingController {}const EXCLUDE = ['/api/{*splat}', '/socket.io/{*splat}'];+// Mirrors app.module.ts: the module's own catch-all fallback is off, main.ts's document+// handler owns SPA routes. Without this the module answers every unmatched GET with the+// shell, which is the behaviour the missing-asset test below exists to prevent.+const RENDER_DISABLED = '/__openwa_spa_fallback_owned_by_main_ts__';@Module({- imports: [ServeStaticModule.forRoot({ rootPath: plainDist, exclude: EXCLUDE })],+ imports: [ServeStaticModule.forRoot({ rootPath: plainDist, exclude: EXCLUDE, renderPath: RENDER_DISABLED })],controllers: [PingController],})class PlainServeStaticModule {}@Module({- imports: [ServeStaticModule.forRoot({ rootPath: dottedDist, exclude: EXCLUDE })],+ imports: [ServeStaticModule.forRoot({ rootPath: dottedDist, exclude: EXCLUDE, renderPath: RENDER_DISABLED })],controllers: [PingController],})class DottedServeStaticModule {}@@ -145,9 +149,22 @@ describe.each([expect(res.text).not.toContain('OpenWA Dashboard');});- it('never leaks a dotfile out of the build directory', async () => {- // The SPA shell is an acceptable answer here; the file's CONTENTS must never be.+ it('404s a missing asset instead of handing back the SPA shell', async () => {+ // A mistyped <script src> must fail loudly. Answering 200 HTML makes the browser try to+ // parse the shell as JavaScript and report a syntax error, hiding the real cause.+ const res = await request(app.getHttpServer()).get('/assets/missing.js');+ expect(res.status).toBe(404);+ expect(res.text).not.toContain('OpenWA Dashboard');+ });++ it('404s a missing top-level file rather than serving the shell', async () => {+ const res = await request(app.getHttpServer()).get('/favicon-missing.svg');+ expect(res.status).toBe(404);+ });++ it('never serves a dotfile out of the build directory', async () => {const res = await request(app.getHttpServer()).get('/.env.secret');+ expect(res.status).toBe(404);expect(res.text).not.toContain('TOKEN=');});