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

add validation for PI #131

Merged
merged 5 commits into from
Jan 16, 2025
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 @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Added

- Adding validator to remove PI.

## [0.5.9] - 2024-12-05

### Fixed
Expand Down
15 changes: 15 additions & 0 deletions src/Utils/LoggingSanitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Constants from "../Common/Constants";
import OmnichannelHTTPHeaders from "../Common/OmnichannelHTTPHeaders";

export class LoggingSanitizer {
private static emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;

public static stripCustomContextDataValues(customContextData: any): void { // eslint-disable-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
if (customContextData) {
Object.keys(customContextData)?.forEach((contextKey: string) => {
Expand Down Expand Up @@ -43,6 +45,11 @@ export class LoggingSanitizer {
if (Object.keys(headers).includes(OmnichannelHTTPHeaders.authCodeNonce)) {
headers[OmnichannelHTTPHeaders.authCodeNonce] = Constants.hiddenContentPlaceholder;
}

if (Object.keys(headers).includes(OmnichannelHTTPHeaders.authorization)) {
headers[OmnichannelHTTPHeaders.authorization] = Constants.hiddenContentPlaceholder;
}

}
}

Expand Down Expand Up @@ -75,11 +82,19 @@ export class LoggingSanitizer {
}

LoggingSanitizer.stripGeolocation(data);

configObject.data = JSON.stringify(data); // eslint-disable-line security/detect-object-injection
// at this point is better to pass the string to search via regex for specific PI.
configObject.data = LoggingSanitizer.stripEmailDataFromError(configObject.data);
}
}
}

public static stripEmailDataFromError(payload : string): string {
payload = payload.replace(this.emailRegex, Constants.hiddenContentPlaceholder);
return payload;
}

public static stripAxiosErrorSensitiveProperties(errorObject: any): void { // eslint-disable-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
if (errorObject.isAxiosError) {
if (errorObject?.config?.headers) {
Expand Down
217 changes: 123 additions & 94 deletions test/Utils/LoggingSanitizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,92 @@ import { LoggingSanitizer } from "../../src/Utils/LoggingSanitizer";
describe("LoggingSanitized unit tests", () => {
describe("Test removal of sensitive properties from error object", () => {
it("Error object should remove sensitive properties from logging details", (done) => {
const data = {
preChatResponse: {
Type: "InputSubmit",
foo: "foo",
bar: "bar",
},
customContextData: {
contextKey: {
value: "value",
isDisplayable: true
},
},
longitude: "",
latitude: ""
};

const configObject = {
method: "get",
headers: {
Accept: "application/json, text/plain, */*",
AuthenticatedUserToken: "authenticatedUserToken",
AuthCodeNonce: "authCodeNonce"
const data = {
preChatResponse: {
Type: "InputSubmit",
foo: "foo",
bar: "bar",
},
customContextData: {
contextKey: {
value: "value",
isDisplayable: true
},
data: JSON.stringify(data)
};

const errorObject = {
message: "Request failed with status code 401",
name: "Error",
config: configObject,
response: {
config: configObject
},
isAxiosError: true
}
},
longitude: "",
latitude: ""
};

const configObject = {
method: "get",
headers: {
Accept: "application/json, text/plain, */*",
AuthenticatedUserToken: "authenticatedUserToken",
AuthCodeNonce: "authCodeNonce"
},
data: JSON.stringify(data)
};

const errorObject = {
message: "Request failed with status code 401",
name: "Error",
config: configObject,
response: {
config: configObject
},
isAxiosError: true
}

LoggingSanitizer.stripErrorSensitiveProperties(errorObject);
expect(errorObject["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["config"]["headers"]["AuthCodeNonce"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["preChatResponse"]["Type"]).toEqual(data["preChatResponse"]["Type"]);
expect(JSON.parse(errorObject["config"]["data"])["preChatResponse"]["foo"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["preChatResponse"]["bar"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["customContextData"]["contextKey"]["value"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["longitude"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["longitude"]).toEqual(Constants.hiddenContentPlaceholder);

LoggingSanitizer.stripErrorSensitiveProperties(errorObject);
expect(errorObject["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["config"]["headers"]["AuthCodeNonce"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["preChatResponse"]["Type"]).toEqual(data["preChatResponse"]["Type"]);
expect(JSON.parse(errorObject["config"]["data"])["preChatResponse"]["foo"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["preChatResponse"]["bar"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["customContextData"]["contextKey"]["value"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["longitude"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["config"]["data"])["longitude"]).toEqual(Constants.hiddenContentPlaceholder);

expect(errorObject["response"]["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["response"]["config"]["headers"]["AuthCodeNonce"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["preChatResponse"]["Type"]).toEqual(data["preChatResponse"]["Type"]);
expect(JSON.parse(errorObject["response"]["config"]["data"])["preChatResponse"]["foo"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["preChatResponse"]["bar"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["customContextData"]["contextKey"]["value"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["longitude"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["longitude"]).toEqual(Constants.hiddenContentPlaceholder);
done();
expect(errorObject["response"]["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["response"]["config"]["headers"]["AuthCodeNonce"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["preChatResponse"]["Type"]).toEqual(data["preChatResponse"]["Type"]);
expect(JSON.parse(errorObject["response"]["config"]["data"])["preChatResponse"]["foo"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["preChatResponse"]["bar"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["customContextData"]["contextKey"]["value"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["longitude"]).toEqual(Constants.hiddenContentPlaceholder);
expect(JSON.parse(errorObject["response"]["config"]["data"])["longitude"]).toEqual(Constants.hiddenContentPlaceholder);
done();
});

it("Test when authentication token is an object", (done) => {
const configObject = {
"method": "get",
"headers": {
"Accept": "application/json, text/plain, */*",
"AuthenticatedUserToken": {}
},
};

const errorObject = {
message: "Request failed with status code 401",
name: "Error",
config: configObject,
response: {
config: configObject
},
isAxiosError: true
}
LoggingSanitizer.stripErrorSensitiveProperties(errorObject);
expect(errorObject["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["response"]["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
done();
const configObject = {
"method": "get",
"headers": {
"Accept": "application/json, text/plain, */*",
"AuthenticatedUserToken": {}
},
};

const errorObject = {
message: "Request failed with status code 401",
name: "Error",
config: configObject,
response: {
config: configObject
},
isAxiosError: true
}
LoggingSanitizer.stripErrorSensitiveProperties(errorObject);
expect(errorObject["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["response"]["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
done();
});

it("Test process error object when error is null", (done) => {
const errorObject = null
LoggingSanitizer.stripErrorSensitiveProperties(errorObject);
expect(errorObject).toEqual(null);
done();
const errorObject = null
LoggingSanitizer.stripErrorSensitiveProperties(errorObject);
expect(errorObject).toEqual(null);
done();
});

it("Test process error object when headers is undefined", (done) => {
Expand All @@ -115,13 +115,13 @@ describe("LoggingSanitized unit tests", () => {
};

const errorObject: any = {
message: "Request failed with status code 401",
name: "Error",
config: configObject,
response: {
config: configObject
},
isAxiosError: true
message: "Request failed with status code 401",
name: "Error",
config: configObject,
response: {
config: configObject
},
isAxiosError: true
}

LoggingSanitizer.stripErrorSensitiveProperties(errorObject);
Expand Down Expand Up @@ -150,26 +150,29 @@ describe("LoggingSanitized unit tests", () => {
headers: {
Accept: "application/json, text/plain, */*",
AuthenticatedUserToken: "authenticatedUserToken",
AuthCodeNonce: "authCodeNonce"
AuthCodeNonce: "authCodeNonce",
Authorization: "abc-1234"
},
};

const errorObject = {
message: "Request failed with status code 401",
name: "Error",
config: configObject,
response: {
config: configObject
},
isAxiosError: true
message: "Request failed with status code 401",
name: "Error",
config: configObject,
response: {
config: configObject
},
isAxiosError: true
}

LoggingSanitizer.stripErrorSensitiveProperties(errorObject);
expect(errorObject["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["config"]["headers"]["AuthCodeNonce"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["config"]["headers"]["Authorization"]).toEqual(Constants.hiddenContentPlaceholder);

expect(errorObject["response"]["config"]["headers"]["AuthenticatedUserToken"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["response"]["config"]["headers"]["AuthCodeNonce"]).toEqual(Constants.hiddenContentPlaceholder);
expect(errorObject["response"]["config"]["headers"]["Authorization"]).toEqual(Constants.hiddenContentPlaceholder);
done();
});
});
Expand Down Expand Up @@ -226,4 +229,30 @@ describe("LoggingSanitized unit tests", () => {
done();
});
});
});

describe("Test removal email address from data", () => {

it("Test removal email address from data", (done) => {
elopezanaya marked this conversation as resolved.
Show resolved Hide resolved
const data = "{\"locale\":\"en-us\",\"chatId\":\"19:[email protected]\",\"isProactiveChat\":false,\"browser\":\"Chrome\",\"device\":\"Desktop\",\"originurl\":\"https://chat.azure.com/livechat.html?a=blahblah&tenant=blahblah&[email protected]&oid=123456&[email protected]&theme=azure\",\"os\":\"Windows\"}";
const expected = "{\"locale\":\"en-us\",\"chatId\":\"19:[email protected]\",\"isProactiveChat\":false,\"browser\":\"Chrome\",\"device\":\"Desktop\",\"originurl\":\"https://chat.azure.com/livechat.html?a=blahblah&tenant=blahblah&loginhint=*content hidden*&oid=123456&preferredUsername=*content hidden*&theme=azure\",\"os\":\"Windows\"}";
const resp = LoggingSanitizer.stripEmailDataFromError(data);
expect(resp).toEqual(expected);
done();
});
it("Test removal email address present in payload", (done) => {
const data = "{\"ChatId\":\"19:whatevs.v2\",\"EmailAddress\":\"[email protected]\",\"DefaultAttachmentMessage\":\"The following attachment was uploaded during the conversation:\",\"CustomerLocale\":\"en-us\"}";
const expected = "{\"ChatId\":\"19:whatevs.v2\",\"EmailAddress\":\"*content hidden*\",\"DefaultAttachmentMessage\":\"The following attachment was uploaded during the conversation:\",\"CustomerLocale\":\"en-us\"}";
const resp = LoggingSanitizer.stripEmailDataFromError(data);
expect(resp).toEqual(expected);
done();
});

it("Test removal email address returns same string", (done) => {
const data = "{\"ChatId\":\"19:whatevs.v2\"}";
const expected = "{\"ChatId\":\"19:whatevs.v2\"}";
const resp = LoggingSanitizer.stripEmailDataFromError(data);
expect(resp).toEqual(expected);
done();
});
});
});
Loading