-
Notifications
You must be signed in to change notification settings - Fork 176
/
unit.test.ts
286 lines (285 loc) · 7.88 KB
/
unit.test.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* 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.
*/
import { RuleTester } from 'eslint';
import { rule } from './';
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: 'module' } });
ruleTester.run(
'Recovering fingerprints from web application technologies should not be possible',
rule,
{
valid: [
{
code: `
const express = require('express');
const app = express();
app.disable("x-powered-by"); // Compliant
`,
},
{
code: `
const express = require('express');
const hidePoweredBy = require('hide-powered-by');
const app = express();
app.use(hidePoweredBy()); // Compliant
`,
},
{
code: `
const express = require('express');
const app = express();
app.set("x-powered-by", false); // Compliant
`,
},
{
code: `
const express = require('express');
const app = express();
app.set("X-PoWeReD-bY", false); // Compliant
`,
},
{
code: `
const express = require('express');
const app = express();
app.use(require('helmet')()); // Compliant
`,
},
{
code: `
import express from 'express';
import disableXPoweredBy from 'hide-powered-by';
const app = express();
app.use(disableXPoweredBy()); // Compliant
`,
},
{
code: `
const express = require('express');
const app = express();
const helmet = require('helmet');
const h = helmet();
app.use(h);
`,
},
{
code: `
// middleware instance is saved to variable instead of directly passed to 'app.use()'
const express = require('express');
const helmet = require('helmet');
const helmetInstance = helmet();
const app = express();
app.use(helmetInstance); // compliant
`,
},
{
code: `
const express = require('express');
const app = express();
configure(app); // app escapes, probably configured elsewhere.
`,
},
{
code: `
const express = require('express');
const helmet = require('helmet');
const app = express();
app.use(helmet.hidePoweredBy());
`,
},
{
code: `
const express = require('express');
const app = express();
app.use(require('helmet').hidePoweredBy());
`,
},
{
code: `
const express = require('express');
const app = express();
app.use(a, b, [c, d, require('helmet')(), e, f], g, h);
`,
},
{
code: `
const express = require('express');
const app = express();
const helmet = require('helmet');
const securityMiddlewares = [
armor(),
helmet(),
sword(),
horse(),
];
app.use(usefulStuff, securityMiddlewares, moreStuff);
`,
},
{
code: `
const { app } = apps[42]; // Compliant (not obviously an express-app, ignored)
const app2 = apps[42];
const { subcomponent } = require('express')();
apps[42] = require('express')(); // Limitation: we don't keep track of 'app' here.
`,
},
{
code: `
const express = require('express');
module.exports.createExpressApp = function() {
var appEscaping = express(); // should be compliant, because express object is returned from the function
return appEscaping;
};
`,
},
{
code: `
const express = require('express');
function f() {
return 42; // a return before the application is discovered, for coverage
}
module.exports.createExpressApp = function() {
var appEscaping = express();
return appEscaping;
};
`,
},
],
invalid: [
{
code: `
const express = require('express');
const app = express(); // Noncompliant
`,
errors: [
{
message: 'This framework implicitly discloses version information by default. Make sure it is safe here.',
line: 3,
endLine: 3,
column: 15,
endColumn: 18,
},
],
},
{
code: `
import * as express from 'express';
const app = express(); // Noncompliant
`,
errors: [
{
message: 'This framework implicitly discloses version information by default. Make sure it is safe here.',
line: 3,
endLine: 3,
column: 15,
endColumn: 18,
},
],
},
{
code: `
const app = require('express')(); // Noncompliant
`,
errors: [
{
message: 'This framework implicitly discloses version information by default. Make sure it is safe here.',
line: 2,
endLine: 2,
column: 15,
endColumn: 18,
},
],
},
{
code: `
const express = require('express');
const app = express();
app.set("x-powered-by", true); // Noncompliant
`,
errors: [
{
message: 'Make sure disclosing the fingerprinting of this web technology is safe here.',
line: 3,
endLine: 3,
column: 15,
endColumn: 18,
},
],
},
{
code: `
const express = require('express');
const app = express();
`,
errors: 1,
},
{
code: `
const express = require('express');
const theforce = require('the-force');
const app = express();
app.use(theforce()); // That doesn't help here.
app.use(somethingunknown()); // That doesn't help either.
`,
errors: 1,
},
{
code: `
const express = require('express');
const helmet = require('helmet');
const helmetInst = helmet();
const app = express();
// imported, but forgot to use helmet
`,
errors: 1,
},
{
code: `
const express = require('express');
const app = express();
app.use(a, b, [c, d, require('sth-useless')(), e, f], g, h);
`,
errors: 1,
},
{
code: `
const express = require('express');
const app = express();
const securityMiddlewares = [
armor(),
sword(),
horse(),
];
app.use(usefulStuff, securityMiddlewares, something.unknown());
`,
errors: 1,
},
{
code: `
const express = require('express');
module.exports.createExpressApp = function() {
var appEscaping = express();
return appEscaping.someSubproperty; // that's not sufficient, does not count as escaped app.
};
`,
errors: 1,
},
],
},
);