fix(auth): [Security] Avoid writing session credentials to the platform log & mask sensitive values - #1391
Open
hieuwu wants to merge 2 commits into
Open
fix(auth): [Security] Avoid writing session credentials to the platform log & mask sensitive values#1391hieuwu wants to merge 2 commits into
hieuwu wants to merge 2 commits into
Conversation
…session credentials to the platform log
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.
What kind of change does this PR introduce?
Stop writing session credentials to the platform log
What is the current behavior?
UserSession is a data class holding four bearer credentials as plain Strings, so the compiler-synthesized toString() prints all four verbatim. Four call sites interpolated credential-bearing values straight into log lines:
logger.d { "Importing session $session from $source, ..." }accessToken,refreshToken,providerToken,providerRefreshTokenvia$session$source. AsSessionSource.Refresh/UserChanged/UserIdentitiesChangedwrap aUserSessionlogger.d { "Fragment parts: $sessionParts" }access_tokenandrefresh_tokenlogger.d { "Parsing fragment/hash $hash" }logger.e(e) { "Couldn't retrieve user using access token $accessToken..." }ERROR, which is enabled under the shipped default log levelPlease link any relevant issues here.
What is the new behavior?
Fixed at the type as root, not at the call sites. New data class that if wraps a
UserSessionwill not redintroduce the leak1. Mask at the type boundary.
UserSessionoverridestoString()to render every credential throughStringMasking.maskString, keeping a two-character prefix and the length so the value stays debuggable.SessionSource.Refresh/UserChanged/UserIdentitiesChangedare data classes wrapping aUserSession, theirtoString()delegates to the overridden one2. Mask the OAuth redirect fragment. Not related to
UserSession, two new helpers inStringMaskingto cover more sensitive parametersmaskParameters(Map<String, String>)for a parsed mapmaskParameterString(String)for a rawa=b&c=dfragment or query string.Both mask a denylist of credential parameter names (
access_token,refresh_token,provider_token,provider_refresh_token,id_token,code,code_verifier) and leave everything else readable, soexpires_in=3600&token_type=bearerstill shows.maskParameterStringpreserves ordering and passes malformed segments through unchanged, so the output stays useful for diagnosing a bad redirect.3. Header masking is now case-insensitive and wider.
SENSITIVE_HEADERSbecame a lowercaseSetmatched againstkey.lowercase(), extended withcookie,set-cookie,proxy-authorizationandx-api-key. Bearer-prefix handling is driven by the value (startsWith("Bearer ")) rather than an exact-case key comparison, and every value of a multi-valued header is masked rather than just the first.Additional context
References and practices.
Domain primitives: a value with a security invariant enforces that invariant itself rather than delegating it to every consumer, and
toString()is the exact seam where it belongs, because that is what string interpolation calls. Johnsson, Deogun & Sawano, Secure by Design (Manning, 2019), Ch. 5; Ch. 9 ("Handling failures securely") makes the narrower point that log and exception output must not carry the secret that caused the failure.RFC 6749 §10.3 — access token credentials must be kept confidential in transit and
storage, and the section explicitly notes that the implicit grant exposes the token in the
URI fragment. A platform log is storage; this is the control the four call sites broke.
https://www.rfc-editor.org/rfc/rfc6749#section-10.3
OWASP MASVS-STORAGE-2 "The app prevents leakage of sensitive data" (OWASP MASVS v2),
naming logs as an unintentional leakage sink, with MASWE-0005: Insertion of Sensitive Data
into Logs as the mapped weakness.
https://mas.owasp.org/MASVS/controls/MASVS-STORAGE-2/
OWASP ASVS 4.0.3 §7.1.1 the application must not log credentials; ASVS maps this
requirement to CWE-532. Masking here reduces exposure consistent with the intent of 7.1.1;
the control's own remedy for session tokens is an irreversible hashed form, which a
prefix-plus-length rendering is not.
CWE-532 Insertion of Sensitive Information into Log File.
https://cwe.mitre.org/data/definitions/532.html
RFC 6749 §4.2.2 defines the implicit-grant redirect fragment and its parameters
(
access_token,token_type,expires_in,refresh_token,state), delivered inapplication/x-www-form-urlencodedform. This is the parameter setSENSITIVE_PARAMETERSis drawn from. https://www.rfc-editor.org/rfc/rfc6749#section-4.2.2