Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/in-app-rules-for-deobfuscation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"posthog": minor
"posthog-android": minor
---

Send `inAppIncludes` and `inAppExcludes` with captured exceptions as `$exception_in_app_includes` and `$exception_in_app_excludes` when frames carry a mapping id, so PostHog can apply them to deobfuscated class names on minified builds.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public class PostHogErrorTrackingConfig
*
* Matched against the RUNTIME class name, before any symbolication. On a minified build
* (ProGuard/R8) the runtime names are the obfuscated ones, so prefixes written against your
* source packages generally will not match; PostHog also re-derives `in_app` server-side
* after deobfuscation, which can override what the SDK sent.
* source packages generally will not match. When [com.posthog.PostHogConfig.releaseIdentifier]
* is set, the SDK also sends this list with the exception so PostHog can apply it to the
* deobfuscated class names.
*/
public val inAppIncludes: MutableList<String> = mutableListOf(),
/**
Expand Down Expand Up @@ -70,10 +71,9 @@ public class PostHogErrorTrackingConfig
*
* Same matching caveats as [inAppIncludes]: prefixes are compared against the RUNTIME class
* name before any symbolication, so on minified (ProGuard/R8) builds an exclude written
* against your source packages generally will not match, and PostHog's server-side
* deobfuscation may reclassify frames as `in_app` afterwards regardless of what the SDK
* sent. Making excludes survive deobfuscation needs a server-side in-app contract
* (follow-up); until then treat this as best-effort on non-minified builds.
* against your source packages generally will not match. When
* [com.posthog.PostHogConfig.releaseIdentifier] is set, the SDK also sends this list with the
* exception so PostHog can apply it to the deobfuscated class names.
*
* Defaults to empty.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,18 @@ public class ThrowableCoercer {
exceptionProperties["\$exception_list"] = cappedExceptions
}

// Frames carrying a map_id are deobfuscated server-side, where the runtime class names the
// SDK matched against are replaced. Sending the rules lets the server classify the real
// class names the same way instead of overriding the SDK's in_app flags.
if (!releaseIdentifier.isNullOrEmpty()) {
if (inAppIncludes.isNotEmpty()) {
exceptionProperties[EXCEPTION_IN_APP_INCLUDES_ATTRIBUTE] = inAppIncludes.toList()
}
if (inAppExcludes.isNotEmpty()) {
exceptionProperties[EXCEPTION_IN_APP_EXCLUDES_ATTRIBUTE] = inAppExcludes.toList()
}
}

return exceptionProperties
}

Expand All @@ -356,6 +368,8 @@ public class ThrowableCoercer {
internal companion object {
const val EXCEPTION_LEVEL_FATAL = "fatal"
const val EXCEPTION_LEVEL_ATTRIBUTE = "\$exception_level"
private const val EXCEPTION_IN_APP_INCLUDES_ATTRIBUTE = "\$exception_in_app_includes"
private const val EXCEPTION_IN_APP_EXCLUDES_ATTRIBUTE = "\$exception_in_app_excludes"

/**
* The cause chain starting at [root], identity-deduplicated and capped at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,41 @@ internal class ThrowableCoercerTest {
override fun hashCode(): Int = token.hashCode()
}

@Test
fun `sends in_app rules for server-side reclassification when frames carry a map_id`() {
val t = throwableWith("boom", arrayOf(frame("a1.d", "a")))

val props =
coercer.fromThrowableToPostHogProperties(
t,
inAppIncludes = listOf("com.app"),
releaseIdentifier = "com.app@1.0+1",
inAppExcludes = listOf("com.app.vendored"),
)

assertEquals(listOf("com.app"), props["\$exception_in_app_includes"])
assertEquals(listOf("com.app.vendored"), props["\$exception_in_app_excludes"])
}

@Test
fun `omits in_app rules when there is nothing for the server to reclassify`() {
val t = throwableWith("boom", arrayOf(frame("com.app.A", "a")))

// without a map_id the server never deobfuscates, so the SDK's classification is final
val noMapping =
coercer.fromThrowableToPostHogProperties(
t,
inAppIncludes = listOf("com.app"),
inAppExcludes = listOf("com.app.vendored"),
)
assertFalse(noMapping.containsKey("\$exception_in_app_includes"))
assertFalse(noMapping.containsKey("\$exception_in_app_excludes"))

val noRules = coercer.fromThrowableToPostHogProperties(t, releaseIdentifier = "com.app@1.0+1")
assertFalse(noRules.containsKey("\$exception_in_app_includes"))
assertFalse(noRules.containsKey("\$exception_in_app_excludes"))
}

@Test
fun `empty includes marks everything in_app by default`() {
val t =
Expand Down
Loading