eslint-plugin-express-security 1.4.0 → 1.5.0

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.
Files changed (59) hide show
  1. package/README.md +7 -0
  2. package/package.json +5 -8
  3. package/src/index.d.ts +17 -40
  4. package/src/index.js +26 -66
  5. package/src/oxlint.d.ts +17 -20
  6. package/src/oxlint.js +0 -18
  7. package/src/rules/no-client-controlled-authorization/index.js +194 -0
  8. package/src/rules/no-cors-credentials-wildcard/index.js +0 -22
  9. package/src/rules/no-disabled-helmet-protections/index.js +125 -0
  10. package/src/rules/no-error-details-in-response/index.js +0 -26
  11. package/src/rules/no-exposed-debug-endpoints/index.js +0 -19
  12. package/src/rules/no-express-unsafe-regex-route/index.js +1 -32
  13. package/src/rules/no-graphql-introspection-production/index.js +0 -21
  14. package/src/rules/no-host-header-in-links/index.js +0 -31
  15. package/src/rules/no-idor-resource-access/index.js +126 -0
  16. package/src/rules/no-insecure-cookie-options/index.js +0 -12
  17. package/src/rules/no-missing-cors-check/index.js +4 -54
  18. package/src/rules/no-missing-csrf-protection/index.js +0 -25
  19. package/src/rules/no-missing-security-headers/index.js +0 -40
  20. package/src/rules/no-permissive-cors/index.js +0 -27
  21. package/src/rules/no-permissive-trust-proxy/index.js +106 -0
  22. package/src/rules/no-sensitive-data-in-query/index.js +1 -26
  23. package/src/rules/no-static-root-exposure/index.js +0 -21
  24. package/src/rules/no-unsafe-csp-directives/index.js +264 -0
  25. package/src/rules/no-user-controlled-redirect/index.js +0 -26
  26. package/src/rules/no-user-controlled-render-locals/index.js +0 -45
  27. package/src/rules/require-case-insensitive-path-guard/index.js +0 -21
  28. package/src/rules/require-csrf-protection/index.js +0 -42
  29. package/src/rules/require-express-body-parser-limits/index.js +0 -24
  30. package/src/rules/require-helmet/index.js +1 -25
  31. package/src/rules/require-query-type-guard/index.js +0 -20
  32. package/src/rules/require-rate-limiting/index.js +0 -13
  33. package/src/rules/require-route-authentication/index.js +225 -0
  34. package/src/rules/require-strict-transport-security/index.js +171 -0
  35. package/src/types/index.js +0 -5
  36. package/src/utils/index.js +81 -0
  37. package/AGENTS.md +0 -100
  38. package/CHANGELOG.md +0 -192
  39. package/src/rules/no-cors-credentials-wildcard/index.d.ts +0 -26
  40. package/src/rules/no-error-details-in-response/index.d.ts +0 -57
  41. package/src/rules/no-exposed-debug-endpoints/index.d.ts +0 -14
  42. package/src/rules/no-express-unsafe-regex-route/index.d.ts +0 -27
  43. package/src/rules/no-graphql-introspection-production/index.d.ts +0 -26
  44. package/src/rules/no-host-header-in-links/index.d.ts +0 -56
  45. package/src/rules/no-insecure-cookie-options/index.d.ts +0 -39
  46. package/src/rules/no-missing-cors-check/index.d.ts +0 -28
  47. package/src/rules/no-missing-csrf-protection/index.d.ts +0 -30
  48. package/src/rules/no-missing-security-headers/index.d.ts +0 -26
  49. package/src/rules/no-permissive-cors/index.d.ts +0 -28
  50. package/src/rules/no-sensitive-data-in-query/index.d.ts +0 -51
  51. package/src/rules/no-static-root-exposure/index.d.ts +0 -45
  52. package/src/rules/no-user-controlled-redirect/index.d.ts +0 -44
  53. package/src/rules/no-user-controlled-render-locals/index.d.ts +0 -49
  54. package/src/rules/require-case-insensitive-path-guard/index.d.ts +0 -59
  55. package/src/rules/require-csrf-protection/index.d.ts +0 -28
  56. package/src/rules/require-express-body-parser-limits/index.d.ts +0 -31
  57. package/src/rules/require-helmet/index.d.ts +0 -29
  58. package/src/rules/require-query-type-guard/index.d.ts +0 -54
  59. package/src/rules/require-rate-limiting/index.d.ts +0 -31
