Skip to content
Merged
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
3 changes: 2 additions & 1 deletion packages/runtime-core/src/redaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export function redactString(value: string, options: RedactStringOptions = {}):
.replace(/https?:\/\/[^\s"'<>]+/gi, (match) => redactUrl(match, options))
.replace(SECRET_LIKE_VALUE_GLOBAL_PATTERN, REDACTED_VALUE)
.replace(/([?&][^=&#\s"'<>]+)=([^&#\s"'<>]+)/g, options.redactQueryAssignments ? `$1=${REDACTED_VALUE}` : "$&")
.replace(/((?:[A-Za-z0-9_-]*)(?:access[_-]?token|auth|bearer|code|cookie|credential|key|login|nonce|pass|password|secret|session|state|token)(?:[A-Za-z0-9_-]*)(?:["'\s:=]+))[^&#\s"'<>]+/gi, `$1${REDACTED_VALUE}`)
.replace(/\b(Bearer[ \t]+)[^&#\s"'<>]+/gi, `$1${REDACTED_VALUE}`)
.replace(/((?:[A-Za-z0-9_-]*)(?:access[_-]?token|auth|bearer|code|cookie|credential|key|login|nonce|pass|password|secret|session|state|token)(?:[A-Za-z0-9_-]*)(?:["'\s]*[:=]["'\s]*))[^&#\s"'<>]+/gi, `$1${REDACTED_VALUE}`)
}

export function redactError(error: unknown, options: RedactStringOptions = {}): Error {
Expand Down
24 changes: 24 additions & 0 deletions tests/redaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ assert.equal(containsSecretLikeValue("token sk-abcdefghijklmnopqrstuvwxyz"), tru
assert.equal(containsSecretLikeValue("token [redacted]"), false)
assert.equal(redactString("token sk-abcdefghijklmnopqrstuvwxyz"), "token [redacted]")

const proseCases: Array<[string, string]> = [
["Local wp-admin auth fixture user could not be loaded.", "Local wp-admin auth fixture user could not be loaded."],
["The token validation fixture passed.", "The token validation fixture passed."],
["Apply the cookie policy to this session state.", "Apply the cookie policy to this session state."],
[" at <anonymous> (/workspace/auth-fixture.ts:7:21)", " at <anonymous> (/workspace/auth-fixture.ts:7:21)"],
]

for (const [input, expected] of proseCases) {
assert.equal(redactString(input), expected, `ordinary prose should survive: ${input}`)
}

const secretContextCases: Array<[string, string]> = [
["Local wp-admin auth: fixture-password user could not be loaded.", "Local wp-admin auth: [redacted] user could not be loaded."],
["The token=fixture-token validation failed.", "The token=[redacted] validation failed."],
["Authorization: Bearer fixture-auth-token", "Authorization: [redacted]"],
["Bearer fixture-auth-token", "Bearer [redacted]"],
["Cookie: wordpress_logged_in=fixture-cookie", "Cookie: [redacted]"],
['{"username":"fixture","password":"fixture-password"}', '{"username":"fixture","password":"[redacted]"}'],
]

for (const [input, expected] of secretContextCases) {
assert.equal(redactString(input), expected, `secret context should redact: ${input}`)
}

assert.deepEqual(
redactJsonValue({ token: "abc", nested: { api_key: "def", visible: "ok" }, list: [{ password: "secret" }] }, { redactStrings: false }),
{ token: "[redacted]", nested: { api_key: "[redacted]", visible: "ok" }, list: [{ password: "[redacted]" }] },
Expand Down
Loading