From 5853bec60a3850e0ee9c80d8451654dcdb11b58d Mon Sep 17 00:00:00 2001 From: carbon_evolution Date: Sun, 18 Oct 2026 12:23:24 +0800 Subject: [PATCH] fix(analytics): make Umami opt-in, drop visitor-IP event (OWASP A06) The middleware posted every page view, plus a custom "Network Log" event carrying the visitor's IP, to a hardcoded http://umami-umami-1:3000 with the upstream author's website id as a default. - Off by default: nothing is sent unless UMAMI_URL and UMAMI_WEBSITE_ID are both set; otherwise return NextResponse.next() with no fetch. - No hardcoded host or default website id; invalid/non-http URL disables. - Remove the "Network Log" IP event. The client IP is forwarded only as X-Forwarded-For so Umami can do its own geo lookup. - 2s timeout on the analytics fetch; failures are swallowed, still via event.waitUntil so page loads never wait on analytics. - docker-compose: drop the external umami_default network, which made `docker compose up` fail on hosts without upstream's Umami stack. - Document the variables and what is sent in .env.example; update SECURITY.md A06. Add middleware unit tests. Co-Authored-By: Claude Opus 5.5 --- .env.example | 10 +++ SECURITY.md | 2 +- docker-compose.yml | 5 -- src/middleware.test.ts | 150 +++++++++++++++++++++++++++++++++++++++++ src/middleware.ts | 81 ++++++++++++++-------- 5 files changed, 214 insertions(+), 34 deletions(-) create mode 100644 src/middleware.test.ts diff --git a/.env.example b/.env.example index b893b8b..5f7a6cb 100644 --- a/.env.example +++ b/.env.example @@ -116,6 +116,16 @@ OSIRIS_PORT=3000 # osintdefender,insiderpaper,aljazeeraenglish,nexta_live,war_monitor # OSIRIS_TELEGRAM_CHANNELS= +# Umami page-view analytics — OPTIONAL, OFF by default. Nothing is sent +# anywhere unless BOTH are set, pointing at an Umami instance YOU run. +# UMAMI_URL is the base URL (OSIRIS posts to /api/send). +# Privacy: when enabled, each page load sends the path, hostname, referrer and +# User-Agent; the visitor IP goes only as an X-Forwarded-For header (for +# Umami's own geo lookup), never in the event payload. If Umami runs in another +# Compose project, attach its network to the osiris service via an override. +# UMAMI_URL=http://umami:3000 +# UMAMI_WEBSITE_ID= + # Set by the Docker image already — override only if needed. # NODE_ENV=production # PORT=3000 diff --git a/SECURITY.md b/SECURITY.md index bf12fc5..3a72d87 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -87,7 +87,7 @@ ZAP baseline (2026-10-18): **0 High, 5 Medium, 5 Low, 5 Informational** alert ty - ✅ Scanner repository: Semgrep (JavaScript, Node, secrets, command-injection rules) on every PR. ### A06 — Insecure Design · 🟠 Medium -- ⚠️ **Analytics middleware forwards every page view, including the visitor's IP address, to a hardcoded `http://umami-umami-1:3000`** with a default site ID inherited from the upstream project. This is undisclosed IP logging aimed at infrastructure you may not control; it should be opt-in via configuration. +- ✅ **Analytics is opt-in and off by default**: the middleware sends nothing unless `UMAMI_URL` and `UMAMI_WEBSITE_ID` are both set (no hardcoded host or upstream site ID). The separate visitor-IP "Network Log" event was removed; page views time out after 2s and fail silently. Covered by unit tests. - ⚠️ Active recon capability exposed on a public dashboard (see A01). - ⚠️ The SSRF guard checks DNS at validation time; a DNS-rebinding race remains possible (documented in `src/lib/ssrf-guard.ts`). - ✅ Fail-closed defaults for optional integrations (webhook #44, scanner without key → 503); fixed-upstream proxies instead of caller-supplied URLs. diff --git a/docker-compose.yml b/docker-compose.yml index 97ac57c..2810508 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,7 +27,6 @@ services: restart: unless-stopped networks: - default - - umami_default osiris-cache: image: nginx:alpine @@ -57,10 +56,6 @@ services: volumes: nginx-cache: -networks: - umami_default: - external: true - # ── CasaOS app-store metadata (ignored by plain Docker Compose) ── x-casaos: architectures: diff --git a/src/middleware.test.ts b/src/middleware.test.ts new file mode 100644 index 0000000..3d6f94c --- /dev/null +++ b/src/middleware.test.ts @@ -0,0 +1,150 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { NextRequest } from 'next/server'; +import type { NextFetchEvent } from 'next/server'; +import { middleware } from './middleware'; + +type FetchArgs = [input: string | URL | Request, init?: RequestInit]; + +function fakeEvent() { + const pending: Promise[] = []; + const event = { + waitUntil: (p: Promise) => { + pending.push(p); + }, + } as unknown as NextFetchEvent; + return { event, pending }; +} + +function request(path = '/', headers: Record = {}) { + return new NextRequest(`https://osiris.example${path}`, { + headers: { 'user-agent': 'vitest-agent', ...headers }, + }); +} + +function bodyOf(call: FetchArgs) { + return JSON.parse(String(call[1]?.body)) as { + type: string; + payload: Record; + }; +} + +describe('analytics middleware', () => { + let fetchMock: ReturnType Promise>>; + + beforeEach(() => { + fetchMock = vi.fn<(...args: FetchArgs) => Promise>( + async () => new Response('{}', { status: 200 }), + ); + vi.stubGlobal('fetch', fetchMock); + vi.stubEnv('UMAMI_URL', ''); + vi.stubEnv('UMAMI_WEBSITE_ID', ''); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + vi.unstubAllEnvs(); + }); + + it('sends nothing when analytics is not configured', async () => { + const { event, pending } = fakeEvent(); + const res = middleware(request('/', { 'x-forwarded-for': '203.0.113.7' }), event); + await Promise.all(pending); + expect(res.status).toBe(200); + expect(res.headers.get('x-middleware-next')).toBe('1'); + expect(fetchMock).not.toHaveBeenCalled(); + expect(pending).toHaveLength(0); + }); + + it('sends nothing when only one of UMAMI_URL / UMAMI_WEBSITE_ID is set', async () => { + for (const [url, id] of [ + ['http://umami.internal:3000', ''], + ['', 'site-123'], + ]) { + vi.stubEnv('UMAMI_URL', url); + vi.stubEnv('UMAMI_WEBSITE_ID', id); + const { event, pending } = fakeEvent(); + middleware(request('/'), event); + await Promise.all(pending); + } + expect(fetchMock).not.toHaveBeenCalled(); + }); + + it('sends nothing when UMAMI_URL is not a valid http(s) URL', async () => { + vi.stubEnv('UMAMI_URL', 'not a url'); + vi.stubEnv('UMAMI_WEBSITE_ID', 'site-123'); + const { event, pending } = fakeEvent(); + middleware(request('/'), event); + await Promise.all(pending); + expect(fetchMock).not.toHaveBeenCalled(); + }); + + it('sends a single page view to the configured endpoint and website id', async () => { + vi.stubEnv('UMAMI_URL', 'http://umami.internal:3000/'); + vi.stubEnv('UMAMI_WEBSITE_ID', 'site-123'); + const { event, pending } = fakeEvent(); + const res = middleware( + request('/dashboard', { 'x-forwarded-for': '203.0.113.7, 10.0.0.1', referer: 'https://ref.example/' }), + event, + ); + await Promise.all(pending); + + expect(res.headers.get('x-middleware-next')).toBe('1'); + expect(fetchMock).toHaveBeenCalledTimes(1); + const call = fetchMock.mock.calls[0]; + expect(String(call[0])).toBe('http://umami.internal:3000/api/send'); + expect(call[1]?.method).toBe('POST'); + expect(call[1]?.signal).toBeInstanceOf(AbortSignal); + + const body = bodyOf(call); + expect(body.type).toBe('event'); + expect(body.payload.website).toBe('site-123'); + expect(body.payload.url).toBe('/dashboard'); + expect(body.payload.hostname).toBe('osiris.example'); + expect(body.payload.referrer).toBe('https://ref.example/'); + // A page view carries no custom event name or data, and never the IP. + expect(body.payload).not.toHaveProperty('name'); + expect(body.payload).not.toHaveProperty('data'); + expect(JSON.stringify(body)).not.toContain('203.0.113.7'); + + // Client IP is only forwarded as a header, for Umami's own geo lookup. + const headers = new Headers(call[1]?.headers); + expect(headers.get('x-forwarded-for')).toBe('203.0.113.7'); + expect(headers.get('user-agent')).toBe('vitest-agent'); + }); + + it('never sends the visitor-IP "Network Log" event', async () => { + vi.stubEnv('UMAMI_URL', 'http://umami.internal:3000'); + vi.stubEnv('UMAMI_WEBSITE_ID', 'site-123'); + vi.stubEnv('UMAMI_TRACK_IP', 'true'); + const { event, pending } = fakeEvent(); + middleware(request('/', { 'cf-connecting-ip': '198.51.100.9' }), event); + await Promise.all(pending); + + for (const call of fetchMock.mock.calls) { + const body = bodyOf(call); + expect(body.payload.name).not.toBe('Network Log'); + expect(JSON.stringify(body.payload)).not.toContain('198.51.100.9'); + } + }); + + it('does not throw or reject when the analytics request fails', async () => { + vi.stubEnv('UMAMI_URL', 'http://umami.internal:3000'); + vi.stubEnv('UMAMI_WEBSITE_ID', 'site-123'); + fetchMock.mockRejectedValue(new TypeError('fetch failed')); + const { event, pending } = fakeEvent(); + const res = middleware(request('/'), event); + expect(res.headers.get('x-middleware-next')).toBe('1'); + await expect(Promise.all(pending)).resolves.toBeDefined(); + expect(fetchMock).toHaveBeenCalledTimes(1); + }); + + it('does not throw when fetch throws synchronously', () => { + vi.stubEnv('UMAMI_URL', 'http://umami.internal:3000'); + vi.stubEnv('UMAMI_WEBSITE_ID', 'site-123'); + fetchMock.mockImplementation(() => { + throw new TypeError('boom'); + }); + const { event } = fakeEvent(); + expect(() => middleware(request('/'), event)).not.toThrow(); + }); +}); diff --git a/src/middleware.ts b/src/middleware.ts index 92617e5..92c54a0 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,38 +1,63 @@ import { NextResponse } from 'next/server'; import type { NextRequest, NextFetchEvent } from 'next/server'; +// Optional, self-hosted Umami page-view analytics. OFF by default: nothing is +// sent anywhere unless the operator sets BOTH UMAMI_URL and UMAMI_WEBSITE_ID. +const ANALYTICS_TIMEOUT_MS = 2000; + +function umamiEndpoint(): { endpoint: string; websiteId: string } | null { + const base = process.env.UMAMI_URL?.trim(); + const websiteId = process.env.UMAMI_WEBSITE_ID?.trim(); + if (!base || !websiteId) return null; + try { + const url = new URL(base); + if (url.protocol !== 'http:' && url.protocol !== 'https:') return null; + url.pathname = `${url.pathname.replace(/\/+$/, '')}/api/send`; + return { endpoint: url.toString(), websiteId }; + } catch { + return null; + } +} + export function middleware(request: NextRequest, event: NextFetchEvent) { - const url = request.nextUrl.pathname; - - const ip = request.headers.get('cf-connecting-ip') || request.headers.get('x-forwarded-for') || '127.0.0.1'; - const userAgent = request.headers.get('user-agent') || 'Unknown OSIRIS Client'; - - const basePayload = { + const umami = umamiEndpoint(); + if (!umami) return NextResponse.next(); + + // The client IP is forwarded only as a header so Umami can do its own geo + // lookup / visitor hashing; it is never put in the event payload. + const ip = (request.headers.get('cf-connecting-ip') || request.headers.get('x-forwarded-for') || '') + .split(',')[0] + .trim(); + const headers: Record = { + 'Content-Type': 'application/json', + 'User-Agent': request.headers.get('user-agent') || 'Unknown OSIRIS Client', + }; + if (ip) headers['x-forwarded-for'] = ip; + + const payload = { hostname: request.nextUrl.hostname, - language: "en-US", - referrer: request.headers.get('referer') || "", - screen: "1920x1080", - title: "OSIRIS", - url: url, - website: process.env.UMAMI_WEBSITE_ID || "cd8f216c-fc3f-45f5-ba1a-e10309a61d18" + language: 'en-US', + referrer: request.headers.get('referer') || '', + screen: '1920x1080', + title: 'OSIRIS', + url: request.nextUrl.pathname, + website: umami.websiteId, }; - const pageView = fetch('http://umami-umami-1:3000/api/send', { - method: 'POST', - headers: { 'Content-Type': 'application/json', 'User-Agent': userAgent, 'x-forwarded-for': ip }, - body: JSON.stringify({ payload: basePayload, type: "event" }) - }).catch(() => {}); - - const ipEvent = fetch('http://umami-umami-1:3000/api/send', { - method: 'POST', - headers: { 'Content-Type': 'application/json', 'User-Agent': userAgent, 'x-forwarded-for': ip }, - body: JSON.stringify({ - payload: { ...basePayload, name: "Network Log", data: { IP: ip } }, - type: "event" - }) - }).catch(() => {}); - - event.waitUntil(Promise.all([pageView, ipEvent])); + try { + const pageView = fetch(umami.endpoint, { + method: 'POST', + headers, + body: JSON.stringify({ payload, type: 'event' }), + signal: AbortSignal.timeout(ANALYTICS_TIMEOUT_MS), + }).then( + () => undefined, + () => undefined, + ); + event.waitUntil(pageView); + } catch { + // Analytics must never break page loads. + } return NextResponse.next(); }