Skip to content

Commit 9cd8def

Browse files
authored
fix: redact headers, cookies, url params in har file (#22595)
fix: redact headers, cookies, url params in har file Signed-off-by: David Kwon <[email protected]>
1 parent 080f22f commit 9cd8def

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

tests/e2e/utils/CheReporter.ts

+32
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,44 @@ class CheReporter extends mocha.reporters.Spec {
181181
const networkLogsEntries: logging.Entry[] = await this.driverHelper.getDriver().manage().logs().get('performance');
182182
const events: any[] = networkLogsEntries.map((entry): any[] => JSON.parse(entry.message).message);
183183
const har: any = chromeHar.harFromMessages(events, { includeTextFromResponseBody: true });
184+
this.redactHarContent(har);
185+
184186
const networkLogsStream: WriteStream = fs.createWriteStream(harFileName);
185187
networkLogsStream.write(Buffer.from(JSON.stringify(har)), (): void => {
186188
networkLogsStream.end();
187189
});
188190
});
189191
}
192+
193+
redactHarContent(har: any): void {
194+
har.log?.entries?.forEach((entry: any): void => {
195+
let text: string | undefined = entry.request?.postData?.text;
196+
if (text) {
197+
text = StringUtil.updateUrlQueryValue(text, 'csrf', '<REDACTED>');
198+
text = StringUtil.updateUrlQueryValue(text, 'username', '<REDACTED>');
199+
entry.request.postData.text = StringUtil.updateUrlQueryValue(text, 'password', '<REDACTED>');
200+
}
201+
202+
const cookies: any = entry.request?.cookies;
203+
if (cookies) {
204+
cookies.forEach((cookie: any): void => {
205+
if (cookie.name?.startsWith('_oauth_proxy')) {
206+
cookie.value = '<REDACTED>';
207+
}
208+
});
209+
}
210+
211+
const headers: any = entry.request?.headers;
212+
if (headers) {
213+
headers.forEach((header: any): void => {
214+
if (header.name?.toLowerCase() === 'cookie') {
215+
header.value = StringUtil.updateCookieValue(header.value, '_oauth_proxy', '<REDACTED>');
216+
header.value = StringUtil.updateCookieValue(header.value, '_oauth_proxy_csrf', '<REDACTED>');
217+
}
218+
});
219+
}
220+
});
221+
}
190222
}
191223

192224
export = CheReporter;

tests/e2e/utils/StringUtil.ts

+28
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,32 @@ export class StringUtil {
5959

6060
return command.replace(/[{}]/g, '').replace(/(?<!")\${?[a-zA-Z0-9_+\-\s]+\b}?/gm, '"$&"');
6161
}
62+
63+
/**
64+
* replaces the cookie value of the specified cookie
65+
* @param cookie cookie names and values, seperated with ;
66+
* @param name name of cookie to replace its value for
67+
* @param replaceStr the new value of the cookie
68+
* @return updated cookie string with the cookie value replaced
69+
*/
70+
static updateCookieValue(cookie: string, name: string, replaceStr: string): string {
71+
Logger.trace();
72+
73+
const regex: RegExp = new RegExp(`(${name})=[^;]+`, 'g');
74+
return cookie.replace(regex, `$1=${replaceStr}`);
75+
}
76+
77+
/**
78+
* replaces the query value of the specified query
79+
* @param queryString query string (ie. query=value&query2=value2)
80+
* @param name name of the query to replace
81+
* @param replaceStr new query value
82+
* @returns updated queryString with the query value replaced
83+
*/
84+
static updateUrlQueryValue(queryString: string, name: string, replaceStr: string): string {
85+
Logger.trace();
86+
87+
const regex: RegExp = new RegExp(`(${name})=[^&]+`, 'g');
88+
return queryString.replace(regex, `$1=${replaceStr}`);
89+
}
6290
}

0 commit comments

Comments
 (0)