-
Notifications
You must be signed in to change notification settings - Fork 19
/
har_sanitize.tsx
238 lines (213 loc) · 6.02 KB
/
har_sanitize.tsx
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
import { Cookie, Har, Header, Param, QueryString } from "har-format";
/* eslint-disable @typescript-eslint/no-explicit-any */
export type PossibleScrubItems = {
headers: string[];
cookies: string[];
queryArgs: string[];
postParams: string[];
mimeTypes: string[];
};
const defaultMimeTypesList = ["application/javascript", "text/javascript"];
const defaultWordList = [
"Authorization",
"SAMLRequest",
"SAMLResponse",
"access_token",
"appID",
"assertion",
"auth",
"authenticity_token",
"challenge",
"client_id",
"client_secret",
"code",
"code_challenge",
"code_verifier",
"email",
"facetID",
"fcParams",
"id_token",
"password",
"refresh_token",
"serverData",
"shdf",
"state",
"token",
"usg",
"vses2",
"x-client-data",
];
export const defaultScrubItems = [...defaultMimeTypesList, ...defaultWordList];
// The default list of regexes that aren't word dependent
// Uses double list so it matches format of word regex
const defaultRegex = [
[
// Redact signature on JWTs
{
regex: new RegExp(
`\\b(ey[A-Za-z0-9-_=]+)\\.(ey[A-Za-z0-9-_=]+)\\.[A-Za-z0-9-_.+/=]+\\b`,
"g",
),
replacement: `$1.$2.redacted`,
},
],
];
function buildRegex(word: string) {
return [
{
// [full word]=[capture]
regex: new RegExp(
`([\\s";,&?]+${word}=)([\\w+-_/=#|.%&:!*()\`~'"]+?)(&|\\\\",|",|"\\s|"}}|;){1}`,
"g",
),
replacement: `$1[${word} redacted]$3`,
},
// Set up this way in case "value" isn't directly after "name"
// {
// "name": "[word]",
// "something": "not wanted",
// "value": "[capture]"
// }
{
regex: new RegExp(
`("name": "${word}",[\\s\\w+:"-\\%!*()\`~'.,#]*?"value": ")((?:\\\\"|[^"])*?)(")`,
"g",
),
replacement: `$1[${word} redacted]$3`,
},
// "name" comes after "value"
// {
// "value": "[capture]",
// "something": "not wanted",
// "name": "[word]"
// }
{
regex: new RegExp(
`("value": ")([\\w+-_:&+=#$~/()\\\\.\\,*!|%"\\s;]+)("[,\\s}}]+)([\\s\\w+:"-\\\\%!*\`()~'#.]*"name": "${word}")`,
"g",
),
replacement: `$1[${word} redacted]$3$4`,
},
];
}
function removeContentForMimeTypes(input: string, scrubList: string[]) {
const harJSON = JSON.parse(input);
const entries = harJSON.log.entries;
if (!entries) {
throw new Error("failed to find entries in HAR file");
}
for (const entry of entries) {
const response = entry.response;
if (response && scrubList.includes(response.content.mimeType)) {
response.content.text = `[${response.content.mimeType} redacted]`;
}
}
return JSON.stringify(harJSON, null, 2);
}
export function getHarInfo(input: string): PossibleScrubItems {
const output = {
headers: new Set<string>(),
queryArgs: new Set<string>(),
cookies: new Set<string>(),
postParams: new Set<string>(),
mimeTypes: new Set<string>(),
};
const harJSON: Har = JSON.parse(input);
const entries = harJSON.log.entries;
if (!entries) {
throw new Error("failed to find entries in HAR file");
}
for (const entry of entries) {
const response = entry.response;
response.headers.map((header: Header) => output.headers.add(header.name));
response.cookies.map((cookie: Cookie) => output.cookies.add(cookie.name));
output.mimeTypes.add(response.content.mimeType);
const request = entry.request;
request.headers.map((header: Header) => output.headers.add(header.name));
request.queryString.map((arg: QueryString) =>
output.queryArgs.add(arg.name),
);
request.cookies.map((cookie: Cookie) => output.cookies.add(cookie.name));
if (request.postData) {
request.postData.params?.map((param: Param) =>
output.postParams.add(param.name),
);
}
}
return {
headers: [...output.headers].sort(),
queryArgs: [...output.queryArgs].sort(),
cookies: [...output.cookies].sort(),
postParams: [...output.postParams].sort(),
mimeTypes: [...output.mimeTypes].sort(),
};
}
function getScrubMimeTypes(
options?: SanitizeOptions,
possibleScrubItems?: PossibleScrubItems,
) {
if (options?.allMimeTypes && !!possibleScrubItems) {
return possibleScrubItems.mimeTypes;
}
return options?.scrubMimetypes || defaultMimeTypesList;
}
function getScrubWords(
options?: SanitizeOptions,
possibleScrubItems?: PossibleScrubItems,
) {
let scrubWords = options?.scrubWords || [];
if (options?.allCookies && !!possibleScrubItems) {
scrubWords = scrubWords.concat(possibleScrubItems.cookies);
}
if (options?.allHeaders && !!possibleScrubItems) {
scrubWords = scrubWords.concat(possibleScrubItems.headers);
}
if (options?.allQueryArgs && !!possibleScrubItems) {
scrubWords = scrubWords.concat(possibleScrubItems.queryArgs);
}
if (options?.allPostParams && !!possibleScrubItems) {
scrubWords = scrubWords.concat(possibleScrubItems.postParams);
}
return scrubWords || defaultScrubItems;
}
type SanitizeOptions = {
scrubWords?: string[];
scrubMimetypes?: string[];
allCookies?: boolean;
allHeaders?: boolean;
allQueryArgs?: boolean;
allMimeTypes?: boolean;
allPostParams?: boolean;
};
export function sanitize(input: string, options?: SanitizeOptions) {
console.log("options", JSON.stringify(options, null, 2));
let possibleScrubItems: PossibleScrubItems | undefined;
if (
options?.allCookies ||
options?.allHeaders ||
options?.allMimeTypes ||
options?.allQueryArgs ||
options?.allPostParams
) {
// we have to parse the HAR to get the full list of things we could scrub
possibleScrubItems = getHarInfo(input);
}
// Remove specific mime responses first
input = removeContentForMimeTypes(
input,
getScrubMimeTypes(options, possibleScrubItems),
);
// trim the list of words we are looking for down to the ones actually in the HAR file
const wordList = getScrubWords(options, possibleScrubItems).filter((val) =>
input.includes(val),
);
// build list of regexes needed to actually scrub the file
const wordSpecificScrubList = wordList.map((word) => buildRegex(word));
const allScrubList = defaultRegex.concat(wordSpecificScrubList);
for (const scrubList of allScrubList) {
for (const scrub of scrubList) {
input = input.replace(scrub.regex, scrub.replacement);
}
}
return input;
}