@undercurrentai/eslint-plugin-ai-guard 2.0.0-beta.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 eslint-plugin-ai-guard contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,252 @@
1
+ <p align="center">
2
+ <h1 align="center">@undercurrentai/eslint-plugin-ai-guard</h1>
3
+ <p align="center">
4
+ <strong>๐Ÿ›ก๏ธ Framework-aware security lint for JS/TS routes and webhooks.</strong>
5
+ <br/>
6
+ <sub>Missing-auth / missing-authz / unverified-webhook detection across Express, Fastify, Hono, NestJS, and Next.js โ€” where <code>@typescript-eslint</code> and <code>eslint-plugin-security</code> don't reach.</sub>
7
+ </p>
8
+ <p align="center">
9
+ <a href="https://www.npmjs.com/package/@undercurrentai/eslint-plugin-ai-guard"><img src="https://img.shields.io/npm/v/@undercurrentai/eslint-plugin-ai-guard.svg?style=flat-square" alt="npm version"></a>
10
+ <a href="https://github.com/undercurrentai/eslint-plugin-ai-guard/actions"><img src="https://img.shields.io/github/actions/workflow/status/undercurrentai/eslint-plugin-ai-guard/ci.yml?style=flat-square&label=CI" alt="CI"></a>
11
+ <a href="https://www.npmjs.com/package/@undercurrentai/eslint-plugin-ai-guard"><img src="https://img.shields.io/npm/dm/@undercurrentai/eslint-plugin-ai-guard.svg?style=flat-square" alt="downloads"></a>
12
+ <a href="https://github.com/undercurrentai/eslint-plugin-ai-guard/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@undercurrentai/eslint-plugin-ai-guard.svg?style=flat-square" alt="license"></a>
13
+ </p>
14
+ </p>
15
+
16
+ > โš ๏ธ **Pre-release** โ€” `v2.0.0-beta.3` is queued for the first-ever npm publish. The npm version / downloads shields above will render as unknown until publish lands. The CI workflow (`CI` โ€” typecheck + build + test on Node 20/22/24, plus lint + docs:build on Node 20) fires on every push and PR. To use the plugin pre-publish, clone the repo and `npm link`. This banner will be removed from README once `@undercurrentai/eslint-plugin-ai-guard@2.0.0-beta.3` is live on npm.
17
+
18
+ ---
19
+
20
+ > **Lineage.** Forked from [YashJadhav21/eslint-plugin-ai-guard](https://github.com/YashJadhav21/eslint-plugin-ai-guard) (MIT) at v1.1.11. The `@undercurrent` fork extends that surface with the framework-aware trio (auth / authz / webhook signature) and a companion CLI, while contributing framework-agnostic correctness fixes back upstream under our dual-track policy. See [`docs/migration/v1-to-v2.md`](./docs/migration/v1-to-v2.md).
21
+
22
+ ## What this catches that other linters don't
23
+
24
+ Three rules are the reason this plugin exists:
25
+
26
+ - **`require-framework-auth`** โ€” flags route handlers with no visible authentication across Express 5, Fastify 5, Hono 4, NestJS 11, and Next.js 15 App Router. Decorator-aware (`@UseGuards`), options-object-aware (`{ preHandler: [auth] }`), filename-aware (`app/**/route.ts`), chained-route-aware (`router.route('/x').post(auth, ...)`).
27
+ - **`require-framework-authz`** โ€” flags handlers that touch `req.params.id` (or equivalents) with no visible ownership / policy check. Detects CASL `ability.can`, Casbin `enforcer.enforce`, Cerbos `cerbos.checkResource`, Permit.io `permit.check` via import-verified detection.
28
+ - **`require-webhook-signature`** โ€” flags webhook handlers without cryptographic signature verification. Recognizes Stripe (`constructEvent`), GitHub (`crypto.timingSafeEqual`), Svix (`Webhook.verify`), Slack (`createSlackEventAdapter`); filters out receivers that look like general auth (`jwt.verify`) to avoid false positives.
29
+
30
+ `@typescript-eslint` has none of these. `eslint-plugin-security` has none of these. Semgrep's free JS ruleset covers a subset of the auth case for Express only โ€” not the full breadth. This is the defensible differentiation.
31
+
32
+ The plugin also ships a broader set of code-quality and security rules (floating promises, empty catches, hardcoded secrets, eval-dynamic, SQL-concat, unsafe-deserialize) that overlap in part with `@typescript-eslint` / `eslint-plugin-security` / ESLint core. Those are useful convenience, but the framework-aware trio is what would be missing from any other stack.
33
+
34
+ ## Why AI-generated code is a common trigger
35
+
36
+ The rules above fire on any code โ€” human or agent-authored. They happen to fire heavily on AI-generated code because missing auth on new routes, unverified webhook handlers, and skipped ownership checks are three of the most consistent defects in LLM output. CodeRabbit's 2025 study of 470 AI-generated PRs found 1.7ร— more issues and 2.74ร— more security vulnerabilities than human code, with a pattern-based failure profile that linters are specifically well-suited to catch ([source](https://www.coderabbit.ai/)). CVE-2025-55346 โ€” unsafe dynamic `Function(...)` constructor RCE (CVSS 9.8, Aug 2025) โ€” is a recent in-the-wild instance of that failure class. The plugin is useful for any security-sensitive codebase; it's *especially* useful if your team is shipping Copilot / Cursor / Claude Code output without a dedicated review.
37
+
38
+ ## Install
39
+
40
+ ```bash
41
+ npm install --save-dev @undercurrentai/eslint-plugin-ai-guard@next
42
+ ```
43
+
44
+ ## ๐Ÿš€ Quick Start โ€“ CLI (no config needed)
45
+
46
+ ```bash
47
+ npx ai-guard run # recommended preset (lowest noise)
48
+ npx ai-guard run --strict
49
+ npx ai-guard run --security
50
+ npx ai-guard init # auto-creates ESLint config for you
51
+ npx ai-guard init --dry-run
52
+ npx ai-guard doctor # diagnoses setup issues
53
+ npx ai-guard baseline # track only *new* issues going forward
54
+ ```
55
+
56
+ That's it. **Zero configuration required.**
57
+
58
+ ## ๐Ÿค– Set Up AI Agent Rules
59
+
60
+ Generate instruction files so Claude Code, Cursor, and GitHub Copilot
61
+ automatically avoid the most common AI-generated anti-patterns:
62
+
63
+ ```bash
64
+ npx ai-guard init-context
65
+ ```
66
+
67
+ Follow the prompts to select your agent(s). Or generate all at once:
68
+
69
+ ```bash
70
+ npx ai-guard init-context --all
71
+ ```
72
+
73
+ This writes:
74
+
75
+ - `CLAUDE.md` โ€” read automatically by Claude Code
76
+ - `.cursorrules` โ€” read automatically by Cursor
77
+ - `.github/copilot-instructions.md` โ€” read automatically by GitHub Copilot
78
+
79
+ Your AI tools will now avoid these patterns before you even run the linter.
80
+ Use `--force` to regenerate after upgrading to a new version with new rules.
81
+
82
+ ## ๐Ÿงช Real-World Usage Philosophy
83
+
84
+ `ai-guard` is designed for production adoption in existing codebases:
85
+
86
+ 1. **Recommended preset is intentionally low-noise** to avoid overwhelming teams on day one.
87
+ 2. **Strict preset enables full enforcement** for mature teams that want maximum coverage.
88
+ 3. **Security preset focuses only on security rules** with critical issues as errors.
89
+
90
+ ## ๐ŸŽฌ Real Workspace Demo
91
+
92
+ See how `ai-guard` catches a common AI-generated async bug that silent failures in production:
93
+
94
+ ```typescript
95
+ // โŒ BAD: AI often forgets to await or wrap in Promise.all
96
+ const userIds = [1, 2, 3];
97
+ userIds.map(async (id) => {
98
+ return await fetchUser(id);
99
+ });
100
+ // โš ๏ธ ai-guard flags: Async callback passed to Array.map(). Returns Promise[], not values.
101
+
102
+ // โœ… GOOD: ai-guard recommended fix
103
+ const users = await Promise.all(userIds.map(async (id) => {
104
+ return await fetchUser(id);
105
+ }));
106
+ // โœจ ai-guard: No issues found.
107
+ ```
108
+
109
+ ### Terminal Output
110
+
111
+ ![ai-guard linting demo](./assets/example_1.png)
112
+ ![ai-guard linting demo](./assets/example_2.png)
113
+
114
+ *The terminal output above shows `ai-guard` catching multiple AI-generated anti-patterns in a single run.*
115
+
116
+ ## Rules (Recommended Preset)
117
+
118
+ ### ๐ŸŽฏ Error Handling
119
+
120
+ - **`ai-guard/no-empty-catch`** (Error)
121
+ Disallow empty catch blocks. AI tools frequently generate try/catch with empty bodies that silently swallow errors.
122
+ - **`ai-guard/no-broad-exception`** (Warn)
123
+ Disallow catching `any` or `unknown` without instance narrowing. AI tools default to `catch (e: any)` which obscures the underlying failure.
124
+ - **`ai-guard/no-catch-log-rethrow`** (Off in `recommended`, Error in `strict`)
125
+ Disallow catch blocks that only log and rethrow the same error. AI tools often generate this noisy pattern without adding recovery or context.
126
+ - **`ai-guard/no-catch-without-use`** (Off in `recommended`, Error in `strict`)
127
+ Disallow unused catch parameters. AI tools frequently add `catch (e)` while ignoring the error object entirely.
128
+ - **`ai-guard/no-duplicate-logic-block`** (Off in `recommended`, Error in `strict`)
129
+ Disallow consecutive duplicated logic blocks. AI tools often copy-paste identical code that should be consolidated.
130
+
131
+ ### โฑ๏ธ Async Stability
132
+
133
+ - **`ai-guard/no-async-array-callback`** (Warn)
134
+ Disallow async functions in `.map()`, `.filter()`, etc. AI tools frequently suggest `array.map(async ...)` expecting resolved values, creating silent bugs.
135
+ - **`ai-guard/no-floating-promise`** (Error)
136
+ Require awaiting or handling promises. AI tools frequently generate un-awaited async calls that silently swallow rejections.
137
+ - **`ai-guard/no-await-in-loop`** (Warn)
138
+ Disallow sequential `await` inside loops. AI tools frequently use `for (const x of y) await z(x)` causing O(n) latency instead of parallel `Promise.all()`.
139
+ - **`ai-guard/no-async-without-await`** (Warn)
140
+ Disallow async functions that do not use `await`. AI tools frequently add `async` by default, creating misleading function signatures.
141
+ - **`ai-guard/no-redundant-await`** (Off in `recommended`, Error in `strict`)
142
+ Disallow redundant `return await` outside try/catch/finally. AI tools often emit this pattern even when returning the Promise directly is equivalent.
143
+
144
+ ### ๐Ÿ›ก๏ธ Security
145
+
146
+ - **`ai-guard/no-hardcoded-secret`** (Error)
147
+ Disallow hardcoded keys/passwords. AI tools frequently provide examples with placeholder secrets that accidentally make it into production.
148
+ - **`ai-guard/no-eval-dynamic`** (Error)
149
+ Disallow dynamic `eval()` or `new Function()`.
150
+ - **`ai-guard/no-sql-string-concat`** (Warn in `recommended`, Error in `security`/`strict`)
151
+ Disallow variable concatenation/interpolation in SQL queries. AI tools frequently generate dangerous code enabling SQL injection.
152
+ - **`ai-guard/no-unsafe-deserialize`** (Warn in `recommended`/`security`, Error in `strict`)
153
+ Disallow `JSON.parse()` on likely untrusted inputs (like `req.body`) without visible validation.
154
+ - **`ai-guard/require-framework-auth`** (Warn in `recommended`/`security`, Error in `strict`)
155
+ Enforce authentication on routes across Express 5, Fastify 5, Hono 4, NestJS 11, and Next.js 15 App Router. Decorator-aware for NestJS (`@UseGuards`); filename-aware for Next.js (`app/**/route.ts` exported handlers).
156
+ - **`ai-guard/require-framework-authz`** (Warn in `recommended`/`security`, Error in `strict`)
157
+ Require visible ownership/policy checks when handlers access resource identifiers. Detects CASL, Casbin, Cerbos, and Permit.io patterns when imported.
158
+ - **`ai-guard/require-webhook-signature`** (Warn in `recommended`/`security`, Error in `strict`)
159
+ Require HMAC signature verification in webhook handlers. Recognizes Stripe, GitHub, Svix, and Slack patterns.
160
+ - **`ai-guard/require-auth-middleware`** *(deprecated โ€” use `require-framework-auth`)*
161
+ Legacy v1 rule. Continues to emit findings with a `[ai-guard deprecated]` prefix.
162
+ - **`ai-guard/require-authz-check`** *(deprecated โ€” use `require-framework-authz`)*
163
+ Legacy v1 rule. Continues to emit findings with a `[ai-guard deprecated]` prefix.
164
+
165
+ ### ๐Ÿงน Code Quality
166
+
167
+ - **`ai-guard/no-console-in-handler`** (Off in `recommended`, Error in `strict`)
168
+ Disallow `console.*` inside HTTP route handlers. AI tools often leave debug logs in handlers that leak internals and pollute production logs.
169
+
170
+ ### Configs
171
+
172
+ | Config | Description |
173
+ | --- | --- |
174
+ | `recommended` | Adoption-first preset: high-confidence issues as `error`, context-sensitive rules as `warn`/`off` |
175
+ | `strict` | All rules at `error` โ€” for teams that want maximum coverage |
176
+ | `framework` | The 3 framework-aware rules (auth, authz, webhook signature) โ€” drop-in for v2.x users |
177
+ | `security` | Security-only rules: critical issues at `error`, contextual checks at `warn` |
178
+
179
+ ### Config Examples
180
+
181
+ #### Flat Config: strict
182
+
183
+ ```javascript
184
+ import aiGuard from "@undercurrentai/eslint-plugin-ai-guard";
185
+
186
+ export default [
187
+ {
188
+ plugins: { "ai-guard": aiGuard },
189
+ rules: { ...aiGuard.configs.strict.rules }
190
+ }
191
+ ];
192
+ ```
193
+
194
+ #### Flat Config: security
195
+
196
+ ```javascript
197
+ import aiGuard from "@undercurrentai/eslint-plugin-ai-guard";
198
+
199
+ export default [
200
+ {
201
+ plugins: { "ai-guard": aiGuard },
202
+ rules: { ...aiGuard.configs.security.rules }
203
+ }
204
+ ];
205
+ ```
206
+
207
+ ## Why This Exists
208
+
209
+ AI coding assistants generate code that **looks correct** but has subtle structural issues:
210
+
211
+ - ๐Ÿ•ณ๏ธ **Empty catch blocks** โ€” errors vanish silently
212
+ - โณ **`array.map(async ...)`** โ€” returns `Promise[]`, not resolved values
213
+ - ๐Ÿ”ฅ **Floating promises** โ€” `fetchData()` without `await` = silent failures
214
+
215
+ These patterns pass TypeScript and existing linters. `ai-guard` catches them.
216
+
217
+ ## Supported Environments
218
+
219
+ - **ESLint** 9.x (flat config). ESLint 8 legacy config: stay on the upstream `eslint-plugin-ai-guard@1.x`.
220
+ - **Node.js** โ‰ฅ 20 (Node 18 is EOL as of 2025-04-30, and `@inquirer/prompts` โ€” used by `ai-guard init-context` / `preset` โ€” requires `node:util`'s `styleText` introduced in Node 20.12.0).
221
+ - **TypeScript** and JavaScript
222
+
223
+ ## Development
224
+
225
+ ```bash
226
+ git clone https://github.com/undercurrentai/eslint-plugin-ai-guard.git
227
+ cd eslint-plugin-ai-guard
228
+ npm install
229
+ npm run test # Run test suite
230
+ npm run build # Build CJS + ESM
231
+ npm run typecheck # TypeScript check
232
+ ```
233
+
234
+ ## Contributing
235
+
236
+ Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
237
+
238
+ **Rule requests:** Open an issue using the [Rule Request template](https://github.com/undercurrentai/eslint-plugin-ai-guard/issues/new).
239
+
240
+ **False positive reports:** Open an issue using the [False Positive template](https://github.com/undercurrentai/eslint-plugin-ai-guard/issues/new) โ€” we take zero false positives seriously.
241
+
242
+ **Upstream contributions (dual-track):** This fork also contributes fixes and quality improvements back to the [original upstream repo](https://github.com/YashJadhav21/eslint-plugin-ai-guard) where they align with upstream's scope.
243
+
244
+ ## License
245
+
246
+ [MIT](LICENSE) โ€” free forever. No rules behind a paywall.
247
+
248
+ ---
249
+
250
+ <p align="center">
251
+ Built to make AI-assisted development safer. โšก
252
+ </p>