-
Notifications
You must be signed in to change notification settings - Fork 189
Enhance API Security #108
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
Closed
tembo
wants to merge
4
commits into
staging
from
tembo/assess-api-security-prevent-injection-vulnerabilities
Closed
Enhance API Security #108
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3241d21
feat(security): Implement comprehensive API security measures
tembo[bot] 56e92dc
fix(security): refine SQL validator to detect suspicious stacked queries
tembo[bot] b52026d
fix(security): Address code review feedback on injection prevention
tembo[bot] c3f3789
fix(security): improve SQL/XSS validation and input allowlisting
tembo[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| import { describe, expect, test } from 'bun:test'; | ||
| import { validateSQL } from '../agent/utils/sql-validator'; | ||
|
|
||
| describe('Security Validations', () => { | ||
| describe('SQL Injection Prevention', () => { | ||
| test('should reject malicious SQL keywords', () => { | ||
| const maliciousQueries = [ | ||
| "SELECT * FROM users; DROP TABLE users; --", | ||
| "SELECT * FROM users WHERE id = 1 OR 1=1", | ||
| "SELECT * FROM users UNION SELECT password FROM admin", | ||
| "SELECT * FROM users; INSERT INTO admin VALUES ('hacker', 'pass')", | ||
| "SELECT * FROM users; DELETE FROM logs", | ||
| "SELECT * FROM users; UPDATE users SET password = 'hacked'", | ||
| "SELECT * FROM users; CREATE TABLE malicious (id INT)", | ||
| "SELECT * FROM users; ALTER TABLE users ADD COLUMN hacked VARCHAR(255)", | ||
| "SELECT * FROM users; EXEC xp_cmdshell 'net user'", | ||
| "SELECT * FROM users; EXECUTE sp_executesql @sql", | ||
| "SELECT * FROM users; TRUNCATE TABLE logs", | ||
| "SELECT * FROM users; BACKUP DATABASE test TO DISK", | ||
| "SELECT * FROM users; RESTORE DATABASE test FROM DISK", | ||
| "SELECT * FROM users; GRANT ALL PRIVILEGES ON *.*", | ||
| "SELECT * FROM users; REVOKE ALL PRIVILEGES ON *.*", | ||
| "SELECT * FROM users; SHOW GRANTS FOR root", | ||
| "SELECT * FROM users; SHOW USERS", | ||
| "SELECT * FROM users; SYSTEM DROP DATABASE test", | ||
| "SELECT * FROM users; ATTACH DATABASE 'test.db' AS test", | ||
| "SELECT * FROM users; DETACH DATABASE test", | ||
| "SELECT * FROM users; OPTIMIZE TABLE users", | ||
| "SELECT * FROM users; CHECK TABLE users", | ||
| "SELECT * FROM users; REPAIR TABLE users", | ||
| "SELECT * FROM users; ANALYZE TABLE users", | ||
| ]; | ||
|
|
||
| for (const query of maliciousQueries) { | ||
| expect(validateSQL(query)).toBe(false); | ||
| } | ||
| }); | ||
|
|
||
| test('should reject dangerous SQL patterns', () => { | ||
| const dangerousPatterns = [ | ||
| "SELECT * FROM users -- comment", | ||
| "SELECT * FROM users /* comment */", | ||
| "SELECT * FROM users; SELECT * FROM admin", | ||
| "SELECT * FROM users INTO OUTFILE '/tmp/users.txt'", | ||
| "SELECT LOAD_FILE('/etc/passwd')", | ||
| "SELECT * FROM users INTO DUMPFILE '/tmp/users.txt'", | ||
| "SELECT * FROM users; SHOW PROCESSLIST", | ||
| "SELECT * FROM INFORMATION_SCHEMA.TABLES", | ||
| "SELECT * FROM mysql.user", | ||
| "SELECT pg_user FROM pg_stat_activity", | ||
| "SELECT * FROM users UNION ALL SELECT * FROM admin", | ||
| ]; | ||
|
|
||
| for (const query of dangerousPatterns) { | ||
| expect(validateSQL(query)).toBe(false); | ||
| } | ||
| }); | ||
|
|
||
| test('should allow safe SELECT queries', () => { | ||
| const safeQueries = [ | ||
| "SELECT id, name FROM users WHERE active = 1", | ||
| "SELECT COUNT(*) FROM orders WHERE date > '2023-01-01'", | ||
| "SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id", | ||
| "WITH top_users AS (SELECT * FROM users ORDER BY score DESC LIMIT 10) SELECT * FROM top_users", | ||
| "SELECT name FROM users WHERE created_at BETWEEN '2023-01-01' AND '2023-12-31'", | ||
| ]; | ||
|
|
||
| for (const query of safeQueries) { | ||
| expect(validateSQL(query)).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| test('should reject non-SELECT queries', () => { | ||
| const nonSelectQueries = [ | ||
| "INSERT INTO users VALUES (1, 'test')", | ||
| "UPDATE users SET name = 'test'", | ||
| "DELETE FROM users", | ||
| "DROP TABLE users", | ||
| "CREATE TABLE test (id INT)", | ||
| "ALTER TABLE users ADD COLUMN test VARCHAR(255)", | ||
| ]; | ||
|
|
||
| for (const query of nonSelectQueries) { | ||
| expect(validateSQL(query)).toBe(false); | ||
| } | ||
| }); | ||
|
|
||
| test('should reject overly long queries', () => { | ||
| const longQuery = "SELECT * FROM users WHERE " + "a = 1 AND ".repeat(1000) + "b = 2"; | ||
| expect(validateSQL(longQuery)).toBe(false); | ||
| }); | ||
|
|
||
| test('should handle edge cases', () => { | ||
| expect(validateSQL('')).toBe(false); | ||
| expect(validateSQL(' ')).toBe(false); | ||
| // @ts-ignore - testing invalid input | ||
| expect(validateSQL(null)).toBe(false); | ||
| // @ts-ignore - testing invalid input | ||
| expect(validateSQL(undefined)).toBe(false); | ||
| // @ts-ignore - testing invalid input | ||
| expect(validateSQL(123)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Input Sanitization', () => { | ||
| test('should detect XSS patterns', () => { | ||
| const xssPatterns = [ | ||
| "<script>alert('xss')</script>", | ||
| "javascript:alert('xss')", | ||
| "vbscript:alert('xss')", | ||
| "onload=alert('xss')", | ||
| "onerror=alert('xss')", | ||
| "onclick=alert('xss')", | ||
| "onmouseover=alert('xss')", | ||
| "eval(alert('xss'))", | ||
| "expression(alert('xss'))", | ||
| "data:text/html,<script>alert('xss')</script>", | ||
| "data:application/javascript,alert('xss')", | ||
| "<script>alert('xss')</script>", | ||
| "<script>alert('xss')</script>", | ||
| ]; | ||
|
|
||
| // These would be caught by our sanitization functions | ||
| // Testing that our patterns detect them correctly | ||
| const hasXSSPattern = (input: string): boolean => { | ||
| const dangerousPatterns = [ | ||
| /<script/i, | ||
| /javascript:/i, | ||
| /vbscript:/i, | ||
| /onload=/i, | ||
| /onerror=/i, | ||
| /onclick=/i, | ||
| /onmouse/i, | ||
| /eval\(/i, | ||
| /expression\(/i, | ||
| /data:text\/html/i, | ||
| /data:application/i, | ||
| /&#x/i, | ||
| /&#\d/i, | ||
| ]; | ||
| return dangerousPatterns.some(pattern => pattern.test(input)); | ||
| }; | ||
|
|
||
| for (const pattern of xssPatterns) { | ||
| expect(hasXSSPattern(pattern)).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| test('should allow safe input', () => { | ||
| const safeInputs = [ | ||
| "Hello World", | ||
| "user@example.com", | ||
| "123-456-7890", | ||
| "Safe content with normal characters", | ||
| "Product Name - Version 1.0", | ||
| ]; | ||
|
|
||
| const hasXSSPattern = (input: string): boolean => { | ||
| const dangerousPatterns = [ | ||
| /<script/i, | ||
| /javascript:/i, | ||
| /vbscript:/i, | ||
| /onload=/i, | ||
| /onerror=/i, | ||
| /onclick=/i, | ||
| /onmouse/i, | ||
| /eval\(/i, | ||
| /expression\(/i, | ||
| /data:text\/html/i, | ||
| /data:application/i, | ||
| /&#x/i, | ||
| /&#\d/i, | ||
| ]; | ||
| return dangerousPatterns.some(pattern => pattern.test(input)); | ||
| }; | ||
|
|
||
| for (const input of safeInputs) { | ||
| expect(hasXSSPattern(input)).toBe(false); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
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.
🛠️ Refactor suggestion
Update keyword scanning loop to use regex patterns.
Switch from substring includes to RegExp.test to eliminate false positives and catch EXEC() without trailing spaces.
📝 Committable suggestion