Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove secrets during recording #401

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Improved filtering out secrets from recordings

## [2.0.0-76] - 2024-04-08

### Fixed
Expand Down
52 changes: 43 additions & 9 deletions src/utils/AutoSetupPolly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { fileURLToPath } from 'url';

import { state } from '../index';
import { getTokens } from '../ops/AuthenticateOps';
import { encode, isBase64Encoded } from './Base64Utils';
import { decode, encode, isBase64Encoded } from './Base64Utils';

const { setupPolly } = pollyJest;

Expand Down Expand Up @@ -88,7 +88,7 @@ export function filterRecording(recording: {
headers: [{ name: string; value: string }];
postData: { text: any };
};
response: { content: { text: any } };
response: { content: { mimeType: string; text: any } };
}) {
// request headers
if (recording.request?.headers) {
Expand Down Expand Up @@ -125,13 +125,47 @@ export function filterRecording(recording: {
// response body
if (recording.response?.content?.text) {
let body = recording.response.content.text;
try {
const json = JSON.parse(body);
if (json['access_token']) json['access_token'] = '<access token>';
if (json['id_token']) json['id_token'] = '<id token>';
body = JSON.stringify(json);
} catch (error) {
// ignore
// JSON content
if (
recording.response.content.mimeType === 'application/json;charset=UTF-8'
) {
try {
const json = JSON.parse(body);
if (json['access_token']) json['access_token'] = '<access token>';
if (json['id_token']) json['id_token'] = '<id token>';
if (json.accessKey) json.accessKey = '<access key>';
if (json.result) {
for (const obj of json.result) {
// check for scripts
if (obj.script) {
try {
let script = decode(obj.script);
script = script.replace(
/(var .*?(?:Sid|sid|Secret|secret|PhoneNumberFrom) = (?:"|'))(.*?)((?:"|'))/g,
'$1<secret>$3'
);
obj.script = encode(script);
} catch (error) {
//
}
}
}
}
body = JSON.stringify(json);
} catch (error) {
// ignore
}
}
// Text and XML content
if (recording.response.content.mimeType === 'text/xml;charset=utf-8') {
try {
body = body.replace(
/<ds:X509Certificate>.+?<\/ds:X509Certificate>/gs,
`<ds:X509Certificate>${encode('<certificate>')}</ds:X509Certificate>`
);
} catch (error) {
// ignore
}
}
recording.response.content.text = body;
}
Expand Down
52 changes: 43 additions & 9 deletions src/utils/SetupPollyForFrodoLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LogLevelDesc } from 'loglevel';
import path from 'path';

import { State } from '../shared/State';
import { encode, isBase64Encoded } from './Base64Utils';
import { decode, encode, isBase64Encoded } from './Base64Utils';
import { debugMessage, printMessage } from './Console';

const FRODO_MOCK_HOSTS = process.env.FRODO_MOCK_HOSTS
Expand Down Expand Up @@ -208,7 +208,7 @@ function filterRecording(recording: {
headers: [{ name: string; value: string }];
postData: { text: any };
};
response: { content: { text: any } };
response: { content: { mimeType: string; text: any } };
}) {
// request headers
if (recording.request?.headers) {
Expand Down Expand Up @@ -245,13 +245,47 @@ function filterRecording(recording: {
// response body
if (recording.response?.content?.text) {
let body = recording.response.content.text;
try {
const json = JSON.parse(body);
if (json['access_token']) json['access_token'] = '<access token>';
if (json['id_token']) json['id_token'] = '<id token>';
body = JSON.stringify(json);
} catch (error) {
// ignore
// JSON content
if (
recording.response.content.mimeType === 'application/json;charset=UTF-8'
) {
try {
const json = JSON.parse(body);
if (json['access_token']) json['access_token'] = '<access token>';
if (json['id_token']) json['id_token'] = '<id token>';
if (json.accessKey) json.accessKey = '<access key>';
if (json.result) {
for (const obj of json.result) {
// check for scripts
if (obj.script) {
try {
let script = decode(obj.script);
script = script.replace(
/(var .*?(?:Sid|sid|Secret|secret|PhoneNumberFrom) = (?:"|'))(.*?)((?:"|'))/g,
'$1<secret>$3'
);
obj.script = encode(script);
} catch (error) {
//
}
}
}
}
body = JSON.stringify(json);
} catch (error) {
// ignore
}
}
// Text and XML content
if (recording.response.content.mimeType === 'text/xml;charset=utf-8') {
try {
body = body.replace(
/<ds:X509Certificate>.+?<\/ds:X509Certificate>/gs,
`<ds:X509Certificate>${encode('<certificate>')}</ds:X509Certificate>`
);
} catch (error) {
// ignore
}
}
recording.response.content.text = body;
}
Expand Down
Loading