Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore: move external image verification to a protected api route (#2692)

Marc Klingenaf89b4594b2d
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index 02162767dd..768ae5df68 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -50,7 +50,7 @@ const MarkdownImage: Components["img"] = ({ src, alt }) => {
if (!isPresent(src)) return null;
- const isValidImage = api.public.validateImgUrl.useQuery(src);
+ const isValidImage = api.utilities.validateImgUrl.useQuery(src);
if (isValidImage.isLoading) {
return (
<Skeleton className="h-8 w-1/2 items-center p-2 text-xs">
diff --git a/web/src/server/api/root.ts b/web/src/server/api/root.ts
index fb49e1ecdd..ffa1c23fd9 100644
--- a/web/src/server/api/root.ts
+++ b/web/src/server/api/root.ts
@@ -20,6 +20,7 @@ import { scoreConfigsRouter } from "@/src/server/api/routers/scoreConfigs";
import { publicRouter } from "@/src/server/api/routers/public";
import { credentialsRouter } from "@/src/features/auth-credentials/server/credentialsRouter";
import { batchExportRouter } from "@/src/server/api/routers/batchExport";
+import { utilsRouter } from "@/src/server/api/routers/utilities";
/**
* This is the primary router for your server.
@@ -48,6 +49,7 @@ export const appRouter = createTRPCRouter({
llmApiKey: llmApiKeyRouter,
public: publicRouter,
credentials: credentialsRouter,
+ utilities: utilsRouter,
});
// export type definition of API
diff --git a/web/src/server/api/routers/public.ts b/web/src/server/api/routers/public.ts
index 2500d558e9..5d9f3dd5e3 100644
--- a/web/src/server/api/routers/public.ts
+++ b/web/src/server/api/routers/public.ts
@@ -46,38 +46,6 @@ const ReleaseApiRes = z.array(
}),
);
-const isValidURL = (urlString: string): boolean => {
- try {
- const url = new URL(urlString);
- return url.protocol === "https:";
- } catch (error) {
- console.log(error);
- return false;
- }
-};
-
-const isValidImageUrl = async (url: string): Promise<boolean> => {
- try {
- const response = await fetch(url, { method: "HEAD" });
- if (!response.ok) {
- return false;
- }
-
- const contentType = response.headers.get("content-type");
- const contentLength = response.headers.get("content-length");
-
- // ensure image is not larger than 1MB in size
- if (contentLength && parseInt(contentLength, 10) > 1 * 1024 * 1024) {
- return false;
- }
-
- return !!contentType && contentType.startsWith("image/");
- } catch (error) {
- console.log(error);
- return false;
- }
-};
-
export const publicRouter = createTRPCRouter({
checkUpdate: publicProcedure.query(async () => {
// Skip update check on Langfuse Cloud
@@ -123,14 +91,4 @@ export const publicRouter = createTRPCRouter({
url: langfuseRelease.url,
};
}),
- validateImgUrl: publicProcedure
- .input(z.string().url())
- .query(async ({ input: url }) => {
- if (!isValidURL(url)) {
- return { isValid: false };
- }
-
- const isValidImage = await isValidImageUrl(url);
- return { isValid: isValidImage };
- }),
});
diff --git a/web/src/server/api/routers/utilities.ts b/web/src/server/api/routers/utilities.ts
new file mode 100644
index 0000000000..0f4e2fa5a1
--- /dev/null
+++ b/web/src/server/api/routers/utilities.ts
@@ -0,0 +1,47 @@
+import { createTRPCRouter, protectedProcedure } from "@/src/server/api/trpc";
+import { z } from "zod";
+
+const isValidURL = (urlString: string): boolean => {
+ try {
+ const url = new URL(urlString);
+ return url.protocol === "https:";
+ } catch (error) {
+ console.log(error);
+ return false;
+ }
+};
+
+const isValidImageUrl = async (url: string): Promise<boolean> => {
+ try {
+ const response = await fetch(url, { method: "HEAD" });
+ if (!response.ok) {
+ return false;
+ }
+
+ const contentType = response.headers.get("content-type");
+ const contentLength = response.headers.get("content-length");
+
+ // ensure image is not larger than 1MB in size
+ if (contentLength && parseInt(contentLength, 10) > 1 * 1024 * 1024) {
+ return false;
+ }
+
+ return !!contentType && contentType.startsWith("image/");
+ } catch (error) {
+ console.log(error);
+ return false;
+ }
+};
+
+export const utilsRouter = createTRPCRouter({
+ validateImgUrl: protectedProcedure
+ .input(z.string().url())
+ .query(async ({ input: url }) => {
+ if (!isValidURL(url)) {
+ return { isValid: false };
+ }
+
+ const isValidImage = await isValidImageUrl(url);
+ return { isValid: isValidImage };
+ }),
+});