fix: validate repo parameter before cache and GitHub API calls#1767
Merged
Priyanshu-byte-coder merged 1 commit intoMay 31, 2026
Conversation
The repo-analytics endpoint accepted arbitrary values for the ?repo= query parameter and inserted them directly into GitHub API URLs and cache keys without any format validation: GET /api/metrics/repo-analytics?repo=octocat/Hello-World/issues would produce requests to: GET https://api.github.com/repos/octocat/Hello-World/issues GET https://api.github.com/repos/octocat/Hello-World/issues/contributors ... constructing entirely different GitHub API endpoints than intended. Extra path segments, path traversal patterns ("owner/.."), and other malformed values were also accepted, consuming GitHub API rate-limit quota and polluting the metrics cache. Changes to src/app/api/metrics/repo-analytics/route.ts: - Export parseRepoParam() — validates the raw string against a strict regex that accepts exactly "owner/repo" where: * owner: 1–39 chars, alphanumeric + hyphens, not starting/ending with a hyphen (mirrors GitHub's username rules) * repo: 1–100 chars, alphanumeric + dots + hyphens + underscores * exactly one slash separator, no additional segments Additionally rejects "." and ".." as repo names. - Return 400 immediately when validation fails — before computing the cache key or calling fetch(). - Build GitHub API URLs from encodeURIComponent(owner)/encodeURIComponent(repo) so each path segment is individually safe even if the regex is ever relaxed in the future. - Use the validated, trimmed owner/repo pair in the cache key rather than the raw query-string value. test/repo-analytics-validation.test.ts — 37 new tests: parseRepoParam unit tests: - valid standard/edge cases (single chars, hyphens, dots, max lengths) - whitespace trimming - missing/empty segments - extra path segments (the reported issue, regression for Priyanshu-byte-coder#1700) - path traversal: "..", ".", "../../admin" - invalid characters: spaces, @, ?, null bytes - length violations: owner >39, repo >101 - owner hyphen rules: leading/trailing hyphens GET route integration tests: - 401 without session - 400 for missing parameter (no fetch or cache calls) - 400 for three-segment path — regression for Priyanshu-byte-coder#1700 - 400 for bare name, path traversal, hyphen violations, whitespace - none of the 400 paths call fetch() or withMetricsCache() - valid owner/repo reaches withMetricsCache - cache key uses validated form, not raw input - invalid input never generates a cache key Closes Priyanshu-byte-coder#1700
|
@Ridanshi is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel. A member of the Team first needs to authorize it. |
GSSoC Label Checklist 🏷️@Priyanshu-byte-coder — please apply the appropriate labels before merging: Difficulty (pick one):
Quality (optional):
Validation (required to score):
|
9878cb9
into
Priyanshu-byte-coder:main
4 of 5 checks passed
|
🎉 Merged! Thanks for contributing to DevTrack. If the project has been useful to you, a ⭐ star on the repo is the easiest way to support it — it helps DevTrack get discovered by more developers. Keep an eye on open issues for your next contribution! |
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.
Closes #1700
Problem
GET /api/metrics/repo-analyticsaccepted arbitrary values for the?repo=query parameter and interpolated them directly into GitHub API URL paths and cache keys with no format validation:would silently construct and call:
These are entirely different GitHub API endpoints (issue lists, not repository metadata). The only guard was
if (!repoParam), which passes for any non-empty string.Consequences confirmed by code reading:
owner/repo/issues)owner/..are accepted and forwardedrepo-analytics-${repoParam}), so malformed inputs create permanent cache entries for non-existent resourcesRoot cause
Three lines in
src/app/api/metrics/repo-analytics/route.ts:Fix
src/app/api/metrics/repo-analytics/route.tsIntroduces
parseRepoParam(raw)— validates the raw string against a strict regex before any other processing:Rules enforced:
.and..are explicitly excluded as repo names even though they match the character setOn validation failure → 400 Bad Request is returned immediately, before the cache key is computed or any
fetch()is called.GitHub API URLs are then built from
encodeURIComponent(owner)/encodeURIComponent(repo)so each path segment is individually URL-safe.Request lifecycle (after fix)
Tests
test/repo-analytics-validation.test.ts— 37 new tests:owner/repo/issues), four-segment pathowner/..,owner/.,../../../admin, double leading slash@,?, null bytesfetch()andwithMetricsCache()withMetricsCacheis called; cache key uses validated, trimmed formAll 37 pass. The one pre-existing failure in
test/dateUtils.test.ts(timezone boundary) is unrelated to this change.