/
no-implied-eval.ts
181 lines (158 loc) · 5.08 KB
/
no-implied-eval.ts
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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import * as ts from 'typescript';
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as util from '../util';
const FUNCTION_CONSTRUCTOR = 'Function';
const GLOBAL_CANDIDATES = new Set(['global', 'window', 'globalThis']);
const EVAL_LIKE_METHODS = new Set([
'setImmediate',
'setInterval',
'setTimeout',
'execScript',
]);
export default util.createRule({
name: 'no-implied-eval',
meta: {
docs: {
description: 'Disallow the use of `eval()`-like methods',
recommended: 'error',
extendsBaseRule: true,
requiresTypeChecking: true,
},
messages: {
noImpliedEvalError: 'Implied eval. Consider passing a function.',
noFunctionConstructor:
'Implied eval. Do not use the Function constructor to create functions.',
},
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const program = parserServices.program;
const checker = parserServices.program.getTypeChecker();
function getCalleeName(
node: TSESTree.LeftHandSideExpression,
): string | null {
if (node.type === AST_NODE_TYPES.Identifier) {
return node.name;
}
if (
node.type === AST_NODE_TYPES.MemberExpression &&
node.object.type === AST_NODE_TYPES.Identifier &&
GLOBAL_CANDIDATES.has(node.object.name)
) {
if (node.property.type === AST_NODE_TYPES.Identifier) {
return node.property.name;
}
if (
node.property.type === AST_NODE_TYPES.Literal &&
typeof node.property.value === 'string'
) {
return node.property.value;
}
}
return null;
}
function isFunctionType(node: TSESTree.Node): boolean {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);
const symbol = type.getSymbol();
if (
symbol &&
tsutils.isSymbolFlagSet(
symbol,
ts.SymbolFlags.Function | ts.SymbolFlags.Method,
)
) {
return true;
}
if (symbol && symbol.escapedName === FUNCTION_CONSTRUCTOR) {
const declarations = symbol.getDeclarations() ?? [];
for (const declaration of declarations) {
const sourceFile = declaration.getSourceFile();
if (program.isSourceFileDefaultLibrary(sourceFile)) {
return true;
}
}
}
const signatures = checker.getSignaturesOfType(
type,
ts.SignatureKind.Call,
);
return signatures.length > 0;
}
function isBind(node: TSESTree.Node): boolean {
return node.type === AST_NODE_TYPES.MemberExpression
? isBind(node.property)
: node.type === AST_NODE_TYPES.Identifier && node.name === 'bind';
}
function isFunction(node: TSESTree.Node): boolean {
switch (node.type) {
case AST_NODE_TYPES.ArrowFunctionExpression:
case AST_NODE_TYPES.FunctionDeclaration:
case AST_NODE_TYPES.FunctionExpression:
return true;
case AST_NODE_TYPES.Literal:
case AST_NODE_TYPES.TemplateLiteral:
return false;
case AST_NODE_TYPES.CallExpression:
return isBind(node.callee) || isFunctionType(node);
default:
return isFunctionType(node);
}
}
function isReferenceToGlobalFunction(calleeName: string): boolean {
const ref = context
.getScope()
.references.find(ref => ref.identifier.name === calleeName);
// ensure it's the "global" version
return !ref?.resolved || ref.resolved.defs.length === 0;
}
function checkImpliedEval(
node: TSESTree.NewExpression | TSESTree.CallExpression,
): void {
const calleeName = getCalleeName(node.callee);
if (calleeName === null) {
return;
}
if (calleeName === FUNCTION_CONSTRUCTOR) {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node.callee);
const type = checker.getTypeAtLocation(tsNode);
const symbol = type.getSymbol();
if (symbol) {
const declarations = symbol.getDeclarations() ?? [];
for (const declaration of declarations) {
const sourceFile = declaration.getSourceFile();
if (program.isSourceFileDefaultLibrary(sourceFile)) {
context.report({ node, messageId: 'noFunctionConstructor' });
return;
}
}
} else {
context.report({ node, messageId: 'noFunctionConstructor' });
return;
}
}
if (node.arguments.length === 0) {
return;
}
const [handler] = node.arguments;
if (
EVAL_LIKE_METHODS.has(calleeName) &&
!isFunction(handler) &&
isReferenceToGlobalFunction(calleeName)
) {
context.report({ node: handler, messageId: 'noImpliedEvalError' });
}
}
return {
NewExpression: checkImpliedEval,
CallExpression: checkImpliedEval,
};
},
});