-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement scrubber for end user data in exceptions
- Loading branch information
1 parent
92c5730
commit 7d46bd3
Showing
12 changed files
with
150 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'slate': minor | ||
'slate-react': minor | ||
--- | ||
|
||
Add new Slate.Scrubber interface to allow scrubbing end user data from exception | ||
text. The default behavior remains unchanged. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Scrubber API | ||
|
||
When Slate throws an exception, it includes a stringified representation of the | ||
relevant data. For example, if your application makes an API call to access the | ||
child of a text Node (an impossible operation), Slate will throw an exception | ||
like this: | ||
|
||
``` | ||
Cannot get the child of a text node: {"text": "This is my text node."} | ||
``` | ||
|
||
If your rich text editor can include sensitive customer data, you may want to | ||
scrub or obfuscate that text. To help with that, you can use the Scrubber API. | ||
Here's an example of recursively scrubbing the `'text'` fields of any entity | ||
that gets logged. | ||
|
||
```typescript | ||
import { Scrubber } from 'slate' | ||
|
||
Scrubber.setScrubber((key, value) => { | ||
if (key === 'text') return '... scrubbed ...' | ||
return value | ||
}) | ||
``` | ||
|
||
By setting the scrubber in this way, the error example given above will be | ||
printed as | ||
|
||
``` | ||
Cannot get the child of a text node: {"text": "... scrubbed ..."} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export type Scrubber = (key: string, value: unknown) => unknown | ||
|
||
export interface ScrubberInterface { | ||
setScrubber(scrubber: Scrubber | undefined): void | ||
stringify(value: any): string | ||
} | ||
|
||
let _scrubber: Scrubber | undefined = undefined | ||
|
||
/** | ||
* This interface implements a stringify() function, which is used by Slate | ||
* internally when generating exceptions containing end user data. Developers | ||
* using Slate may call Scrubber.setScrubber() to alter the behavior of this | ||
* stringify() function. | ||
* | ||
* For example, to prevent the cleartext logging of 'text' fields within Nodes: | ||
* | ||
* import { Scrubber } from 'slate'; | ||
* Scrubber.setScrubber((key, val) => { | ||
* if (key === 'text') return '...scrubbed...' | ||
* return val | ||
* }); | ||
* | ||
*/ | ||
export const Scrubber: ScrubberInterface = { | ||
setScrubber(scrubber: Scrubber | undefined): void { | ||
_scrubber = scrubber | ||
}, | ||
|
||
stringify(value: any): string { | ||
return JSON.stringify(value, _scrubber) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Node, Scrubber } from 'slate' | ||
|
||
export const input = { | ||
customField: 'some very long custom field value that will get scrubbed', | ||
anotherField: 'this field should not get scrambled', | ||
} | ||
|
||
export const test = (value: Node) => { | ||
Scrubber.setScrubber((key, value) => key == 'customField' ? '... scrubbed ...' : value); | ||
const stringified = Scrubber.stringify(value) | ||
Scrubber.setScrubber(undefined) | ||
|
||
const unmarshaled = JSON.parse(stringified) | ||
return ( | ||
// ensure that first field has been scrubbed | ||
unmarshaled.customField === '... scrubbed ...' && | ||
// ensure that second field is unaltered | ||
unmarshaled.anotherField === input.anotherField | ||
) | ||
} | ||
|
||
export const output = true |