security: rate limit /api/analyze, redact error details, harden external links#9
Merged
Merged
Conversation
…nal links - Add IP-keyed in-memory rate limiter (lib/analysis/rate-limit.ts) - general bucket: 6 req / 60s per IP - forceRefresh bucket: 2 req / 10min per IP (stricter for cache-bypassing) - both return 429 + Retry-After header + details.retryAfterSeconds - Wire limiter into /api/analyze after same-origin check - Cap request body size to 4 KiB and repoUrl length to 512 chars - Sanitize outbound error payload: whitelist detail fields, replace unknown Error messages with a generic copy to avoid leaking internal paths/stacks - Add HSTS + Cross-Origin-Opener-Policy headers alongside existing ones - Protocol-check external links (README links, owner homepage, preview images) via safeExternalHref / safeImageSrc to block javascript:/data: schemes Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/api/analyzewith two buckets:repoUrlcapped at 512 chars.retryAfterSeconds,resetAt,authenticated,githubAuthMode) and replaces unknownErrormessages with a generic copy to avoid leaking internal paths/stacks.Cross-Origin-Opener-Policy: same-originto existing security headers.safeExternalHref/safeImageSrc, blockingjavascript:/data:schemes before they reachhreforsrc.Why
/api/analyzewas protected against browser cross-site POST but fully open to server-to-server calls — a single attacker could exhaust our GitHub token quota and inflate Vercel function usage.forceRefresh=truebypasses server cache; it deserves a tighter limit than normal analyze.<a href>without protocol checks, riskingjavascript:XSS from a malicious repo README.Test plan
pnpm exec tsc --noEmitpnpm lintpnpm buildpnpm test:unit— 240 tests (26 files + 1 new rate-limit test file, +8 tests)/api/analyzefrom local shell 7+ times within a minute → expect a 429 withRetry-Afterheader anddetails.retryAfterSeconds.javascript:link, analyze it → expect the link rendered as non-clickable text.🤖 Generated with Claude Code