-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathAgent.ts
More file actions
569 lines (506 loc) · 16 KB
/
Agent.ts
File metadata and controls
569 lines (506 loc) · 16 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* eslint-disable max-lines-per-function, no-console */
import { hostname, platform, release } from "os";
import { convertRequestBodyToString } from "../helpers/convertRequestBodyToString";
import { getAgentVersion } from "../helpers/getAgentVersion";
import { getSemverNodeVersion } from "../helpers/getNodeVersion";
import { ip } from "../helpers/ipAddress";
import { filterEmptyRequestHeaders } from "../helpers/filterEmptyRequestHeaders";
import { limitLengthMetadata } from "../helpers/limitLengthMetadata";
import { RateLimiter } from "../ratelimiting/RateLimiter";
import { fetchBlockedLists } from "./api/fetchBlockedLists";
import { ReportingAPI, ReportingAPIResponse } from "./api/ReportingAPI";
import { AgentInfo, DetectedAttack } from "./api/Event";
import { Token } from "./api/Token";
import { Kind } from "./Attack";
import { pollForChanges } from "./realtime/pollForChanges";
import { Context } from "./Context";
import { Hostnames } from "./Hostnames";
import { InspectionStatistics } from "./InspectionStatistics";
import { Logger } from "./logger/Logger";
import { Routes } from "./Routes";
import { ServiceConfig } from "./ServiceConfig";
import { Source } from "./Source";
import { Users } from "./Users";
import { wrapInstalledPackages } from "./wrapInstalledPackages";
import { Wrapper } from "./Wrapper";
import { isAikidoCI } from "../helpers/isAikidoCI";
import { AttackLogger } from "./AttackLogger";
type WrappedPackage = { version: string | null; supported: boolean };
export class Agent {
private started = false;
private sendHeartbeatEveryMS = 10 * 60 * 1000;
private checkIfHeartbeatIsNeededEveryMS = 60 * 1000;
private lastHeartbeat = performance.now();
private reportedInitialStats = false;
private interval: NodeJS.Timeout | undefined = undefined;
private preventedPrototypePollution = false;
private incompatiblePackages: Record<string, string> = {};
private wrappedPackages: Record<string, WrappedPackage> = {};
private timeoutInMS = 30 * 1000;
private hostnames = new Hostnames(200);
private users = new Users(1000);
private serviceConfig = new ServiceConfig(
[],
Date.now(),
[],
[],
true,
[],
[]
);
private routes: Routes = new Routes(200);
private rateLimiter: RateLimiter = new RateLimiter(5000, 120 * 60 * 1000);
private statistics = new InspectionStatistics({
maxPerfSamplesInMemory: 5000,
maxCompressedStatsInMemory: 20, // per operation
});
private middlewareInstalled = false;
private attackLogger = new AttackLogger(1000);
constructor(
private block: boolean,
private readonly logger: Logger,
private readonly api: ReportingAPI,
private readonly token: Token | undefined,
private readonly serverless: string | undefined
) {
if (typeof this.serverless === "string" && this.serverless.length === 0) {
throw new Error("Serverless cannot be an empty string");
}
}
shouldBlock() {
return this.block;
}
getHostnames() {
return this.hostnames;
}
getInspectionStatistics() {
return this.statistics;
}
unableToPreventPrototypePollution(
incompatiblePackages: Record<string, string>
) {
this.incompatiblePackages = incompatiblePackages;
const list: string[] = [];
for (const pkg in incompatiblePackages) {
list.push(`${pkg}@${incompatiblePackages[pkg]}`);
}
this.logger.log(
`Unable to prevent prototype pollution, incompatible packages found: ${list.join(" ")}`
);
}
onPrototypePollutionPrevented() {
this.logger.log("Prevented prototype pollution!");
// Will be sent in the next heartbeat
this.preventedPrototypePollution = true;
this.incompatiblePackages = {};
}
/**
* Reports to the API that this agent has started
*/
async onStart() {
if (this.token) {
const result = await this.api.report(
this.token,
{
type: "started",
time: Date.now(),
agent: this.getAgentInfo(),
},
// We don't use `this.timeoutInMS` for startup event
// Since Node.js is single threaded, the HTTP request is fired before other imports are required
// It might take a long time before our code resumes
60 * 1000
);
this.checkForReportingAPIError(result);
this.updateServiceConfig(result);
await this.updateBlockedLists();
}
}
checkForReportingAPIError(result: ReportingAPIResponse) {
if (!result.success) {
if (result.error === "invalid_token") {
console.error(
"Aikido: We were unable to connect to the Aikido platform. Please verify that your token is correct."
);
} else {
console.error(
`Aikido: Failed to connect to the Aikido platform: ${result.error}`
);
}
}
}
onErrorThrownByInterceptor({
error,
module,
method,
}: {
error: Error;
module: string;
method: string;
}) {
this.logger.log(
`Internal error in module "${module}" in method "${method}"\n${error.stack}`
);
}
/**
* This function gets called when an attack is detected, it reports this attack to the API
*/
onDetectedAttack({
module,
operation,
kind,
blocked,
source,
request,
stack,
paths,
metadata,
payload,
}: {
module: string;
operation: string;
kind: Kind;
blocked: boolean;
source: Source;
request: Context;
stack: string;
paths: string[];
metadata: Record<string, string>;
payload: unknown;
}) {
const attack: DetectedAttack = {
type: "detected_attack",
time: Date.now(),
attack: {
module: module,
operation: operation,
blocked: blocked,
path: paths.length > 0 ? paths[0] : "",
stack: stack,
source: source,
metadata: limitLengthMetadata(metadata, 4096),
kind: kind,
payload: JSON.stringify(payload).substring(0, 4096),
user: request.user,
},
request: {
method: request.method,
url: request.url,
ipAddress: request.remoteAddress,
userAgent:
typeof request.headers["user-agent"] === "string"
? request.headers["user-agent"]
: undefined,
body: convertRequestBodyToString(request.body),
headers: filterEmptyRequestHeaders(request.headers),
source: request.source,
route: request.route,
},
agent: this.getAgentInfo(),
};
this.getInspectionStatistics().onDetectedAttack({
blocked,
});
this.attackLogger.log(attack);
if (this.token) {
this.api.report(this.token, attack, this.timeoutInMS).catch(() => {
this.logger.log("Failed to report attack");
});
}
}
/**
* Sends a heartbeat via the API to the server (only when not in serverless mode)
*/
private heartbeat(timeoutInMS = this.timeoutInMS) {
this.sendHeartbeat(timeoutInMS).catch(() => {
this.logger.log("Failed to do heartbeat");
});
}
getUsers() {
return this.users;
}
getConfig() {
return this.serviceConfig;
}
private updateServiceConfig(response: ReportingAPIResponse) {
if (response.success) {
if (typeof response.block === "boolean") {
if (response.block !== this.block) {
this.block = response.block;
this.logger.log(
`Block mode has been set to ${this.block ? "on" : "off"}`
);
}
}
if (response.endpoints) {
this.serviceConfig.updateConfig(
response.endpoints && Array.isArray(response.endpoints)
? response.endpoints
: [],
typeof response.configUpdatedAt === "number"
? response.configUpdatedAt
: Date.now(),
response.blockedUserIds && Array.isArray(response.blockedUserIds)
? response.blockedUserIds
: [],
response.allowedIPAddresses &&
Array.isArray(response.allowedIPAddresses)
? response.allowedIPAddresses
: [],
typeof response.receivedAnyStats === "boolean"
? response.receivedAnyStats
: true
);
}
const minimumHeartbeatIntervalMS = 2 * 60 * 1000;
if (
typeof response.heartbeatIntervalInMS === "number" &&
response.heartbeatIntervalInMS >= minimumHeartbeatIntervalMS
) {
this.sendHeartbeatEveryMS = response.heartbeatIntervalInMS;
}
}
}
private async sendHeartbeat(timeoutInMS: number) {
if (this.token) {
this.logger.log("Heartbeat...");
const stats = this.statistics.getStats();
const routes = this.routes.asArray();
const outgoingDomains = this.hostnames.asArray();
const users = this.users.asArray();
const endedAt = Date.now();
this.statistics.reset();
this.routes.clear();
this.hostnames.clear();
this.users.clear();
const response = await this.api.report(
this.token,
{
type: "heartbeat",
time: Date.now(),
agent: this.getAgentInfo(),
stats: {
operations: stats.operations,
startedAt: stats.startedAt,
endedAt: endedAt,
requests: stats.requests,
userAgents: stats.userAgents,
ipAddresses: stats.ipAddresses,
},
hostnames: outgoingDomains,
routes: routes,
users: users,
middlewareInstalled: this.middlewareInstalled,
},
timeoutInMS
);
this.updateServiceConfig(response);
}
}
/**
* Starts a heartbeat when not in serverless mode : Make contact with api every x seconds.
*/
private startHeartbeats() {
if (this.serverless) {
this.logger.log(
"Running in serverless environment, not starting heartbeats"
);
return;
}
if (!this.token) {
this.logger.log("No token provided, not starting heartbeats");
return;
}
/* c8 ignore next 3 */
if (this.interval) {
throw new Error("Interval already started");
}
this.interval = setInterval(() => {
const now = performance.now();
const diff = now - this.lastHeartbeat;
const shouldSendHeartbeat = diff > this.sendHeartbeatEveryMS;
const hasCompressedStats = this.statistics.hasCompressedStats();
const canSendInitialStats =
!this.serviceConfig.hasReceivedAnyStats() && !this.statistics.isEmpty();
const shouldReportInitialStats =
!this.reportedInitialStats &&
(hasCompressedStats || canSendInitialStats);
if (shouldSendHeartbeat || shouldReportInitialStats) {
this.heartbeat();
this.lastHeartbeat = now;
this.reportedInitialStats = true;
}
}, this.checkIfHeartbeatIsNeededEveryMS);
this.interval.unref();
}
private async updateBlockedLists() {
if (!this.token) {
return;
}
if (this.serverless) {
// Not supported in serverless mode
return;
}
try {
const {
blockedIPAddresses,
blockedUserAgents,
allowedIPAddresses,
monitoredIPAddresses,
monitoredUserAgents,
userAgentDetails,
} = await fetchBlockedLists(this.token);
this.serviceConfig.updateBlockedIPAddresses(blockedIPAddresses);
this.serviceConfig.updateBlockedUserAgents(blockedUserAgents);
this.serviceConfig.updateAllowedIPAddresses(allowedIPAddresses);
this.serviceConfig.updateMonitoredIPAddresses(monitoredIPAddresses);
this.serviceConfig.updateMonitoredUserAgents(monitoredUserAgents);
this.serviceConfig.updateUserAgentDetails(userAgentDetails);
} catch (error: any) {
console.error(`Aikido: Failed to update blocked lists: ${error.message}`);
}
}
private startPollingForConfigChanges() {
pollForChanges({
token: this.token,
serverless: this.serverless,
logger: this.logger,
lastUpdatedAt: this.serviceConfig.getLastUpdatedAt(),
onConfigUpdate: (config) => {
this.updateServiceConfig({ success: true, ...config });
this.updateBlockedLists().catch((error) => {
this.logger.log(`Failed to update blocked lists: ${error.message}`);
});
},
});
}
private getAgentInfo(): AgentInfo {
return {
dryMode: !this.block,
/* c8 ignore next */
hostname: hostname() || "",
version: getAgentVersion(),
library: "firewall-node",
/* c8 ignore next */
ipAddress: ip() || "",
packages: Object.keys(this.wrappedPackages).reduce(
(packages: Record<string, string>, pkg) => {
const details = this.wrappedPackages[pkg];
if (details.version && details.supported) {
packages[pkg] = details.version;
}
return packages;
},
{}
),
incompatiblePackages: {
prototypePollution: this.incompatiblePackages,
},
preventedPrototypePollution: this.preventedPrototypePollution,
nodeEnv: process.env.NODE_ENV || "",
serverless: !!this.serverless,
stack: Object.keys(this.wrappedPackages).concat(
this.serverless ? [this.serverless] : []
),
os: {
name: platform(),
version: release(),
},
platform: {
version: getSemverNodeVersion(),
arch: process.arch,
},
};
}
start(wrappers: Wrapper[]) {
if (this.started) {
throw new Error("Agent already started!");
}
this.started = true;
this.logger.log(`Starting agent v${getAgentVersion()}...`);
if (!this.block) {
this.logger.log("Dry mode enabled, no requests will be blocked!");
}
if (this.token) {
this.logger.log("Found token, reporting enabled!");
} else {
this.logger.log("No token provided, disabling reporting.");
if (!this.block && !isAikidoCI()) {
console.log(
"AIKIDO: Running in monitoring only mode without reporting to Aikido Cloud. Set AIKIDO_BLOCK=true to enable blocking."
);
}
}
wrapInstalledPackages(wrappers, this.serverless);
// Send startup event and wait for config
// Then start heartbeats and polling for config changes
this.onStart()
.then(() => {
this.startHeartbeats();
this.startPollingForConfigChanges();
})
.catch((err) => {
console.error(`Aikido: Failed to start agent: ${err.message}`);
});
}
onFailedToWrapMethod(module: string, name: string, error: Error) {
this.logger.log(
`Failed to wrap method ${name} in module ${module}: ${error.message}`
);
}
onFailedToWrapModule(module: string, error: Error) {
this.logger.log(`Failed to wrap module ${module}: ${error.message}`);
}
onPackageWrapped(name: string, details: WrappedPackage) {
if (this.wrappedPackages[name]) {
// Already reported as wrapped
return;
}
this.wrappedPackages[name] = details;
if (details.version) {
if (details.supported) {
this.logger.log(`${name}@${details.version} is supported!`);
} else {
this.logger.log(`${name}@${details.version} is not supported!`);
}
}
}
onConnectHostname(hostname: string, port: number) {
this.hostnames.add(hostname, port);
}
onRouteExecute(context: Context) {
this.routes.addRoute(context);
}
hasGraphQLSchema(method: string, path: string) {
return this.routes.hasGraphQLSchema(method, path);
}
onGraphQLSchema(method: string, path: string, schema: string) {
this.routes.setGraphQLSchema(method, path, schema);
}
onGraphQLExecute(
method: string,
path: string,
type: "query" | "mutation",
topLevelFields: string[]
) {
topLevelFields.forEach((field) => {
this.routes.addGraphQLField(method, path, type, field);
});
}
getRoutes() {
return this.routes;
}
log(message: string) {
this.logger.log(message);
}
async flushStats(timeoutInMS: number) {
this.statistics.forceCompress();
await this.sendHeartbeat(timeoutInMS);
}
getRateLimiter() {
return this.rateLimiter;
}
onMiddlewareExecuted() {
this.middlewareInstalled = true;
}
}