diff --git a/playlet-lib/src/components/Telemetry/LastExitCrash.bs b/playlet-lib/src/components/Telemetry/LastExitCrash.bs index c21553d5..d9dab58d 100644 --- a/playlet-lib/src/components/Telemetry/LastExitCrash.bs +++ b/playlet-lib/src/components/Telemetry/LastExitCrash.bs @@ -69,7 +69,6 @@ function ReportLastExitCrash() as void for each key in lastExitInfo message += `\n${key}: ${ToString(lastExitInfo[key])}` end for - message = RedactAuthHeaders(message) Sentry.SendEvents([{ level: "fatal" @@ -82,12 +81,3 @@ function ReportLastExitCrash() as void userId: m.userId }]) end function - -function RedactAuthHeaders(text as string) as string - authRegex = /Authorization: [^\r\n]*/i - matches = authRegex.MatchAll(text) - for each match in matches - text = text.Replace(match[0], "Authorization: ") - end for - return text -end function diff --git a/playlet-lib/src/components/Telemetry/Sentry.bs b/playlet-lib/src/components/Telemetry/Sentry.bs index e1bf2aa9..810f966e 100644 --- a/playlet-lib/src/components/Telemetry/Sentry.bs +++ b/playlet-lib/src/components/Telemetry/Sentry.bs @@ -45,7 +45,7 @@ namespace Sentry } } - message = event.message + message = Redact(event.message) if event.fingerprint = invalid event.fingerprint = GetFingerprint(message) @@ -69,7 +69,7 @@ namespace Sentry if (not SUPPORTS_ATTACHMENTS) and hasAttachments extra = {} for each attachment in event.attachments - extra[attachment.filename] = `content_type:${attachment.content_type}\n${attachment.data}` + extra[attachment.filename] = `content_type:${attachment.content_type}\n${Redact(attachment.data)}` end for eventPayload.extra = extra end if @@ -83,7 +83,7 @@ namespace Sentry if SUPPORTS_ATTACHMENTS and hasAttachments tmpBuffer = CreateObject("roByteArray") for each attachment in event.attachments - data = attachment.data + data = Redact(attachment.data) attachment.Delete("data") attachment["type"] = "attachment" tmpBuffer.FromAsciiString(data) @@ -146,4 +146,22 @@ namespace Sentry return message end function + + function Redact(text as dynamic) as dynamic + if StringUtils.IsNullOrEmpty(text) + return text + end if + + authHeader = /Authorization: [^\r\n]*/i + for each match in authHeader.MatchAll(text) + text = text.Replace(match[0], "Authorization: ") + end for + + refreshToken = /("refresh_token"\s*:\s*)"[^"]*"/i + for each match in refreshToken.MatchAll(text) + text = text.Replace(match[0], `${match[1]}""`) + end for + + return text + end function end namespace diff --git a/playlet-lib/src/source/tests/RedactTelemetrySecrets.spec.bs b/playlet-lib/src/source/tests/RedactTelemetrySecrets.spec.bs new file mode 100644 index 00000000..425501bf --- /dev/null +++ b/playlet-lib/src/source/tests/RedactTelemetrySecrets.spec.bs @@ -0,0 +1,49 @@ +import "pkg:/components/Telemetry/Sentry.bs" + +namespace Tests + + @suite + class SentryRedactTests extends Rooibos.BaseTestSuite + + @describe("Sentry.Redact") + + @it("masks refresh_token in a JSON body, keeping the other fields") + function jsonRefreshToken() + body = `{"client_id":"pub-id","client_secret":"pub-secret","refresh_token":"1//0secret-Ab_c.d","grant_type":"refresh_token"}` + expected = `{"client_id":"pub-id","client_secret":"pub-secret","refresh_token":"","grant_type":"refresh_token"}` + m.assertEqual(Sentry.Redact(body), expected) + end function + + @it("masks refresh_token inside a longer telemetered message, keeping the error and URL") + function embeddedInMessage() + message = "POST https://www.youtube.com/o/oauth2/token failed: HTTP 400" + Chr(10) + ` -d '{"refresh_token":"1//0secret","grant_type":"refresh_token"}'` + expected = "POST https://www.youtube.com/o/oauth2/token failed: HTTP 400" + Chr(10) + ` -d '{"refresh_token":"","grant_type":"refresh_token"}'` + m.assertEqual(Sentry.Redact(message), expected) + end function + + @it("does not treat refresh_token used as a value as a key") + function secretNameAsValue() + body = `{"grant_type":"refresh_token"}` + m.assertEqual(Sentry.Redact(body), body) + end function + + @it("masks an Authorization header") + function authHeader() + m.assertEqual(Sentry.Redact("visitorId: abc" + Chr(10) + "Authorization: Bearer ya29.secret"), "visitorId: abc" + Chr(10) + "Authorization: ") + end function + + @it("leaves a body without secrets untouched") + function noSecrets() + body = `{"screen_ids":"abc","count":0}` + m.assertEqual(Sentry.Redact(body), body) + end function + + @it("passes non-string input through unchanged") + function nonString() + m.assertInvalid(Sentry.Redact(invalid)) + m.assertEqual(Sentry.Redact(42), 42) + end function + + end class + +end namespace