-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathprotect.ts
More file actions
218 lines (195 loc) · 5.99 KB
/
protect.ts
File metadata and controls
218 lines (195 loc) · 5.99 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
import type { HttpFunction } from "@google-cloud/functions-framework";
import type { Handler } from "aws-lambda";
import { ChildProcess } from "../sinks/ChildProcess";
import { Fetch } from "../sinks/Fetch";
import { FileSystem } from "../sinks/FileSystem";
import { HTTPRequest } from "../sinks/HTTPRequest";
import { MariaDB } from "../sinks/MariaDB";
import { MongoDB } from "../sinks/MongoDB";
import { MySQL } from "../sinks/MySQL";
import { MySQL2 } from "../sinks/MySQL2";
import { Path } from "../sinks/Path";
import { Postgres } from "../sinks/Postgres";
import { Undici } from "../sinks/Undici";
import { Express } from "../sources/Express";
import {
createCloudFunctionWrapper,
FunctionsFramework,
} from "../sources/FunctionsFramework";
import { Hono } from "../sources/Hono";
import { HTTPServer } from "../sources/HTTPServer";
import { createLambdaWrapper } from "../sources/Lambda";
import { PubSub } from "../sources/PubSub";
import { Agent } from "./Agent";
import { getInstance, setInstance } from "./AgentSingleton";
import { ReportingAPI } from "./api/ReportingAPI";
import { ReportingAPINodeHTTP } from "./api/ReportingAPINodeHTTP";
import { ReportingAPIRateLimitedClientSide } from "./api/ReportingAPIRateLimitedClientSide";
import { ReportingAPIRateLimitedServerSide } from "./api/ReportingAPIRateLimitedServerSide";
import { ReportingAPIThatValidatesToken } from "./api/ReportingAPIThatValidatesToken";
import { Token } from "./api/Token";
import { getAPIURL } from "./getAPIURL";
import { Logger } from "./logger/Logger";
import { LoggerConsole } from "./logger/LoggerConsole";
import { LoggerNoop } from "./logger/LoggerNoop";
import { GraphQL } from "../sources/GraphQL";
import { Xml2js } from "../sources/Xml2js";
import { FastXmlParser } from "../sources/FastXmlParser";
import { SQLite3 } from "../sinks/SQLite3";
import { XmlMinusJs } from "../sources/XmlMinusJs";
import { Hapi } from "../sources/Hapi";
import { Shelljs } from "../sinks/Shelljs";
import { NodeSQLite } from "../sinks/NodeSQLite";
import { BetterSQLite3 } from "../sinks/BetterSQLite3";
import { isDebugging } from "../helpers/isDebugging";
import { shouldBlock } from "../helpers/shouldBlock";
import { Postgresjs } from "../sinks/Postgresjs";
import { Fastify } from "../sources/Fastify";
import { Koa } from "../sources/Koa";
import { Restify } from "../sources/Restify";
import { ReactRouter } from "../sources/ReactRouter";
import { ClickHouse } from "../sinks/ClickHouse";
import { Prisma } from "../sinks/Prisma";
import { AwsSDKVersion2 } from "../sinks/AwsSDKVersion2";
import { OpenAI } from "../sinks/OpenAI";
import { AwsSDKVersion3 } from "../sinks/AwsSDKVersion3";
import { AiSDK } from "../sinks/AiSDK";
import { Mistral } from "../sinks/Mistral";
import { Anthropic } from "../sinks/Anthropic";
import { GoogleGenAi } from "../sinks/GoogleGenAi";
import type { FetchListsAPI } from "./api/FetchListsAPI";
import { FetchListsAPINodeHTTP } from "./api/FetchListsAPINodeHTTP";
import shouldEnableFirewall from "../helpers/shouldEnableFirewall";
function getLogger(): Logger {
if (isDebugging()) {
return new LoggerConsole();
}
return new LoggerNoop();
}
function validatesToken(api: ReportingAPI) {
return new ReportingAPIThatValidatesToken(api);
}
function clientSideRateLimited(api: ReportingAPI) {
return new ReportingAPIRateLimitedClientSide(api, {
maxEventsPerInterval: 100,
intervalInMs: 60 * 60 * 1000,
});
}
function serverSideRateLimited(api: ReportingAPI) {
return new ReportingAPIRateLimitedServerSide(api);
}
function getAPI(): ReportingAPI {
return validatesToken(
serverSideRateLimited(
clientSideRateLimited(new ReportingAPINodeHTTP(getAPIURL()))
)
);
}
function getFetchListsAPI(): FetchListsAPI {
return new FetchListsAPINodeHTTP();
}
function getTokenFromEnv(): Token | undefined {
return process.env.AIKIDO_TOKEN
? new Token(process.env.AIKIDO_TOKEN)
: undefined;
}
function startAgent({
serverless,
newInstrumentation,
}: {
serverless: string | undefined;
newInstrumentation: boolean;
}) {
const current = getInstance();
if (current) {
return current;
}
const agent = new Agent(
shouldBlock(),
getLogger(),
getAPI(),
getTokenFromEnv(),
serverless,
newInstrumentation,
getFetchListsAPI()
);
setInstance(agent);
agent.start(getWrappers());
return agent;
}
export function getWrappers() {
return [
new Express(),
new MongoDB(),
new Postgres(),
new MySQL(),
new MySQL2(),
new PubSub(),
new FunctionsFramework(),
new ChildProcess(),
new FileSystem(),
new HTTPRequest(),
new Fetch(),
new Undici(),
new Path(),
new HTTPServer(),
new Hono(),
new GraphQL(),
new OpenAI(),
new Mistral(),
new Anthropic(),
new Xml2js(),
new FastXmlParser(),
new SQLite3(),
new XmlMinusJs(),
new Shelljs(),
new Hapi(),
new MariaDB(),
new NodeSQLite(),
new BetterSQLite3(),
new Postgresjs(),
new Fastify(),
new Koa(),
new Restify(),
new ReactRouter(),
new ClickHouse(),
new Prisma(),
new AwsSDKVersion3(),
// new Function(), Disabled because functionName.constructor === Function is false after patching global
new AwsSDKVersion2(),
new AiSDK(),
new GoogleGenAi(),
];
}
export function protect() {
startAgent({
serverless: undefined,
newInstrumentation: false,
});
}
export function lambda(): (handler: Handler) => Handler {
if (!shouldEnableFirewall()) {
return (handler: Handler) => handler;
}
startAgent({
serverless: "lambda",
newInstrumentation: false,
});
return createLambdaWrapper;
}
export function cloudFunction(): (handler: HttpFunction) => HttpFunction {
if (!shouldEnableFirewall()) {
return (handler: HttpFunction) => handler;
}
startAgent({
serverless: "gcp",
newInstrumentation: false,
});
return createCloudFunctionWrapper;
}
export function protectWithNewInstrumentation() {
startAgent({
serverless: undefined,
newInstrumentation: true,
});
}