[SECURITY] SQL injection via string concatenation#6
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 database access function with critical SQL injection vulnerability due to direct string interpolation of user input. It lacks input validation and proper query parameterization, presenting a significant security risk. Key FindingsBugs
Security
Performance
Recommended FixesGeneral
Security Fixes
Performance Fixes
Suggested Tests
const db = { execute: jest.fn() };
// Mock the db module or object used in the src/db.js
jest.mock('../src/db', () => ({
db
}), { virtual: true });
const { getUserByName } = require('../src/db');
describe('getUserByName', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should return result of db.execute with correct query for a normal name', () => {
db.execute.mockReturnValueOnce(Promise.resolve([{ id: 1, name: 'Alice' }]));
const name = 'Alice';
const expectedQuery = "SELECT * FROM users WHERE name = 'Alice'";
const result = getUserByName(name);
expect(db.execute).toHaveBeenCalledWith(expectedQuery);
expect(result).toEqual(Promise.resolve([{ id: 1, name: 'Alice' }]));
});
it('should correctly form query with single-character name', () => {
const name = 'A';
const expectedQuery = "SELECT * FROM users WHERE name = 'A'";
getUserByName(name);
expect(db.execute).toHaveBeenCalledWith(expectedQuery);
});
it('should correctly form query with empty string name', () => {
const name = '';
const expectedQuery = "SELECT * FROM users WHERE name = ''";
getUserByName(name);
expect(db.execute).toHaveBeenCalledWith(expectedQuery);
});
it('should correctly handle name containing single quote characters', () => {
const name = "O'Brien";
const expectedQuery = "SELECT * FROM users WHERE name = 'O'Brien'";
getUserByName(name);
expect(db.execute).toHaveBeenCalledWith(expectedQuery);
});
it('should correctly handle name with SQL injection attempt', () => {
const name = "Robert'; DROP TABLE users;--";
const expectedQuery = "SELECT * FROM users WHERE name = 'Robert'; DROP TABLE users;--'";
getUserByName(name);
expect(db.execute).toHaveBeenCalledWith(expectedQuery);
});
it('should call db.execute even if name is null or undefined', () => {
getUserByName(null);
expect(db.execute).toHaveBeenCalledWith("SELECT * FROM users WHERE name = 'null'");
getUserByName(undefined);
expect(db.execute).toHaveBeenCalledWith("SELECT * FROM users WHERE name = 'undefined'");
});
it('should not modify the input argument', () => {
const name = 'Alice';
const nameCopy = name.slice();
getUserByName(name);
expect(name).toBe(nameCopy);
});
it('should return a Promise as returned by db.execute', () => {
const mockReturn = Promise.resolve([{ id: 2, name: 'Bob' }]);
db.execute.mockReturnValue(mockReturn);
const result = getUserByName('Bob');
expect(result).toBeInstanceOf(Promise);
return expect(result).resolves.toEqual([{ id: 2, name: 'Bob' }]);
});
it('should throw if db.execute throws', () => {
const error = new Error('DB failure');
db.execute.mockImplementation(() => { throw error; });
expect(() => getUserByName('test')).toThrow(error);
});
});Auto-Fix AvailableAdd the 4 issues found. Reviewed by RepoSpace AI. |
- src/db.js: Fixed SQL injection vulnerability by using parameterized queries.
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.
User input concatenated directly into SQL query.