-
Notifications
You must be signed in to change notification settings - Fork 176
/
rule.ts
157 lines (146 loc) · 5.33 KB
/
rule.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
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// https://sonarsource.github.io/rspec/#/rspec/S5689/javascript
import { Rule } from 'eslint';
import * as estree from 'estree';
import { Express, isMethodInvocation, getFullyQualifiedName } from '../helpers';
const HELMET = 'helmet';
const HIDE_POWERED_BY = 'hide-powered-by';
const HEADER_X_POWERED_BY = 'X-Powered-By'.toLowerCase();
const PROTECTING_MIDDLEWARES = [HELMET, HIDE_POWERED_BY];
/** Expected number of arguments in `app.set`. */
const APP_SET_NUM_ARGS = 2;
export const rule: Rule.RuleModule = {
meta: {
messages: {
headerSet: 'Make sure disclosing the fingerprinting of this web technology is safe here.',
headerDefault:
'This framework implicitly discloses version information by default. Make sure it is safe here.',
},
},
create(context: Rule.RuleContext) {
let appInstantiation: estree.Identifier | null = null;
let isSafe = false;
let isExplicitelyUnsafe = false;
return {
Program() {
appInstantiation = null;
isSafe = false;
isExplicitelyUnsafe = true;
},
CallExpression: (node: estree.Node) => {
if (!isSafe && appInstantiation) {
const callExpr = node as estree.CallExpression;
isSafe =
Express.isUsingMiddleware(context, callExpr, appInstantiation, isProtecting(context)) ||
isDisabledXPoweredBy(callExpr, appInstantiation) ||
isSetFalseXPoweredBy(callExpr, appInstantiation) ||
isAppEscaping(callExpr, appInstantiation);
isExplicitelyUnsafe = isSetTrueXPoweredBy(callExpr, appInstantiation);
}
},
VariableDeclarator: (node: estree.Node) => {
if (!isSafe && !appInstantiation) {
const varDecl = node as estree.VariableDeclarator;
const app = Express.attemptFindAppInstantiation(varDecl, context);
if (app) {
appInstantiation = app;
}
}
},
ReturnStatement: (node: estree.Node) => {
if (!isSafe && appInstantiation) {
const ret = node as estree.ReturnStatement;
isSafe = isAppEscapingThroughReturn(ret, appInstantiation);
}
},
'Program:exit'() {
if (!isSafe && appInstantiation) {
let messageId = 'headerDefault';
if (isExplicitelyUnsafe) {
messageId = 'headerSet'
}
context.report({
node: appInstantiation,
messageId,
});
}
},
};
},
};
/**
* Checks whether node looks like `helmet.hidePoweredBy()`.
*/
function isHidePoweredByFromHelmet(context: Rule.RuleContext, n: estree.Node): boolean {
if (n.type === 'CallExpression') {
return getFullyQualifiedName(context, n) === `${HELMET}.hidePoweredBy`;
}
return false;
}
function isProtecting(context: Rule.RuleContext): (n: estree.Node) => boolean {
return (n: estree.Node) =>
Express.isMiddlewareInstance(context, PROTECTING_MIDDLEWARES, n) ||
isHidePoweredByFromHelmet(context, n);
}
function isDisabledXPoweredBy(
callExpression: estree.CallExpression,
app: estree.Identifier,
): boolean {
if (isMethodInvocation(callExpression, app.name, 'disable', 1)) {
const arg0 = callExpression.arguments[0];
return arg0.type === 'Literal' && String(arg0.value).toLowerCase() === HEADER_X_POWERED_BY;
}
return false;
}
function isSetFalseXPoweredBy(
callExpression: estree.CallExpression,
app: estree.Identifier,
): boolean {
return getSetTrueXPoweredByValue(callExpression, app) === false;
}
function isSetTrueXPoweredBy(
callExpression: estree.CallExpression,
app: estree.Identifier,
): boolean {
return getSetTrueXPoweredByValue(callExpression, app) === true;
}
function getSetTrueXPoweredByValue(callExpression: estree.CallExpression, app: estree.Identifier) {
if (isMethodInvocation(callExpression, app.name, 'set', APP_SET_NUM_ARGS)) {
const [headerName, onOff] = callExpression.arguments;
if (
headerName.type === 'Literal' &&
String(headerName.value).toLowerCase() === HEADER_X_POWERED_BY &&
onOff.type === 'Literal'
) {
return onOff.value;
}
}
return undefined;
}
function isAppEscaping(callExpr: estree.CallExpression, app: estree.Identifier): boolean {
return Boolean(
callExpr.arguments.find(arg => arg.type === 'Identifier' && arg.name === app.name),
);
}
function isAppEscapingThroughReturn(ret: estree.ReturnStatement, app: estree.Identifier): boolean {
const arg = ret.argument;
return Boolean(arg && arg.type === 'Identifier' && arg.name === app.name);
}