eslint-plugin-express-security 1.5.4 → 1.5.5
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/package.json +2 -2
- package/src/index.js +1 -113
- package/src/oxlint.js +1 -3
- package/src/rules/no-client-controlled-authorization/index.js +1 -194
- package/src/rules/no-cors-credentials-wildcard/index.js +1 -127
- package/src/rules/no-disabled-helmet-protections/index.js +1 -125
- package/src/rules/no-error-details-in-response/index.js +1 -176
- package/src/rules/no-exposed-debug-endpoints/index.js +1 -96
- package/src/rules/no-express-unsafe-regex-route/index.js +1 -154
- package/src/rules/no-graphql-introspection-production/index.js +1 -143
- package/src/rules/no-host-header-in-links/index.js +1 -235
- package/src/rules/no-idor-resource-access/index.js +1 -126
- package/src/rules/no-insecure-cookie-options/index.js +1 -158
- package/src/rules/no-missing-cors-check/index.js +1 -351
- package/src/rules/no-missing-csrf-protection/index.js +1 -161
- package/src/rules/no-missing-security-headers/index.js +1 -206
- package/src/rules/no-permissive-cors/index.js +1 -185
- package/src/rules/no-permissive-trust-proxy/index.js +1 -106
- package/src/rules/no-sensitive-data-in-query/index.js +1 -182
- package/src/rules/no-static-root-exposure/index.js +1 -267
- package/src/rules/no-unsafe-csp-directives/index.js +1 -264
- package/src/rules/no-user-controlled-redirect/index.js +1 -117
- package/src/rules/no-user-controlled-render-locals/index.js +1 -268
- package/src/rules/require-case-insensitive-path-guard/index.js +1 -180
- package/src/rules/require-csrf-protection/index.js +1 -201
- package/src/rules/require-express-body-parser-limits/index.js +1 -181
- package/src/rules/require-helmet/index.js +1 -166
- package/src/rules/require-query-type-guard/index.js +1 -258
- package/src/rules/require-rate-limiting/index.js +1 -136
- package/src/rules/require-route-authentication/index.js +1 -225
- package/src/rules/require-strict-transport-security/index.js +1 -171
- package/src/types/index.js +1 -2
- package/src/utils/index.js +1 -81
|
@@ -1,182 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noSensitiveDataInQuery = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const DEFAULT_SENSITIVE = [
|
|
6
|
-
'password',
|
|
7
|
-
'token',
|
|
8
|
-
'secret',
|
|
9
|
-
'apiKey',
|
|
10
|
-
'api_key',
|
|
11
|
-
'auth',
|
|
12
|
-
'credential',
|
|
13
|
-
'ssn',
|
|
14
|
-
'card',
|
|
15
|
-
];
|
|
16
|
-
const MAX_PARAM_NAME_LENGTH = 128;
|
|
17
|
-
function tokenize(name) {
|
|
18
|
-
return name
|
|
19
|
-
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
20
|
-
.toLowerCase()
|
|
21
|
-
.split(/[^a-z0-9]+/)
|
|
22
|
-
.filter((token) => token.length > 0);
|
|
23
|
-
}
|
|
24
|
-
function containsSubsequence(tokens, term) {
|
|
25
|
-
for (let i = 0; i + term.length <= tokens.length; i++) {
|
|
26
|
-
let match = true;
|
|
27
|
-
for (let j = 0; j < term.length; j++) {
|
|
28
|
-
if (tokens[i + j] !== term[j]) {
|
|
29
|
-
match = false;
|
|
30
|
-
break;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
if (match)
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
exports.noSensitiveDataInQuery = (0, eslint_devkit_1.createRule)({
|
|
39
|
-
name: 'no-sensitive-data-in-query',
|
|
40
|
-
meta: {
|
|
41
|
-
type: 'problem',
|
|
42
|
-
hasSuggestions: true,
|
|
43
|
-
docs: {
|
|
44
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-sensitive-data-in-query.md',
|
|
45
|
-
description: 'Disallow reading sensitive-named parameters (password, token, secret, ...) from req.query',
|
|
46
|
-
cwe: 'CWE-598',
|
|
47
|
-
cvss: 6.5,
|
|
48
|
-
},
|
|
49
|
-
messages: {
|
|
50
|
-
sensitiveQueryParam: (0, eslint_devkit_1.formatLLMMessage)({
|
|
51
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
52
|
-
issueName: 'Sensitive Data in Query String (CWE-598)',
|
|
53
|
-
cwe: 'CWE-598',
|
|
54
|
-
description: "Query parameter '{{name}}' is sensitive. Query strings are stored in access logs, proxy logs, browser history and leak via the Referer header.",
|
|
55
|
-
severity: 'MEDIUM',
|
|
56
|
-
fix: 'Move the value to the POST request body (req.body) or an Authorization header; never accept secrets via the query string.',
|
|
57
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/598.html',
|
|
58
|
-
}),
|
|
59
|
-
moveToBody: (0, eslint_devkit_1.formatLLMMessage)({
|
|
60
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
61
|
-
issueName: 'Move the secret out of the query string',
|
|
62
|
-
description: 'Accept the value from the POST body or an Authorization header instead',
|
|
63
|
-
severity: 'LOW',
|
|
64
|
-
fix: "app.post('/login', (req, res) => { const { password } = req.body; })",
|
|
65
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/598.html',
|
|
66
|
-
}),
|
|
67
|
-
},
|
|
68
|
-
schema: [
|
|
69
|
-
{
|
|
70
|
-
type: 'object',
|
|
71
|
-
properties: {
|
|
72
|
-
sensitiveParams: {
|
|
73
|
-
type: 'array',
|
|
74
|
-
items: { type: 'string' },
|
|
75
|
-
description: 'Additional sensitive parameter names (extends the defaults)',
|
|
76
|
-
},
|
|
77
|
-
extraPatterns: {
|
|
78
|
-
type: 'array',
|
|
79
|
-
items: { type: 'string' },
|
|
80
|
-
description: 'Additional regex patterns (case-insensitive) matched against the raw parameter name',
|
|
81
|
-
},
|
|
82
|
-
allowedParams: {
|
|
83
|
-
type: 'array',
|
|
84
|
-
items: { type: 'string' },
|
|
85
|
-
description: 'Parameter names explicitly allowed in the query string (exact, case-insensitive)',
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
additionalProperties: false,
|
|
89
|
-
},
|
|
90
|
-
],
|
|
91
|
-
},
|
|
92
|
-
defaultOptions: [{}],
|
|
93
|
-
create(context, [options]) {
|
|
94
|
-
const { sensitiveParams, extraPatterns, allowedParams } = options;
|
|
95
|
-
const terms = [...DEFAULT_SENSITIVE, ...(sensitiveParams ?? [])].map((term) => tokenize(term));
|
|
96
|
-
const patterns = [];
|
|
97
|
-
for (const pattern of extraPatterns ?? []) {
|
|
98
|
-
try {
|
|
99
|
-
patterns.push(new RegExp(pattern, 'i'));
|
|
100
|
-
}
|
|
101
|
-
catch {
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
const allowedSet = new Set((allowedParams ?? []).map((name) => name.toLowerCase()));
|
|
105
|
-
function isRequestIdent(name) {
|
|
106
|
-
const lower = name.toLowerCase();
|
|
107
|
-
return lower === 'req' || lower === 'request' || lower === 'ctx';
|
|
108
|
-
}
|
|
109
|
-
function isReqQuery(node) {
|
|
110
|
-
return (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
111
|
-
!node.computed &&
|
|
112
|
-
node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
113
|
-
isRequestIdent(node.object.name) &&
|
|
114
|
-
node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
115
|
-
node.property.name === 'query');
|
|
116
|
-
}
|
|
117
|
-
function isSensitive(name) {
|
|
118
|
-
if (allowedSet.has(name.toLowerCase()))
|
|
119
|
-
return false;
|
|
120
|
-
const tokens = tokenize(name);
|
|
121
|
-
for (const term of terms) {
|
|
122
|
-
if (term.length === 1) {
|
|
123
|
-
const single = term[0];
|
|
124
|
-
if (tokens.some((token) => token === single || token === `${single}s`)) {
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
else if (containsSubsequence(tokens, term)) {
|
|
129
|
-
return true;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
if (name.length > MAX_PARAM_NAME_LENGTH)
|
|
133
|
-
return false;
|
|
134
|
-
return patterns.some((pattern) => pattern.test(name));
|
|
135
|
-
}
|
|
136
|
-
function report(node, name) {
|
|
137
|
-
context.report({
|
|
138
|
-
node,
|
|
139
|
-
messageId: 'sensitiveQueryParam',
|
|
140
|
-
data: { name },
|
|
141
|
-
suggest: [{ messageId: 'moveToBody', fix: () => null }],
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
return {
|
|
145
|
-
MemberExpression(node) {
|
|
146
|
-
if (!isReqQuery(node.object))
|
|
147
|
-
return;
|
|
148
|
-
let name = null;
|
|
149
|
-
if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
150
|
-
!node.computed) {
|
|
151
|
-
name = node.property.name;
|
|
152
|
-
}
|
|
153
|
-
else if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
154
|
-
typeof node.property.value === 'string') {
|
|
155
|
-
name = node.property.value;
|
|
156
|
-
}
|
|
157
|
-
if (name !== null && isSensitive(name))
|
|
158
|
-
report(node, name);
|
|
159
|
-
},
|
|
160
|
-
VariableDeclarator(node) {
|
|
161
|
-
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectPattern)
|
|
162
|
-
return;
|
|
163
|
-
if (node.init === null || !isReqQuery(node.init))
|
|
164
|
-
return;
|
|
165
|
-
for (const prop of node.id.properties) {
|
|
166
|
-
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
|
|
167
|
-
continue;
|
|
168
|
-
let name = null;
|
|
169
|
-
if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && !prop.computed) {
|
|
170
|
-
name = prop.key.name;
|
|
171
|
-
}
|
|
172
|
-
else if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
173
|
-
typeof prop.key.value === 'string') {
|
|
174
|
-
name = prop.key.value;
|
|
175
|
-
}
|
|
176
|
-
if (name !== null && isSensitive(name))
|
|
177
|
-
report(prop, name);
|
|
178
|
-
}
|
|
179
|
-
},
|
|
180
|
-
};
|
|
181
|
-
},
|
|
182
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noSensitiveDataInQuery=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const DEFAULT_SENSITIVE=["password","token","secret","apiKey","api_key","auth","credential","ssn","card"];const MAX_PARAM_NAME_LENGTH=128;function tokenize(name){return name.replace(/([a-z0-9])([A-Z])/g,"$1 $2").toLowerCase().split(/[^a-z0-9]+/).filter(token=>token.length>0)}function containsSubsequence(tokens,term){for(let i=0;i+term.length<=tokens.length;i++){let match=true;for(let j=0;j<term.length;j++){if(tokens[i+j]!==term[j]){match=false;break}}if(match)return true}return false}exports.noSensitiveDataInQuery=(0,eslint_devkit_1.createRule)({name:"no-sensitive-data-in-query",meta:{type:"problem",hasSuggestions:true,docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-sensitive-data-in-query.md",description:"Disallow reading sensitive-named parameters (password, token, secret, ...) from req.query",cwe:"CWE-598",cvss:6.5},messages:{sensitiveQueryParam:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Sensitive Data in Query String (CWE-598)",cwe:"CWE-598",description:"Query parameter '{{name}}' is sensitive. Query strings are stored in access logs, proxy logs, browser history and leak via the Referer header.",severity:"MEDIUM",fix:"Move the value to the POST request body (req.body) or an Authorization header; never accept secrets via the query string.",documentationLink:"https://cwe.mitre.org/data/definitions/598.html"}),moveToBody:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Move the secret out of the query string",description:"Accept the value from the POST body or an Authorization header instead",severity:"LOW",fix:"app.post('/login', (req, res) => { const { password } = req.body; })",documentationLink:"https://cwe.mitre.org/data/definitions/598.html"})},schema:[{type:"object",properties:{sensitiveParams:{type:"array",items:{type:"string"},description:"Additional sensitive parameter names (extends the defaults)"},extraPatterns:{type:"array",items:{type:"string"},description:"Additional regex patterns (case-insensitive) matched against the raw parameter name"},allowedParams:{type:"array",items:{type:"string"},description:"Parameter names explicitly allowed in the query string (exact, case-insensitive)"}},additionalProperties:false}]},defaultOptions:[{}],create(context,[options]){const{sensitiveParams,extraPatterns,allowedParams}=options;const terms=[...DEFAULT_SENSITIVE,...sensitiveParams??[]].map(term=>tokenize(term));const patterns=[];for(const pattern of extraPatterns??[]){try{patterns.push(new RegExp(pattern,"i"))}catch{}}const allowedSet=new Set((allowedParams??[]).map(name=>name.toLowerCase()));function isRequestIdent(name){const lower=name.toLowerCase();return lower==="req"||lower==="request"||lower==="ctx"}function isReqQuery(node){return node.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&!node.computed&&node.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&isRequestIdent(node.object.name)&&node.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.property.name==="query"}function isSensitive(name){if(allowedSet.has(name.toLowerCase()))return false;const tokens=tokenize(name);for(const term of terms){if(term.length===1){const single=term[0];if(tokens.some(token=>token===single||token===`${single}s`)){return true}}else if(containsSubsequence(tokens,term)){return true}}if(name.length>MAX_PARAM_NAME_LENGTH)return false;return patterns.some(pattern=>pattern.test(name))}function report(node,name){context.report({node,messageId:"sensitiveQueryParam",data:{name},suggest:[{messageId:"moveToBody",fix:()=>null}]})}return{MemberExpression(node){if(!isReqQuery(node.object))return;let name=null;if(node.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&!node.computed){name=node.property.name}else if(node.property.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof node.property.value==="string"){name=node.property.value}if(name!==null&&isSensitive(name))report(node,name)},VariableDeclarator(node){if(node.id.type!==eslint_devkit_1.AST_NODE_TYPES.ObjectPattern)return;if(node.init===null||!isReqQuery(node.init))return;for(const prop of node.id.properties){if(prop.type!==eslint_devkit_1.AST_NODE_TYPES.Property)continue;let name=null;if(prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&!prop.computed){name=prop.key.name}else if(prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof prop.key.value==="string"){name=prop.key.value}if(name!==null&&isSensitive(name))report(prop,name)}}}}});
|
|
@@ -1,267 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noStaticRootExposure = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const DEFAULT_ALLOWED_ROOTS = ['public', 'static', 'dist', 'build', 'assets'];
|
|
6
|
-
const JOIN_METHODS = new Set(['join', 'resolve']);
|
|
7
|
-
function quoteLiteral(value) {
|
|
8
|
-
const json = JSON.stringify(value);
|
|
9
|
-
const inner = json.slice(1, -1);
|
|
10
|
-
return /['\\]/.test(inner) ? json : `'${inner}'`;
|
|
11
|
-
}
|
|
12
|
-
exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
13
|
-
name: 'no-static-root-exposure',
|
|
14
|
-
meta: {
|
|
15
|
-
type: 'problem',
|
|
16
|
-
hasSuggestions: true,
|
|
17
|
-
docs: {
|
|
18
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-static-root-exposure.md',
|
|
19
|
-
description: 'Disallow express.static() roots that expose the application directory and any serve-index usage',
|
|
20
|
-
cwe: 'CWE-548',
|
|
21
|
-
cvss: 7.5,
|
|
22
|
-
},
|
|
23
|
-
messages: {
|
|
24
|
-
staticRoot: (0, eslint_devkit_1.formatLLMMessage)({
|
|
25
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
26
|
-
issueName: 'Application Root Served Statically (CWE-548)',
|
|
27
|
-
cwe: 'CWE-548',
|
|
28
|
-
description: 'express.static() serves {{exposed}} — the application root. This publishes .env, package-lock.json, .git metadata and the server source itself.',
|
|
29
|
-
severity: 'HIGH',
|
|
30
|
-
fix: "Serve a dedicated asset directory instead: express.static(path.join(__dirname, 'public')).",
|
|
31
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
32
|
-
}),
|
|
33
|
-
traversalSegments: (0, eslint_devkit_1.formatLLMMessage)({
|
|
34
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
35
|
-
issueName: 'Static Root Escapes the Application Directory (CWE-548)',
|
|
36
|
-
cwe: 'CWE-548',
|
|
37
|
-
description: 'The static root contains a ".." segment, so files OUTSIDE the intended directory become publicly reachable.',
|
|
38
|
-
severity: 'HIGH',
|
|
39
|
-
fix: 'Point express.static() at a dedicated asset directory inside the project, with no ".." segments.',
|
|
40
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
41
|
-
}),
|
|
42
|
-
nonLiteralPath: (0, eslint_devkit_1.formatLLMMessage)({
|
|
43
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
44
|
-
issueName: 'Dynamic Static-Root Segment (CWE-548)',
|
|
45
|
-
cwe: 'CWE-548',
|
|
46
|
-
description: 'The static root is built from a non-literal segment, so the served directory cannot be verified at lint time and may expose unintended files.',
|
|
47
|
-
severity: 'MEDIUM',
|
|
48
|
-
fix: "Use a string literal for every path segment: path.join(__dirname, 'public').",
|
|
49
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
50
|
-
}),
|
|
51
|
-
unknownRoot: (0, eslint_devkit_1.formatLLMMessage)({
|
|
52
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
53
|
-
issueName: 'Unrecognized Static-Assets Directory (CWE-548)',
|
|
54
|
-
cwe: 'CWE-548',
|
|
55
|
-
description: '"{{root}}" is not an allowlisted static-assets directory. Serving arbitrary directories risks exposing files that were never meant to be public.',
|
|
56
|
-
severity: 'MEDIUM',
|
|
57
|
-
fix: 'Serve a dedicated asset directory (public/, static/, dist/, build/, assets/) or add the directory to the allowedRoots option.',
|
|
58
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
59
|
-
}),
|
|
60
|
-
directoryListing: (0, eslint_devkit_1.formatLLMMessage)({
|
|
61
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
62
|
-
issueName: 'Directory Listing Enabled (CWE-548)',
|
|
63
|
-
cwe: 'CWE-548',
|
|
64
|
-
description: 'serve-index exposes a browsable listing of every file in the served directory, giving attackers a map of the filesystem.',
|
|
65
|
-
severity: 'HIGH',
|
|
66
|
-
fix: 'Remove the serve-index middleware; serve an explicit index file or a curated file list instead.',
|
|
67
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
68
|
-
}),
|
|
69
|
-
scopeToSubdir: "Serve path.join(__dirname, '{{root}}') instead",
|
|
70
|
-
},
|
|
71
|
-
schema: [
|
|
72
|
-
{
|
|
73
|
-
type: 'object',
|
|
74
|
-
properties: {
|
|
75
|
-
allowedRoots: {
|
|
76
|
-
type: 'array',
|
|
77
|
-
items: { type: 'string' },
|
|
78
|
-
description: 'Directory names accepted as the first path segment of a static root',
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
additionalProperties: false,
|
|
82
|
-
},
|
|
83
|
-
],
|
|
84
|
-
},
|
|
85
|
-
defaultOptions: [{}],
|
|
86
|
-
create(context, [options]) {
|
|
87
|
-
const { allowedRoots } = options;
|
|
88
|
-
const roots = new Set(allowedRoots ?? DEFAULT_ALLOWED_ROOTS);
|
|
89
|
-
const suggestionRoot = (allowedRoots ?? DEFAULT_ALLOWED_ROOTS)[0] ?? 'public';
|
|
90
|
-
const serveIndexNames = new Set(['serveIndex']);
|
|
91
|
-
function isDirnameNode(node) {
|
|
92
|
-
return (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && node.name === '__dirname');
|
|
93
|
-
}
|
|
94
|
-
function isProcessCwdCall(node) {
|
|
95
|
-
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression)
|
|
96
|
-
return false;
|
|
97
|
-
const callee = node.callee;
|
|
98
|
-
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
99
|
-
return false;
|
|
100
|
-
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
101
|
-
return false;
|
|
102
|
-
if (callee.object.name !== 'process')
|
|
103
|
-
return false;
|
|
104
|
-
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
105
|
-
return false;
|
|
106
|
-
return callee.property.name === 'cwd';
|
|
107
|
-
}
|
|
108
|
-
function isPathJoinCall(node) {
|
|
109
|
-
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression)
|
|
110
|
-
return false;
|
|
111
|
-
const callee = node.callee;
|
|
112
|
-
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
113
|
-
return false;
|
|
114
|
-
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
115
|
-
return false;
|
|
116
|
-
if (callee.object.name !== 'path')
|
|
117
|
-
return false;
|
|
118
|
-
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
119
|
-
return false;
|
|
120
|
-
return JOIN_METHODS.has(callee.property.name);
|
|
121
|
-
}
|
|
122
|
-
function isExpressStaticCall(node) {
|
|
123
|
-
const callee = node.callee;
|
|
124
|
-
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
125
|
-
return false;
|
|
126
|
-
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
127
|
-
return false;
|
|
128
|
-
if (callee.property.name !== 'static')
|
|
129
|
-
return false;
|
|
130
|
-
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
131
|
-
return false;
|
|
132
|
-
return callee.object.name.toLowerCase() === 'express';
|
|
133
|
-
}
|
|
134
|
-
function segmentsOf(value) {
|
|
135
|
-
return value.split(/[\\/]+/).filter((seg) => seg !== '' && seg !== '.');
|
|
136
|
-
}
|
|
137
|
-
function reportRoot(node, exposed) {
|
|
138
|
-
context.report({
|
|
139
|
-
node,
|
|
140
|
-
messageId: 'staticRoot',
|
|
141
|
-
data: { exposed },
|
|
142
|
-
suggest: [
|
|
143
|
-
{
|
|
144
|
-
messageId: 'scopeToSubdir',
|
|
145
|
-
data: { root: suggestionRoot },
|
|
146
|
-
fix: (fixer) => fixer.replaceText(node, `path.join(__dirname, ${quoteLiteral(suggestionRoot)})`),
|
|
147
|
-
},
|
|
148
|
-
],
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
function checkJoinRoot(call) {
|
|
152
|
-
const literalSegments = [];
|
|
153
|
-
let hasNonLiteral = false;
|
|
154
|
-
let hasTraversal = false;
|
|
155
|
-
for (const arg of call.arguments) {
|
|
156
|
-
if (isDirnameNode(arg) || isProcessCwdCall(arg))
|
|
157
|
-
continue;
|
|
158
|
-
if (arg.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
159
|
-
typeof arg.value === 'string') {
|
|
160
|
-
const segments = segmentsOf(arg.value);
|
|
161
|
-
if (segments.includes('..'))
|
|
162
|
-
hasTraversal = true;
|
|
163
|
-
literalSegments.push(...segments);
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
hasNonLiteral = true;
|
|
167
|
-
}
|
|
168
|
-
if (hasTraversal) {
|
|
169
|
-
context.report({ node: call, messageId: 'traversalSegments' });
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
if (hasNonLiteral) {
|
|
173
|
-
context.report({ node: call, messageId: 'nonLiteralPath' });
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
if (literalSegments.length === 0) {
|
|
177
|
-
reportRoot(call, context.sourceCode.getText(call));
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
if (!roots.has(literalSegments[0])) {
|
|
181
|
-
context.report({
|
|
182
|
-
node: call,
|
|
183
|
-
messageId: 'unknownRoot',
|
|
184
|
-
data: { root: literalSegments[0] },
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
function checkStaticRoot(node) {
|
|
189
|
-
if (isDirnameNode(node)) {
|
|
190
|
-
reportRoot(node, '__dirname');
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
if (isProcessCwdCall(node)) {
|
|
194
|
-
reportRoot(node, 'process.cwd()');
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Literal) {
|
|
198
|
-
if (typeof node.value !== 'string')
|
|
199
|
-
return;
|
|
200
|
-
const segments = segmentsOf(node.value);
|
|
201
|
-
if (segments.includes('..')) {
|
|
202
|
-
context.report({ node, messageId: 'traversalSegments' });
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
if (segments.length === 0) {
|
|
206
|
-
reportRoot(node, `'${node.value}'`);
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
if (!roots.has(segments[0])) {
|
|
210
|
-
context.report({
|
|
211
|
-
node,
|
|
212
|
-
messageId: 'unknownRoot',
|
|
213
|
-
data: { root: segments[0] },
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
if (isPathJoinCall(node)) {
|
|
219
|
-
checkJoinRoot(node);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
return {
|
|
223
|
-
ImportDeclaration(node) {
|
|
224
|
-
if (node.source.value !== 'serve-index')
|
|
225
|
-
return;
|
|
226
|
-
for (const specifier of node.specifiers) {
|
|
227
|
-
serveIndexNames.add(specifier.local.name);
|
|
228
|
-
}
|
|
229
|
-
},
|
|
230
|
-
VariableDeclarator(node) {
|
|
231
|
-
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
232
|
-
return;
|
|
233
|
-
if (!node.init)
|
|
234
|
-
return;
|
|
235
|
-
if (node.init.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression)
|
|
236
|
-
return;
|
|
237
|
-
const callee = node.init.callee;
|
|
238
|
-
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
239
|
-
return;
|
|
240
|
-
if (callee.name !== 'require')
|
|
241
|
-
return;
|
|
242
|
-
const requiredModule = node.init.arguments[0];
|
|
243
|
-
if (!requiredModule)
|
|
244
|
-
return;
|
|
245
|
-
if (requiredModule.type !== eslint_devkit_1.AST_NODE_TYPES.Literal)
|
|
246
|
-
return;
|
|
247
|
-
if (requiredModule.value !== 'serve-index')
|
|
248
|
-
return;
|
|
249
|
-
serveIndexNames.add(node.id.name);
|
|
250
|
-
},
|
|
251
|
-
CallExpression(node) {
|
|
252
|
-
const callee = node.callee;
|
|
253
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
254
|
-
serveIndexNames.has(callee.name)) {
|
|
255
|
-
context.report({ node, messageId: 'directoryListing' });
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
if (!isExpressStaticCall(node))
|
|
259
|
-
return;
|
|
260
|
-
const rootArg = node.arguments[0];
|
|
261
|
-
if (!rootArg)
|
|
262
|
-
return;
|
|
263
|
-
checkStaticRoot(rootArg);
|
|
264
|
-
},
|
|
265
|
-
};
|
|
266
|
-
},
|
|
267
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noStaticRootExposure=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const DEFAULT_ALLOWED_ROOTS=["public","static","dist","build","assets"];const JOIN_METHODS=new Set(["join","resolve"]);function quoteLiteral(value){const json=JSON.stringify(value);const inner=json.slice(1,-1);return/['\\]/.test(inner)?json:`'${inner}'`}exports.noStaticRootExposure=(0,eslint_devkit_1.createRule)({name:"no-static-root-exposure",meta:{type:"problem",hasSuggestions:true,docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-static-root-exposure.md",description:"Disallow express.static() roots that expose the application directory and any serve-index usage",cwe:"CWE-548",cvss:7.5},messages:{staticRoot:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Application Root Served Statically (CWE-548)",cwe:"CWE-548",description:"express.static() serves {{exposed}} \u2014 the application root. This publishes .env, package-lock.json, .git metadata and the server source itself.",severity:"HIGH",fix:"Serve a dedicated asset directory instead: express.static(path.join(__dirname, 'public')).",documentationLink:"https://cwe.mitre.org/data/definitions/548.html"}),traversalSegments:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Static Root Escapes the Application Directory (CWE-548)",cwe:"CWE-548",description:'The static root contains a ".." segment, so files OUTSIDE the intended directory become publicly reachable.',severity:"HIGH",fix:'Point express.static() at a dedicated asset directory inside the project, with no ".." segments.',documentationLink:"https://cwe.mitre.org/data/definitions/548.html"}),nonLiteralPath:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Dynamic Static-Root Segment (CWE-548)",cwe:"CWE-548",description:"The static root is built from a non-literal segment, so the served directory cannot be verified at lint time and may expose unintended files.",severity:"MEDIUM",fix:"Use a string literal for every path segment: path.join(__dirname, 'public').",documentationLink:"https://cwe.mitre.org/data/definitions/548.html"}),unknownRoot:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Unrecognized Static-Assets Directory (CWE-548)",cwe:"CWE-548",description:'"{{root}}" is not an allowlisted static-assets directory. Serving arbitrary directories risks exposing files that were never meant to be public.',severity:"MEDIUM",fix:"Serve a dedicated asset directory (public/, static/, dist/, build/, assets/) or add the directory to the allowedRoots option.",documentationLink:"https://cwe.mitre.org/data/definitions/548.html"}),directoryListing:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Directory Listing Enabled (CWE-548)",cwe:"CWE-548",description:"serve-index exposes a browsable listing of every file in the served directory, giving attackers a map of the filesystem.",severity:"HIGH",fix:"Remove the serve-index middleware; serve an explicit index file or a curated file list instead.",documentationLink:"https://cwe.mitre.org/data/definitions/548.html"}),scopeToSubdir:"Serve path.join(__dirname, '{{root}}') instead"},schema:[{type:"object",properties:{allowedRoots:{type:"array",items:{type:"string"},description:"Directory names accepted as the first path segment of a static root"}},additionalProperties:false}]},defaultOptions:[{}],create(context,[options]){const{allowedRoots}=options;const roots=new Set(allowedRoots??DEFAULT_ALLOWED_ROOTS);const suggestionRoot=(allowedRoots??DEFAULT_ALLOWED_ROOTS)[0]??"public";const serveIndexNames=new Set(["serveIndex"]);function isDirnameNode(node){return node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.name==="__dirname"}function isProcessCwdCall(node){if(node.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression)return false;const callee=node.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return false;if(callee.object.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;if(callee.object.name!=="process")return false;if(callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;return callee.property.name==="cwd"}function isPathJoinCall(node){if(node.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression)return false;const callee=node.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return false;if(callee.object.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;if(callee.object.name!=="path")return false;if(callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;return JOIN_METHODS.has(callee.property.name)}function isExpressStaticCall(node){const callee=node.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return false;if(callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;if(callee.property.name!=="static")return false;if(callee.object.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;return callee.object.name.toLowerCase()==="express"}function segmentsOf(value){return value.split(/[\\/]+/).filter(seg=>seg!==""&&seg!==".")}function reportRoot(node,exposed){context.report({node,messageId:"staticRoot",data:{exposed},suggest:[{messageId:"scopeToSubdir",data:{root:suggestionRoot},fix:fixer=>fixer.replaceText(node,`path.join(__dirname, ${quoteLiteral(suggestionRoot)})`)}]})}function checkJoinRoot(call){const literalSegments=[];let hasNonLiteral=false;let hasTraversal=false;for(const arg of call.arguments){if(isDirnameNode(arg)||isProcessCwdCall(arg))continue;if(arg.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof arg.value==="string"){const segments=segmentsOf(arg.value);if(segments.includes(".."))hasTraversal=true;literalSegments.push(...segments);continue}hasNonLiteral=true}if(hasTraversal){context.report({node:call,messageId:"traversalSegments"});return}if(hasNonLiteral){context.report({node:call,messageId:"nonLiteralPath"});return}if(literalSegments.length===0){reportRoot(call,context.sourceCode.getText(call));return}if(!roots.has(literalSegments[0])){context.report({node:call,messageId:"unknownRoot",data:{root:literalSegments[0]}})}}function checkStaticRoot(node){if(isDirnameNode(node)){reportRoot(node,"__dirname");return}if(isProcessCwdCall(node)){reportRoot(node,"process.cwd()");return}if(node.type===eslint_devkit_1.AST_NODE_TYPES.Literal){if(typeof node.value!=="string")return;const segments=segmentsOf(node.value);if(segments.includes("..")){context.report({node,messageId:"traversalSegments"});return}if(segments.length===0){reportRoot(node,`'${node.value}'`);return}if(!roots.has(segments[0])){context.report({node,messageId:"unknownRoot",data:{root:segments[0]}})}return}if(isPathJoinCall(node)){checkJoinRoot(node)}}return{ImportDeclaration(node){if(node.source.value!=="serve-index")return;for(const specifier of node.specifiers){serveIndexNames.add(specifier.local.name)}},VariableDeclarator(node){if(node.id.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return;if(!node.init)return;if(node.init.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression)return;const callee=node.init.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return;if(callee.name!=="require")return;const requiredModule=node.init.arguments[0];if(!requiredModule)return;if(requiredModule.type!==eslint_devkit_1.AST_NODE_TYPES.Literal)return;if(requiredModule.value!=="serve-index")return;serveIndexNames.add(node.id.name)},CallExpression(node){const callee=node.callee;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&serveIndexNames.has(callee.name)){context.report({node,messageId:"directoryListing"});return}if(!isExpressStaticCall(node))return;const rootArg=node.arguments[0];if(!rootArg)return;checkStaticRoot(rootArg)}}}});
|