-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathdetect-non-literal-fs-filename.js
99 lines (91 loc) · 3.3 KB
/
detect-non-literal-fs-filename.js
1
2
3
4
5
6
7
8
9
10
11
12
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
/**
* Tries to detect calls to fs functions that take a non Literal value as the filename parameter
* @author Adam Baldwin
*/
'use strict';
const fsMetaData = require('../utils/data/fsFunctionData.json');
const funcNames = Object.keys(fsMetaData);
const fsPackageNames = ['fs', 'node:fs', 'fs/promises', 'node:fs/promises', 'fs-extra'];
const { getImportAccessPath } = require('../utils/import-utils');
const { isStaticExpression } = require('../utils/is-static-expression');
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'error',
docs: {
description: 'Detects variable in filename argument of "fs" calls, which might allow an attacker to access anything on your system.',
category: 'Possible Security Vulnerability',
recommended: true,
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-fs-filename.md',
},
},
create(context) {
const sourceCode = context.sourceCode || context.getSourceCode();
return {
CallExpression(node) {
// don't check require. If all arguments are Literals, it's surely safe!
if ((node.callee.type === 'Identifier' && node.callee.name === 'require') || node.arguments.every((argument) => argument.type === 'Literal')) {
return;
}
const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
const pathInfo = getImportAccessPath({
node: node.callee,
scope,
packageNames: fsPackageNames,
});
if (!pathInfo) {
return;
}
let fnName;
if (pathInfo.path.length === 1) {
// Check for:
// | var something = require('fs').readFile;
// | something(a);
// ,
// | var something = require('fs');
// | something.readFile(c);
// ,
// | var { readFile: something } = require('fs')
// | readFile(filename);
// ,
// | import { readFile as something } from 'fs';
// | something(filename);
// , or
// | import * as something from 'fs';
// | something.readFile(c);
fnName = pathInfo.path[0];
} else if (pathInfo.path.length === 2) {
// Check for:
// | var something = require('fs').promises;
// | something.readFile(filename)
fnName = pathInfo.path[1];
} else {
return;
}
if (!funcNames.includes(fnName)) {
return false;
}
const packageName = pathInfo.packageName;
const indices = [];
for (const index of fsMetaData[fnName] || []) {
if (index >= node.arguments.length) {
continue;
}
const argument = node.arguments[index];
if (isStaticExpression({ node: argument, scope })) {
continue;
}
indices.push(index);
}
if (indices.length) {
context.report({
node,
message: `Found ${fnName} from package "${packageName}" with non literal argument at index ${indices.join(',')}`,
});
}
},
};
},
};