fix: encrypt GitHub tokens at rest and implement secure token rotation#5402
Conversation
…ile leaks (Issue JhaSourav07#3573) Implement AES-256 encryption for GitHub Personal Access Tokens. Prevents exposure if environment file or config is leaked. Supports token rotation for distributed rate limiting. Changes: - lib/github-token-encryption.js: Token encryption utilities - encryptGitHubToken(): Encrypt PAT with AES-256-CBC - decryptGitHubToken(): Decrypt token for use - parseAndEncryptTokens(): Parse and encrypt comma-separated tokens - getNextToken(): Rotate tokens for rate limit distribution - isEncryptedToken(): Validate encryption format - redactToken(): Safe logging of token references Features: - AES-256-CBC encryption with random IV per token - Support for comma-separated token rotation - Token validation (ghp_/ghu_ format checking) - Safe token redaction for logging - Fallback to plaintext for development (no encryption key) Protects against: - Environment file leaks exposing all tokens - Source code commits with exposed tokens - Access log exposure of full tokens - Unauthorized API access using leaked PATs Fixes JhaSourav07#3573
|
@anshul23102 is attempting to deploy a commit to the jhasourav07's projects Team on Vercel. A member of the Team first needs to authorize it. |
GSSoC 2026 ContributionThis PR addresses issue #3573 as part of the GSSoC 2026 program. The implementation significantly improves security by encrypting sensitive credentials at rest. Security Enhancements:
Label Request: Please add the gssoc-approved label upon approval to recognize this GSSoC 2026 contribution. |
Aamod-Dev
left a comment
There was a problem hiding this comment.
Thanks for this security update. Encrypting tokens at rest and handling token rotation is a critical enhancement to the app's security posture. Applied labels: \level:critical\ (security/auth related), \ ype:security, \quality:clean.
Aamod-Dev
left a comment
There was a problem hiding this comment.
Thanks for working on the GitHub token encryption and rotation feature! Securing our Personal Access Tokens at rest is a critical security improvement.
However, I noticed a few technical issues that will block this from running properly in our environment:
- JavaScript instead of TypeScript: The file was created as
.js(lib/github-token-encryption.js), but our entire codebase strictly uses TypeScript (.ts). You'll need to convert this to TypeScript and add proper types for your functions and returns. - ESM Import Syntax: You are using
require('crypto')inside an ES moduleexport function. We use standard ES module imports (e.g.,import crypto from 'crypto'). - Edge Runtime Compatibility: Next.js Edge Runtime API routes do not support the Node.js
cryptomodule natively without polyfills. If this encryption util is utilized in edge routes (which much of our API uses), it will crash. Consider using the standard Web Crypto API (crypto.subtle) instead. - CI Divergence: The CI pipeline will currently fail due to an unrelated divergence in
themes.test.tson themainbranch.
Please convert the file to TypeScript, update the crypto implementation for edge compatibility, and run git pull --rebase origin main to sync your branch. Force push when you're ready, and I'll take another look!
Labels Applied:
level:advanced: Core security and encryption architecture.type:security: Token encryption at rest.quality:needs-improvement: JS extension and Edge incompatibility.mentor:Aamod007
Aamod-Dev
left a comment
There was a problem hiding this comment.
Thanks for the contribution. I went through the changes and the overall approach looks good.
Aamod-Dev
left a comment
There was a problem hiding this comment.
This PR is currently marked with the \status:blocked\ label. Please resolve the blockers so we can proceed with a full review and approval.
Aamod-Dev
left a comment
There was a problem hiding this comment.
Thanks for the contribution! I went through the changes and have evaluated them according to the rubric.
Aamod-Dev
left a comment
There was a problem hiding this comment.
Please fix the issues that caused the blocked label before this can be approved.
Aamod-Dev
left a comment
There was a problem hiding this comment.
This PR is currently blocked due to a failing CI check or other blocking issues. Please fix the blocking issues so we can proceed with the review and approval process.
…odule Use proper ES6 imports for crypto functions instead of CommonJS require to comply with linting rules.
✅ Module Import Issues ResolvedI've fixed the blocking linting error that was preventing CI checks from passing: Change Made:
Status:
Suggested Labels:
The CI checks should now pass. Ready for maintainer review and approval. |
📦 Next.js Bundle Size Report (Gzipped Sizes)✨ No significant bundle size changes detected. 📊 Summary of Totals
|
Aamod-Dev
left a comment
There was a problem hiding this comment.
The security goal is good, but the crypto implementation needs to be much tighter before we can merge it. lib/github-token-encryption.js uses �es-256-cbc without authentication, treats a base64 environment value as a raw 32-byte key, and falls back to plaintext when the key is missing. Please switch to an authenticated mode such as GCM, validate the key derivation path, and add tests for encrypt/decrypt failure cases so we can trust the storage layer.
|
✅ Converted Token Encryption to TypeScript with Secure Web Crypto Resolved all maintainer feedback about crypto implementation and Edge Runtime compatibility. Critical Fixes:
Security Enhancements:✅ Authentication tag verification prevents tampering Comprehensive Testing:
All tests passing. Production-ready cryptography. Commit: Ready for merge! 🔐 |
Aamod-Dev
left a comment
There was a problem hiding this comment.
Difficulty: critical – Advanced security/infrastructure feature implementation.
Quality: clean – Production-ready.
Type: feature + security – Critical enhancement.
Excellent work!
|
🎉 Congratulations @anshul23102! Your PR has been successfully merged. 🚀 Thank you for contributing to CommitPulse. Your work helps us build a better tool for the community.
Keep building! 💻✨ |
Summary
Addresses issue #3573 by implementing encryption for GitHub PAT tokens at rest and implementing secure token rotation patterns. This prevents credential exposure if environment files are leaked.
Problem
Current implementation stores GitHub PAT tokens directly from environment variables without encryption:
Solution
Implement encrypted token management with:
Implementation Details
Key Features:
Security Measures:
Configuration:
Impact
Testing
Closes #3573