A safe, pure JavaScript SQL injection detection library with zero dependencies.
- π Zero dependencies - No supply chain risks
- π Pure JavaScript - No Python, no external processes
- π― High accuracy - Detects 20+ attack patterns
- π Risk scoring - Threat levels from SAFE to CRITICAL
- π PostgreSQL support - Detects
pg_sleep,pg_read_file, etc. - π§ TypeScript ready - Full type definitions included
# From GitHub Packages
npm install @ap0h/sqli-detector --registry=https://npm.pkg.github.comOr add to your .npmrc:
@ap0h:registry=https://npm.pkg.github.com
Then:
npm install @ap0h/sqli-detectorconst { detectSql, sanitize } = require('@ap0h/sqli-detector');
// Detect SQL injection
const result = detectSql("' OR '1'='1");
console.log(result.isSqlInjection); // true
console.log(result.threatLevel); // 'HIGH'const { detectSql } = require('@ap0h/sqli-detector');
const express = require('express');
const app = express();
// Middleware to protect against SQL injection
function sqlInjectionGuard(req, res, next) {
for (const [key, value] of Object.entries(req.query)) {
if (typeof value !== 'string') continue;
const result = detectSql(value);
if (result.isSqlInjection) {
console.log(`π¨ SQL Injection blocked!`);
console.log(` Parameter: ${key}`);
console.log(` Value: ${value}`);
console.log(` Threat: ${result.threatLevel}`);
console.log(` Score: ${result.score}`);
return res.status(400).json({ error: 'Invalid input' });
}
}
next();
}
app.use(sqlInjectionGuard);
// Your protected endpoint
app.get('/api/search', (req, res) => {
const search = req.query.q;
// Safe to use with parameterized query
res.json({ results: [] });
});
app.listen(3000);GET /api/search?q='1'='1' OR pg_sleep(5)--
Console output:
π¨ SQL Injection blocked!
Parameter: q
Value: '1'='1' OR pg_sleep(5)--
Threat: CRITICAL
Score: 25
Response: 400 Bad Request
GET /api/search?q=bitcoin β β
Allowed (score: 0)
GET /api/search?q=ethereum%20price β β
Allowed (score: 0)
GET /api/search?q=O'Brien β β
Allowed (score: 0)
Main detection function.
const result = detectSql("' UNION SELECT * FROM users--");
// Result:
{
isSqlInjection: true,
score: 15,
threatLevel: 'HIGH', // 'SAFE' | 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL'
tokens: ['UNION', 'COMMENT']
}Options:
threshold(number, default: 5) - Score threshold for detectiondetailed(boolean, default: false) - Include match details
Get detailed analysis with explanations.
const { explain } = require('@ap0h/sqli-detector');
const analysis = explain("' OR '1'='1");
// Returns:
{
isSqlInjection: true,
score: 12,
threatLevel: 'HIGH',
findings: [
{
pattern: 'BOOLEAN',
explanation: 'Boolean-based injection pattern',
matches: ["'1'='1"]
},
{
pattern: 'TAUTOLOGY',
explanation: 'Tautology attack (always-true condition)',
matches: ["' OR '1'='1"]
}
],
recommendation: 'BLOCK this input - high likelihood of SQL injection attack'
}Basic string escaping. Always prefer parameterized queries!
const { sanitize } = require('@ap0h/sqli-detector');
sanitize("O'Brien"); // "O''Brien"
sanitize("test\\path"); // "test\\\\path"Promise-based detection for async workflows.
const { detectSqlAsync } = require('@ap0h/sqli-detector');
const result = await detectSqlAsync(userInput);
if (!result.success) {
// SQL injection detected - block request
}| Attack Type | Example | Threat Level |
|---|---|---|
| Boolean injection | ' OR '1'='1 |
HIGH |
| UNION injection | ' UNION SELECT * FROM users-- |
HIGH |
| DROP TABLE | '; DROP TABLE users;-- |
CRITICAL |
| Time-based (MySQL) | ' AND SLEEP(5)-- |
HIGH |
| Time-based (PostgreSQL) | `' | |
| Login bypass | admin'-- |
MEDIUM |
| File read | LOAD_FILE('/etc/passwd') |
CRITICAL |
| Schema enumeration | information_schema.tables |
HIGH |
| CHAR bypass | CHAR(65)+CHAR(66) |
MEDIUM |
| Comment injection | admin'/* |
MEDIUM |
| Level | Score | Action |
|---|---|---|
| SAFE | 0 | Allow |
| LOW | 1-4 | Allow (monitor) |
| MEDIUM | 5-9 | Block or review |
| HIGH | 10-19 | Block |
| CRITICAL | 20+ | Block + alert |
This detector is a first line of defense. Always combine with:
// β
ALWAYS use parameterized queries
const results = await db.query(
'SELECT * FROM users WHERE name = $1',
[userInput] // Parameterized - safe even if injection slips through
);
// β NEVER concatenate user input into SQL
const results = await db.query(
`SELECT * FROM users WHERE name = '${userInput}'` // DANGEROUS!
);Full TypeScript support included:
import { detectSql, DetectionResult } from '@ap0h/sqli-detector';
const result: DetectionResult = detectSql(userInput);
if (result.isSqlInjection) {
// Handle attack
}If this library helped protect your application, consider supporting development:
Solana: 25iWiaiKvUjkG1DoA9MujPiEdE6kTbKokchfE4ohNMnT
MIT
Issues and PRs welcome at GitHub.