Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
Add utility function to redact object properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcrm committed Sep 6, 2019
1 parent 0fae510 commit da1fc3d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
21 changes: 21 additions & 0 deletions lib/support/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,24 @@ export async function sdmPostWebhook(
throw new Error(e);
}
}

/**
* This utility function can be used to recursively "redact" a string property on the event object.
* Substituted property values will have a value of "Redacted" unless optional new value is supplied.
*
* @param {Object} o
* @param {string} property
* @param {string} newValue Optional value to replace the object property value with.
*/
export async function redactObjectProperty(o: any, property: string, newValue: string = "Redacted"): Promise<any> {
for (const v of Object.keys(o)) {
if (o[v] && typeof o[v] === "object") {
await redactObjectProperty(o[v], property, newValue);
} else if (o[v] && v.toLowerCase() === property.toLowerCase() && typeof o[v] === "string") {
o[v] = newValue;
} else if ((o[v] === null || o[v] === undefined) && v.toLowerCase() === property.toLowerCase()) {
o[v] = newValue;
}
}
return o;
}
57 changes: 52 additions & 5 deletions test/support/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
import * as autoClient from "@atomist/automation-client";
import * as assert from "power-assert";
import * as sinon from "sinon";
import {
addAtomistSignatureHeader,
createHmacSignature,
sdmPostWebhook,
} from "../../lib/support/util";
import {addAtomistSignatureHeader, createHmacSignature, redactObjectProperty, sdmPostWebhook} from "../../lib/support/util";
import * as fakeHttpClients from "../testUtils/fakeHttpFactor.test";
import { fakeHeaders } from "../testUtils/fakeRelayer.test";
describe("util", () => {
Expand Down Expand Up @@ -115,4 +111,55 @@ describe("util", () => {
assert.strictEqual(dests.length, d.callCount);
});
});
describe("redactObjectProperty", () => {
it ("should redact specified properties", async () => {
const testObject = {
a: "test",
b: "test",
c: {
a: "test",
b: "test",
},
};
const result = await redactObjectProperty(testObject, "b");

assert.strictEqual(result.a, "test");
assert.strictEqual(result.b, "Redacted");
assert.strictEqual(result.c.a, "test");
assert.strictEqual(result.c.b, "Redacted");
});
it ("should redact specified properties with supplied new value", async () => {
const testObject = {
a: "test",
b: "test",
c: {
a: "test",
b: "test",
},
};
const result = await redactObjectProperty(testObject, "b", "new value");
assert.strictEqual(result.a, "test");
assert.strictEqual(result.b, "new value");
assert.strictEqual(result.c.a, "test");
assert.strictEqual(result.c.b, "new value");
});
it ("should not exceed call stack with large object", async () => {
const t: any = {};
let i = 0;
while (i < 50000) {
t[`test${i}`] = {
a: `value${i}`,
b: `value ${i}`,
c: {
a: `value${i}`,
b: `value${i}`,
},
};
i++;
}
await redactObjectProperty(t, "b", "new value");
assert(t[Object.keys(t)[0]].b === "new value");
assert(t[Object.keys(t)[0]].c.b === "new value");
});
});
});

0 comments on commit da1fc3d

Please sign in to comment.