Skip to content

fix(api): reject non-http preview URLs and stop caching errors - #431

Open
Seyamalam wants to merge 1 commit into
codercops:devfrom
Seyamalam:fix/preview-reject-non-http-urls
Open

Seyamalam wants to merge 1 commit into
codercops:devfrom
Seyamalam:fix/preview-reject-non-http-urls

Conversation

@Seyamalam

Copy link
Copy Markdown
Contributor

Description

/api/preview validates url with z.string().url(), which Zod accepts for any scheme — so ftp:, file:, mailto:, javascript: and data: URLs reach fetchMetaTags() and surface as a 502 (or, for data:, a nonsense 200 report) instead of the caller's 400. Separately, every response — errors included — goes out with Cache-Control: public, max-age=300, so a site that times out keeps serving the same cached 502 for five minutes (/api/og had the same issue, fixed in #109).

This adds a shared httpUrlSchema in src/lib/api-validation.ts and uses it for both previewQuerySchema and the autofill url. Missing, empty, or unparseable values report url: A valid URL is required; a parsable non-http(s) URL gets a 400 saying only http and https URLs are supported; uppercase schemes like HTTPS:// keep working. The 400 and 502 responses now send Cache-Control: no-store; 200 keeps public, max-age=300.

Closes #414

How I Tested This

  • npx vitest run tests/api/preview.test.ts — 34 tests: the route GET is called with @/lib/meta-fetcher mocked (like tests/api/og.test.ts), covering missing/empty/unparseable url, all five bad schemes (ftp:, file:, data:, javascript:, mailto: — asserting fetchMetaTags is never called), http/https/uppercase-scheme success, 502 on fetch failure, the no-store vs max-age=300 headers, and the same scheme rejections through POST /api/ai/autofill.
  • Verified the tests catch the old behavior: stashed the source changes and reran — the scheme cases returned 502 instead of 400 and error caching assertions failed; restored and all green.
  • npm run check (0 errors), full npm run test (2151 tests), and npm run build all pass.

AI Assistance

Devin CLI (SWE-2 High): implemented the shared schema, the route header change, the autofill schema swap, and the test file under human direction; diff reviewed and verified by the operator.

Type of Change

  • Bug fix

Screenshots

N/A — API behavior change only, no UI.

Checklist

  • This PR targets dev
  • I was assigned the linked issue
  • npm run check passes
  • npm run test passes
  • For a bug fix: the new test fails without my change
  • Tested locally with npm run dev (route tests exercise the handler directly; curl spot-checks in tests)
  • Screenshots included for visual changes (N/A)
  • New templates follow the Template Guidelines (N/A)

Created with SWE-2 High in Devin CLI.

/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>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant