Security Fix Summary
Issue Resolved
Critical Security Vulnerability: Gemini API key was embedded in static JavaScript files deployed to GitHub Pages, making it publicly accessible to anyone who inspected the website source code.
Evidence of Breach
- Location:
gh-pagesbranch, fileassets/js/main.*.js - API key pattern found:
[REDACTED] - Risk Level: CRITICAL - Full API access exposed
Solution Implemented
Migrated from insecure client-side API key to secure serverless proxy architecture using Netlify Functions.
Before (Vulnerable)
User Browser
↓
Static HTML/JS (contains API key) ← EXPOSED TO PUBLIC
↓
Gemini API
After (Secure)
User Browser
↓
Static HTML/JS (no secrets)
↓
Netlify Function (API key in environment) ← SERVER-SIDE ONLY
↓
Gemini API
Technical Changes
1. Removed API Key from Build Process
File: docusaurus.config.js
- customFields: {
- GEMINI_API_KEY: process.env.GEMINI_API_KEY || '',
- },
2. Created Secure Proxy Function
File: netlify/functions/gemini-proxy.mjs (NEW)
- Handles all Gemini API requests server-side
- API key stored in Netlify environment (never sent to client)
- Input validation: 10K char limit for messages
- Secure error handling: no information disclosure
- Debug logging: no sensitive data exposure
3. Updated Frontend
File: src/components/AutoBot/index.tsx
- const genAI = new GoogleGenerativeAI(apiKey); // Direct API call
+ const response = await fetch('/api/gemini-proxy', { // Proxy call
4. Added Netlify Configuration
File: netlify.toml (NEW)
- Build configuration
- Function routing:
/api/*→/.netlify/functions/*
Security Verification
Build Artifact Analysis
# Build without API key
yarn build
# Verify no API key in output
grep -r "AIza" build/ # No results ✅
grep -r "GEMINI_API_KEY" build/ # No results ✅
Security Scan Results
- CodeQL: 0 alerts ✅
- API Key Exposure: None found ✅
- Information Disclosure: Prevented ✅
Deployment Requirements
Required Actions
- Create Netlify Account: https://netlify.com
- Connect Repository: Link ShaftHQ/shafthq.github.io
- Set Environment Variable:
- Name:
GEMINI_API_KEY - Value: Your Gemini API key
- Scope: All deploys
- Name:
- Deploy: Netlify auto-deploys from master branch
Post-Deployment Verification
- Visit deployed site
- Open AutoBot chatbot
- Send test message
- Check DevTools → Network tab
- Verify request goes to
/api/gemini-proxy - Inspect JavaScript bundles to confirm no API key present
Local Development
Setup
# Install dependencies
yarn install
# Install Netlify CLI
npm install -g netlify-cli
# Set up environment
cp .env.example .env
# Edit .env and add your GEMINI_API_KEY
# Run dev server with functions
netlify dev
Important: Use netlify dev instead of yarn start to enable Netlify Functions locally.
Migration Path
For detailed step-by-step migration instructions, see:
- MIGRATION_GUIDE.md - Complete deployment guide
- README.md - Updated setup instructions
API Key Recommendations
For Production (Netlify)
- Generate new API key in Google AI Studio
- Restrict to "Generative Language API" only
- Store in Netlify environment variables
- Do not add HTTP referrer restrictions (key is server-side)
For Testing (GitHub Actions)
- Add
GEMINI_API_KEYto repository secrets - Used only for running tests in CI/CD
- Never exposed in build artifacts
Old Key Action Required
Historical action: the exposed API key required immediate revocation.
- Go to Google Cloud Console
- Find the compromised key
- Delete or regenerate it immediately
Compliance Checklist
- API key removed from source code
- API key removed from build artifacts
- API key removed from git history (in customFields, not hardcoded)
- Secure storage implemented (Netlify environment)
- Input validation added
- Error messages sanitized
- Security scan passed
- Documentation updated
- Migration guide created
Benefits Achieved
- Security: API key never exposed to clients
- Control: Server-side validation and rate limiting possible
- Monitoring: Centralized logging of API usage
- Maintenance: Easy to rotate keys without redeployment
- Compliance: Follows industry best practices
Support & Questions
- Technical Details: See
MIGRATION_GUIDE.md - Local Setup: See
README.md - Issues: Open GitHub issue or contact team via Slack
Status: ✅ READY FOR DEPLOYMENT Security Level: 🔒 SECURE Action Required: Deploy to Netlify and revoke old API key