Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ export const url = makeValidator<string>((x: string) => {
export const json = makeStructuredValidator((x: string) => {
try {
return JSON.parse(x)
} catch (e) {
throw new EnvError(`Invalid json: "${x}"`)
} catch {
// Not logging the json value as it could contain sensitive data
throw new EnvError(`Invalid json`)
}
})
10 changes: 10 additions & 0 deletions tests/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ test('json()', () => {

expect(() => cleanEnv({ FOO: 'abc' }, { FOO: json() }, makeSilent)).toThrow()

// The error message should not include the raw value, as it could contain sensitive data
let thrownError: unknown
try {
cleanEnv({ FOO: '{"password": "hunter2"' }, { FOO: json() }, makeSilent)
} catch (e) {
thrownError = e
}
expect((thrownError as Error).message).toBe('Invalid json')
expect((thrownError as Error).message).not.toContain('hunter2')

// default value should be passed through without running through JSON.parse()
expect(
cleanEnv(
Expand Down