🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(url): support AWS S3 signed images in trace/observation preview (#4575)
marliessophie5be98906e30a
diff --git a/web/src/server/api/routers/utilities.ts b/web/src/server/api/routers/utilities.tsindex 07adaa4edd..bef2dc46a9 100644--- a/web/src/server/api/routers/utilities.ts+++ b/web/src/server/api/routers/utilities.ts@@ -2,6 +2,7 @@ import { createTRPCRouter, protectedProcedure } from "@/src/server/api/trpc";import { z } from "zod";import { promises as dns } from "dns";import { Address4, Address6 } from "ip-address";+import { logger } from "@langfuse/shared/src/server";const IP_4_LOOPBACK_SUBNET = "127.0.0.0/8";const IP_4_LINK_LOCAL_SUBNET = "169.254.0.0/16";@@ -38,7 +39,7 @@ const isPrivateIp = (ipAddress: string): boolean => {}return false;} catch (error) {- console.error("IP parsing error:", error);+ logger.info("IP parsing error:", error);return false;}};@@ -52,13 +53,13 @@ const resolveHostname = async (try {addresses4 = await dns.resolve4(hostname);} catch (error) {- console.log("IPv4 DNS resolution error:", error);+ logger.info("IPv4 DNS resolution error:", error);}try {addresses6 = await dns.resolve6(hostname);} catch (error) {- console.log("IPv6 DNS resolution error:", error);+ logger.info("IPv6 DNS resolution error:", error);}return { addresses4, addresses6 };@@ -81,26 +82,115 @@ const isValidAndSecureUrl = async (urlString: string): Promise<boolean> => {ipAddresses.addresses6.every((ip) => !isPrivateIp(ip))));} catch (error) {- console.error("Invalid URL:", error);+ logger.info("Invalid URL:", error);+ return false;+ }+};++// Define a Zod schema for pre-signed S3 URL validation, based on https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html; 04.12.24+const s3UrlSchema = z.object({+ hostname: z+ .string()+ .refine((hostname) => hostname.endsWith(".amazonaws.com"), {+ message: "Invalid hostname. Must be an S3 URL.",+ }),+ path: z.string(),+ query: z.object({+ "X-Amz-Algorithm": z.string().optional(),+ "X-Amz-Credential": z.string().optional(),+ "X-Amz-Date": z.string().optional(),+ "X-Amz-Expires": z.string().optional(),+ "X-Amz-SignedHeaders": z.string().optional(),+ "X-Amz-Signature": z.string(),+ "X-Amz-Security-Token": z.string().optional(),+ "x-id": z.string().optional(), // Optional ID parameter, not documented in AWS docs as it is SDK specific, https://github.com/aws/aws-sdk-go-v2/blob/04e7aca073a0a7ed479aa37cad88a1cf58a979a1/service/s3/internal/customizations/presign_test.go#L35-L41+ }),+});++const isImageContent = (response: Response): boolean => {+ const contentType = response.headers.get("content-type");+ return !!contentType && contentType.startsWith("image/");+};++/**+ * Validate if a URL is a valid and live pre-signed S3 URL+ * @param url The pre-signed S3 URL to validate+ * @returns True if the URL is valid and reachable, false otherwise+ */+const isValidPresignedS3Url = async (url: string): Promise<boolean> => {+ try {+ const parsedUrl = new URL(url);++ // Validate hostname and query parameters+ const result = s3UrlSchema.safeParse({+ hostname: parsedUrl.hostname,+ path: parsedUrl.pathname,+ query: Object.fromEntries(parsedUrl.searchParams.entries()),+ });++ if (!result.success) {+ logger.info("Invalid pre-signed S3 URL:", result.error.message);+ return false;+ }++ // Perform a HEAD request to check reachability+ const response = await fetch(url, {+ method: "HEAD",+ signal: AbortSignal.timeout(5000),+ });++ // Status 200 indicates the URL is valid+ if (response.ok && isImageContent(response)) {+ return true;+ }++ // Attempt a GET request, as most pre-signed URLs are restricted to GET requests only+ if (response.status === 403) {+ logger.info(+ "HEAD request returned 403, attempting GET for validation...",+ );++ const getResponse = await fetch(url, {+ method: "GET",+ signal: AbortSignal.timeout(5000),+ headers: { Range: "bytes=0-1" }, // Fetch only the first byte, expected server response for valid pre-signed URLs is 206 Partial Content+ });++ return getResponse.ok && isImageContent(getResponse); // 200 or 206 Partial Content indicates success+ }++ return false;+ } catch (error) {+ if (error instanceof z.ZodError) {+ logger.info("URL Validation Error:", error.message);+ } else if (error instanceof Error) {+ logger.info("HEAD Request Error:", error.message);+ } else {+ logger.info("Unknown error:", error);+ }return false;}};const isValidImageUrl = async (url: string): Promise<boolean> => {try {+ // Pre-signed URLs (AWS S3) often have restricted access methods. Some only allow GET requests, others only HEAD. We need to try both.+ if (await isValidPresignedS3Url(url)) {+ return true;+ }+const response = await fetch(url, {method: "HEAD",signal: AbortSignal.timeout(5000),});+if (!response.ok) {return false;}- const contentType = response.headers.get("content-type");-- return !!contentType && contentType.startsWith("image/");+ return isImageContent(response);} catch (error) {- console.error("Invalid image error:", error);+ logger.info("Invalid image error:", error);return false;}};