Repository navigation
Support obfuscated feature flag assignment keys #3941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leoromanovsky
wants to merge
24
commits into
develop
Choose a base branch
from
flag-key-obfuscation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d600473
Support obfuscated feature flag assignment keys
leoromanovsky 185a1fa
fix(flags): keep capability negotiation independent of versions
leoromanovsky b3779f8
test(flags): keep obfuscation coverage in JVM suite
leoromanovsky 39ae47b
Merge origin/develop into flag-key-obfuscation
leoromanovsky c63e2d6
fix(flags): gate encoded cache reads until source is known
leoromanovsky 16292d0
Use the feature flags capabilities header
leoromanovsky 751cb76
fix(flags): cache lookup hashes and strengthen obfuscation regressions
leoromanovsky 1c1f39f
Merge develop and preserve obfuscation with first-flags callbacks
leoromanovsky d8a2840
FFLSDK-254: Merge develop into flag-key-obfuscation
leoromanovsky 65fc35c
FFLSDK-254: Mark obfuscation test setup, actions, and assertions
leoromanovsky 3c503fc
FFLSDK-254: Use Mockito annotations in obfuscation tests
leoromanovsky 26d87d6
FFLSDK-254: Generate incidental obfuscation test fixtures
leoromanovsky ad7aa8d
FFLSDK-254: Limit obfuscation suppressions to specific calls
leoromanovsky 1d236bc
FFLSDK-254: Declare obfuscation parser exceptions
leoromanovsky 71cc4df
FFLSDK-254: Generate assignment and cache JSON models
leoromanovsky 935ada0
FFLSDK-261: Add context to generated assignments test
leoromanovsky 586f86a
FFLSDK-261: Explain obfuscation copy visibility
leoromanovsky 36c7d5a
FFLSDK-261: Merge latest develop
leoromanovsky 723409b
FFLSDK-261: Apply documented Kotlin conventions
leoromanovsky f4a6d1f
FFLSDK-261: Tighten obfuscation test assertions
leoromanovsky 27ba428
FFLSDK-261: Keep evaluation tests independent of HTTP
leoromanovsky 413d7bb
Merge remote-tracking branch 'origin/develop' into flag-key-obfuscation
leoromanovsky 586c287
FFLSDK-261: Add obfuscation coverage to Flags FIT tests
leoromanovsky 952b148
FFLSDK-261: Apply JSON parsing and unit test review feedback
leoromanovsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
17 changes: 17 additions & 0 deletions
17
features/dd-sdk-android-flags/src/main/json/flags/assignment.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "title": "Assignment", | ||
| "description": "An assignment after conversion of legacy JSON values to the SDK string representation.", | ||
| "type": "object", | ||
| "properties": { | ||
| "variationType": { "type": "string" }, | ||
| "variationValue": { "type": "string" }, | ||
| "doLog": { "type": "boolean" }, | ||
| "allocationKey": { "type": "string" }, | ||
| "variationKey": { "type": "string" }, | ||
| "extraLogging": { "type": "object", "additionalProperties": true }, | ||
| "reason": { "type": "string" }, | ||
| "serialId": { "type": "integer" } | ||
| }, | ||
| "required": ["variationType", "variationValue", "doLog", "allocationKey", "variationKey", "extraLogging", "reason"] | ||
| } |
22 changes: 22 additions & 0 deletions
22
features/dd-sdk-android-flags/src/main/json/flags/assignments-response.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "title": "AssignmentsResponse", | ||
| "type": "object", | ||
| "properties": { | ||
| "data": { | ||
| "type": "object", | ||
| "properties": { | ||
| "attributes": { | ||
| "type": "object", | ||
| "properties": { | ||
| "flags": { "type": "object", "additionalProperties": true } | ||
| }, | ||
| "required": ["flags"], | ||
| "additionalProperties": true | ||
| } | ||
| }, | ||
| "required": ["attributes"] | ||
| } | ||
| }, | ||
| "required": ["data"] | ||
| } |
28 changes: 28 additions & 0 deletions
28
features/dd-sdk-android-flags/src/main/json/flags/flags-state.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "title": "FlagsState", | ||
| "type": "object", | ||
| "properties": { | ||
| "evaluationContext": { | ||
| "type": "object", | ||
| "properties": { | ||
| "targetingKey": { "type": "string" }, | ||
| "attributes": { "type": "object", "additionalProperties": true } | ||
| }, | ||
| "required": ["targetingKey"] | ||
| }, | ||
| "flags": { "type": "object", "additionalProperties": true }, | ||
| "encodedFlags": { "type": "object", "additionalProperties": true }, | ||
| "lastUpdateTimestamp": { "type": "integer" }, | ||
| "obfuscated": { "type": "boolean" }, | ||
| "obfuscation": { | ||
| "type": "object", | ||
| "properties": { | ||
| "scheme": { "type": "string" }, | ||
| "salt": { "type": "string" } | ||
| }, | ||
| "required": ["scheme", "salt"] | ||
| } | ||
| }, | ||
| "required": ["evaluationContext", "lastUpdateTimestamp"] | ||
| } |
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
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
81 changes: 81 additions & 0 deletions
81
...roid-flags/src/main/kotlin/com/datadog/android/flags/internal/model/FlagKeyObfuscation.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| package com.datadog.android.flags.internal.model | ||
|
|
||
| import androidx.collection.LruCache | ||
| import com.google.gson.JsonParseException | ||
| import java.security.MessageDigest | ||
| import java.util.Locale | ||
|
|
||
| /** Salt validated before lookup hashes are computed. */ | ||
| // Keep copy() private so callers cannot bypass salt validation. | ||
| @ConsistentCopyVisibility | ||
| internal data class FlagKeyObfuscation private constructor(val salt: String) { | ||
| @Suppress("UnsafeThirdPartyFunctionCall") // The cache limit is a positive constant. | ||
| private val lookupKeys = LruCache<String, String>(LOOKUP_CACHE_LIMIT) | ||
|
leoromanovsky marked this conversation as resolved.
|
||
|
|
||
| fun encode(key: String): String? { | ||
| // Reject unpaired surrogates instead of silently replacing their UTF-8 encoding. | ||
| if (!isWellFormedUnicode(key)) return null | ||
|
leoromanovsky marked this conversation as resolved.
|
||
| @Suppress("UnsafeThirdPartyFunctionCall") // The key is non-null. | ||
| val cachedKey = lookupKeys.get(key) | ||
| return cachedKey ?: run { | ||
| @Suppress("UnsafeThirdPartyFunctionCall") // SHA-256 is mandatory on Android. | ||
| val digest = MessageDigest.getInstance("SHA-256") | ||
| digest.update("datadog.feature-flags.flag-key.v1\u0000".toByteArray(Charsets.UTF_8)) | ||
|
leoromanovsky marked this conversation as resolved.
|
||
| @Suppress("UnsafeThirdPartyFunctionCall") // The size is positive; the salt is validated hexadecimal. | ||
| val saltBytes = ByteArray(SALT_BYTES) { salt.substring(it * 2, it * 2 + 2).toInt(HEX_RADIX).toByte() } | ||
| digest.update(saltBytes) | ||
| digest.update(key.toByteArray(Charsets.UTF_8)) | ||
| digest.digest().joinToString("") { | ||
| "%02x".format(Locale.US, it) | ||
| }.also { | ||
| @Suppress("UnsafeThirdPartyFunctionCall") // The key and encoded value are non-null. | ||
| lookupKeys.put(key, it) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal companion object { | ||
| const val SCHEME = "flag-key-sha256-v1" | ||
| const val CAPABILITY = "assignment-encoding-flag-key-256-v1" | ||
|
typotter marked this conversation as resolved.
|
||
| private const val SALT_BYTES = 16 | ||
| private const val HEX_RADIX = 16 | ||
| private const val LOOKUP_CACHE_LIMIT = 1024 | ||
|
|
||
| @Throws(JsonParseException::class) | ||
| fun fromSalt(salt: String): FlagKeyObfuscation { | ||
| if (!isLowercaseHex(salt, SALT_BYTES)) { | ||
| @Suppress("ThrowingInternalException") // Caught by the response or cache parser. | ||
| throw JsonParseException("Flag-key salt must contain 32 lowercase hexadecimal characters") | ||
| } | ||
| return FlagKeyObfuscation(salt) | ||
| } | ||
|
|
||
| private fun isLowercaseHex(value: String, bytes: Int): Boolean = | ||
| value.length == bytes * 2 && value.all { it in '0'..'9' || it in 'a'..'f' } | ||
|
|
||
| @Suppress("ReturnCount") | ||
| private fun isWellFormedUnicode(value: String): Boolean { | ||
| var index = 0 | ||
| while (index < value.length) { | ||
| val char = value[index++] | ||
| if (char.isHighSurrogate()) { | ||
| if (index >= value.length || !value[index++].isLowSurrogate()) return false | ||
| } else if (char.isLowSurrogate()) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal data class PrecomputedAssignments( | ||
| val flags: Map<String, PrecomputedFlag>, | ||
| val obfuscation: FlagKeyObfuscation? = null | ||
| ) | ||
|
leoromanovsky marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.