[SECURITY] Introduce hardcoded API key in auth module#2
Open
GAURAV-1313 wants to merge 2 commits into
Open
Conversation
✅ Deploy Preview for nofriction canceled.
|
Owner
Author
AI Code ReviewSummaryThis PR introduces a new authentication module with a hardcoded API key and a function to fetch user data from an external API. The use of a hardcoded secret is a critical security vulnerability. Other issues include missing input validation and potential exposure of sensitive information. Key FindingsBugs
Security
Recommended FixesGeneral
Security Fixes
Suggested Tests
import { fetchUserData } from "../src/auth";
// Mock global fetch
global.fetch = jest.fn();
describe("fetchUserData", () => {
const API_KEY = "sk-hardcoded-secret-12345";
const baseUrl = "https://api.example.com/users/";
beforeEach(() => {
jest.clearAllMocks();
});
it("should call fetch with correct URL and authorization header for a valid userId", async () => {
const userId = "abc123";
const mockResponse = { json: jest.fn().mockResolvedValue({ id: userId }) };
fetch.mockResolvedValue(mockResponse);
const result = fetchUserData(userId);
expect(fetch).toHaveBeenCalledWith(`${baseUrl}${userId}`, {
headers: { Authorization: `Bearer ${API_KEY}` }
});
expect(result).toBeInstanceOf(Promise);
});
it.each(["", null, undefined])(
"should call fetch with correct URL even if userId is '%s'",
(userId) => {
fetch.mockResolvedValue({ json: jest.fn() });
fetchUserData(userId);
expect(fetch).toHaveBeenCalledWith(`${baseUrl}${userId}`, {
headers: { Authorization: `Bearer ${API_KEY}` }
});
}
);
it("should work correctly when userId is a number (converted to string in URL)", () => {
const userId = 0;
fetch.mockResolvedValue({ json: jest.fn() });
fetchUserData(userId);
expect(fetch).toHaveBeenCalledWith(`${baseUrl}${userId}`, {
headers: { Authorization: `Bearer ${API_KEY}` }
});
});
it("should reject if fetch rejects", async () => {
const userId = "fail-case";
const error = new Error("Network error");
fetch.mockRejectedValue(error);
await expect(fetchUserData(userId)).rejects.toThrow("Network error");
});
it("should not mutate the userId argument", () => {
const userId = "immutableId";
const userIdCopy = userId;
fetch.mockResolvedValue({ json: jest.fn() });
fetchUserData(userId);
expect(userId).toBe(userIdCopy);
});
});Auto-Fix AvailableAdd the 4 issues found. Reviewed by RepoSpace AI. |
- src/auth.js: Replaced hardcoded API key with an environment variable for improved security.
Owner
Author
Auto-Fix ResultsApplied (1)
Generated by RepoSpace AI. Review AI commits before merging. |
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.
Adds a hardcoded API key vulnerability.