forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (ianstormtaylor#4999)
- Loading branch information
1 parent
f3c69cc
commit 2431957
Showing
7 changed files
with
93 additions
and
25 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
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,24 @@ | ||
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 |