OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
refactor(api): centralize outbound fetching and tighten input handling
Carl-Gerhard Lindesvärd373be136190a
diff --git a/apps/api/package.json b/apps/api/package.jsonindex 3b845cc46..80f48a063 100644--- a/apps/api/package.json+++ b/apps/api/package.json@@ -50,6 +50,7 @@"fastify-raw-body": "^5.0.0","fastify-zod-openapi": "^5.6.1","groupmq": "catalog:",+ "ipaddr.js": "^2.5.0","jsonwebtoken": "^9.0.2","pino": "catalog:","pino-pretty": "catalog:",@@ -59,6 +60,7 @@"sqlstring": "^2.3.3","superjson": "^1.13.3","svix": "^1.24.0",+ "undici": "^8.10.0","url-metadata": "^5.4.1","uuid": "^9.0.1","zod": "catalog:"diff --git a/apps/api/src/controllers/misc.controller.ts b/apps/api/src/controllers/misc.controller.tsindex 3fa50c0d6..75ac7ea6e 100644--- a/apps/api/src/controllers/misc.controller.ts+++ b/apps/api/src/controllers/misc.controller.ts@@ -1,8 +1,14 @@import crypto from 'node:crypto';import { logger } from '@/utils/logger';import { parseUrlMeta } from '@/utils/parseUrlMeta';+import {+ ALLOWED_IMAGE_CONTENT_TYPES,+ normalizeContentType,+ processImage,+ processOgImage,+} from '@/utils/image-proxy';+import { BlockedUrlError, assertPublicUrl, safeFetch } from '@/utils/safe-fetch';import type { FastifyReply, FastifyRequest } from 'fastify';-import sharp from 'sharp';import {DEFAULT_IP_HEADER_ORDER,@@ -24,9 +30,17 @@ const USER_AGENT = 'OpenPanel-FaviconProxy/1.0 (+https://openpanel.dev)';// Helper functionsfunction createCacheKey(url: string, prefix = 'favicon'): string {const hash = crypto.createHash('sha256').update(url).digest('hex');- return `${prefix}:v2:${hash}`;+ // v3: entries written before the SVG/raw-passthrough fix could hold+ // attacker-controlled bytes with an attacker-chosen content type, so the+ // old namespace is abandoned rather than served from.+ return `${prefix}:v3:${hash}`;}+/**+ * Shape check only. The destination is validated by `assertPublicUrl` /+ * `safeFetch` right before each outbound request, because a hostname can+ * resolve differently between validation and connection.+ */function validateUrl(raw?: string): URL | null {try {if (!raw) throw new Error('Missing ?url');@@ -66,158 +80,63 @@ async function setToCacheBinary(]);}-// Fetch image with timeout and size limits+// Fetch image with SSRF protection, timeout and size limitsasync function fetchImage(url: URL,): Promise<{ buffer: Buffer; contentType: string; status: number }> {- const controller = new AbortController();- const timeout = setTimeout(() => controller.abort(), 1000); // 10s timeout-try {- const response = await fetch(url.toString(), {- redirect: 'follow',- signal: controller.signal,+ // `safeFetch` validates and pins every hop, so a redirect cannot be used+ // to reach an internal address after the initial URL checks out.+ const result = await safeFetch(url, {+ timeoutMs: 10_000,+ maxBytes: MAX_BYTES,headers: {'user-agent': USER_AGENT,accept: 'image/*,*/*;q=0.8',},});- clearTimeout(timeout);-- if (!response.ok) {+ if (result.status !== 200) {return {buffer: Buffer.alloc(0),contentType: 'text/plain',- status: response.status,+ status: result.status,};}- // Size guard- const contentLength = Number(response.headers.get('content-length') ?? '0');- if (contentLength > MAX_BYTES) {- throw new Error(`Remote file too large: ${contentLength} bytes`);- }-- const arrayBuffer = await response.arrayBuffer();- const buffer = Buffer.from(arrayBuffer);-- // Additional size check for actual content- if (buffer.length > MAX_BYTES) {- throw new Error('Remote file exceeded size limit');+ const contentType = normalizeContentType(+ result.headers.get('content-type'),+ );+ if (!ALLOWED_IMAGE_CONTENT_TYPES.has(contentType)) {+ logger.debug(+ { url: url.toString(), contentType },+ 'Refusing non-image response',+ );+ return { buffer: Buffer.alloc(0), contentType: 'text/plain', status: 415 };}- const contentType =- response.headers.get('content-type') || 'application/octet-stream';- return { buffer, contentType, status: 200 };+ return { buffer: result.body, contentType, status: 200 };} catch (error) {- clearTimeout(timeout);+ if (error instanceof BlockedUrlError) {+ logger.warn(+ { url: url.toString(), reason: error.message },+ 'Blocked image fetch',+ );+ }return { buffer: Buffer.alloc(0), contentType: 'text/plain', status: 500 };}}-// Check if URL is an ICO file-function isIcoFile(url: string, contentType?: string): boolean {- return (- url.toLowerCase().endsWith('.ico') ||- contentType === 'image/x-icon' ||- contentType === 'image/vnd.microsoft.icon'- );-}-function isSvgFile(url: string, contentType?: string): boolean {- return url.toLowerCase().endsWith('.svg') || contentType === 'image/svg+xml';-}--// Process image with Sharp (resize to 30x30 PNG)-async function processImage(- buffer: Buffer,- originalUrl?: string,- contentType?: string,-): Promise<Buffer> {- // If it's an ICO file, just return it as-is (no conversion needed)- if (originalUrl && isIcoFile(originalUrl, contentType)) {- logger.debug(- { originalUrl, bufferSize: buffer.length },- 'Serving ICO file directly',- );- return buffer;- }-- if (originalUrl && isSvgFile(originalUrl, contentType)) {- logger.debug(- { originalUrl, bufferSize: buffer.length },- 'Serving SVG file directly',- );- return buffer;- }-- // If buffer isnt to big just return it as well- if (buffer.length < 5000) {- logger.debug(- { originalUrl, bufferSize: buffer.length },- 'Serving image directly without processing',- );- return buffer;- }-- try {- // For other formats, process with Sharp- return await sharp(buffer)- .resize(30, 30, {- fit: 'cover',- })- .png()- .toBuffer();- } catch (error) {- logger.warn(- {- err: error,- originalUrl,- bufferSize: buffer.length,- },- 'Sharp failed to process image, trying fallback',- );-- throw error;- }-}--// Process OG image with Sharp (resize to 300px width)-async function processOgImage(- buffer: Buffer,- originalUrl?: string,- contentType?: string,-): Promise<Buffer> {- // If buffer is small enough, return it as-is- if (buffer.length < 10000) {- logger.debug(- { originalUrl, bufferSize: buffer.length },- 'Serving OG image directly without processing',- );- return buffer;- }-- try {- // For OG images, process with Sharp to 300px width, maintaining aspect ratio- return await sharp(buffer)- .resize(300, null, {- fit: 'inside',- withoutEnlargement: true,- })- .png()- .toBuffer();- } catch (error) {- logger.warn(- {- err: error,- originalUrl,- bufferSize: buffer.length,- },- 'Sharp failed to process OG image, trying fallback',- );-- throw error;- }+/**+ * These bytes come from a third-party server, so make sure a browser treats+ * them strictly as an image: no MIME sniffing, no scripts, no embedding+ * privileges on the API origin.+ */+function setImageSecurityHeaders(reply: FastifyReply, contentType: string) {+ reply.header('Content-Type', contentType);+ reply.header('X-Content-Type-Options', 'nosniff');+ reply.header('Content-Security-Policy', "default-src 'none'; sandbox");+ reply.header('Cross-Origin-Resource-Policy', 'cross-origin');}// Check if URL is a direct image@@ -245,12 +164,16 @@ export async function getFavicon(.send('Not found');}+ // Check the destination before anything else, so a blocked host is not+ // leaked to the DuckDuckGo fallback below either.+ await assertPublicUrl(url);+const cacheKey = createCacheKey(url.toString());// Check cache firstconst cached = await getFromCacheBinary(cacheKey);if (cached) {- reply.header('Content-Type', cached.contentType);+ setImageSecurityHeaders(reply, cached.contentType);reply.header('Cache-Control', 'public, max-age=604800, immutable');return reply.send(cached.buffer);}@@ -354,33 +277,31 @@ export async function getFavicon('Favicon processing result',);- // Determine the correct content type for caching and response- const isIco = isIcoFile(imageUrl.toString(), contentType);- const isSvg = isSvgFile(imageUrl.toString(), contentType);- let responseContentType = contentType;-- if (isIco) {- responseContentType = 'image/x-icon';- } else if (isSvg) {- responseContentType = 'image/svg+xml';- } else if (- processedBuffer.length < 5000 &&- buffer.length === processedBuffer.length- ) {- // Image was returned as-is, keep original content type- responseContentType = contentType;- } else {- // Image was processed by Sharp, it's now a PNG- responseContentType = 'image/png';- }+ // `processImage` either passed an ICO through untouched or rasterized to+ // PNG, so the response type is derived from what we produced rather than+ // from whatever the upstream server claimed.+ const responseContentType =+ processedBuffer === buffer ? 'image/x-icon' : 'image/png';// Cache the result with correct content typeawait setToCacheBinary(cacheKey, processedBuffer, responseContentType);- reply.header('Content-Type', responseContentType);+ setImageSecurityHeaders(reply, responseContentType);reply.header('Cache-Control', 'public, max-age=3600, immutable');return reply.send(processedBuffer);} catch (error: any) {+ if (error instanceof BlockedUrlError) {+ logger.warn(+ { url: request.query.url, reason: error.message },+ 'Blocked favicon fetch',+ );+ reply.header('Cache-Control', 'no-store');+ return reply+ .status(400)+ .header('Content-Type', 'text/plain')+ .send('Bad request');+ }+logger.error({ err: error, url: request.query.url },'Favicon fetch error',@@ -525,12 +446,14 @@ export async function getOgImage(if (!url) {return getFavicon(request, reply);}+ await assertPublicUrl(url);+const cacheKey = createCacheKey(url.toString(), 'og');// Check cache firstconst cached = await getFromCacheBinary(cacheKey);if (cached) {- reply.header('Content-Type', cached.contentType);+ setImageSecurityHeaders(reply, cached.contentType);reply.header('Cache-Control', 'public, max-age=604800, immutable');return reply.send(cached.buffer);}@@ -552,26 +475,34 @@ export async function getOgImage(}// Fetch the image- const { buffer, contentType, status } = await fetchImage(imageUrl);+ const { buffer, status } = await fetchImage(imageUrl);if (status !== 200 || buffer.length === 0) {return getFavicon(request, reply);}- // Process the image (resize to 1200x630 for OG standards, or serve as-is if reasonable size)- const processedBuffer = await processOgImage(- buffer,- imageUrl.toString(),- contentType,- );+ // Rasterize to a 300px-wide PNG+ const processedBuffer = await processOgImage(buffer, imageUrl.toString());// Cache the resultawait setToCacheBinary(cacheKey, processedBuffer, 'image/png');- reply.header('Content-Type', 'image/png');+ setImageSecurityHeaders(reply, 'image/png');reply.header('Cache-Control', 'public, max-age=3600, immutable');return reply.send(processedBuffer);} catch (error: any) {+ if (error instanceof BlockedUrlError) {+ logger.warn(+ { url: request.query.url, reason: error.message },+ 'Blocked OG image fetch',+ );+ reply.header('Cache-Control', 'no-store');+ return reply+ .status(400)+ .header('Content-Type', 'text/plain')+ .send('Bad request');+ }+logger.error({ err: error, url: request.query.url },'OG image fetch error',diff --git a/apps/api/src/controllers/tools.controller.ts b/apps/api/src/controllers/tools.controller.tsindex fe4d21c71..752a087c4 100644--- a/apps/api/src/controllers/tools.controller.ts+++ b/apps/api/src/controllers/tools.controller.ts@@ -2,12 +2,20 @@ import * as dns from 'node:dns/promises';import * as net from 'node:net';import * as tls from 'node:tls';import { getClientIpFromHeaders } from '@openpanel/common/server/get-client-ip';+import {+ BlockedUrlError,+ assertPublicHostname,+ assertPublicUrl,+ isBlockedIp,+ safeFetch,+} from '@/utils/safe-fetch';import { getGeoLocation } from '@openpanel/geo';import * as cheerio from 'cheerio';import type { FastifyReply, FastifyRequest } from 'fastify';const TIMEOUT_MS = 10000;const MAX_REDIRECTS = 10;+const MAX_HTML_BYTES = 5_000_000;interface RedirectHop {url: string;@@ -138,24 +146,21 @@ async function checkRobotsTxt(path: string,): Promise<'allowed' | 'blocked' | 'error'> {try {- const robotsUrl = new URL('/robots.txt', baseUrl).toString();- const controller = new AbortController();- const timeout = setTimeout(() => controller.abort(), 5000);+ const robotsUrl = new URL('/robots.txt', baseUrl);- const response = await fetch(robotsUrl, {- signal: controller.signal,+ const response = await safeFetch(robotsUrl, {+ timeoutMs: 5000,+ maxBytes: 1_000_000,headers: {'User-Agent': 'OpenPanel-SiteChecker/1.0',},});- clearTimeout(timeout);-- if (!response.ok) {+ if (response.status < 200 || response.status >= 300) {return 'error';}- const text = await response.text();+ const text = response.body.toString('utf8');const rules = text.split('\n').filter((line) => {const trimmed = line.trim();return trimmed && !trimmed.startsWith('#');@@ -178,19 +183,17 @@ async function checkRobotsTxt(async function checkSitemap(baseUrl: string): Promise<boolean> {try {- const sitemapUrl = new URL('/sitemap.xml', baseUrl).toString();- const controller = new AbortController();- const timeout = setTimeout(() => controller.abort(), 3000);+ const sitemapUrl = new URL('/sitemap.xml', baseUrl);- const response = await fetch(sitemapUrl, {- signal: controller.signal,+ const response = await safeFetch(sitemapUrl, {+ timeoutMs: 3000,+ maxBytes: 1_000_000,headers: {'User-Agent': 'OpenPanel-SiteChecker/1.0',},});- clearTimeout(timeout);- return response.ok;+ return response.status >= 200 && response.status < 300;} catch {return false;}@@ -199,10 +202,19 @@ async function checkSitemap(baseUrl: string): Promise<boolean> {async function getSSLInfo(hostname: string,): Promise<SiteCheckResult['technical']['ssl'] | null> {+ // Raw TLS handshakes are as good a probe as an HTTP request, so the host has+ // to clear the same check, and we connect to the address we validated.+ let address: string;+ try {+ [address] = (await assertPublicHostname(hostname)) as [string];+ } catch {+ return null;+ }+return new Promise((resolve) => {const socket = tls.connect({- host: hostname,+ host: address,port: 443,servername: hostname,rejectUnauthorized: false,@@ -251,7 +263,8 @@ interface IPInfo {}async function getIPInfo(ip: string): Promise<IPInfo> {- if (!ip || ip === '127.0.0.1' || ip === '::1') {+ // Don't hand internal addresses to a third-party lookup service.+ if (!ip || isBlockedIp(ip)) {return { isp: null, asn: null, organization: null };}@@ -295,12 +308,21 @@ async function measureConnectionTime(hostname: string,port: number,): Promise<{ connectTime: number; tlsTime: number }> {+ // Without this check the timing measurement doubles as an internal port+ // scanner, since open and closed ports are trivially distinguishable.+ let address: string;+ try {+ [address] = (await assertPublicHostname(hostname)) as [string];+ } catch {+ return { connectTime: 0, tlsTime: 0 };+ }+return new Promise((resolve) => {const start = Date.now();let connectTime = 0;let tlsTime = 0;- const socket = net.createConnection(port, hostname, () => {+ const socket = net.createConnection(port, address, () => {connectTime = Date.now() - start;if (port === 443) {@@ -372,7 +394,7 @@ async function fetchWithRedirects(: 80;const dnsStart = Date.now();- await dns.resolve4(hostname).catch(() => {});+ await assertPublicHostname(hostname);dnsTime = Date.now() - dnsStart;if (port === 443 || port === 80) {@@ -387,18 +409,19 @@ async function fetchWithRedirects(let firstRequestStartTime = 0;for (let i = 0; i < maxRedirects; i++) {- const controller = new AbortController();- const timeout = setTimeout(() => controller.abort(), TIMEOUT_MS);-try {const hopStartTime = Date.now();if (i === 0) {firstRequestStartTime = hopStartTime;}- const response = await fetch(currentUrl, {- signal: controller.signal,- redirect: 'manual',+ // `followRedirects: false` keeps the hop-by-hop timing this endpoint+ // reports, while still validating and pinning each hop individually —+ // a redirect cannot hand us off to an internal address.+ const response = await safeFetch(currentUrl, {+ followRedirects: false,+ timeoutMs: TIMEOUT_MS,+ maxBytes: MAX_HTML_BYTES,headers: {'User-Agent':'Mozilla/5.0 (compatible; OpenPanel-SiteChecker/1.0; +https://openpanel.dev)',@@ -408,7 +431,6 @@ async function fetchWithRedirects(},});- clearTimeout(timeout);const hopResponseTime = Date.now() - hopStartTime;if (i === 0 && ttfbTime === 0) {@@ -434,12 +456,11 @@ async function fetchWithRedirects(}}- finalHtml = await response.text();+ finalHtml = response.body.toString('utf8');finalHeaders = response.headers;finalStatusCode = response.status;break;} catch (error) {- clearTimeout(timeout);if (error instanceof Error && error.name === 'AbortError') {throw new Error('Request timeout');}@@ -516,6 +537,18 @@ export async function siteChecker(url = new URL(`https://${urlParam}`);}+ // Reject non-public destinations before any outbound connection is made.+ // Each hop and each auxiliary probe re-checks independently, since a+ // hostname can resolve differently between this check and the connection.+ try {+ await assertPublicUrl(url);+ } catch (error) {+ if (error instanceof BlockedUrlError) {+ return reply.status(400).send({ error: 'Invalid URL' });+ }+ throw error;+ }+try {const { finalUrl, redirectChain, html, headers, statusCode, timing } =await fetchWithRedirects(url.toString());@@ -652,6 +685,10 @@ export async function siteChecker(return reply.send(result);} catch (error) {request.log.error({ err: error }, 'Site checker error');+ if (error instanceof BlockedUrlError) {+ return reply.status(400).send({ error: 'Invalid URL' });+ }+return reply.status(500).send({error:error instanceof Error ? error.message : 'Failed to analyze site',diff --git a/apps/api/src/utils/image-proxy.test.ts b/apps/api/src/utils/image-proxy.test.tsnew file mode 100644index 000000000..69ff9cfe8--- /dev/null+++ b/apps/api/src/utils/image-proxy.test.ts@@ -0,0 +1,140 @@+import sharp from 'sharp';+import { describe, expect, it } from 'vitest';+import {+ ALLOWED_IMAGE_CONTENT_TYPES,+ hasIcoMagicBytes,+ normalizeContentType,+ processImage,+ processOgImage,+} from './image-proxy';++const PNG_MAGIC = Buffer.from([0x89, 0x50, 0x4e, 0x47]);++const maliciousSvg = Buffer.from(+ '<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64">' ++ '<rect width="64" height="64" fill="red"/>' ++ '<script>alert(document.domain)</script>' ++ '</svg>',+);++async function makePng() {+ return sharp({+ create: {+ width: 64,+ height: 64,+ channels: 3,+ background: { r: 255, g: 0, b: 0 },+ },+ })+ .png()+ .toBuffer();+}++describe('processImage', () => {+ it('rasterizes SVG instead of serving it verbatim (GHSA-r7hx-q6f4-vj6h)', async () => {+ const result = await processImage(+ maliciousSvg,+ 'https://evil.example/x.svg',+ 'image/svg+xml',+ );++ expect(result.subarray(0, 4)).toEqual(PNG_MAGIC);+ expect(result.includes(Buffer.from('script'))).toBe(false);+ expect(result.includes(Buffer.from('alert'))).toBe(false);+ expect(result.equals(maliciousSvg)).toBe(false);+ });++ it('rasterizes SVG even when only the content type gives it away', async () => {+ const result = await processImage(+ maliciousSvg,+ 'https://evil.example/favicon',+ 'image/svg+xml',+ );++ expect(result.subarray(0, 4)).toEqual(PNG_MAGIC);+ expect(result.includes(Buffer.from('alert'))).toBe(false);+ });++ it('rasterizes small images rather than passing the bytes through', async () => {+ // A sub-5KB body used to be returned verbatim with the upstream content+ // type, which let an attacker serve arbitrary content from the API origin.+ const png = await makePng();+ expect(png.length).toBeLessThan(5000);++ const result = await processImage(+ png,+ 'https://evil.example/small.png',+ 'image/png',+ );++ expect(result.subarray(0, 4)).toEqual(PNG_MAGIC);+ });++ it('rejects a non-image body claiming to be an icon', async () => {+ const html = Buffer.from('<html><script>alert(1)</script></html>');++ // Sharp cannot decode it and the ICO magic bytes are absent, so it must+ // not reach the passthrough.+ expect(hasIcoMagicBytes(html)).toBe(false);+ await expect(+ processImage(html, 'https://evil.example/x.ico', 'image/x-icon'),+ ).rejects.toThrow();+ });++ it('passes a real ICO through untouched', async () => {+ // Minimal ICO header: reserved=0, type=1, count=1+ const ico = Buffer.concat([+ Buffer.from([0x00, 0x00, 0x01, 0x00, 0x01, 0x00]),+ Buffer.alloc(32),+ ]);++ const result = await processImage(+ ico,+ 'https://example.com/favicon.ico',+ 'image/x-icon',+ );++ expect(result).toBe(ico);+ });+});++describe('processOgImage', () => {+ it('rasterizes SVG instead of serving it verbatim', async () => {+ const result = await processOgImage(+ maliciousSvg,+ 'https://evil.example/x.svg',+ );++ expect(result.subarray(0, 4)).toEqual(PNG_MAGIC);+ expect(result.includes(Buffer.from('alert'))).toBe(false);+ });++ it('rasterizes small images rather than passing the bytes through', async () => {+ const png = await makePng();+ expect(png.length).toBeLessThan(10000);++ const result = await processOgImage(png, 'https://example.com/og.png');+ expect(result.subarray(0, 4)).toEqual(PNG_MAGIC);+ });+});++describe('content type handling', () => {+ it('strips parameters and lowercases', () => {+ expect(normalizeContentType('Image/SVG+XML; charset=utf-8')).toBe(+ 'image/svg+xml',+ );+ expect(normalizeContentType(null)).toBe('');+ });++ it('does not allow non-image types through the proxy', () => {+ for (const type of [+ 'text/html',+ 'application/json',+ 'text/plain',+ 'application/octet-stream',+ '',+ ]) {+ expect(ALLOWED_IMAGE_CONTENT_TYPES.has(type), type).toBe(false);+ }+ });+});diff --git a/apps/api/src/utils/image-proxy.ts b/apps/api/src/utils/image-proxy.tsnew file mode 100644index 000000000..6572a0bb8--- /dev/null+++ b/apps/api/src/utils/image-proxy.ts@@ -0,0 +1,129 @@+import { logger } from '@/utils/logger';+import sharp from 'sharp';++/**+ * Image normalization for the favicon/OG proxy.+ *+ * These bytes come from a third-party server and are then served from the API+ * origin, which also serves the credentialed `/trpc` and `/oauth` endpoints.+ * Anything returned verbatim is therefore attacker-controlled content on a+ * trusted origin — an SVG passed through untouched executes its `<script>` as+ * the victim. Everything Sharp can decode is rasterized to PNG, which drops+ * active content along with every other non-pixel payload.+ */++/**+ * Content types we are willing to hand back to a browser. Anything else is+ * dropped rather than proxied.+ */+export const ALLOWED_IMAGE_CONTENT_TYPES = new Set([+ 'image/png',+ 'image/jpeg',+ 'image/jpg',+ 'image/gif',+ 'image/webp',+ 'image/avif',+ 'image/svg+xml',+ 'image/x-icon',+ 'image/vnd.microsoft.icon',+ 'image/bmp',+ 'image/tiff',+]);++export function normalizeContentType(raw: string | null): string {+ return (raw ?? '').split(';')[0]!.trim().toLowerCase();+}++/** Check if URL is an ICO file */+export function isIcoFile(url: string, contentType?: string): boolean {+ return (+ url.toLowerCase().endsWith('.ico') ||+ contentType === 'image/x-icon' ||+ contentType === 'image/vnd.microsoft.icon'+ );+}++/**+ * ICO is the one format Sharp cannot decode, so it is passed through raw.+ * Verify the magic bytes (reserved=0, type=1) so the passthrough cannot be+ * used to serve something that merely claims to be an icon.+ */+export function hasIcoMagicBytes(buffer: Buffer): boolean {+ return (+ buffer.length >= 4 &&+ buffer[0] === 0x00 &&+ buffer[1] === 0x00 &&+ buffer[2] === 0x01 &&+ buffer[3] === 0x00+ );+}++/** Process image with Sharp (resize to 30x30 PNG) */+export async function processImage(+ buffer: Buffer,+ originalUrl?: string,+ contentType?: string,+): Promise<Buffer> {+ // ICO is the only format Sharp cannot decode, so it is the only passthrough.+ // Everything else — SVG very much included — is rasterized.+ if (+ originalUrl &&+ isIcoFile(originalUrl, contentType) &&+ hasIcoMagicBytes(buffer)+ ) {+ logger.debug(+ { originalUrl, bufferSize: buffer.length },+ 'Serving ICO file directly',+ );+ return buffer;+ }++ try {+ return await sharp(buffer)+ .resize(30, 30, {+ fit: 'cover',+ })+ .png()+ .toBuffer();+ } catch (error) {+ logger.warn(+ {+ err: error,+ originalUrl,+ bufferSize: buffer.length,+ },+ 'Sharp failed to process image',+ );++ throw error;+ }+}++/** Process OG image with Sharp (resize to 300px width) */+export async function processOgImage(+ buffer: Buffer,+ originalUrl?: string,+): Promise<Buffer> {+ // Always rasterize. Returning the upstream bytes verbatim would let an+ // attacker serve arbitrary content from the API origin.+ try {+ return await sharp(buffer)+ .resize(300, null, {+ fit: 'inside',+ withoutEnlargement: true,+ })+ .png()+ .toBuffer();+ } catch (error) {+ logger.warn(+ {+ err: error,+ originalUrl,+ bufferSize: buffer.length,+ },+ 'Sharp failed to process OG image',+ );++ throw error;+ }+}diff --git a/apps/api/src/utils/safe-fetch.test.ts b/apps/api/src/utils/safe-fetch.test.tsnew file mode 100644index 000000000..c04951dfa--- /dev/null+++ b/apps/api/src/utils/safe-fetch.test.ts@@ -0,0 +1,115 @@+import http from 'node:http';+import type { AddressInfo } from 'node:net';+import { afterAll, beforeAll, describe, expect, it } from 'vitest';+import { BlockedUrlError, isBlockedIp, safeFetch } from './safe-fetch';++describe('isBlockedIp', () => {+ it('blocks the ranges an SSRF payload aims at', () => {+ const blocked = [+ '169.254.169.254', // AWS/GCP/Azure instance metadata+ '127.0.0.1',+ '127.1.2.3',+ '0.0.0.0',+ '10.0.0.1',+ '172.16.0.1',+ '172.31.255.255',+ '192.168.1.1',+ '100.64.0.1', // CGNAT+ '198.18.0.1', // benchmarking+ '224.0.0.1', // multicast+ '255.255.255.255',+ '::1',+ '::',+ 'fd00::1', // unique local+ 'fe80::1', // link local+ 'ff00::1', // multicast+ '::ffff:127.0.0.1', // IPv4-mapped loopback+ '::ffff:169.254.169.254', // IPv4-mapped metadata+ '2002:7f00:1::', // 6to4 wrapping 127.0.0.1+ '64:ff9b::7f00:1', // NAT64 wrapping 127.0.0.1+ 'not-an-ip',+ ];++ for (const ip of blocked) {+ expect(isBlockedIp(ip), ip).toBe(true);+ }+ });++ it('allows public addresses', () => {+ for (const ip of ['8.8.8.8', '1.1.1.1', '93.184.216.34', '2606:4700::1']) {+ expect(isBlockedIp(ip), ip).toBe(false);+ }+ });+});++describe('safeFetch', () => {+ let secretServer: http.Server;+ let redirectServer: http.Server;+ let secretPort: number;+ let redirectPort: number;++ beforeAll(async () => {+ secretServer = http.createServer((_req, res) => {+ res.writeHead(200, { 'content-type': 'text/plain' });+ res.end('INTERNAL-SECRET');+ });+ await new Promise<void>((resolve) =>+ secretServer.listen(0, '127.0.0.1', resolve),+ );+ secretPort = (secretServer.address() as AddressInfo).port;++ redirectServer = http.createServer((_req, res) => {+ res.writeHead(302, {+ location: `http://127.0.0.1:${secretPort}/secret.png`,+ });+ res.end();+ });+ await new Promise<void>((resolve) =>+ redirectServer.listen(0, '127.0.0.1', resolve),+ );+ redirectPort = (redirectServer.address() as AddressInfo).port;+ });++ afterAll(async () => {+ await new Promise<void>((resolve) => secretServer.close(() => resolve()));+ await new Promise<void>((resolve) => redirectServer.close(() => resolve()));+ });++ it('refuses to fetch a loopback address directly', async () => {+ await expect(+ safeFetch(`http://127.0.0.1:${secretPort}/secret.png`, {+ timeoutMs: 3000,+ }),+ ).rejects.toBeInstanceOf(BlockedUrlError);+ });++ it('refuses to fetch a hostname that resolves to loopback', async () => {+ await expect(+ safeFetch(`http://localhost:${secretPort}/secret.png`, {+ timeoutMs: 3000,+ }),+ ).rejects.toBeInstanceOf(BlockedUrlError);+ });++ it('refuses a redirect that points at an internal address', async () => {+ // The reachability gate on the caller side only inspects the first URL,+ // so the redirect hop has to be re-validated or the guard is bypassable.+ await expect(+ safeFetch(`http://127.0.0.1:${redirectPort}/x.png`, { timeoutMs: 3000 }),+ ).rejects.toBeInstanceOf(BlockedUrlError);+ });++ it('refuses cloud instance metadata', async () => {+ await expect(+ safeFetch('http://169.254.169.254/latest/meta-data/', {+ timeoutMs: 3000,+ }),+ ).rejects.toBeInstanceOf(BlockedUrlError);+ });++ it('refuses non-http schemes', async () => {+ await expect(safeFetch('file:///etc/passwd')).rejects.toBeInstanceOf(+ BlockedUrlError,+ );+ });+});diff --git a/apps/api/src/utils/safe-fetch.ts b/apps/api/src/utils/safe-fetch.tsnew file mode 100644index 000000000..8e3019866--- /dev/null+++ b/apps/api/src/utils/safe-fetch.ts@@ -0,0 +1,305 @@+import dns from 'node:dns/promises';+import net from 'node:net';+import ipaddr from 'ipaddr.js';+import { Agent, fetch as undiciFetch } from 'undici';++/**+ * SSRF guard for the endpoints that fetch user-supplied URLs (the favicon/OG+ * proxy and the public site checker).+ *+ * Two things have to be true for the guard to hold:+ *+ * 1. Every address a hostname resolves to must be publicly routable. Checking+ * only the first A record lets an attacker publish a second AAAA record.+ * 2. The socket must connect to the address we validated. Resolving and then+ * handing the hostname to `fetch` re-resolves it, so a DNS rebind between+ * the two lookups reaches the internal target anyway. We pin the validated+ * address into the connection instead.+ *+ * Redirects are followed manually so every hop goes through the same checks.+ */++export class BlockedUrlError extends Error {+ constructor(message: string) {+ super(message);+ this.name = 'BlockedUrlError';+ }+}++/**+ * ipaddr.js range names that are not publicly routable. `unicast` is the only+ * range we accept; this list is spelled out so the intent is reviewable.+ */+const BLOCKED_RANGES = new Set([+ 'unspecified', // 0.0.0.0/8, ::+ 'broadcast', // 255.255.255.255+ 'multicast', // 224.0.0.0/4, ff00::/8+ 'linkLocal', // 169.254.0.0/16 (cloud metadata), fe80::/10+ 'loopback', // 127.0.0.0/8, ::1+ 'private', // 10/8, 172.16/12, 192.168/16+ 'uniqueLocal', // fc00::/7+ 'carrierGradeNat', // 100.64.0.0/10+ 'reserved', // 192.0.0.0/24, 198.18/15, 240/4, 2001::/32, ...+ 'benchmarking',+ 'as112',+ 'amt',+ 'rfc6052', // 64:ff9b::/96+ 'rfc6145',+ 'teredo',+ '6to4', // 2002::/16+]);++export function isBlockedIp(ip: string): boolean {+ let parsed: ipaddr.IPv4 | ipaddr.IPv6;+ try {+ parsed = ipaddr.parse(ip);+ } catch {+ // Unparseable means we cannot prove it is safe.+ return true;+ }++ // ::ffff:127.0.0.1 and friends must be judged as the IPv4 address they carry.+ if (parsed.kind() === 'ipv6') {+ const v6 = parsed as ipaddr.IPv6;+ if (v6.isIPv4MappedAddress()) {+ return isBlockedIp(v6.toIPv4Address().toString());+ }+ }++ return BLOCKED_RANGES.has(parsed.range());+}++/**+ * Resolve a hostname, rejecting it entirely if any address is non-public.+ */+async function resolvePublicAddresses(hostname: string): Promise<string[]> {+ // WHATWG URL keeps the brackets around IPv6 literals ("[::1]").+ const host =+ hostname.startsWith('[') && hostname.endsWith(']')+ ? hostname.slice(1, -1)+ : hostname;++ // A bare IP in the URL never hits DNS, so check it directly.+ if (net.isIP(host)) {+ if (isBlockedIp(host)) {+ throw new BlockedUrlError(`Refusing to connect to ${host}`);+ }+ return [host];+ }++ let addresses: { address: string }[];+ try {+ // `lookup` rather than `resolve4`/`resolve6` so /etc/hosts and the system+ // resolver are honoured the same way the real connection would.+ addresses = await dns.lookup(host, { all: true, verbatim: true });+ } catch {+ throw new BlockedUrlError(`Could not resolve ${host}`);+ }++ if (addresses.length === 0) {+ throw new BlockedUrlError(`Could not resolve ${host}`);+ }++ for (const { address } of addresses) {+ if (isBlockedIp(address)) {+ throw new BlockedUrlError(+ `Refusing to connect to ${host} (resolves to a non-public address)`,+ );+ }+ }++ return addresses.map((entry) => entry.address);+}++/**+ * Validate a URL's scheme and destination without fetching it. Use this to+ * guard non-fetch outbound connections (raw TCP, TLS handshakes).+ * Throws {@link BlockedUrlError} when the URL must not be requested.+ */+export async function assertPublicUrl(url: URL): Promise<string[]> {+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {+ throw new BlockedUrlError('Only http/https URLs are allowed');+ }+ return resolvePublicAddresses(url.hostname);+}++/** Validate a bare hostname (no URL available), e.g. before a TLS probe. */+export async function assertPublicHostname(+ hostname: string,+): Promise<string[]> {+ return resolvePublicAddresses(hostname);+}++/**+ * A dispatcher that only ever connects to `address`, so the socket cannot end+ * up somewhere other than the address we just validated.+ */+function createPinnedAgent(address: string): Agent {+ const family = net.isIPv6(address) ? 6 : 4;+ return new Agent({+ connect: {+ lookup: (_hostname, options, callback) => {+ if (options.all) {+ (callback as unknown as (+ err: null,+ addresses: { address: string; family: number }[],+ ) => void)(null, [{ address, family }]);+ return;+ }+ callback(null, address, family);+ },+ },+ });+}++export interface SafeFetchOptions {+ headers?: Record<string, string>;+ method?: string;+ /** Abort the whole chain after this many milliseconds. */+ timeoutMs?: number;+ maxRedirects?: number;+ /**+ * When false, a 3xx is returned as-is instead of being followed. Callers+ * that walk the chain themselves still get per-hop validation and pinning.+ */+ followRedirects?: boolean;+ /** Reject responses whose body exceeds this many bytes. */+ maxBytes?: number;+}++export interface SafeFetchResult {+ status: number;+ headers: Headers;+ body: Buffer;+ finalUrl: string;+ /** Every URL that was requested, in order, with the status it returned. */+ chain: { url: string; status: number }[];+}++const DEFAULT_MAX_REDIRECTS = 5;+const DEFAULT_TIMEOUT_MS = 10_000;+const DEFAULT_MAX_BYTES = 5_000_000;+const REDIRECT_STATUS = new Set([301, 302, 303, 307, 308]);++async function readBodyWithLimit(+ body: ReadableStream<Uint8Array> | null,+ maxBytes: number,+): Promise<Buffer> {+ if (!body) {+ return Buffer.alloc(0);+ }++ const chunks: Uint8Array[] = [];+ let total = 0;+ const reader = body.getReader();++ try {+ while (true) {+ const { done, value } = await reader.read();+ if (done) {+ break;+ }+ if (!value) {+ continue;+ }+ total += value.byteLength;+ if (total > maxBytes) {+ throw new BlockedUrlError('Response exceeded the size limit');+ }+ chunks.push(value);+ }+ } finally {+ reader.releaseLock();+ await body.cancel().catch(() => {+ // best effort+ });+ }++ return Buffer.concat(chunks);+}++/**+ * `fetch` with SSRF protection: every hop is resolved, checked and pinned to+ * the validated address, and the body is read under a size cap.+ * Throws {@link BlockedUrlError} if any hop points somewhere non-public.+ */+export async function safeFetch(+ input: string | URL,+ options: SafeFetchOptions = {},+): Promise<SafeFetchResult> {+ const followRedirects = options.followRedirects ?? true;+ const maxRedirects = followRedirects+ ? (options.maxRedirects ?? DEFAULT_MAX_REDIRECTS)+ : 0;+ const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;+ const chain: { url: string; status: number }[] = [];++ const controller = new AbortController();+ const timeout = setTimeout(+ () => controller.abort(),+ options.timeoutMs ?? DEFAULT_TIMEOUT_MS,+ );++ try {+ let current = input instanceof URL ? input : new URL(input);++ for (let hop = 0; ; hop++) {+ const [address] = await assertPublicUrl(current);+ const agent = createPinnedAgent(address!);++ let status: number;+ let headers: Headers;+ let body: Buffer;+ let location: string | null;++ try {+ const response = await undiciFetch(current, {+ method: options.method ?? 'GET',+ headers: options.headers,+ signal: controller.signal,+ // Handled below so every hop is re-validated.+ redirect: 'manual',+ dispatcher: agent,+ });++ status = response.status;+ headers = response.headers as unknown as Headers;+ location = response.headers.get('location');++ if (followRedirects && REDIRECT_STATUS.has(status) && location) {+ await response.body?.cancel().catch(() => {+ // best effort+ });+ body = Buffer.alloc(0);+ } else {+ body = await readBodyWithLimit(+ response.body as ReadableStream<Uint8Array> | null,+ maxBytes,+ );+ }+ } finally {+ await agent.close().catch(() => {+ // best effort+ });+ }++ chain.push({ url: current.toString(), status });++ if (!(followRedirects && REDIRECT_STATUS.has(status) && location)) {+ return { status, headers, body, finalUrl: current.toString(), chain };+ }++ if (hop >= maxRedirects) {+ throw new BlockedUrlError('Too many redirects');+ }++ try {+ current = new URL(location, current);+ } catch {+ throw new BlockedUrlError('Invalid redirect location');+ }+ }+ } finally {+ clearTimeout(timeout);+ }+}diff --git a/packages/db/src/engine/compute.ts b/packages/db/src/engine/compute.tsindex db60c79fd..4fc53452f 100644--- a/packages/db/src/engine/compute.ts+++ b/packages/db/src/engine/compute.ts@@ -1,7 +1,7 @@import { round } from '@openpanel/common';import { alphabetIds } from '@openpanel/constants';import type { IChartFormula } from '@openpanel/validation';-import * as mathjs from 'mathjs';+import { evaluateFormula } from './formula';import type { ConcreteSeries } from './types';/**@@ -110,19 +110,12 @@ export function compute(});// Evaluate formula for total_count- let formulaTotalCount: number | undefined;- try {- const result = mathjs- .parse(formula.formula)- .compile()- .evaluate(totalCountScope) as number;- formulaTotalCount =- Number.isNaN(result) || !Number.isFinite(result)- ? undefined- : round(result, 2);- } catch (error) {- formulaTotalCount = undefined;- }+ const totalCountResult = evaluateFormula(+ formula.formula,+ totalCountScope,+ );+ const formulaTotalCount =+ totalCountResult === undefined ? undefined : round(totalCountResult, 2);// Calculate formula for each dateconst formulaData = sortedDates.map((date) => {@@ -160,22 +153,11 @@ export function compute(});// Evaluate formula- let count: number;- try {- count = mathjs- .parse(formula.formula)- .compile()- .evaluate(scope) as number;- } catch (error) {- count = 0;- }+ const result = evaluateFormula(formula.formula, scope);return {date,- count:- Number.isNaN(count) || !Number.isFinite(count)- ? 0- : round(count, 2),+ count: result === undefined ? 0 : round(result, 2),total_count: formulaTotalCount,};});diff --git a/packages/db/src/engine/formula.test.ts b/packages/db/src/engine/formula.test.tsnew file mode 100644index 000000000..5a11c413d--- /dev/null+++ b/packages/db/src/engine/formula.test.ts@@ -0,0 +1,123 @@+import { describe, expect, it } from 'vitest';+import { evaluateFormula, isValidFormula } from './formula';++const scope = { A: 10, B: 4, C: 0 };++describe('evaluateFormula', () => {+ it('evaluates the arithmetic the chart UI advertises', () => {+ expect(evaluateFormula('A+B', scope)).toBe(14);+ expect(evaluateFormula('A-B', scope)).toBe(6);+ expect(evaluateFormula('A*B', scope)).toBe(40);+ expect(evaluateFormula('A/B', scope)).toBe(2.5);+ expect(evaluateFormula('A%B', scope)).toBe(2);+ expect(evaluateFormula('B^2', scope)).toBe(16);+ });++ it('supports parentheses, unary signs and numeric constants', () => {+ expect(evaluateFormula('(A-B)*100', scope)).toBe(600);+ expect(evaluateFormula('-A', scope)).toBe(-10);+ expect(evaluateFormula('+A', scope)).toBe(10);+ expect(evaluateFormula('A/B*100', scope)).toBe(250);+ expect(evaluateFormula('1.5*A', scope)).toBe(15);+ });++ it('supports the numeric helpers saved reports may already use', () => {+ expect(evaluateFormula('round(A/B, 1)', scope)).toBe(2.5);+ expect(evaluateFormula('abs(B-A)', scope)).toBe(6);+ expect(evaluateFormula('max(A, B)', scope)).toBe(10);+ expect(evaluateFormula('min(A, B)', scope)).toBe(4);+ expect(evaluateFormula('floor(A/B)', scope)).toBe(2);+ expect(evaluateFormula('ceil(A/B)', scope)).toBe(3);+ expect(evaluateFormula('sqrt(B)', scope)).toBe(2);+ });++ it('returns undefined for non-finite results instead of throwing', () => {+ expect(evaluateFormula('A/C', scope)).toBeUndefined();+ expect(evaluateFormula('C/C', scope)).toBeUndefined();+ });++ it('caches compiled formulas without leaking scope between evaluations', () => {+ expect(evaluateFormula('A+B', { A: 1, B: 2 })).toBe(3);+ expect(evaluateFormula('A+B', { A: 10, B: 20 })).toBe(30);+ });+});++describe('isValidFormula', () => {+ it('accepts plain arithmetic', () => {+ for (const formula of ['A+B', 'A/B', '(A-B)*100', '-A', 'A%B', 'B^2']) {+ expect(isValidFormula(formula)).toBe(true);+ }+ });++ it('rejects the mathjs constructor-recovery payload (GHSA-7476-c5cc-8999)', () => {+ // Verbatim from the advisory: recovers the native Function constructor via+ // Index.toJSON() dimensions + DenseMatrix.subset() callbacks.+ const payload = `m = matrix([[0,0],[0,0]]);+idx = index([0,1],[0,1]);+dims = idx.toJSON()["dimensions"];+d1 = {+ "size": s1() = matrix([1]).toArray(),+ "max": x1() = 0,+ "forEach": e1(callback) = callback(0, matrix(["flatMap"]).toArray())+};+d2 = {+ "size": s2() = matrix([1]).toArray(),+ "max": x2() = 0,+ "forEach": e2(callback) = callback(0, matrix(["constructor"]).toArray())+};+dims.splice(0, 1, d1);+dims.splice(1, 1, d2);+m.subset(idx, [[0]]);+FunctionCtor = m.get(matrix([0,0]).toArray());+run = FunctionCtor(+ "return process.getBuiltinModule('node:child_process')" ++ ".execFileSync('/usr/bin/id').toString()"+);+run()`;++ expect(isValidFormula(payload)).toBe(false);+ expect(evaluateFormula(payload, scope)).toBeUndefined();+ });++ it('rejects every node type outside plain arithmetic', () => {+ const rejected = [+ 'A; B', // BlockNode+ 'x = A', // AssignmentNode+ 'f(x) = x', // FunctionAssignmentNode+ 'config({})', // FunctionNode, not in the allowlist+ 'matrix([[0,0],[0,0]])', // FunctionNode / ArrayNode+ 'index([0,1],[0,1])', // FunctionNode, not in the allowlist+ 'A.subset(B)', // AccessorNode — blocks every method call+ '{"a": 1}', // ObjectNode+ '[1, 2, 3]', // ArrayNode+ 'A[1]', // IndexNode / AccessorNode+ 'A.constructor', // AccessorNode+ '1:5', // RangeNode+ 'A > B ? A : B', // ConditionalNode+ 'A!', // factorial OperatorNode+ 'A and B', // logical OperatorNode+ ];++ for (const formula of rejected) {+ expect(isValidFormula(formula), formula).toBe(false);+ }+ });++ it('rejects symbols that are neither series references nor allowed functions', () => {+ expect(isValidFormula('process')).toBe(false);+ expect(isValidFormula('pi')).toBe(false);+ expect(isValidFormula('A + e')).toBe(false);+ expect(isValidFormula('matrix')).toBe(false);+ });++ it('discards a bare function reference as a non-numeric result', () => {+ // `round` passes the AST check as a callee name, but on its own it+ // evaluates to a function object rather than a number.+ expect(evaluateFormula('round', scope)).toBeUndefined();+ });++ it('rejects unparseable and oversized input', () => {+ expect(isValidFormula('A +')).toBe(false);+ expect(isValidFormula('A'.repeat(1001))).toBe(false);+ });+});diff --git a/packages/db/src/engine/formula.ts b/packages/db/src/engine/formula.tsnew file mode 100644index 000000000..e56a86a9f--- /dev/null+++ b/packages/db/src/engine/formula.ts@@ -0,0 +1,190 @@+import { alphabetIds } from '@openpanel/constants';+import * as mathjs from 'mathjs';++/**+ * Chart formulas are a tiny arithmetic language, not a general mathjs program.+ *+ * mathjs' default namespace is powerful enough to recover the native `Function`+ * constructor (via matrix/index objects and their allowed methods), which turns+ * a user-supplied formula into remote code execution inside the API process.+ * Removing individual functions does not close this off, so we validate the+ * parsed AST against an allowlist and reject anything that is not plain+ * arithmetic over the series symbols before the expression is ever compiled.+ */++/** Operators that map to the arithmetic OpenPanel actually offers. */+const ALLOWED_OPERATOR_FUNCTIONS = new Set([+ 'add',+ 'subtract',+ 'multiply',+ 'divide',+ 'mod',+ 'pow',+ 'unaryMinus',+ 'unaryPlus',+]);++/** Series are referenced by their alphabet id (A, B, C, ...). */+const ALLOWED_SYMBOLS = new Set<string>(alphabetIds);++/**+ * Numeric helpers that saved reports may already use. Method calls are not+ * reachable (AccessorNode is rejected), so a call can only ever resolve to one+ * of these names — `matrix`, `index` and friends stay out of reach.+ */+const ALLOWED_FUNCTIONS = new Set([+ 'abs',+ 'ceil',+ 'floor',+ 'round',+ 'sqrt',+ 'min',+ 'max',+ 'log',+ 'log10',+ 'exp',+]);++const MAX_FORMULA_LENGTH = 1000;++export class InvalidFormulaError extends Error {+ constructor(reason: string) {+ super(`Invalid formula: ${reason}`);+ this.name = 'InvalidFormulaError';+ }+}++function assertAllowedNode(node: mathjs.MathNode): void {+ switch (node.type) {+ case 'ConstantNode': {+ const { value } = node as mathjs.ConstantNode;+ if (typeof value !== 'number' || !Number.isFinite(value)) {+ throw new InvalidFormulaError('only finite numeric constants are allowed');+ }+ break;+ }++ case 'SymbolNode': {+ // A function call's callee is itself a SymbolNode, so allowed function+ // names have to pass here too. A bare `round` evaluates to a function+ // object, which `evaluateFormula` discards as non-numeric.+ const { name } = node as mathjs.SymbolNode;+ if (!(ALLOWED_SYMBOLS.has(name) || ALLOWED_FUNCTIONS.has(name))) {+ throw new InvalidFormulaError(`unknown series reference "${name}"`);+ }+ break;+ }++ case 'FunctionNode': {+ const { fn } = node as mathjs.FunctionNode;+ const name = typeof fn === 'string' ? fn : fn?.name;+ if (!(name && ALLOWED_FUNCTIONS.has(name))) {+ throw new InvalidFormulaError(`function "${name}" is not allowed`);+ }+ break;+ }++ case 'ParenthesisNode':+ break;++ case 'OperatorNode': {+ const { fn } = node as mathjs.OperatorNode;+ if (!ALLOWED_OPERATOR_FUNCTIONS.has(fn)) {+ throw new InvalidFormulaError(`operator "${fn}" is not allowed`);+ }+ break;+ }++ default:+ throw new InvalidFormulaError(`${node.type} is not allowed`);+ }++}++/**+ * Parse and validate a formula, returning a compiled expression.+ * Throws {@link InvalidFormulaError} for anything outside plain arithmetic.+ */+function compileFormula(formula: string): mathjs.EvalFunction {+ if (formula.length > MAX_FORMULA_LENGTH) {+ throw new InvalidFormulaError('formula is too long');+ }++ let node: mathjs.MathNode;+ try {+ node = mathjs.parse(formula);+ } catch {+ throw new InvalidFormulaError('could not be parsed');+ }++ // `traverse` visits the root and every descendant, so the allowlist below is+ // applied to the whole expression tree rather than a hand-rolled walk.+ node.traverse(assertAllowedNode);++ return node.compile();+}++/**+ * Formulas are evaluated once per date per series, so the same expression is+ * compiled over and over. Cache the validated, compiled form by source string.+ */+const MAX_CACHE_ENTRIES = 500;+const compiledCache = new Map<string, mathjs.EvalFunction>();++function getCompiledFormula(formula: string): mathjs.EvalFunction {+ const cached = compiledCache.get(formula);+ if (cached) {+ // Refresh recency so hot formulas survive eviction.+ compiledCache.delete(formula);+ compiledCache.set(formula, cached);+ return cached;+ }++ const compiled = compileFormula(formula);++ if (compiledCache.size >= MAX_CACHE_ENTRIES) {+ const oldest = compiledCache.keys().next().value;+ if (oldest !== undefined) {+ compiledCache.delete(oldest);+ }+ }+ compiledCache.set(formula, compiled);++ return compiled;+}++/**+ * Returns true when the formula is plain arithmetic over series references.+ * Used to reject hostile formulas at the API boundary before they are stored.+ */+export function isValidFormula(formula: string): boolean {+ try {+ compileFormula(formula);+ return true;+ } catch {+ return false;+ }+}++/**+ * Evaluate a chart formula against a numeric scope.+ * Returns undefined when the formula is invalid or does not produce a finite+ * number, so callers can fall back without distinguishing the two.+ */+export function evaluateFormula(+ formula: string,+ scope: Record<string, number>,+): number | undefined {+ let result: unknown;+ try {+ result = getCompiledFormula(formula).evaluate(scope);+ } catch {+ return undefined;+ }++ if (typeof result !== 'number' || !Number.isFinite(result)) {+ return undefined;+ }++ return result;+}diff --git a/packages/trpc/src/routers/report.ts b/packages/trpc/src/routers/report.tsindex a876a4c2f..90394541f 100644--- a/packages/trpc/src/routers/report.ts+++ b/packages/trpc/src/routers/report.ts@@ -290,10 +290,19 @@ export const reportRouter = createTRPCRouter({throw new TRPCForbiddenError('You do not have access to this project');}+ // The access check above only proves the caller owns `projectId`. Bind+ // the caller-supplied `dashboardId` to that project as well, otherwise a+ // dashboard from another organization can be read through this handler.+ const dashboard = await getDashboardById(dashboardId, projectId);+ if (!dashboard) {+ throw new TRPCNotFoundError('Dashboard not found');+ }+return db.reportLayout.findMany({where: {report: {dashboardId: dashboardId,+ projectId,},},include: {@@ -318,11 +327,19 @@ export const reportRouter = createTRPCRouter({throw new TRPCForbiddenError('You do not have access to this project');}+ // Same as `getLayouts`: bind the dashboard to the access-checked project+ // before deleting anything, so a foreign dashboard cannot be wiped.+ const dashboard = await getDashboardById(dashboardId, projectId);+ if (!dashboard) {+ throw new TRPCNotFoundError('Dashboard not found');+ }+// Delete all layout data for reports in this dashboardreturn db.reportLayout.deleteMany({where: {report: {dashboardId: dashboardId,+ projectId,},},});diff --git a/packages/validation/src/index.ts b/packages/validation/src/index.tsindex bd1c7a45e..03f85682b 100644--- a/packages/validation/src/index.ts+++ b/packages/validation/src/index.ts@@ -11,6 +11,14 @@ import {timeWindows,} from '@openpanel/constants';+/**+ * Chart formulas are plain arithmetic over series references (A, B, C, ...).+ * The API validates the parsed expression tree as well; this charset guard+ * rejects the obviously hostile shapes (assignment, indexing, object/array+ * literals, statement separators) at the edge, in the browser and on the API.+ */+const CHART_FORMULA_PATTERN = /^[A-Za-z0-9_ .,+\-*/()%^]*$/;+export function objectToZodEnums<K extends string>(obj: Record<K, any>,): [K, ...K[]] {@@ -107,7 +115,14 @@ export const zChartFormula = z.object({.optional().describe('Unique identifier for the formula configuration'),type: z.literal('formula'),- formula: z.string().describe('The formula expression (e.g., A+B, A/B)'),+ formula: z+ .string()+ .max(1000)+ .regex(+ CHART_FORMULA_PATTERN,+ 'Formula may only contain series references, numbers and arithmetic operators',+ )+ .describe('The formula expression (e.g., A+B, A/B)'),displayName: z.string().optional()diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex d0048263b..6fa0eb71c 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -223,6 +223,9 @@ importers:groupmq:specifier: 'catalog:'version: 2.0.0-next.6([email protected])+ ipaddr.js:+ specifier: ^2.5.0+ version: 2.5.0jsonwebtoken:specifier: ^9.0.2version: 9.0.2@@ -250,6 +253,9 @@ importers:svix:specifier: ^1.24.0version: 1.24.0+ undici:+ specifier: ^8.10.0+ version: 8.10.0url-metadata:specifier: ^5.4.1version: 5.4.1@@ -13609,8 +13615,8 @@ packages:resolution: {integrity: sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==}engines: {node: '>= 0.10'}- resolution: {integrity: sha512-Ag3wB2o37wslZS19hZqorUnrnzSkpOVy+IiiDEiTqNubEYpYuHWIf6K4psgN2ZWKExS4xhVCrRVfb/wfW8fWJA==}+ resolution: {integrity: sha512-aq+t5NAc+cS6rZQQVWC2x98CPqGtKKTMDd4Gaodv0wShnItdKg/51djkGJ1hqH+Oy0ivDftCbSLCQob8zso01w==}engines: {node: '>= 10'}@@ -18515,10 +18521,6 @@ packages:resolution: {integrity: sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==}- resolution: {integrity: sha512-7oZJCPvvMvTd0OlqWsIxTuItTpJBpU1tcbVl24FMn3xt3+VSunwUasmfPJRE57oNO1KsZ4PgA1xTdAX4hq8NyQ==}- engines: {node: '>=20.18.1'}-resolution: {integrity: sha512-y+8YjDFzWdQlSE9N5nzKMT3g4a5UBX1HKowfdXh0uvAnTaqqwqB92Jt4UXBAeKekDs5IaDKyJFR4X1gYVCgXcw==}engines: {node: '>=20.18.1'}@@ -18527,6 +18529,10 @@ packages:resolution: {integrity: sha512-6KQ/+QxK49Z/p3HO6E5ZCZWNnCasyZLa5ExaVYyvPxUwKtbCPMKELJOqh7EqOle0t9cH/7d2TaaTRRa6Nhs4YQ==}engines: {node: '>=20.18.1'}+ resolution: {integrity: sha512-HvltHd7avK13QIw/oLe4qoOLyoVSoafqJ2jYOrtMRBkbYT31eiBQ8O0ehRKZiEZCMEyLFQNIADpgCWC5fALvYQ==}+ engines: {node: '>=22.19.0'}+resolution: {integrity: sha512-i7qRCmY42zmCwnYlh9H2SvLEypEFGye5iRmEMKjcGi7zk9UquigRjFtTLz0TYqr0ZGLZhaMHl/foy1bZR+Cwlw==}@@ -24444,7 +24450,7 @@ snapshots:'@fastify/[email protected]':dependencies:'@fastify/forwarded': 3.0.0- ipaddr.js: 2.2.0+ ipaddr.js: 2.5.0'@fastify/[email protected]':dependencies:@@ -32096,7 +32102,7 @@ snapshots:parse5: 7.3.0parse5-htmlparser2-tree-adapter: 7.1.0parse5-parser-stream: 7.1.2- undici: 7.15.0+ undici: 7.24.8whatwg-mimetype: 4.0.0@@ -35459,7 +35465,7 @@ snapshots:- [email protected]: {}+ [email protected]: {}@@ -39894,7 +39900,7 @@ snapshots:dependencies:- ipaddr.js: 2.2.0+ ipaddr.js: 2.5.0@@ -41641,12 +41647,12 @@ snapshots:- [email protected]: {}-+ [email protected]: {}+dependencies:pathe: 2.0.3