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

Consider JSON.stringify's whitelisting capability #4

Open
rsc-rfrench opened this issue Feb 19, 2019 · 1 comment
Open

Consider JSON.stringify's whitelisting capability #4

rsc-rfrench opened this issue Feb 19, 2019 · 1 comment
Labels
question Further information is requested

Comments

@rsc-rfrench
Copy link
Contributor

JSON.stringify allows a 2nd argument called replacer:

A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.

This might allow clean log to provide a facility like log.info(msg, secrets) that ensures the values in secrets are scrubbed from log output. Warrants further research.

@rsc-rfrench rsc-rfrench added the question Further information is requested label Feb 19, 2019
@jveldboom-rsc
Copy link
Contributor

I'd like to explore this as well. But I do want to keep the focus on keeping the library relatively simple.

What about something like using environment variables for the replacer? This would prevent the library from keeping a whitelist. (not saying we use this but just wanting to continue the discussion)

process.env.LOG_SUPPRESS = 'email,password'

const replacer = (key, value) => {
  if (process.env.LOG_SUPPRESS) {
    let suppress = process.env.LOG_SUPPRESS.split(',')
    if (suppress.includes(key)) return undefined
  }
  
  return value
}

let data = {
  name: 'John',
  password: '1234567',
  age: 55,
  email: '[email protected]'
}

console.log(JSON.stringify(data, replacer, 2))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants