Skip to content

Preview API: reject non-HTTP URLs with a 400 and stop caching error responses #414

Open

Description

@anurag629

/api/preview validates url with z.string().url('A valid URL is required') (src/lib/api-validation.ts lines 106 to 108). Zod accepts any scheme there, so ftp:, file:, mailto:, javascript: and data: URLs get through to fetchMetaTags() and fail (or succeed) inside fetch. Calling the route's GET under Node, where npm run test runs it, with the values URL-encoded:

/api/preview                                       → 400 {"error":"url: Invalid input: expected string, received undefined"}
/api/preview?url=                                  → 400 {"error":"url: A valid URL is required"}
/api/preview?url=ftp://example.com                 → 502 {"error":"fetch failed"}
/api/preview?url=file:///etc/passwd                → 502 {"error":"fetch failed"}
/api/preview?url=data:text/html,<title>hi</title>  → 200, a full report (grade F) for the data: URL

The first message is Zod's internal wording, not the one the schema sets. The scheme cases are the caller's mistake, so they should be a 400 that says so, not a 502 that reads as if the target site is down.

Second, every response, errors included, uses one headers object with Cache-Control: public, max-age=300 (src/pages/api/preview.ts lines 10 to 14, reused by the 400 at line 22 and the 502 at lines 43 to 46). The preview page fetches with the default cache mode (src/components/preview/usePreviewState.ts line 89), so after a site times out or answers with a 5xx, pressing Check again within five minutes can get the same error straight from the browser cache without trying the site. /api/og had the same problem and fixed it in #109.

Smart Autofill validates its url with the same z.string().url() (src/pages/api/ai/autofill.ts line 19).

What to do

Acceptance criteria

  • No url, url= and url=example.com return 400 with url: A valid URL is required
  • ftp:, file:, data:, javascript: and mailto: URLs return 400, and fetchMetaTags isn't called
  • http://, https:// and uppercase-scheme URLs work as before
  • 400 and 502 responses send no-store, 200 responses keep public, max-age=300
  • /api/ai/autofill rejects the same schemes with a 400
  • Route tests that call GET with @/lib/meta-fetcher mocked. Today tests/api/preview.test.ts only checks the schema; tests/api/og.test.ts shows how to mock a module and call a handler.
  • npm run check, npm run test and npm run build pass

How to test
Besides the tests, run npm run dev and try the URLs above with curl -i, checking the status and the cache-control header.


Want to work on this? Comment below and I'll assign it to you. Please take one issue at a time, branch off dev, and open your PR against dev with Closes #414 in the description.

Activity

  1. joshuaallgaier2-cmd commented on Nov 15, 2026

    @joshuaallgaier2-cmd

    I'll take this issue.

  2. joshuaallgaier2-cmd commented on Nov 15, 2026

    @joshuaallgaier2-cmd

    I'd like to take this. Added httpUrlSchema (http/https only) used by preview and autofill, no-store on 400/502, 200 keeps public max-age=300.

  3. joshuaallgaier2-cmd commented on Nov 15, 2026

    @joshuaallgaier2-cmd

    I'd like to take this. Added httpUrlSchema (http/https only) used by preview and autofill, no-store on 400/502, 200 keeps public max-age=300. Preview/og route tests pass.

  4. Seyamalam commented on Nov 20, 2026

    @Seyamalam
    Contributor

    Note

    🤖 SWE-2 High (Devin) responding on behalf of Seyamalam

    I'd like to work on this next (my #425 and #429 were merged). Plan: add a shared http(s)-only URL schema in src/lib/api-validation.ts (missing/empty/unparseable → url: A valid URL is required, other schemes → a 400 saying only http and https are supported, uppercase schemes still pass), use it for previewQuerySchema and the autofill url, send Cache-Control: no-store on the 400/502 while 200 keeps public, max-age=300, and extend tests/api/preview.test.ts into route tests calling GET with @/lib/meta-fetcher mocked. Could you assign it to me?

  5. anurag629 commented on Nov 21, 2026

    @anurag629
    MemberAuthor

    Hey @Seyamalam, it's yours! That plan covers everything in the issue, so go ahead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggood first issueGood for newcomers

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions