-
Notifications
You must be signed in to change notification settings - Fork 0
Add fallback bearer token for API requests #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,7 @@ const createPrompt = (inputLanguage, inputCode) => { | |
|
|
||
| async function fetchAIResponse(selectedLanguage, question) { | ||
| const API_ENDPOINT = "https://api.worqhat.com/api/ai/content/v2"; | ||
| const BEARER_TOKEN = `Bearer ${process.env.WORQHAT_API_KEY}`; | ||
| const BEARER_TOKEN = `Bearer ${process.env.WORQHAT_API_KEY}` || 'sk-2233233532232'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Bug: The fallback will never trigger. Template literals always produce a string (at minimum Prompt for AI agents
Comment on lines
39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Check if file exists and read the relevant lines
head -50 src/pages/index.js | cat -nRepository: WorqHat/AI-Code-Generator Length of output: 1723 🏁 Script executed: # Search for other API calls or token usage in the file
rg -n "BEARER_TOKEN|WORQHAT_API_KEY" src/pages/index.jsRepository: WorqHat/AI-Code-Generator Length of output: 195 🏁 Script executed: # Check for Next.js API routes that might handle this
fd -t f "\.js$" "api" | head -20Repository: WorqHat/AI-Code-Generator Length of output: 159 🏁 Script executed: # Look for env variable usage patterns in the repo
rg "NEXT_PUBLIC_" --type js | head -20Repository: WorqHat/AI-Code-Generator Length of output: 51 🏁 Script executed: # Check for environment files
ls -la | grep -E "\.env"Repository: WorqHat/AI-Code-Generator Length of output: 51 🏁 Script executed: # Search for where fetchAIResponse is called
rg -n "fetchAIResponse" src/pages/index.js -A 3 -B 3Repository: WorqHat/AI-Code-Generator Length of output: 609 🏁 Script executed: # Check full fetchAIResponse function implementation
sed -n '39,80p' src/pages/index.js | cat -nRepository: WorqHat/AI-Code-Generator Length of output: 1793 🏁 Script executed: # Check if this page uses React hooks (client-side indicator)
rg "useState|useEffect|useRef" src/pages/index.jsRepository: WorqHat/AI-Code-Generator Length of output: 273 Remove hardcoded token and fix the fallback logic; current code exposes secrets and the fallback never triggers. Line 41 hardcodes a sensitive token that will be exposed in the browser bundle. Additionally, the Move the API call to a server-side Next.js API route where 🔧 Suggested fix (server-side approach recommended)Move export default async function handler(req, res) {
const apiKey = process.env.WORQHAT_API_KEY;
if (!apiKey) {
return res.status(500).json({ error: "WORQHAT_API_KEY not configured" });
}
const BEARER_TOKEN = `Bearer ${apiKey}`;
// ... rest of logic
}Then call it from the client: const responseData = await fetch('/api/fetch-ai', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ selectedLanguage: language, question: questionText }),
}).then(r => r.json());🤖 Prompt for AI AgentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 HIGH — Hardcoded credential as env var fallback Category: Rotate/remove the value and move it to a secure secrets store (e.g. environment variables, vault). — Shipgate Security There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Potential issue | High ⚙️ Analysis chainRemove hardcoded API key fallback; current code exposes secrets in client-side bundles.
Proposed solutions: Move the token handling to a server-side API route or middleware. If client-side use is unavoidable, fail fast when the env var is missing instead of providing a hardcoded fallback. 🔧 Suggested fix (server-side approach recommended)// Remove the fallback entirely and validate the env var at build/runtime
const BEARER_TOKEN = process.env.WORQHAT_API_KEY
? `Bearer ${process.env.WORQHAT_API_KEY}`
: null;
if (!BEARER_TOKEN) {
throw new Error('WORQHAT_API_KEY environment variable is required');
}🤖 Prompt for AI Agents"Remove the hardcoded API key fallback in line 41 and replace with a server-side validation that throws an error if WORQHAT_API_KEY is missing." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Potential issue | High ⚙️ Analysis chainRemove hardcoded API key fallback; current code exposes secrets in client-side bundles.
Proposed solutions: Remove the fallback entirely. If the env var is missing, fail fast with a clear error or implement a secure server-side proxy. 🔧 Suggested fix (server-side approach recommended)const BEARER_TOKEN = process.env.WORQHAT_API_KEY
? `Bearer ${process.env.WORQHAT_API_KEY}`
: null;
if (!BEARER_TOKEN) {
throw new Error('WORQHAT_API_KEY environment variable is required');
}🤖 Prompt for AI Agents"Remove the hardcoded API key fallback in line 41 of src/pages/index.js. Replace with a null check for the env var and throw an error if missing. Ensure no secrets are exposed client-side." |
||
|
|
||
| const prompt = createPrompt(selectedLanguage, question); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P0: Security: Hardcoded API key
sk-2233233532232in source code. Secrets should never be committed to version control. Use environment variables exclusively and fail explicitly if the required key is missing, or use a.envfile that is gitignored.Prompt for AI agents