Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/lib/api-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof previewQuerySchema>;
Expand Down
3 changes: 2 additions & 1 deletion src/pages/api/ai/autofill.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 }) => {
Expand Down
16 changes: 9 additions & 7 deletions src/pages/api/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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') }
);
}
};
142 changes: 141 additions & 1 deletion tests/api/preview.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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,<title>hi</title>',
'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<typeof import('@/lib/meta-fetcher')>();
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<NonNullable<typeof GET>>[0];
}

function stubMeta() {
vi.mocked(fetchMetaTags).mockImplementation(async (url: string) =>
parseMetaTags('<html><head><title>Example</title></head></html>', 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<NonNullable<typeof POST>>[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');
});
});
Loading