Skip to content

add support to modify the logic for match #29

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 24 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ interface INetworkRequestResponse {
}

export default class LogrocketFuzzySearch {
public static setup(fields: string[]) {
const instance = new LogrocketFuzzySearch(fields);
public static setup(fields: string[], isSubStringMatch?: boolean) {
const instance = new LogrocketFuzzySearch(fields, isSubStringMatch);

return {
requestSanitizer: instance.requestSanitizer.bind(instance),
Expand All @@ -17,14 +17,16 @@ export default class LogrocketFuzzySearch {
}

public fields: string[] = [];
public isSubStringMatch?: boolean = false;

constructor(privateFields: string[]) {
constructor(privateFields: string[], isSubStringMatch?: boolean) {
this.fields = privateFields;
this.isSubStringMatch = isSubStringMatch;
}

public requestSanitizer(request: INetworkRequestResponse): object | any {
// avoid parsing GET requests as there will be no body
if (request.method === 'GET') {
if (request.method === "GET") {
return request;
}

Expand All @@ -37,8 +39,10 @@ export default class LogrocketFuzzySearch {

private _networkHandler(networkRequestReponse: INetworkRequestResponse) {
const { body, headers } = networkRequestReponse;
const requestContentType: string = headers && (headers['Content-Type'] || '');
const isUrlEncodedRequest: boolean = requestContentType.includes('form-urlencoded');
const requestContentType: string =
headers && (headers["Content-Type"] || "");
const isUrlEncodedRequest: boolean =
requestContentType.includes("form-urlencoded");
let parsedBody: object;

try {
Expand Down Expand Up @@ -72,16 +76,16 @@ export default class LogrocketFuzzySearch {
where type/value keynames are generic and instead
the value matching the type keyname should be masked.
*/
const isTypeValuePair = key === 'type' && 'value' in body;
const isTypeValuePair = key === "type" && "value" in body;

if (typeof keyName === 'object') {
if (typeof keyName === "object") {
if (!isTypeValuePair) {
this._searchBody(keyName);
}
}

if (isTypeValuePair) {
this._mask(body, body.type, 'value');
this._mask(body, body.type, "value");
} else {
this._mask(body, key);
}
Expand All @@ -96,14 +100,21 @@ export default class LogrocketFuzzySearch {
const isSensitiveFieldName = this._match(searchKeyName);

if (isSensitiveFieldName) {
body[maskKeyName] = '*';
body[maskKeyName] = "*";
}
}

private _match(keyName: string = ''): boolean {
private _match(keyName: string = ""): boolean {
const { fields } = this;
const normalizedKeyName = keyName.toLowerCase();

return fields.some((field) => normalizedKeyName.indexOf(field.toLowerCase()) > -1);
if (this.isSubStringMatch) {
return fields.some(
(field) => normalizedKeyName.indexOf(field.toLowerCase()) > -1
);
} else {
const fieldsLowerCase = fields.map((x) => x.toLowerCase());
return fieldsLowerCase.includes(normalizedKeyName);
}
}
}
}