Skip to content

fix(security): validate and sanitize github usernames in API routes#866

Merged
Priyanshu-byte-coder merged 1 commit into
Priyanshu-byte-coder:mainfrom
harshitanagpal05:fix/security-857-username-validation
May 24, 2026
Merged

fix(security): validate and sanitize github usernames in API routes#866
Priyanshu-byte-coder merged 1 commit into
Priyanshu-byte-coder:mainfrom
harshitanagpal05:fix/security-857-username-validation

Conversation

@harshitanagpal05
Copy link
Copy Markdown
Contributor

Summary

Fixes [Security] GitHub API URL path traversal and search query injection via unsanitized username parameter #857.

Changes

  • Added shared validator: src/lib/validate-github-username.ts
  • Applied validation guard to:
    • src/app/api/metrics/compare/route.ts
    • src/app/api/metrics/contributions/route.ts
    • src/app/api/badge/commits/route.ts
    • src/app/api/badge/streak-shield/route.ts
  • Added URL hardening:
    • encodeURIComponent() for GitHub path segments where username is interpolated
    • URL/URLSearchParams for query construction to prevent injection
  • Added tests:
    • test/validate-github-username.test.js

Security impact

  • Blocks path traversal attempts via username in GitHub API path usage.
  • Blocks search query injection in unauthenticated badge endpoints.

Validation

@vercel
Copy link
Copy Markdown

vercel Bot commented May 23, 2026

@harshitanagpal05 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.

@github-actions github-actions Bot added gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix type:security GSSoC type bonus: security (+20 pts) type:testing GSSoC type bonus: tests (+10 pts) labels May 23, 2026
@github-actions
Copy link
Copy Markdown

GSSoC Label Checklist 🏷️

@Priyanshu-byte-coder — please apply the appropriate labels before merging:

Difficulty (pick one):

  • level:beginner — 20 pts
  • level:intermediate — 35 pts
  • level:advanced — 55 pts
  • level:critical — 80 pts

Quality (optional):

  • quality:clean — ×1.2 multiplier
  • quality:exceptional — ×1.5 multiplier

Validation (required to score):

  • gssoc:approved — counts for points
  • gssoc:invalid / gssoc:spam / gssoc:ai-slop — does not score

Type labels (type:*) are auto-detected from files and title. Review and adjust if needed.
Points formula: (difficulty × quality_multiplier) + type_bonus

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your first PR on DevTrack! 🎉

A maintainer will review it within 48 hours. While you wait:

  • Make sure CI is passing (type-check + lint)
  • Double-check the PR description is filled out and the issue is linked
  • Feel free to ask questions in Discussions if you need help

If you find DevTrack useful, a ⭐ star on the repo is always appreciated — it helps the project grow and attract more contributors!

@Priyanshu-byte-coder
Copy link
Copy Markdown
Owner

Changes needed before merge:

compare/route.ts — cache still uses next: { revalidate: 3600 }
All 4 fetch() calls in the compare route still pass next: { revalidate: 3600 }. This was the root cause of the cross-user data leak fixed in commit 435d85f — Next.js keys the cache by URL, so User A's authenticated GitHub response gets served to User B querying the same username. Please change all 4 to cache: "no-store":

// All fetch() calls in compare/route.ts
{ headers: { Authorization: `Bearer ${session.accessToken}` }, cache: "no-store" }

The validate-github-username.ts utility and URL API injection prevention are excellent — ready to merge once the cache issue is fixed. Please rebase on main first (commit 435d85f is already there).

@Priyanshu-byte-coder Priyanshu-byte-coder added gssoc:approved GSSoC: PR approved for scoring level:intermediate GSSoC: Intermediate difficulty (35 pts) labels May 23, 2026
@harshitanagpal05
Copy link
Copy Markdown
Contributor Author

hey! @Priyanshu-byte-coder, I’ve implemented the requested fix in the compare API route and pushed it to the PR branch. All GitHub fetch calls in route.ts now use cache: "no-store" instead of next: { revalidate: 3600 }.

@Priyanshu-byte-coder
Copy link
Copy Markdown
Owner

Good security fix — username validation is important. Note: PR #876 also creates src/lib/validate-github-username.ts with a slightly different implementation (only isValidGitHubUsername, no normalizeGitHubUsername). Please rebase against main once #876 is merged so we avoid duplicate file creation conflicts. Your version with normalizeGitHubUsername is the more complete one and should be kept.

@harshitanagpal05 harshitanagpal05 force-pushed the fix/security-857-username-validation branch from 6ea7290 to a13a6c5 Compare May 24, 2026 09:49
@harshitanagpal05
Copy link
Copy Markdown
Contributor Author

The security fix is done and the branch has been rebased/pushed. The merge conflicts are resolved, and the old rebase note is no longer accurate, so it can be removed. The only remaining blocker shown in GitHub is workflow approval / pending CI, not the code changes.

@harshitanagpal05
Copy link
Copy Markdown
Contributor Author

and one more thing #876 wasn’t merged into main when I last rebased, so there was nothing to reconcile from it yet. If #876 lands later and changes the same file, we can rebase again then, but right now the branch is already updated and conflict-free.

@Priyanshu-byte-coder
Copy link
Copy Markdown
Owner

This PR has merge conflicts with main. Please rebase on the latest main branch and re-request review. The approach is approved — just needs conflict resolution.

@Priyanshu-byte-coder Priyanshu-byte-coder merged commit 11ae8e3 into Priyanshu-byte-coder:main May 24, 2026
3 checks passed
@github-actions
Copy link
Copy Markdown

🎉 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!

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

Labels

gssoc:approved GSSoC: PR approved for scoring gssoc26 GSSoC 2026 contribution level:intermediate GSSoC: Intermediate difficulty (35 pts) type:bug GSSoC type bonus: bug fix type:security GSSoC type bonus: security (+20 pts) type:testing GSSoC type bonus: tests (+10 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Security] GitHub API URL path traversal and search query injection via unsanitized username parameter

2 participants