diff --git a/plugins/sql-macros/index.test.ts b/plugins/sql-macros/index.test.ts new file mode 100644 index 0000000..6fed422 --- /dev/null +++ b/plugins/sql-macros/index.test.ts @@ -0,0 +1,198 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import { SqlMacrosPlugin } from './index' +import { DataSource } from '../../src/types' +import { StarbaseDBConfiguration } from '../../src/handler' + +describe('SqlMacrosPlugin (plugins/sql-macros/index.ts)', () => { + let plugin: SqlMacrosPlugin + let mockExecuteQuery: ReturnType + let mockDataSource: DataSource + + beforeEach(() => { + plugin = new SqlMacrosPlugin({ preventSelectStar: false }) + mockExecuteQuery = vi.fn() + mockDataSource = { + source: 'internal', + rpc: { + executeQuery: mockExecuteQuery, + }, + } as unknown as DataSource + }) + + describe('Initialization & Registration', () => { + it('should initialize with correct plugin name and options', () => { + const p = new SqlMacrosPlugin({ preventSelectStar: true }) + expect(p.name).toBe('starbasedb:sql-macros') + expect(p.preventSelectStar).toBe(true) + }) + + it('should register middleware and capture config from context', async () => { + const mockApp = { + use: vi.fn(), + } + await plugin.register(mockApp as any) + expect(mockApp.use).toHaveBeenCalledTimes(1) + + // Test captured middleware function + const middleware = mockApp.use.mock.calls[0][0] + const mockContext = { + get: vi.fn().mockReturnValue({ role: 'admin' }), + } + const next = vi.fn() + + await middleware(mockContext, next) + expect(plugin.config).toEqual({ role: 'admin' }) + expect(next).toHaveBeenCalledTimes(1) + }) + }) + + describe('Select Star Prevention (checkSelectStar)', () => { + beforeEach(() => { + plugin = new SqlMacrosPlugin({ preventSelectStar: true }) + plugin.config = { role: 'user' } as StarbaseDBConfiguration + }) + + it('should throw an error on SELECT * for non-admin users', async () => { + await expect( + plugin.beforeQuery({ + sql: 'SELECT * FROM customers;', + dataSource: mockDataSource, + }) + ).rejects.toThrow( + 'SELECT * is not allowed. Please specify explicit columns.' + ) + }) + + it('should allow explicit column selection on SELECT queries', async () => { + const result = await plugin.beforeQuery({ + sql: 'SELECT id, email, created_at FROM customers;', + dataSource: mockDataSource, + }) + + expect(result.sql).toContain( + 'SELECT id, email, created_at FROM customers' + ) + }) + + it('should allow SELECT * when user role is admin', async () => { + plugin.config = { role: 'admin' } as StarbaseDBConfiguration + + const result = await plugin.beforeQuery({ + sql: 'SELECT * FROM customers;', + dataSource: mockDataSource, + }) + + expect(result.sql).toBe('SELECT * FROM customers;') + }) + + it('should not throw error on non-SELECT statements (INSERT/UPDATE/DELETE)', async () => { + const result = await plugin.beforeQuery({ + sql: 'UPDATE customers SET active = 1 WHERE id = 5;', + dataSource: mockDataSource, + }) + + expect(result.sql).toContain('UPDATE customers SET active = 1') + }) + + it('should gracefully return original SQL if SQL syntax is invalid in checkSelectStar', async () => { + const malformedSql = 'SELECT FROM WHERE INVALID;' + const result = await plugin.beforeQuery({ + sql: malformedSql, + dataSource: mockDataSource, + }) + + expect(result.sql).toBe(malformedSql) + }) + }) + + describe('Exclude Columns Macro ($exclude / replaceExcludeColumns)', () => { + beforeEach(() => { + plugin = new SqlMacrosPlugin({ preventSelectStar: false }) + }) + + it('should return original SQL if dataSource is missing', async () => { + const sql = 'SELECT id FROM users;' + const result = await plugin.beforeQuery({ sql }) + expect(result.sql).toBe(sql) + }) + + it('should return original SQL if dataSource.source is not internal', async () => { + const externalDataSource = { + source: 'postgres', + rpc: { executeQuery: mockExecuteQuery }, + } as unknown as DataSource + + const sql = 'SELECT $_exclude(password) FROM users;' + const result = await plugin.beforeQuery({ + sql, + dataSource: externalDataSource, + }) + + expect(result.sql).toBe(sql) + expect(mockExecuteQuery).not.toHaveBeenCalled() + }) + + it('should return original SQL for pragma_table_info queries', async () => { + const pragmaSql = 'SELECT * FROM pragma_table_info("users");' + const result = await plugin.beforeQuery({ + sql: pragmaSql, + dataSource: mockDataSource, + }) + + expect(result.sql).toBe(pragmaSql) + expect(mockExecuteQuery).not.toHaveBeenCalled() + }) + + it('should replace $_exclude with remaining columns for a single excluded column', async () => { + mockExecuteQuery.mockResolvedValueOnce([ + { column_name: 'id' }, + { column_name: 'username' }, + { column_name: 'password_hash' }, + { column_name: 'email' }, + ]) + + const sql = 'SELECT $_exclude(password_hash) FROM users;' + const result = await plugin.beforeQuery({ + sql, + dataSource: mockDataSource, + }) + + expect(mockExecuteQuery).toHaveBeenCalledTimes(1) + expect(result.sql).toContain('`id`') + expect(result.sql).toContain('`username`') + expect(result.sql).toContain('`email`') + expect(result.sql).not.toContain('password_hash') + }) + + it('should replace $_exclude with multiple excluded columns', async () => { + mockExecuteQuery.mockResolvedValueOnce([ + { column_name: 'id' }, + { column_name: 'secret_key' }, + { column_name: 'token' }, + { column_name: 'public_name' }, + ]) + + const sql = 'SELECT $_exclude(secret_key, token) FROM accounts;' + const result = await plugin.beforeQuery({ + sql, + dataSource: mockDataSource, + }) + + expect(result.sql).toContain('`id`') + expect(result.sql).toContain('`public_name`') + expect(result.sql).not.toContain('secret_key') + expect(result.sql).not.toContain('token') + }) + + it('should handle queries without any $_exclude function normally', async () => { + const sql = 'SELECT id, title FROM posts;' + const result = await plugin.beforeQuery({ + sql, + dataSource: mockDataSource, + }) + + expect(result.sql).toBe(sql) + expect(mockExecuteQuery).not.toHaveBeenCalled() + }) + }) +}) diff --git a/src/allowlist/index.test.ts b/src/allowlist/index.test.ts new file mode 100644 index 0000000..4cc724d --- /dev/null +++ b/src/allowlist/index.test.ts @@ -0,0 +1,169 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import { isQueryAllowed } from './index' +import { DataSource } from '../types' +import { StarbaseDBConfiguration } from '../handler' + +describe('Query Allowlist Module (src/allowlist/index.ts)', () => { + let mockExecuteQuery: ReturnType + let mockDataSource: DataSource + let mockConfig: StarbaseDBConfiguration + + beforeEach(() => { + mockExecuteQuery = vi.fn() + mockDataSource = { + source: 'internal', + rpc: { + executeQuery: mockExecuteQuery, + }, + } as unknown as DataSource + + mockConfig = { + role: 'user', + } as StarbaseDBConfiguration + }) + + describe('Feature Toggle & Administrative Bypass', () => { + it('should return true immediately when allowlist is disabled (isEnabled: false)', async () => { + const result = await isQueryAllowed({ + sql: 'DROP TABLE users;', + isEnabled: false, + dataSource: mockDataSource, + config: mockConfig, + }) + + expect(result).toBe(true) + expect(mockExecuteQuery).not.toHaveBeenCalled() + }) + + it('should bypass allowlist check and return true when config.role is admin', async () => { + mockConfig.role = 'admin' + + const result = await isQueryAllowed({ + sql: 'SELECT * FROM sensitive_audit_logs;', + isEnabled: true, + dataSource: mockDataSource, + config: mockConfig, + }) + + expect(result).toBe(true) + expect(mockExecuteQuery).not.toHaveBeenCalled() + }) + }) + + describe('Validation & Edge Cases', () => { + it('should return an Error object if no SQL is provided', async () => { + mockExecuteQuery.mockResolvedValueOnce([]) + + const result = await isQueryAllowed({ + sql: '', + isEnabled: true, + dataSource: mockDataSource, + config: mockConfig, + }) + + expect(result).toBeInstanceOf(Error) + expect((result as Error).message).toBe( + 'No SQL provided for allowlist check' + ) + }) + + it('should gracefully handle loadAllowlist failure and reject queries', async () => { + mockExecuteQuery.mockRejectedValueOnce( + new Error('DB Connection Timeout') + ) + // Second call for addRejectedQuery + mockExecuteQuery.mockResolvedValueOnce([]) + + await expect( + isQueryAllowed({ + sql: 'SELECT id FROM users;', + isEnabled: true, + dataSource: mockDataSource, + config: mockConfig, + }) + ).rejects.toThrow('Query not allowed') + }) + }) + + describe('AST Matching & Query Normalization', () => { + it('should allow a query that exactly matches an allowlisted query (ignoring trailing semicolon)', async () => { + mockExecuteQuery.mockResolvedValueOnce([ + { + sql_statement: 'SELECT id, name FROM users', + source: 'internal', + }, + ]) + + const result = await isQueryAllowed({ + sql: 'SELECT id, name FROM users;', + isEnabled: true, + dataSource: mockDataSource, + config: mockConfig, + }) + + expect(result).toBe(true) + expect(mockExecuteQuery).toHaveBeenCalledTimes(1) + }) + + it('should allow queries matching the AST structure of allowlisted queries', async () => { + mockExecuteQuery.mockResolvedValueOnce([ + { + sql_statement: + 'SELECT id, email FROM accounts WHERE active = 1', + source: 'internal', + }, + ]) + + const result = await isQueryAllowed({ + sql: 'SELECT id, email FROM accounts WHERE active = 1', + isEnabled: true, + dataSource: mockDataSource, + config: mockConfig, + }) + + expect(result).toBe(true) + }) + + it('should reject a query not matching the allowlist and log it to tmp_allowlist_rejections', async () => { + mockExecuteQuery.mockResolvedValueOnce([ + { + sql_statement: 'SELECT id FROM users', + source: 'internal', + }, + ]) + mockExecuteQuery.mockResolvedValueOnce([]) // for addRejectedQuery + + const unapprovedSql = 'DELETE FROM users WHERE id = 10' + + await expect( + isQueryAllowed({ + sql: unapprovedSql, + isEnabled: true, + dataSource: mockDataSource, + config: mockConfig, + }) + ).rejects.toThrow('Query not allowed') + + // Verify rejected query was inserted into rejection audit table + expect(mockExecuteQuery).toHaveBeenCalledTimes(2) + expect(mockExecuteQuery).toHaveBeenLastCalledWith({ + sql: 'INSERT INTO tmp_allowlist_rejections (sql_statement, source) VALUES (?, ?)', + params: [unapprovedSql, 'internal'], + }) + }) + + it('should handle addRejectedQuery database failure without crashing', async () => { + mockExecuteQuery.mockResolvedValueOnce([]) // Empty allowlist + mockExecuteQuery.mockRejectedValueOnce(new Error('Insert error')) + + await expect( + isQueryAllowed({ + sql: 'SELECT secret_token FROM tokens', + isEnabled: true, + dataSource: mockDataSource, + config: mockConfig, + }) + ).rejects.toThrow('Query not allowed') + }) + }) +})