Skip to content
This repository was archived by the owner on Apr 2, 2023. It is now read-only.

Commit 8f98db3

Browse files
committed
feat: redact method to provide a custom PII redactor
1 parent 4739816 commit 8f98db3

File tree

3 files changed

+189
-95
lines changed

3 files changed

+189
-95
lines changed

README.md

Lines changed: 104 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,104 @@
1-
# pii
2-
3-
[![Test Coverage](https://api.codeclimate.com/v1/badges/1c788782dac545f74307/test_coverage)](https://codeclimate.com/github/tdreyno/pii/test_coverage)
4-
[![npm latest version](https://img.shields.io/npm/v/@tdreyno/pii/latest.svg)](https://www.npmjs.com/package/@tdreyno/pii)
5-
[![Minified Size](https://badgen.net/bundlephobia/minzip/@tdreyno/pii)](https://bundlephobia.com/result?p=@tdreyno/pii)
6-
7-
pii is library for wrapping Personally Identifying Information and providing ways to limit its spread.
8-
9-
## Installation
10-
11-
### Yarn
12-
13-
```sh
14-
yarn add @tdreyno/pii
15-
```
16-
17-
### NPM
18-
19-
```sh
20-
npm install --save @tdreyno/pii
21-
```
22-
23-
## Usage
24-
25-
The concept is simple. Wrap `PII` around any PII data **as soon as it enters your system.**
26-
27-
```typescript
28-
import { PII, unwrapObject } from "@tdreyno/pii"
29-
30-
const handlePost = (postBody: { name: string; phoneNumber: string }) =>
31-
createUser(PII(postBody.name), PII(postBody.phoneNumber))
32-
33-
const createUser = (name: PII<string>, phoneNumber: PII<string>) =>
34-
sendToThirdParty(unwrapObject({ name, phoneNumber }))
35-
```
36-
37-
`PII` wraps any data and obscures the details. Attempts to coerce it to other types or convert to JSON will result in a string result of `PII<REDACTED>`. Once the PII has flowed through your system, use `unwrap` to get the data back out for posting to a third party. Or `unwrapObject` which will recursively unwrap PII inside a post body object.
38-
39-
### Avoiding unsafe unwrapping and rewrapping
40-
41-
If you want to combine two pieces of PII, such as first and last name, you might want to unwrap, combine and then rewrap them. Once you start using unwrap everywhere, it becomes very hard to maintain safetly. Rather, use the built in methods of this library to mutate and combine PII while keeping the data inside the wrapper.
42-
43-
#### Modify PII
44-
45-
```typescript
46-
import { PII, map } from "@tdreyno/pii"
47-
48-
const name = PII("Thomas")
49-
const lowercaseName = map(n => n.toLowerCase(), name) // PII<"thomas">
50-
```
51-
52-
#### Combine two things
53-
54-
```typescript
55-
import { PII, zip2With } from "@tdreyno/pii"
56-
57-
const firstName = PII("Thomas")
58-
const lastName = PII("Reynolds")
59-
const fullName = zip2With(
60-
(first, last) => `${first} ${last}`,
61-
firstName,
62-
lastName,
63-
) // PII<Thomas Reynolds>
64-
```
65-
66-
**Note:** There are methods for up to 4 PII inputs: `zip2With`, `zip3With`, `zip4With`.
67-
68-
#### Reduce a list of PII
69-
70-
```typescript
71-
import { PII, fold } from "@tdreyno/pii"
72-
73-
const address1 = PII(ADDRESS_OBJECT_1)
74-
const address2 = PII(ADDRESS_OBJECT_2)
75-
const address2 = PII(ADDRESS_OBJECT_2)
76-
77-
const allAddresses = fold(
78-
(acc, address) => (acc.push(address), acc),
79-
[],
80-
[address1, address2, address3],
81-
) // PII<[address1, address2, address3]>
82-
```
83-
84-
#### Inspect or create side-effects using PII contents without unwrapping
85-
86-
```typescript
87-
import { PII, tap } from "@tdreyno/pii"
88-
89-
const name = PII("Thomas")
90-
const lowercaseName = tap(n => console.log(n), name) // Logs "Thomas"
91-
```
92-
93-
## License
94-
95-
pii is licensed under the Hippocratic License. It is an [Ethical Source license](https://ethicalsource.dev) derived from the MIT License, amended to limit the impact of the unethical use of open source software.
1+
# pii
2+
3+
[![Test Coverage](https://api.codeclimate.com/v1/badges/1c788782dac545f74307/test_coverage)](https://codeclimate.com/github/tdreyno/pii/test_coverage)
4+
[![npm latest version](https://img.shields.io/npm/v/@tdreyno/pii/latest.svg)](https://www.npmjs.com/package/@tdreyno/pii)
5+
[![Minified Size](https://badgen.net/bundlephobia/minzip/@tdreyno/pii)](https://bundlephobia.com/result?p=@tdreyno/pii)
6+
7+
pii is library for wrapping Personally Identifying Information and providing ways to limit its spread.
8+
9+
## Installation
10+
11+
### Yarn
12+
13+
```sh
14+
yarn add @tdreyno/pii
15+
```
16+
17+
### NPM
18+
19+
```sh
20+
npm install --save @tdreyno/pii
21+
```
22+
23+
## Usage
24+
25+
The concept is simple. Wrap `PII` around any PII data **as soon as it enters your system.**
26+
27+
```typescript
28+
import { PII, unwrapObject } from "@tdreyno/pii"
29+
30+
const handlePost = (postBody: { name: string; phoneNumber: string }) =>
31+
createUser(PII(postBody.name), PII(postBody.phoneNumber))
32+
33+
const createUser = (name: PII<string>, phoneNumber: PII<string>) =>
34+
sendToThirdParty(unwrapObject({ name, phoneNumber }))
35+
```
36+
37+
`PII` wraps any data and obscures the details. Attempts to coerce it to other types or convert to JSON will result in a string result of `PII<REDACTED>`. Once the PII has flowed through your system, use `unwrap` to get the data back out for posting to a third party. Or `unwrapObject` which will recursively unwrap PII inside a post body object.
38+
39+
### Avoiding unsafe unwrapping and rewrapping
40+
41+
If you want to combine two pieces of PII, such as first and last name, you might want to unwrap, combine and then rewrap them. Once you start using unwrap everywhere, it becomes very hard to maintain safetly. Rather, use the built in methods of this library to mutate and combine PII while keeping the data inside the wrapper.
42+
43+
#### Modify PII
44+
45+
```typescript
46+
import { PII, map } from "@tdreyno/pii"
47+
48+
const name = PII("Thomas")
49+
const lowercaseName = map(n => n.toLowerCase(), name) // PII<"thomas">
50+
```
51+
52+
#### Combine two things
53+
54+
```typescript
55+
import { PII, zip2With } from "@tdreyno/pii"
56+
57+
const firstName = PII("Thomas")
58+
const lastName = PII("Reynolds")
59+
const fullName = zip2With(
60+
(first, last) => `${first} ${last}`,
61+
firstName,
62+
lastName,
63+
) // PII<Thomas Reynolds>
64+
```
65+
66+
**Note:** There are methods for up to 4 PII inputs: `zip2With`, `zip3With`, `zip4With`.
67+
68+
#### Reduce a list of PII
69+
70+
```typescript
71+
import { PII, fold } from "@tdreyno/pii"
72+
73+
const address1 = PII(ADDRESS_OBJECT_1)
74+
const address2 = PII(ADDRESS_OBJECT_2)
75+
const address2 = PII(ADDRESS_OBJECT_2)
76+
77+
const allAddresses = fold(
78+
(acc, address) => (acc.push(address), acc),
79+
[],
80+
[address1, address2, address3],
81+
) // PII<[address1, address2, address3]>
82+
```
83+
84+
#### Inspect or create side-effects using PII contents without unwrapping
85+
86+
```typescript
87+
import { PII, tap } from "@tdreyno/pii"
88+
89+
const name = PII("Thomas")
90+
const lowercaseName = tap(n => console.log(n), name) // Logs "Thomas"
91+
```
92+
93+
#### Custom PII Redaction
94+
95+
```typescript
96+
import { PII, redact } from "@tdreyno/pii"
97+
98+
const name = PII("Thomas")
99+
const lowercaseName = redact(() => "REDACTED", name) // Returns "REDACTED"
100+
```
101+
102+
## License
103+
104+
pii is licensed under the Hippocratic License. It is an [Ethical Source license](https://ethicalsource.dev) derived from the MIT License, amended to limit the impact of the unethical use of open source software.

src/__tests__/redact.spec.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { PII, redact } from "../index"
2+
3+
const REDACTED = "REDACTED"
4+
const redactor = () => REDACTED
5+
6+
describe("redact", () => {
7+
it("should redact all wrappers", () => {
8+
expect(redact(redactor, "test")).toBe("test")
9+
expect(redact(redactor, PII("test"))).toBe(REDACTED)
10+
11+
expect(redact(redactor, 1)).toBe(1)
12+
expect(redact(redactor, PII(1))).toBe(REDACTED)
13+
14+
expect(redact(redactor, null)).toBe(null)
15+
expect(redact(redactor, PII(null))).toBe(REDACTED)
16+
17+
expect(redact(redactor, [])).toEqual([])
18+
expect(redact(redactor, PII([]))).toEqual(REDACTED)
19+
20+
expect(redact(redactor, {})).toEqual({})
21+
expect(redact(redactor, PII({}))).toEqual(REDACTED)
22+
23+
expect(redact(redactor, ["test"])).toEqual(["test"])
24+
expect(redact(redactor, PII(["test"]))).toEqual(REDACTED)
25+
26+
expect(redact(redactor, { test: 1 })).toEqual({ test: 1 })
27+
expect(redact(redactor, PII({ test: 1 }))).toEqual(REDACTED)
28+
29+
expect(redact(redactor, ["test", PII(2)])).toEqual(["test", REDACTED])
30+
expect(redact(redactor, PII(["test", PII(2)]))).toEqual(REDACTED)
31+
32+
expect(redact(redactor, { test: 1, two: PII(2) })).toEqual({
33+
test: 1,
34+
two: REDACTED,
35+
})
36+
expect(redact(redactor, PII({ test: 1, two: PII(2) }))).toEqual(REDACTED)
37+
})
38+
39+
it("should handle Map", () => {
40+
const map = new Map([["a", 1]])
41+
42+
expect(redact(redactor, { test: map, two: PII(2) })).toEqual({
43+
test: map,
44+
two: REDACTED,
45+
})
46+
})
47+
48+
it("should handle Set", () => {
49+
const set = new Set(["a", "b"])
50+
51+
expect(redact(redactor, { test: set, two: PII(2) })).toEqual({
52+
test: set,
53+
two: REDACTED,
54+
})
55+
})
56+
57+
it("should ignore weird Numbers", () => {
58+
const num = new Number(1)
59+
60+
expect(redact(redactor, { test: num, two: PII(2) })).toEqual({
61+
test: num,
62+
two: REDACTED,
63+
})
64+
})
65+
})

src/pii.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,23 @@ export const unwrapObject = (input: unknown): unknown =>
163163
primitive: p => p,
164164
object: p => p,
165165
})
166+
167+
export const redact = (redactor: (data: any) => any, input: unknown): unknown =>
168+
visitPII(isPIIType(input) ? redactor(input) : input, {
169+
record: o =>
170+
Object.keys(o).reduce((sum, key) => {
171+
sum[key] = redact(redactor, o[key])
172+
return sum
173+
}, {} as Record<string, unknown>),
174+
map: m =>
175+
new Map(
176+
Array.from(m).map(([k, v]) => [
177+
redact(redactor, k),
178+
redact(redactor, v),
179+
]),
180+
),
181+
array: a => a.map(x => redact(redactor, x)),
182+
set: s => new Set(Array.from(s).map(x => redact(redactor, x))),
183+
primitive: p => p,
184+
object: p => p,
185+
})

0 commit comments

Comments
 (0)