Repository navigation
Add client-side opaque-token log scrubber - #6119
Merged
Merged
Conversation
Redact HIT-tagged opaque tokens from MSAL log output at the FormatLogMessage choke point. Adds TokenScrubber with an AppContext kill switch and unit tests. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a client-side log scrubber that redacts HIT-tagged opaque tokens (eSTS/CCS marker encodings + MSA literal) from MSALβs formatted log output before it reaches any logging sink, with a cached AppContext kill switch.
Changes:
- Introduces
TokenScrubberthat detects HIT markers, expands to the full base64/base64url token run, and replaces with[Redacted opaque token](fast-path returns same string reference when no marker is present). - Hooks scrubbing into the centralized
LoggerHelper.FormatLogMessagechoke point to cover all logger implementations. - Adds unit tests covering multiple marker alignments, MSA literal, JSON embedding,
Set-Cookieexamples, negative cases, and multiple tokens.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/client/Microsoft.Identity.Client/Internal/Logger/TokenScrubber.cs |
Implements HIT-marker detection and whole-token redaction with a cached AppContext disable switch and no-allocation fast path. |
src/client/Microsoft.Identity.Client/Internal/Logger/LoggerHelper.cs |
Routes formatted log messages through TokenScrubber.Scrub(...) so redaction occurs at a single central log-formatting point. |
tests/Microsoft.Identity.Test.Unit/PublicApiTests/TokenScrubberTests.cs |
Adds coverage for redaction behavior across multiple marker variants and representative log-text embeddings. |
Covers token at start/end, whole-string token, base64url expansion across '=', Bearer header, multiple Set-Cookie tokens on one line, adjacent tokens, mixed eSTS+MSA, HTTP response error blob, null/empty inputs. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Addresses PR review: Scrub() previously scanned for patterns twice on the redaction path (ContainsAnyPattern then FindEarliestMatch from index 0). Now performs a single FindEarliestMatch; the no-match fast path still returns the same reference with no allocation. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Nilesh Choudhary (4gust)
marked this pull request as ready for review
July 15, 2026 09:09
Bogdan Gavril (bgavrilMS)
requested changes
Jul 15, 2026
Per review feedback, replace the global FormatLogMessage scrub with a tactical fix: scrub only exception log messages (which carry ESTS request/response/error bodies via MsalServiceException) in the MsalLoggerExtensions exception overloads. Ordinary log lines are no longer scrubbed. TokenScrubber internals are unchanged. Adds LoggerTests integration tests proving exception messages are scrubbed and plain messages are not. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f5fe65a9-3405-47c0-ab3f-8a9a3233af3b
Address review feedback to run the scrubber on just the message content rather than the whole formatted line. ErrorPiiWithPrefix/WarningPiiWithPrefix now scrub exWithPii.ToString() and append the prefix afterward, matching the other exception overloads. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f5fe65a9-3405-47c0-ab3f-8a9a3233af3b
Use exWithPii?.ToString() so ErrorPiiWithPrefix/WarningPiiWithPrefix do not throw when the exception is null, matching the pre-scrubber behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f5fe65a9-3405-47c0-ab3f-8a9a3233af3b
Add an early IsLoggingEnabled check to the exception-logging overloads so exWithPii?.ToString() and TokenScrubber.Scrub(...) only run when the target log level is enabled, avoiding O(n) scans of large exception text on disabled paths. Also normalize the remaining .ToString() calls to ?.ToString() for uniform null tolerance. Behavior-preserving: both logger adapters already gate their Log body on IsLoggingEnabled. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f5fe65a9-3405-47c0-ab3f-8a9a3233af3b
Gladwin Johnson VR (gladjohn)
approved these changes
Jul 15, 2026
Bogdan Gavril (bgavrilMS)
approved these changes
Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Redacts HIT-tagged (Highly Identifiable Token) opaque tokens from MSAL''s own log output before they reach any logging sink. eSTS/CCS now tag outgoing opaque tokens with a marker inside the base64/base64url-encoded value; this change detects those markers and replaces the whole token run with
[Redacted opaque token].Per maintainer feedback, scrubbing is applied as a tactical fix scoped to exception log messages only (which carry ESTS token-call request/response/error bodies via
MsalServiceException), rather than globally at every log line.Changes
src/client/Microsoft.Identity.Client/Internal/Logger/TokenScrubber.csβinternal static class TokenScrubberwithpublic static string Scrub(string).EvoStsArtifactsat each byte-offset alignment (RXZvU3RzQXJ0aWZhY3Rz,V2b1N0c0FydGlmYWN0c,dm9TdHNBcnRpZmFjdH) plus the MSA literalMsaArtifacts.[A-Za-z0-9+/_-=](base64 + base64url +=padding) to cover the whole token run, then replaces it viaStringBuilder.AppContextswitchMicrosoft.Identity.Client.DisableOpaqueTokenScrubbing, read once into a cached static bool. Independent of PII logging.src/client/Microsoft.Identity.Client/Internal/Logger/MsalLoggerExtensions.csβ the exception-logging overloads (ErrorPii(Exception),ErrorPiiWithPrefix,WarningPii(Exception),WarningPiiWithPrefix,InfoPii(Exception)) now scrub the exception text viaTokenScrubber.Scrub(...). The*WithPrefixoverloads scrub only the exception message (prefix appended afterward) and tolerate a null exception. This is the single tactical choke point for ESTS token-call error bodies, which surface asMsalServiceExceptiontext.LoggerHelper.FormatLogMessageβ no global scrubbing; only exception paths are scrubbed.tests/Microsoft.Identity.Test.Unit/PublicApiTests/TokenScrubberTests.csβ unit tests covering the three eSTS offsets, the MSA literal, a token inside JSON, a realSet-Cookie: esctx-...line, a negative same-reference case, and a look-alike that must not be redacted.tests/Microsoft.Identity.Test.Unit/PublicApiTests/LoggerTests.csβ integration tests confirming tagged tokens in logged exceptions are scrubbed and that plain (non-exception) messages are left untouched.Design note: scope of scrubbing
Two gates keep this tight:
Scrub. eSTS token-call errors surface asMsalServiceException, whose text (includingResponseBody) is logged through these overloads. eSTS success bodies (real tokens) are only deserialized, never logged.Scrubonly rewrites a string that contains a HIT-tag pattern; any other content is returned as the same reference (no-op).Testing
Microsoft.Identity.Client(net8.0): succeeded, 0 errors.TokenScrubberTests+LoggerTests(net48): 48 passed, 0 failed, 0 skipped.