eslint-plugin-ai-guard 1.0.0 โ 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +310 -166
- package/dist/cli/index.js +238872 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/index.d.mts +50 -2
- package/dist/index.d.ts +50 -2
- package/dist/index.js +820 -83
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +820 -83
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -3
package/README.md
CHANGED
|
@@ -1,166 +1,310 @@
|
|
|
1
|
-
<p align="center">
|
|
2
|
-
<
|
|
3
|
-
<
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
</p>
|
|
13
|
-
|
|
14
|
-
---
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
##
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://img.shields.io/npm/v/eslint-plugin-ai-guard.svg?style=for-the-badge&color=6366f1" alt="npm version" />
|
|
3
|
+
<img src="https://img.shields.io/github/actions/workflow/status/YashJadhav21/eslint-plugin-ai-guard/ci.yml?style=for-the-badge&label=CI" alt="CI" />
|
|
4
|
+
<img src="https://img.shields.io/npm/dm/eslint-plugin-ai-guard.svg?style=for-the-badge&color=22c55e" alt="downloads" />
|
|
5
|
+
<img src="https://img.shields.io/npm/l/eslint-plugin-ai-guard.svg?style=for-the-badge&color=f59e0b" alt="license" />
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
<h1 align="center">eslint-plugin-ai-guard</h1>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
<strong>The ESLint plugin that catches the bugs AI tools introduce.<br>Zero config. Instant results. 17 rules targeting AI-specific patterns.</strong>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## ๐จ The Problem
|
|
17
|
+
|
|
18
|
+
AI coding assistants (Copilot, Cursor, Claude, ChatGPT) are now used by **90%+ of developers** โ but the code they generate has a problem: **it looks correct and still passes TypeScript checks, while hiding real bugs.**
|
|
19
|
+
|
|
20
|
+
Studies show AI-generated code has:
|
|
21
|
+
|
|
22
|
+
- **1.7ร more issues** per PR than human code *(CodeRabbit, 470 OSS PRs, 2025)*
|
|
23
|
+
- **2.74ร more XSS vulnerabilities** *(CodeRabbit, 2025)*
|
|
24
|
+
- **45% of AI code** introduces at least one security vulnerability *(Veracode, 2025)*
|
|
25
|
+
|
|
26
|
+
The patterns are consistent and predictable:
|
|
27
|
+
|
|
28
|
+
| Pattern | What AI Does | Consequence |
|
|
29
|
+
|---|---|---|
|
|
30
|
+
| **Floating promises** | `fetchData()` without `await` | Silent failures in production |
|
|
31
|
+
| **Empty catch blocks** | `catch (e) {}` | Errors disappear, nothing is logged |
|
|
32
|
+
| **`array.map(async ...)`** | Returns `Promise[]`, not resolved values | Silent data corruption |
|
|
33
|
+
| **Missing auth middleware** | Routes without authentication | Unauthorized access |
|
|
34
|
+
| **SQL string concat** | `"SELECT * WHERE id = " + userId` | SQL injection |
|
|
35
|
+
| **Hardcoded secrets** | API keys in source code | Credential leaks |
|
|
36
|
+
|
|
37
|
+
**Standard ESLint, TypeScript, and existing linters do not catch these.** They were built for human mistakes. AI makes different mistakes โ consistently, at scale.
|
|
38
|
+
|
|
39
|
+
`eslint-plugin-ai-guard` was built specifically for this gap.
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## โ
The Solution
|
|
44
|
+
|
|
45
|
+
`ai-guard` uses AST-based static analysis to detect **17 AI-specific bug patterns** across error handling, async correctness, and security โ before they hit production.
|
|
46
|
+
|
|
47
|
+
It works in two modes:
|
|
48
|
+
|
|
49
|
+
1. **Zero-config CLI** โ `npx ai-guard run` scans any project instantly, no setup required
|
|
50
|
+
2. **ESLint plugin** โ integrates into your existing linting pipeline and editor
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## ๐ Quick Start โ Zero Config
|
|
55
|
+
|
|
56
|
+
No installation, no setup. Run this in any JavaScript or TypeScript project:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npx ai-guard run
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**Example output:**
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
AI GUARD RESULTS
|
|
66
|
+
|
|
67
|
+
โ Scanned: src/
|
|
68
|
+
โ Duration: 843ms
|
|
69
|
+
|
|
70
|
+
Total Issues: 12 errors ยท 8 warnings
|
|
71
|
+
|
|
72
|
+
โโ By Rule โโ
|
|
73
|
+
|
|
74
|
+
โข no-floating-promise: 7
|
|
75
|
+
โข no-empty-catch: 4
|
|
76
|
+
โข no-sql-string-concat: 3
|
|
77
|
+
โข require-auth-middleware: 3
|
|
78
|
+
โข no-hardcoded-secret: 2
|
|
79
|
+
โข no-async-array-callback: 1
|
|
80
|
+
|
|
81
|
+
โโ Top Files โโ
|
|
82
|
+
|
|
83
|
+
โข src/api/users.ts (6)
|
|
84
|
+
โข src/utils/db.ts (4)
|
|
85
|
+
โข src/routes/auth.ts (3)
|
|
86
|
+
|
|
87
|
+
โโ Next Steps โโ
|
|
88
|
+
|
|
89
|
+
โน Run ai-guard baseline to save these issues and track only new ones
|
|
90
|
+
โน Run ai-guard init to wire up ESLint for your editor
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**No issues?**
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
โ No AI issues found โ your code looks clean
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## ๐ฆ Install (Full ESLint Integration)
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
npm install --save-dev eslint-plugin-ai-guard
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
> **Peer dependency:** ESLint โฅ 8.0.0
|
|
108
|
+
|
|
109
|
+
### ESLint v9 โ `eslint.config.mjs`
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
import aiGuard from 'eslint-plugin-ai-guard';
|
|
113
|
+
|
|
114
|
+
export default [
|
|
115
|
+
{
|
|
116
|
+
plugins: { 'ai-guard': aiGuard },
|
|
117
|
+
rules: { ...aiGuard.configs.recommended.rules },
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
ignores: ['.next/**', 'dist/**', 'build/**', 'coverage/**'],
|
|
121
|
+
},
|
|
122
|
+
];
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### ESLint v8 โ `.eslintrc.json`
|
|
126
|
+
|
|
127
|
+
```json
|
|
128
|
+
{
|
|
129
|
+
"plugins": ["ai-guard"],
|
|
130
|
+
"extends": ["plugin:ai-guard/recommended"],
|
|
131
|
+
"ignorePatterns": [".next/", "dist/", "build/", "coverage/"]
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
That's it. **Zero configuration required to get started.**
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## โก CLI Commands
|
|
140
|
+
|
|
141
|
+
The `ai-guard` CLI makes onboarding instant. No ESLint knowledge needed.
|
|
142
|
+
|
|
143
|
+
| Command | Description |
|
|
144
|
+
|---|---|
|
|
145
|
+
| `ai-guard run` | Scan your project with zero config. The most important command. |
|
|
146
|
+
| `ai-guard run --strict` | Use the strict preset โ all 17 rules at `error` |
|
|
147
|
+
| `ai-guard run --security` | Security rules only |
|
|
148
|
+
| `ai-guard run --json` | Machine-readable JSON output for CI |
|
|
149
|
+
| `ai-guard init` | Auto-configure your ESLint config (generates or patches safely) |
|
|
150
|
+
| `ai-guard doctor` | Diagnose setup issues with exact fix commands |
|
|
151
|
+
| `ai-guard preset` | Interactively choose and apply a preset |
|
|
152
|
+
| `ai-guard ignore` | Add default ignore patterns (`.next`, `dist`, `build`) |
|
|
153
|
+
| `ai-guard baseline` | Save current issues; future runs show only *new* problems |
|
|
154
|
+
|
|
155
|
+
### Gradual Adoption (Recommended for Existing Projects)
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
# Step 1: Scan your project
|
|
159
|
+
npx ai-guard run
|
|
160
|
+
|
|
161
|
+
# Step 2: Save the current state as a baseline
|
|
162
|
+
npx ai-guard baseline --save
|
|
163
|
+
|
|
164
|
+
# Step 3: From now on, only new issues will be flagged
|
|
165
|
+
npx ai-guard baseline --check
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
This means you can adopt `ai-guard` on a large existing codebase **without being overwhelmed** on day one.
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## ๐ Rules
|
|
173
|
+
|
|
174
|
+
### โ ๏ธ Error Handling (5 rules)
|
|
175
|
+
|
|
176
|
+
| Rule | Recommended | Description |
|
|
177
|
+
|---|---|---|
|
|
178
|
+
| [`no-empty-catch`](docs/rules/no-empty-catch.md) | `error` | Empty catch blocks silently swallow errors |
|
|
179
|
+
| [`no-broad-exception`](docs/rules/no-broad-exception.md) | `warn` | `catch (e: any)` hides error taxonomy |
|
|
180
|
+
| [`no-catch-log-rethrow`](docs/rules/no-catch-log-rethrow.md) | `off` | Log-then-rethrow adds noise, no recovery |
|
|
181
|
+
| [`no-catch-without-use`](docs/rules/no-catch-without-use.md) | `off` | Unused catch parameter `e` |
|
|
182
|
+
| [`no-duplicate-logic-block`](docs/rules/no-duplicate-logic-block.md) | `off` | Copy-pasted logic blocks |
|
|
183
|
+
|
|
184
|
+
### โฑ๏ธ Async Correctness (5 rules)
|
|
185
|
+
|
|
186
|
+
| Rule | Recommended | Description |
|
|
187
|
+
|---|---|---|
|
|
188
|
+
| [`no-floating-promise`](docs/rules/no-floating-promise.md) | `error` | Un-awaited async calls silently swallow rejections |
|
|
189
|
+
| [`no-async-array-callback`](docs/rules/no-async-array-callback.md) | `warn` | `array.map(async ...)` returns `Promise[]`, not values |
|
|
190
|
+
| [`no-await-in-loop`](docs/rules/no-await-in-loop.md) | `warn` | Sequential `await` in loops โ should use `Promise.all` |
|
|
191
|
+
| [`no-async-without-await`](docs/rules/no-async-without-await.md) | `warn` | `async` function that never uses `await` |
|
|
192
|
+
| [`no-redundant-await`](docs/rules/no-redundant-await.md) | `off` | `return await` outside try/catch โ redundant wrapper |
|
|
193
|
+
|
|
194
|
+
### ๐ก๏ธ Security (6 rules)
|
|
195
|
+
|
|
196
|
+
| Rule | Recommended | Description |
|
|
197
|
+
|---|---|---|
|
|
198
|
+
| [`no-hardcoded-secret`](docs/rules/no-hardcoded-secret.md) | `error` | API keys / passwords in source code |
|
|
199
|
+
| [`no-eval-dynamic`](docs/rules/no-eval-dynamic.md) | `error` | `eval()` or `new Function()` with dynamic input |
|
|
200
|
+
| [`no-sql-string-concat`](docs/rules/no-sql-string-concat.md) | `warn` | String concatenation in SQL queries |
|
|
201
|
+
| [`no-unsafe-deserialize`](docs/rules/no-unsafe-deserialize.md) | `warn` | `JSON.parse()` on untrusted input without validation |
|
|
202
|
+
| [`require-auth-middleware`](docs/rules/require-auth-middleware.md) | `warn` | Express/Fastify routes without auth middleware |
|
|
203
|
+
| [`require-authz-check`](docs/rules/require-authz-check.md) | `warn` | Route handlers accessing resources without ownership check |
|
|
204
|
+
|
|
205
|
+
### ๐งน Code Quality (1 rule)
|
|
206
|
+
|
|
207
|
+
| Rule | Recommended | Description |
|
|
208
|
+
|---|---|---|
|
|
209
|
+
| [`no-console-in-handler`](docs/rules/no-console-in-handler.md) | `off` | `console.*` inside route handlers leaks internals |
|
|
210
|
+
|
|
211
|
+
### Presets
|
|
212
|
+
|
|
213
|
+
| Preset | Use case |
|
|
214
|
+
|---|---|
|
|
215
|
+
| `recommended` | **Start here.** High-confidence issues at `error`, context-sensitive at `warn`/`off`. Low noise. |
|
|
216
|
+
| `strict` | All 17 rules at `error`. For mature codebases ready for full enforcement. |
|
|
217
|
+
| `security` | Security rules only. For AppSec teams and security-focused audits. |
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## ๐ Real-World Results
|
|
222
|
+
|
|
223
|
+
| Project Type | Issues Found | False Positives |
|
|
224
|
+
|---|---|---|
|
|
225
|
+
| Clean utility library | 0 | 0 |
|
|
226
|
+
| Express.js backend | 400+ | 0 |
|
|
227
|
+
| Next.js full-stack app | 180+ | 0 |
|
|
228
|
+
|
|
229
|
+
The recommended preset is calibrated for **near-zero false positives** on real codebases.
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## ๐ง Why This Exists
|
|
234
|
+
|
|
235
|
+
AI models generate code **statistically from training data** โ not semantically from understanding your system. They make the same structural mistakes, consistently, at scale:
|
|
236
|
+
|
|
237
|
+
- They add `async` by default even when not needed
|
|
238
|
+
- They generate `catch (e) {}` placeholders and never fill them in
|
|
239
|
+
- They write SQL queries by string concatenation because that's the pattern they've seen
|
|
240
|
+
- They omit auth middleware because examples they trained on often don't show the full middleware chain
|
|
241
|
+
|
|
242
|
+
These patterns are **static-detectable** using AST rules. You don't need AI to lint AI code โ you need fast, offline, zero-latency static analysis that catches these specific patterns before they hit production.
|
|
243
|
+
|
|
244
|
+
`ai-guard` is that tool.
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## ๐ง CI Integration
|
|
249
|
+
|
|
250
|
+
```yaml
|
|
251
|
+
# .github/workflows/ai-guard.yml
|
|
252
|
+
name: AI Guard
|
|
253
|
+
|
|
254
|
+
on: [push, pull_request]
|
|
255
|
+
|
|
256
|
+
jobs:
|
|
257
|
+
ai-guard:
|
|
258
|
+
runs-on: ubuntu-latest
|
|
259
|
+
steps:
|
|
260
|
+
- uses: actions/checkout@v4
|
|
261
|
+
- uses: actions/setup-node@v4
|
|
262
|
+
with:
|
|
263
|
+
node-version: 20
|
|
264
|
+
- run: npm ci
|
|
265
|
+
- run: npx ai-guard run --json --max-warnings 0
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Or use the ESLint integration in your existing `eslint` CI step โ no separate job needed.
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## ๐ Documentation
|
|
273
|
+
|
|
274
|
+
- **[Rules โ](docs/rules/)** โ Full documentation for all 17 rules
|
|
275
|
+
- **[CLI Reference โ](docs/cli/overview.md)** โ All CLI commands with options and examples
|
|
276
|
+
- **[Getting Started โ](docs/guides/getting-started.md)** โ Step-by-step setup guide
|
|
277
|
+
- **[Migrating an Existing Project โ](docs/guides/migrating-existing-project.md)** โ Adopt ai-guard without disruption
|
|
278
|
+
- **[CI Integration โ](docs/guides/ci-integration.md)** โ GitHub Actions, pre-commit hooks, and more
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## ๐ ๏ธ Development
|
|
283
|
+
|
|
284
|
+
```bash
|
|
285
|
+
git clone https://github.com/YashJadhav21/eslint-plugin-ai-guard.git
|
|
286
|
+
cd eslint-plugin-ai-guard
|
|
287
|
+
npm install
|
|
288
|
+
npm run test # Run test suite
|
|
289
|
+
npm run build # Build plugin + CLI
|
|
290
|
+
npm run typecheck # TypeScript check
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## ๐ค Contributing
|
|
294
|
+
|
|
295
|
+
Contributions are welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
296
|
+
|
|
297
|
+
- **Rule requests** โ [Open an issue](https://github.com/YashJadhav21/eslint-plugin-ai-guard/issues/new)
|
|
298
|
+
- **False positive reports** โ We take these seriously. [Report here](https://github.com/YashJadhav21/eslint-plugin-ai-guard/issues/new)
|
|
299
|
+
- **Security issues** โ Email directly; do not open a public issue
|
|
300
|
+
|
|
301
|
+
## License
|
|
302
|
+
|
|
303
|
+
[MIT](LICENSE) โ free forever. No rules behind a paywall.
|
|
304
|
+
|
|
305
|
+
---
|
|
306
|
+
|
|
307
|
+
<p align="center">
|
|
308
|
+
Built to make AI-assisted development safer. โก<br>
|
|
309
|
+
<sub>If this saved you from a production bug, consider giving it a โญ</sub>
|
|
310
|
+
</p>
|