telemetry redact refresh token
This commit is contained in:
1 parent
bd92fed0d0
commit
7c6df7173c
3 files changed
+70
-13
No files matched your search
@@ -69,7 +69,6 @@ function ReportLastExitCrash() as void
|
|||||||
for each key in lastExitInfo
|
for each key in lastExitInfo
|
||||||
message += `\n${key}: ${ToString(lastExitInfo[key])}`
|
message += `\n${key}: ${ToString(lastExitInfo[key])}`
|
||||||
end for
|
end for
|
||||||
message = RedactAuthHeaders(message)
|
|
||||||
|
|
||||||
Sentry.SendEvents([{
|
Sentry.SendEvents([{
|
||||||
level: "fatal"
|
level: "fatal"
|
||||||
@@ -82,12 +81,3 @@ function ReportLastExitCrash() as void
|
|||||||
userId: m.userId
|
userId: m.userId
|
||||||
}])
|
}])
|
||||||
end function
|
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: <REDACTED>")
|
|
||||||
end for
|
|
||||||
return text
|
|
||||||
end function
|
|
||||||
@@ -45,7 +45,7 @@ namespace Sentry
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
message = event.message
|
message = Redact(event.message)
|
||||||
|
|
||||||
if event.fingerprint = invalid
|
if event.fingerprint = invalid
|
||||||
event.fingerprint = GetFingerprint(message)
|
event.fingerprint = GetFingerprint(message)
|
||||||
@@ -69,7 +69,7 @@ namespace Sentry
|
|||||||
if (not SUPPORTS_ATTACHMENTS) and hasAttachments
|
if (not SUPPORTS_ATTACHMENTS) and hasAttachments
|
||||||
extra = {}
|
extra = {}
|
||||||
for each attachment in event.attachments
|
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
|
end for
|
||||||
eventPayload.extra = extra
|
eventPayload.extra = extra
|
||||||
end if
|
end if
|
||||||
@@ -83,7 +83,7 @@ namespace Sentry
|
|||||||
if SUPPORTS_ATTACHMENTS and hasAttachments
|
if SUPPORTS_ATTACHMENTS and hasAttachments
|
||||||
tmpBuffer = CreateObject("roByteArray")
|
tmpBuffer = CreateObject("roByteArray")
|
||||||
for each attachment in event.attachments
|
for each attachment in event.attachments
|
||||||
data = attachment.data
|
data = Redact(attachment.data)
|
||||||
attachment.Delete("data")
|
attachment.Delete("data")
|
||||||
attachment["type"] = "attachment"
|
attachment["type"] = "attachment"
|
||||||
tmpBuffer.FromAsciiString(data)
|
tmpBuffer.FromAsciiString(data)
|
||||||
@@ -146,4 +146,22 @@ namespace Sentry
|
|||||||
|
|
||||||
return message
|
return message
|
||||||
end function
|
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: <REDACTED>")
|
||||||
|
end for
|
||||||
|
|
||||||
|
refreshToken = /("refresh_token"\s*:\s*)"[^"]*"/i
|
||||||
|
for each match in refreshToken.MatchAll(text)
|
||||||
|
text = text.Replace(match[0], `${match[1]}"<REDACTED>"`)
|
||||||
|
end for
|
||||||
|
|
||||||
|
return text
|
||||||
|
end function
|
||||||
end namespace
|
end namespace
|
||||||
@@ -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":"<REDACTED>","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":"<REDACTED>","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: <REDACTED>")
|
||||||
|
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
|
||||||
Reference in new issue
Block a user