SQL Scanner

other
SQL Scanner

๐Ÿ›ก๏ธ SQL Scanner

A PowerShell CLI tool that scans your .sql files for destructive, risky, and security-sensitive commands โ€” before they hit production.

Catch DROP TABLE, TRUNCATE, unguarded DELETE, permission changes, and more with a single command. Color-coded terminal output makes it instantly clear which files need a closer look.


Preview

  โœ” 001_create_users.sql
  โœ˜ 002_cleanup_staging.sql
      [!!] TRUNCATE (x1)
      [!!] DROP TABLE (x1)
      [!!] DELETE (no WHERE) (x1)
  โœ˜ 003_setup_permissions.sql
      [! ] GRANT (x2)
      [! ] CREATE USER (x1)
  โœ” 004_add_columns.sql

๐Ÿ”ด Red for destructive commands ยท ๐ŸŸก Yellow for permission changes ยท ๐ŸŸ  Amber for schema modifications


Quick Start

# Scan current directory
.\Scan-SqlFiles.ps1

# Scan a specific folder
.\Scan-SqlFiles.ps1 -Path "C:\projects\migrations"

# Include subfolders
.\Scan-SqlFiles.ps1 -Path "C:\deploy\sql" -Recurse

No dependencies. No modules to install. Just download and run.


What It Catches

๐Ÿ”ด HIGH โ€” Destructive & Irreversible

Pattern Example
DROP TABLE DROP TABLE users;
DROP DATABASE DROP DATABASE staging;
DROP SCHEMA DROP SCHEMA legacy;
TRUNCATE TRUNCATE TABLE audit_logs;
DELETE (no WHERE) DELETE FROM sessions;
ALTER TABLE โ€ฆ DROP ALTER TABLE users DROP COLUMN email;
DROP USER / LOGIN DROP USER readonly_svc;
xp_cmdshell EXEC xp_cmdshell 'dir';

๐ŸŸก MEDIUM โ€” Security & Permissions

Pattern Example
GRANT GRANT SELECT ON dbo.users TO app_role;
REVOKE REVOKE INSERT ON dbo.logs FROM dev;
CREATE USER CREATE USER report_reader FOR LOGIN โ€ฆ;
ALTER USER ALTER USER svc WITH DEFAULT_SCHEMA = dbo;
DROP INDEX / VIEW DROP INDEX idx_email ON users;
DROP PROCEDURE DROP PROCEDURE sp_cleanup;
OPENROWSET SELECT * FROM OPENROWSET(โ€ฆ);

๐ŸŸ  LOW โ€” Worth Reviewing

Pattern Example
ALTER TABLE ALTER TABLE users ADD is_active BIT;
EXEC / EXECUTE EXEC sp_update_stats;
BULK INSERT BULK INSERT staging FROM 'โ€ฆ';

Comment-Aware

The scanner strips SQL comments before analysis, so commented-out code won't trigger false positives.

-- DROP TABLE users;              โ† ignored
/* TRUNCATE TABLE old_data; */    โ† ignored
DROP TABLE temp_cache;            โ† flagged

CI/CD Integration

The script returns meaningful exit codes, making it easy to plug into any pipeline as a safety gate.

Exit Code Meaning
0 โœ… All clear โ€” no findings
1 โš ๏ธ Medium severity findings
2 ๐Ÿšจ High severity findings

GitHub Actions

- name: Scan SQL migrations
  shell: pwsh
  run: |
    .\Scan-SqlFiles.ps1 -Path ./migrations -Recurse
    if ($LASTEXITCODE -ge 2) { exit 1 }

Azure DevOps

- task: PowerShell@2
  inputs:
    filePath: Scan-SqlFiles.ps1
    arguments: '-Path $(Build.SourcesDirectory)/sql -Recurse'
  failOnStderr: true

Parameters

Parameter Type Default Description
-Path string . (current dir) Folder containing .sql files
-Recurse switch $false Scan subfolders recursively

Requirements

  • PowerShell 5.1+ (Windows PowerShell) or PowerShell 7+ (cross-platform)
  • No external modules or dependencies

License

MIT โ€” use it, fork it, adapt it to your workflow.