@@ -1,45 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Ofri Peretz
3
- * Licensed under the MIT License. Use of this source code is governed by the
4
- * MIT license that can be found in the LICENSE file.
5
- */
6
- /**
7
- * ESLint Rule: no-static-root-exposure
8
- *
9
- * Detects static-file middleware that exposes the application root — serving
10
- * __dirname, '.', '..', process.cwd(), or a path.join()/path.resolve() with
11
- * no subdirectory or with '..' segments — plus any use of serve-index
12
- * (directory listing). Serving the application root publishes .env,
13
- * package-lock.json, .git metadata and the server source itself.
14
- *
15
- * CWE-548: Exposure of Information Through Directory Listing
16
- * OWASP A05:2021 – Security Misconfiguration
17
- *
18
- * ## Detection method: structural-api
19
- *
20
- * This rule passes the litmus test: it fires on the AST shape of
21
- * `express.static(<root expression>)` and `serveIndex(...)` — not on route
22
- * strings or variable names. The safe pattern is a dedicated asset directory:
23
- * `express.static(path.join(__dirname, 'public'))` (any allowlisted root).
24
- *
25
- * The rule does not resolve variables holding paths
26
- * (`const root = __dirname; express.static(root)`) — a documented false
27
- * negative kept to avoid taint analysis.
28
- *
29
- * @see https://cwe.mitre.org/data/definitions/548.html
30
- * @see https://expressjs.com/en/4x/api.html#express.static
31
- */
32
- import type { TSESLint } from '@interlace/eslint-devkit';
33
- type MessageIds = 'staticRoot' | 'traversalSegments' | 'nonLiteralPath' | 'unknownRoot' | 'directoryListing' | 'scopeToSubdir';
34
- export interface Options {
35
- /**
36
- * Directory names accepted as the first path segment of a static root.
37
- * Default: ['public', 'static', 'dist', 'build', 'assets']
38
- */
39
- allowedRoots?: string[];
40
- }
41
- type RuleOptions = [Options?];
42
- export declare const noStaticRootExposure: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
43
- name: string;
44
- };
45
- export {};
@@ -1,44 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Ofri Peretz
3
- * Licensed under the MIT License. Use of this source code is governed by the
4
- * MIT license that can be found in the LICENSE file.
5
- */
6
- /**
7
- * ESLint Rule: no-user-controlled-redirect
8
- *
9
- * Detects open-redirect vulnerabilities where res.redirect() is called with a
10
- * value directly sourced from user-controlled request properties (req.query,
11
- * req.body, req.params, req.headers). An attacker can craft a link that
12
- * redirects victims to a malicious site after a legitimate-looking interaction.
13
- *
14
- * CWE-601: URL Redirection to Untrusted Site ('Open Redirect')
15
- * OWASP A01:2021 – Broken Access Control
16
- *
17
- * ## Detection method: structural-api
18
- *
19
- * This rule passes the litmus test: it fires on the AST shape of
20
- * `res.redirect(req.query.*)` — not on variable names. Rename `res` to `r`
21
- * and `req` to `q` in the source; the rule still fires because it checks the
22
- * member-access chain `<ident>.redirect(<ident>.<userSourceProp>.*)`.
23
- *
24
- * The rule only fires when the redirect argument is DIRECTLY a request
25
- * property access — it does NOT attempt data-flow or taint analysis.
26
- * `const url = req.query.url; res.redirect(url)` is not detected (the
27
- * indirect case). This conservative approach means zero false positives on
28
- * validated redirects while catching the most common unvalidated pattern.
29
- *
30
- * @see https://cwe.mitre.org/data/definitions/601.html
31
- * @see https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html
32
- */
33
- import type { TSESLint } from '@interlace/eslint-devkit';
34
- export interface Options {
35
- /** Additional response object names beyond the default set. Default: [] */
36
- responseObjects?: string[];
37
- /** Additional request object names beyond the default set. Default: [] */
38
- requestObjects?: string[];
39
- }
40
- type RuleOptions = [Options?];
41
- export declare const noUserControlledRedirect: TSESLint.RuleModule<"openRedirect", RuleOptions, unknown, TSESLint.RuleListener> & {
42
- name: string;
43
- };
44
- export {};
@@ -1,49 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Ofri Peretz
3
- * Licensed under the MIT License. Use of this source code is governed by the
4
- * MIT license that can be found in the LICENSE file.
5
- */
6
- /**
7
- * ESLint Rule: no-user-controlled-render-locals
8
- *
9
- * Detects template-object injection where res.render() receives locals that
10
- * are wholly user-controlled (req.body / req.query / req.params) — directly,
11
- * via an object spread ({ ...req.query }), or through an identifier that was
12
- * assigned a whole request source without field-picking. Also flags render
13
- * calls whose VIEW argument derives from request input (path traversal into
14
- * the template directory).
15
- *
16
- * Express merges the locals object into the view-engine options, so a request
17
- * key such as "layout", "settings", "cache" or "filename" reconfigures the
18
- * engine and can point it at an attacker-chosen file.
19
- *
20
- * CWE-73: External Control of File Name or Path
21
- * OWASP A03:2021 – Injection
22
- *
23
- * ## Detection method: structural-api
24
- *
25
- * This rule passes the litmus test: it fires on the AST shape of
26
- * `<res>.render(view, <req>.<userSourceProp>)` and its spread / single-
27
- * assignment variants — not on variable names alone. Field-picked object
28
- * literals (`{ title: req.body.title }`) are the safe pattern and are never
29
- * flagged. The rule does not perform full taint analysis: reassignments,
30
- * cross-function flow, and deep aliasing are documented false negatives.
31
- *
32
- * @see https://cwe.mitre.org/data/definitions/73.html
33
- * @see https://expressjs.com/en/4x/api.html#res.render
34
- */
35
- import type { TSESLint } from '@interlace/eslint-devkit';
36
- type MessageIds = 'unsafeRenderLocals' | 'userControlledView' | 'pickFieldsExplicitly';
37
- export interface Options {
38
- /**
39
- * Callee names that make a locals expression safe when they wrap it,
40
- * e.g. ['pick', 'sanitizeLocals']. Matches plain calls (pick(...)) and
41
- * member calls (_.pick(...)). Default: []
42
- */
43
- allowSanitizers?: string[];
44
- }
45
- type RuleOptions = [Options?];
46
- export declare const noUserControlledRenderLocals: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
47
- name: string;
48
- };
49
- export {};
@@ -1,59 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Ofri Peretz
3
- * Licensed under the MIT License. Use of this source code is governed by the
4
- * MIT license that can be found in the LICENSE file.
5
- */
6
- /**
7
- * ESLint Rule: require-case-insensitive-path-guard
8
- *
9
- * Detects path-based authorization guards that compare req.path / req.url /
10
- * req.originalUrl case-SENSITIVELY against a protected-looking path. Express
11
- * routers can match case-insensitively (`caseSensitive` defaults to false on
12
- * Router, and regex routes often carry the `i` flag), so a guard that only
13
- * checks `req.path.startsWith('/admin')` is bypassed by `GET /Admin/users` —
14
- * the route still fires, but the authorization check never does.
15
- *
16
- * CWE-178: Improper Handling of Case Sensitivity
17
- * OWASP A01:2021 – Broken Access Control
18
- *
19
- * ## Detection method: structural-api
20
- *
21
- * The rule fires on the AST shape of a case-sensitive comparison against a
22
- * `<reqIdent>.(path|url|originalUrl)` member access:
23
- *
24
- * req.path.startsWith('/admin') — prefix guard
25
- * req.url === '/admin' — equality guard (===, ==, !==, !=)
26
- * req.path.indexOf('/admin') — index guard
27
- * req.path.includes('/admin') — containment guard
28
- * req.path.match(/^\/admin/) — regex guard without the `i` flag
29
- *
30
- * ...but ONLY when the compared value looks like a protected path (default
31
- * patterns: admin, api, dashboard, internal, private — configurable via
32
- * `protectedPaths`, or `checkAllPaths: true` to flag every path guard).
33
- *
34
- * Patterns that are NOT flagged:
35
- * - `req.path.toLowerCase().startsWith('/admin')` — normalized first
36
- * - `req.path.match(/^\/admin/i)` — regex already case-insensitive
37
- * - `app.get('/admin', handler)` — route REGISTRATION, not a guard
38
- * - `const p = req.path; p.startsWith('/admin')` — indirect via a variable
39
- * (no data-flow analysis; documented false negative)
40
- *
41
- * @see https://cwe.mitre.org/data/definitions/178.html
42
- * @see https://owasp.org/Top10/A01_2021-Broken_Access_Control/
43
- */
44
- import type { TSESLint } from '@interlace/eslint-devkit';
45
- type MessageIds = 'caseSensitivePathGuard' | 'addToLowerCase' | 'addIgnoreCaseFlag';
46
- export interface Options {
47
- /**
48
- * Substrings that mark a compared value as a protected path.
49
- * Default: ['admin', 'api', 'dashboard', 'internal', 'private']
50
- */
51
- protectedPaths?: string[];
52
- /** Flag EVERY case-sensitive path guard regardless of the compared value. Default: false */
53
- checkAllPaths?: boolean;
54
- }
55
- type RuleOptions = [Options?];
56
- export declare const requireCaseInsensitivePathGuard: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
57
- name: string;
58
- };
59
- export {};
@@ -1,28 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Ofri Peretz
3
- * Licensed under the MIT License. Use of this source code is governed by the
4
- * MIT license that can be found in the LICENSE file.
5
- */
6
- /**
7
- * ESLint Rule: require-csrf-protection
8
- * Detects Express.js routes handling state-changing requests without CSRF protection
9
- * CWE-352: Cross-Site Request Forgery (CSRF)
10
- *
11
- * @see https://cwe.mitre.org/data/definitions/352.html
12
- * @see https://owasp.org/www-community/attacks/csrf
13
- */
14
- import type { TSESLint } from '@interlace/eslint-devkit';
15
- type MessageIds = 'missingCsrf' | 'addCsrf';
16
- export interface Options {
17
- /** Allow missing CSRF in test files. Default: false */
18
- allowInTests?: boolean;
19
- /** HTTP methods that require CSRF protection. Default: ['post', 'put', 'patch', 'delete'] */
20
- protectedMethods?: string[];
21
- /** Route patterns to ignore (e.g., /api/webhook). Default: [] */
22
- ignorePatterns?: string[];
23
- }
24
- type RuleOptions = [Options?];
25
- export declare const requireCsrfProtection: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
26
- name: string;
27
- };
28
- export {};
@@ -1,31 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Ofri Peretz
3
- * Licensed under the MIT License. Use of this source code is governed by the
4
- * MIT license that can be found in the LICENSE file.
5
- */
6
- /**
7
- * ESLint Rule: require-express-body-parser-limits
8
- * Detects Express.js body parsers missing or with excessive size limits
9
- * CWE-400: Uncontrolled Resource Consumption
10
- *
11
- * CVE-2024-45590 highlights DoS vulnerabilities in body-parser.
12
- * Without size limits, attackers can exhaust server memory.
13
- *
14
- * @see https://cwe.mitre.org/data/definitions/400.html
15
- * @see https://expressjs.com/en/4x/api.html#express.json
16
- */
17
- import type { TSESLint } from '@interlace/eslint-devkit';
18
- type MessageIds = 'missingLimit' | 'excessiveLimit' | 'addLimit';
19
- export interface Options {
20
- /** Allow in test files. Default: false */
21
- allowInTests?: boolean;
22
- /** Maximum allowed limit in bytes. Default: 102400 (100kb) */
23
- maxLimit?: number;
24
- /** Limits that are considered excessive (as strings). Default: ['50mb', '100mb', '500mb', '1gb'] */
25
- excessiveLimits?: string[];
26
- }
27
- type RuleOptions = [Options?];
28
- export declare const requireExpressBodyParserLimits: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
29
- name: string;
30
- };
31
- export {};
@@ -1,29 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Ofri Peretz
3
- * Licensed under the MIT License. Use of this source code is governed by the
4
- * MIT license that can be found in the LICENSE file.
5
- */
6
- /**
7
- * ESLint Rule: require-helmet
8
- * Detects Express.js applications missing helmet middleware for security headers
9
- * CWE-693: Protection Mechanism Failure
10
- *
11
- * @see https://cwe.mitre.org/data/definitions/693.html
12
- * @see https://helmetjs.github.io/
13
- * @see https://owasp.org/www-project-secure-headers/
14
- */
15
- import type { TSESLint } from '@interlace/eslint-devkit';
16
- type MessageIds = 'missingHelmet' | 'addHelmet';
17
- export interface Options {
18
- /** Allow missing helmet in test files. Default: false */
19
- allowInTests?: boolean;
20
- /** Alternative security headers middleware names to accept. Default: [] */
21
- alternativeMiddleware?: string[];
22
- /** Skip rule if helmet/security headers are provided elsewhere (e.g., reverse proxy). Default: false */
23
- assumeHelmetMiddleware?: boolean;
24
- }
25
- type RuleOptions = [Options?];
26
- export declare const requireHelmet: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
27
- name: string;
28
- };
29
- export {};
@@ -1,54 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Ofri Peretz
3
- * Licensed under the MIT License. Use of this source code is governed by the
4
- * MIT license that can be found in the LICENSE file.
5
- */
6
- /**
7
- * ESLint Rule: require-query-type-guard
8
- *
9
- * Detects string methods called on `req.query` values without a preceding
10
- * type guard. Express's qs parser yields `string | string[] | ParsedQs`:
11
- * `?name=a&name=b` produces an array and `?name[$ne]=` produces an object,
12
- * so `req.query.name.replace(...)` throws (DoS) or silently bypasses
13
- * sanitization when an attacker shapes the query string.
14
- *
15
- * CWE-843: Access of Resource Using Incompatible Type ('Type Confusion')
16
- * OWASP A03:2021 – Injection
17
- *
18
- * ## Detection method: structural-api
19
- *
20
- * Two shapes are flagged:
21
- *
22
- * 1. Direct member calls — `req.query.name.replace(...)`,
23
- * `req.query['x'].trim()`.
24
- * 2. Calls on identifiers whose most recent assignment was a raw
25
- * `req.query` member — `const term = req.query.name; term.trim()`.
26
- *
27
- * A value is considered guarded (NOT flagged) after any of:
28
- * - `typeof v === 'string'` (any of === / == / !== / !=, either operand order)
29
- * - `Array.isArray(v)`
30
- * - coercion at the source: `const v = String(req.query.name)` (never
31
- * tracked — the coercer call is the init, not the raw member)
32
- * - re-assignment through a coercer/validator: `v = String(v)`,
33
- * `v = schema.parse(v)` (configurable via `coercers` / `validators`)
34
- *
35
- * Guards are scoped per enclosing function (nested closures see outer
36
- * guards). The rule does no cross-function data-flow: query values passed
37
- * as call arguments are a documented false negative.
38
- *
39
- * @see https://cwe.mitre.org/data/definitions/843.html
40
- * @see https://owasp.org/Top10/A03_2021-Injection/
41
- */
42
- import type { TSESLint } from '@interlace/eslint-devkit';
43
- type MessageIds = 'unguardedQueryStringMethod' | 'wrapString' | 'coerceAtAssignment';
44
- export interface Options {
45
- /** Callee names whose result is a safe string coercion. Default: ['String'] */
46
- coercers?: string[];
47
- /** Callee / method names treated as type-safe sources. Default: ['parse', 'safeParse'] */
48
- validators?: string[];
49
- }
50
- type RuleOptions = [Options?];
51
- export declare const requireQueryTypeGuard: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
52
- name: string;
53
- };
54
- export {};
@@ -1,31 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Ofri Peretz
3
- * Licensed under the MIT License. Use of this source code is governed by the
4
- * MIT license that can be found in the LICENSE file.
5
- */
6
- /**
7
- * ESLint Rule: require-rate-limiting
8
- * Detects Express.js applications missing rate limiting middleware
9
- * CWE-770: Allocation of Resources Without Limits or Throttling
10
- *
11
- * @see https://cwe.mitre.org/data/definitions/770.html
12
- * @see https://owasp.org/www-community/controls/Blocking_Brute_Force_Attacks
13
- */
14
- import type { TSESLint } from '@interlace/eslint-devkit';
15
- type MessageIds = 'missingRateLimiting' | 'addRateLimiting';
16
- export interface Options {
17
- /** Allow missing rate limiting in test files. Default: false */
18
- allowInTests?: boolean;
19
- /** Alternative rate limiting middleware names. Default: [] */
20
- alternativeMiddleware?: string[];
21
- /**
22
- * Skip rule if rate limiting is provided elsewhere (e.g., AWS API Gateway, Cloudflare, nginx).
23
- * Default: false
24
- */
25
- assumeRateLimiting?: boolean;
26
- }
27
- type RuleOptions = [Options?];
28
- export declare const requireRateLimiting: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
29
- name: string;
30
- };
31
- export {};