From 1b5461340b185c25b8eb5fcdf9f7ab892eb0dbb4 Mon Sep 17 00:00:00 2001 From: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:19:23 +0000 Subject: [PATCH] fix(api): reject non-http preview URLs and stop caching errors /api/preview accepted any URL scheme (ftp:, file:, javascript:, data:) which failed deep inside fetch or produced nonsense reports, and cached 400/502 responses for five minutes. A shared http(s)-only schema now rejects bad schemes with a 400, error responses send no-store, and /api/ai/autofill uses the same schema. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- src/lib/api-validation.ts | 18 ++++- src/pages/api/ai/autofill.ts | 3 +- src/pages/api/preview.ts | 16 ++-- tests/api/preview.test.ts | 142 ++++++++++++++++++++++++++++++++++- 4 files changed, 169 insertions(+), 10 deletions(-) diff --git a/src/lib/api-validation.ts b/src/lib/api-validation.ts index 26a4000..d742d8e 100644 --- a/src/lib/api-validation.ts +++ b/src/lib/api-validation.ts @@ -103,8 +103,24 @@ export function resolveDimensions( }; } +// A page the API is asked to fetch: it has to parse as a URL, and only +// http(s) schemes are fetchable — any other scheme is the caller's mistake. +export const httpUrlSchema = z + .url({ error: 'A valid URL is required' }) + .refine( + (value) => { + try { + return /^https?:$/i.test(new URL(value).protocol); + } catch { + // The URL check above already reported the unparseable value. + return true; + } + }, + { message: 'Only http and https URLs are supported' } + ); + export const previewQuerySchema = z.object({ - url: z.string().url('A valid URL is required'), + url: httpUrlSchema, }); export type PreviewQueryParams = z.infer; diff --git a/src/pages/api/ai/autofill.ts b/src/pages/api/ai/autofill.ts index 17eacac..8e45984 100644 --- a/src/pages/api/ai/autofill.ts +++ b/src/pages/api/ai/autofill.ts @@ -1,6 +1,7 @@ import type { APIRoute } from 'astro'; import { z } from 'zod'; import { getProvider } from '@/lib/ai/providers'; +import { httpUrlSchema } from '@/lib/api-validation'; import { fetchMetaTags } from '@/lib/meta-fetcher'; import { buildAutofillPrompt, parseAutofillResponse } from '@/lib/ai/autofill'; import { getTemplate } from '@/templates/registry'; @@ -16,7 +17,7 @@ const autofillSchema = z.object({ provider: z.enum(['openai', 'anthropic', 'google', 'groq', 'openrouter']), apiKey: z.string().min(1), model: z.string().min(1), - url: z.string().url('A valid URL is required'), + url: httpUrlSchema, }); export const POST: APIRoute = async ({ request }) => { diff --git a/src/pages/api/preview.ts b/src/pages/api/preview.ts index 8d63459..861abd6 100644 --- a/src/pages/api/preview.ts +++ b/src/pages/api/preview.ts @@ -7,19 +7,21 @@ import { resolvePlatformPreviews } from '@/lib/platform-specs'; export const GET: APIRoute = async ({ request }) => { const url = new URL(request.url); - const headers = { + // Error answers are not cached: a site that timed out should get a fresh + // try on the next check, not the same 502 for five minutes. + const headers = (cacheControl: string) => ({ 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json', - 'Cache-Control': 'public, max-age=300', - }; + 'Cache-Control': cacheControl, + }); if (request.method === 'OPTIONS') { - return new Response(null, { status: 204, headers }); + return new Response(null, { status: 204, headers: headers('public, max-age=300') }); } const parsed = parseSearchParams(url.searchParams, previewQuerySchema); if (!parsed.success) { - return new Response(JSON.stringify({ error: parsed.error }), { status: 400, headers }); + return new Response(JSON.stringify({ error: parsed.error }), { status: 400, headers: headers('no-store') }); } const targetUrl = parsed.data.url; @@ -36,13 +38,13 @@ export const GET: APIRoute = async ({ request }) => { analysis, platforms, }), - { status: 200, headers } + { status: 200, headers: headers('public, max-age=300') } ); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to fetch URL'; return new Response( JSON.stringify({ error: message }), - { status: 502, headers } + { status: 502, headers: headers('no-store') } ); } }; diff --git a/tests/api/preview.test.ts b/tests/api/preview.test.ts index 4d2655c..ae5f5b8 100644 --- a/tests/api/preview.test.ts +++ b/tests/api/preview.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect } from 'vitest'; +import { afterEach, describe, it, expect, vi } from 'vitest'; import { previewQuerySchema } from '@/lib/api-validation'; describe('Preview API Validation', () => { @@ -20,4 +20,144 @@ describe('Preview API Validation', () => { }); expect(result.success).toBe(false); }); + + it.each(['http://example.com', 'https://example.com', 'HTTPS://example.com'])( + 'accepts %s', + (url) => { + expect(previewQuerySchema.safeParse({ url }).success).toBe(true); + } + ); + + it.each([ + 'ftp://example.com', + 'file:///etc/passwd', + 'data:text/html,hi', + 'javascript:alert(1)', + 'mailto:a@b.com', + ])('rejects the %s scheme with a specific message', (url) => { + const result = previewQuerySchema.safeParse({ url }); + expect(result.success).toBe(false); + if (!result.success) { + expect(result.error.issues[0].message).toBe('Only http and https URLs are supported'); + } + }); + + it.each([{}, { url: undefined }, { url: '' }, { url: 'example.com' }, { url: 'not-a-url' }])( + 'reports %o as "A valid URL is required"', + (input) => { + const result = previewQuerySchema.safeParse(input); + expect(result.success).toBe(false); + if (!result.success) { + expect(result.error.issues[0].message).toBe('A valid URL is required'); + } + } + ); +}); + +// fetchMetaTags is mocked so no real page is fetched; the rest of the route +// (analyzeMeta, resolvePlatformPreviews) runs for real on minimal tags. +vi.mock('@/lib/meta-fetcher', async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + fetchMetaTags: vi.fn(), + }; +}); + +const { GET } = await import('@/pages/api/preview'); +const { POST } = await import('@/pages/api/ai/autofill'); +const { fetchMetaTags, parseMetaTags } = await import('@/lib/meta-fetcher'); + +const BASE = 'http://localhost:4321/api/preview'; + +function makeContext(url: string) { + return { request: new Request(url) } as Parameters>[0]; +} + +function stubMeta() { + vi.mocked(fetchMetaTags).mockImplementation(async (url: string) => + parseMetaTags('Example', url) + ); +} + +describe('GET /api/preview', () => { + afterEach(() => { + vi.resetAllMocks(); + }); + + it.each(['', '?url=', '?url=example.com'])('rejects %s with 400, no-store', async (query) => { + const res = await GET(makeContext(`${BASE}${query}`)); + + expect(res.status).toBe(400); + expect(res.headers.get('cache-control')).toBe('no-store'); + expect(await res.json()).toEqual({ error: 'url: A valid URL is required' }); + expect(fetchMetaTags).not.toHaveBeenCalled(); + }); + + it.each(['ftp://example.com', 'file:///etc/passwd', 'data:text/html,x', 'javascript:alert(1)', 'mailto:a@b.com'])( + 'rejects the %s scheme with 400, no-store', + async (bad) => { + const res = await GET(makeContext(`${BASE}?url=${encodeURIComponent(bad)}`)); + + expect(res.status).toBe(400); + expect(res.headers.get('cache-control')).toBe('no-store'); + const body = await res.json(); + expect(body.error).toBe('url: Only http and https URLs are supported'); + expect(fetchMetaTags).not.toHaveBeenCalled(); + } + ); + + it.each(['http://example.com', 'https://example.com', 'HTTPS://example.com'])( + 'accepts %s and caches the 200 for five minutes', + async (good) => { + stubMeta(); + const res = await GET(makeContext(`${BASE}?url=${encodeURIComponent(good)}`)); + + expect(res.status).toBe(200); + expect(res.headers.get('cache-control')).toBe('public, max-age=300'); + expect(fetchMetaTags).toHaveBeenCalledWith(expect.stringMatching(/^https?:\/\//i)); + } + ); + + it('answers 502 with no-store when the page cannot be fetched', async () => { + vi.mocked(fetchMetaTags).mockRejectedValue(new Error('Failed to fetch URL: 404 Not Found')); + + const res = await GET(makeContext(`${BASE}?url=${encodeURIComponent('https://down.example')}`)); + + expect(res.status).toBe(502); + expect(res.headers.get('cache-control')).toBe('no-store'); + expect(await res.json()).toEqual({ error: 'Failed to fetch URL: 404 Not Found' }); + }); +}); + +describe('POST /api/ai/autofill url validation', () => { + function post(body: unknown) { + const request = new Request('http://localhost:4321/api/ai/autofill', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + }); + return POST({ request } as Parameters>[0]); + } + + const fields = { provider: 'openai', apiKey: 'sk-test', model: 'gpt-4o-mini' }; + + it.each(['ftp://example.com', 'file:///etc/passwd', 'data:text/html,x', 'javascript:alert(1)', 'mailto:a@b.com'])( + 'rejects the %s scheme with a 400', + async (bad) => { + const res = await post({ ...fields, url: bad }); + + expect(res.status).toBe(400); + const body = await res.json(); + expect(body.error).toContain('Only http and https URLs are supported'); + } + ); + + it('rejects an unparseable url with "A valid URL is required"', async () => { + const res = await post({ ...fields, url: 'not-a-url' }); + + expect(res.status).toBe(400); + const body = await res.json(); + expect(body.error).toContain('A valid URL is required'); + }); });