Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/compose-multiplatform-mask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-ios": patch
---

Mask Compose Multiplatform views whole in session replay when `maskAllTextInputs` or `maskAllImages` is on, as for web views, since the SDK can't find the text fields, text and images Compose draws inside them
4 changes: 4 additions & 0 deletions PostHog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
E4E4E4E4E4E4E4E4E4E40002 /* RRStyleTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E4E4E4E4E4E4E4E4E40001 /* RRStyleTest.swift */; };
F5F5F5F5F5F5F5F5F5F50002 /* PostHogBinaryImageInfoTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5F5F5F5F5F5F5F5F5F50001 /* PostHogBinaryImageInfoTest.swift */; };
B2B2B2B2B2B2B2B2B2B20002 /* PostHogReactNativeMaskingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B2B2B2B2B2B2B2B2B20001 /* PostHogReactNativeMaskingTest.swift */; };
C3C3C3C3C3C3C3C3C3C30002 /* PostHogComposeMultiplatformMaskingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C3C3C3C3C3C3C3C3C30001 /* PostHogComposeMultiplatformMaskingTest.swift */; };
B2B2B2B2B2B2B2B2B2B20004 /* PostHogMaskFailClosedTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B2B2B2B2B2B2B2B2B20003 /* PostHogMaskFailClosedTest.swift */; };
C1C1C1C1C1C1C1C1C1C10002 /* PostHogGraphicsImageRendererTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1C1C1C1C1C1C1C1C1C10001 /* PostHogGraphicsImageRendererTest.swift */; };
C1C1C1C1C1C1C1C1C1C10004 /* PostHogScreenshotFailedDrawTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1C1C1C1C1C1C1C1C1C10003 /* PostHogScreenshotFailedDrawTest.swift */; };
Expand Down Expand Up @@ -916,6 +917,7 @@
E4E4E4E4E4E4E4E4E4E40001 /* RRStyleTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RRStyleTest.swift; sourceTree = "<group>"; };
F5F5F5F5F5F5F5F5F5F50001 /* PostHogBinaryImageInfoTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogBinaryImageInfoTest.swift; sourceTree = "<group>"; };
B2B2B2B2B2B2B2B2B2B20001 /* PostHogReactNativeMaskingTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogReactNativeMaskingTest.swift; sourceTree = "<group>"; };
C3C3C3C3C3C3C3C3C3C30001 /* PostHogComposeMultiplatformMaskingTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogComposeMultiplatformMaskingTest.swift; sourceTree = "<group>"; };
B2B2B2B2B2B2B2B2B2B20003 /* PostHogMaskFailClosedTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogMaskFailClosedTest.swift; sourceTree = "<group>"; };
C1C1C1C1C1C1C1C1C1C10001 /* PostHogGraphicsImageRendererTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogGraphicsImageRendererTest.swift; sourceTree = "<group>"; };
C1C1C1C1C1C1C1C1C1C10003 /* PostHogScreenshotFailedDrawTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogScreenshotFailedDrawTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1920,6 +1922,7 @@
E4E4E4E4E4E4E4E4E4E40001 /* RRStyleTest.swift */,
F5F5F5F5F5F5F5F5F5F50001 /* PostHogBinaryImageInfoTest.swift */,
B2B2B2B2B2B2B2B2B2B20001 /* PostHogReactNativeMaskingTest.swift */,
C3C3C3C3C3C3C3C3C3C30001 /* PostHogComposeMultiplatformMaskingTest.swift */,
B2B2B2B2B2B2B2B2B2B20003 /* PostHogMaskFailClosedTest.swift */,
C1C1C1C1C1C1C1C1C1C10001 /* PostHogGraphicsImageRendererTest.swift */,
C1C1C1C1C1C1C1C1C1C10003 /* PostHogScreenshotFailedDrawTest.swift */,
Expand Down Expand Up @@ -3900,6 +3903,7 @@
E4E4E4E4E4E4E4E4E4E40002 /* RRStyleTest.swift in Sources */,
F5F5F5F5F5F5F5F5F5F50002 /* PostHogBinaryImageInfoTest.swift in Sources */,
B2B2B2B2B2B2B2B2B2B20002 /* PostHogReactNativeMaskingTest.swift in Sources */,
C3C3C3C3C3C3C3C3C3C30002 /* PostHogComposeMultiplatformMaskingTest.swift in Sources */,
B2B2B2B2B2B2B2B2B2B20004 /* PostHogMaskFailClosedTest.swift in Sources */,
C1C1C1C1C1C1C1C1C1C10002 /* PostHogGraphicsImageRendererTest.swift in Sources */,
C1C1C1C1C1C1C1C1C1C10004 /* PostHogScreenshotFailedDrawTest.swift in Sources */,
Expand Down
19 changes: 18 additions & 1 deletion PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,15 @@
}
}

// Compose Multiplatform draws text fields, text and images into one surface with no UIKit
// view per element, so like a web view, mask the whole of it
if Self.isComposeMultiplatformView(type(of: view)), isAnyInputSensitive(view) {
maskableWidgets.append(.init(view, in: window))
// an unclipped subview, such as a UIKit interop view, can draw outside the view's mask
findMaskableWidgets(inSubviewsOf: view, window, &maskableWidgets, maskChildren || view.isNoCapture(), culled)
return
Comment thread
veria-ai[bot] marked this conversation as resolved.
Resolved
}

/// SwiftUI: `SwiftUI.UIKitIconPreferringButton` and other subclasses will land here
if let button = view as? UIButton {
if isButtonSensitive(button) {
Expand Down Expand Up @@ -2352,6 +2361,14 @@
"SwiftUI.RBDrawingView", // up to iOS 18
].compactMap(NSClassFromString)

/// Whether `viewClass` is one of the views Compose Multiplatform draws its UI into: a container, a Metal
/// surface and input views, all in the `androidx.compose.ui.window` Kotlin package. Kotlin/Native registers
/// them under names such as `Sharedandroidx.compose.ui.window.ComposeContainerView3` (module name, Kotlin
/// class name, counter), so they're matched by name.
static func isComposeMultiplatformView(_ viewClass: AnyClass) -> Bool {
NSStringFromClass(viewClass).contains("androidx.compose.ui.window.")
}

/// Masks `content`, a view or a layer drawn by `view`, when it is a `.drawingGroup()` or `Canvas`
/// surface. Returns whether a mask was added.
@discardableResult
Expand Down Expand Up @@ -2394,7 +2411,7 @@
if isA(viewClass, [reactNativeTextView, reactNativeParagraphView]) { return .unreadableText }
if isA(viewClass, [UILabel.self]) { return .label }
if isA(viewClass, [WKWebView.self, UISwitch.self, UIPickerView.self, reactNativeSvgView, systemSandboxedView] + swiftUIGenericTypes
+ Self.swiftUIFlattenedContentTypes)
+ Self.swiftUIFlattenedContentTypes) || Self.isComposeMultiplatformView(viewClass)
{
return .opaque
}
Expand Down
167 changes: 167 additions & 0 deletions PostHogTests/PostHogComposeMultiplatformMaskingTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//
// Masking coverage for Compose Multiplatform screens. Compose Multiplatform draws its whole UI
// into a few UIKit views (a container, a Metal surface and input views) with no UIKit text field,
// label or image view per element, so the heuristic walk can't find what to mask inside them.
// Kotlin/Native registers those Kotlin classes with the Objective-C runtime under names such as
// `SampleAppandroidx.compose.ui.window.ComposeContainerView3`: the app module's name, the Kotlin
// class's fully qualified name and a counter. This suite registers UIView subclasses under names
// of that shape, so it needs no Compose dependency.
//

#if os(iOS)
import Foundation
@testable import PostHog
import Testing
import UIKit

/// A UIView subclass registered under `name`, as Kotlin/Native registers a Kotlin subclass of UIView.
private func composeViewClass(_ name: String) -> UIView.Type {
if let existing = NSClassFromString(name) as? UIView.Type {
return existing
}
let cls: AnyClass = objc_allocateClassPair(UIView.self, name, 0)!
objc_registerClassPair(cls)
return cls as! UIView.Type
}

private let composeContainerView = composeViewClass("SampleAppandroidx.compose.ui.window.ComposeContainerView3")
private let composeSurfaceView = composeViewClass("SampleAppandroidx.compose.ui.window.SurfaceMetalView7")

@Suite("Replay masking for Compose Multiplatform views", .serialized)
@MainActor
struct PostHogComposeMultiplatformMaskingTest {
private typealias Sut = (sdk: PostHogSDK, integration: PostHogReplayIntegration)

private func makeSut(maskText: Bool, maskImages: Bool) -> Sut {
let config = PostHogConfig(projectToken: "phc_composeMultiplatformMaskingTest")
config.disableReachabilityForTesting = true
config.sessionReplayConfig.maskAllTextInputs = maskText
config.sessionReplayConfig.maskAllImages = maskImages
PostHogReplayIntegration.clearInstalls()
let sdk = PostHogSDK.with(config)
let integration = PostHogReplayIntegration()
_ = integration.install(sdk)
return (sdk, integration)
}

private func teardown(_ sut: Sut) {
sut.integration.uninstall(sut.sdk)
sut.sdk.close()
}

private func makeWindow(containing view: UIView) -> UIWindow {
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 640))
window.addSubview(view)
window.layoutIfNeeded()
return window
}

/// A Compose screen as Compose Multiplatform 1.12 lays it out: a container holding the
/// Metal surface it draws into.
private func makeComposeScreen(frame: CGRect) -> UIView {
let container = composeContainerView.init(frame: frame)
container.addSubview(composeSurfaceView.init(frame: CGRect(origin: .zero, size: frame.size)))
return container
}

@Test("A Compose Multiplatform view is masked whole under maskAllTextInputs")
func maskedUnderMaskAllTextInputs() {
let sut = makeSut(maskText: true, maskImages: false)
defer { teardown(sut) }

let window = makeWindow(containing: makeComposeScreen(frame: CGRect(x: 0, y: 60, width: 320, height: 500)))

// the container and the surface filling it
let rects = sut.integration.collectMaskableRects(in: window) ?? []
#expect(!rects.isEmpty && rects.allSatisfy { $0 == CGRect(x: 0, y: 60, width: 320, height: 500) })
}

@Test("A Compose Multiplatform view is masked whole under maskAllImages")
func maskedUnderMaskAllImages() {
let sut = makeSut(maskText: false, maskImages: true)
defer { teardown(sut) }

let window = makeWindow(containing: makeComposeScreen(frame: CGRect(x: 0, y: 60, width: 320, height: 500)))

// the container and the surface filling it
let rects = sut.integration.collectMaskableRects(in: window) ?? []
#expect(!rects.isEmpty && rects.allSatisfy { $0 == CGRect(x: 0, y: 60, width: 320, height: 500) })
}

@Test("A Compose Multiplatform view isn't masked when text and image masking are off")
func unmaskedWithMaskingOff() {
let sut = makeSut(maskText: false, maskImages: false)
defer { teardown(sut) }

let window = makeWindow(containing: makeComposeScreen(frame: CGRect(x: 0, y: 60, width: 320, height: 500)))

#expect(sut.integration.collectMaskableRects(in: window) == [])
}

@Test("A ph-no-mask token on a Compose Multiplatform view unmasks it")
func noMaskTokenUnmasks() {
let sut = makeSut(maskText: true, maskImages: true)
defer { teardown(sut) }

let screen = makeComposeScreen(frame: CGRect(x: 0, y: 60, width: 320, height: 500))
screen.accessibilityIdentifier = "ph-no-mask"
let window = makeWindow(containing: screen)

#expect(sut.integration.collectMaskableRects(in: window) == [])
}

@Test("A sensitive UIKit view drawing outside a Compose Multiplatform view stays masked")
func unclippedSensitiveSubviewMasked() {
let sut = makeSut(maskText: true, maskImages: false)
defer { teardown(sut) }

let screen = makeComposeScreen(frame: CGRect(x: 0, y: 60, width: 320, height: 500))
// an interop text field drawing below the screen's rect
let field = UITextField(frame: CGRect(x: 0, y: 500, width: 320, height: 40))
field.text = "secret"
screen.addSubview(field)
let window = makeWindow(containing: screen)

let rects = sut.integration.collectMaskableRects(in: window) ?? []
#expect(rects.contains(CGRect(x: 0, y: 60, width: 320, height: 500)))
#expect(rects.contains(CGRect(x: 0, y: 560, width: 320, height: 40)))
}

@Test("A ph-no-capture Compose Multiplatform view masks its descendants too")
func noCaptureMasksDescendants() {
let sut = makeSut(maskText: false, maskImages: false)
defer { teardown(sut) }

let screen = makeComposeScreen(frame: CGRect(x: 0, y: 60, width: 320, height: 500))
screen.accessibilityIdentifier = "ph-no-capture"
// an unclipped subview drawing below the screen's rect
screen.addSubview(UIView(frame: CGRect(x: 0, y: 500, width: 320, height: 40)))
let window = makeWindow(containing: screen)

let rects = sut.integration.collectMaskableRects(in: window) ?? []
#expect(rects.contains(CGRect(x: 0, y: 60, width: 320, height: 500)))
#expect(rects.contains(CGRect(x: 0, y: 560, width: 320, height: 40)))
}

@Test("A Compose Multiplatform surface is masked without its container")
func surfaceMaskedAlone() {
let sut = makeSut(maskText: true, maskImages: false)
defer { teardown(sut) }

let window = makeWindow(containing: composeSurfaceView.init(frame: CGRect(x: 10, y: 20, width: 300, height: 400)))

#expect(sut.integration.collectMaskableRects(in: window) == [CGRect(x: 10, y: 20, width: 300, height: 400)])
}

@Test("A view whose class name only mentions Compose elsewhere isn't masked")
func unrelatedClassNameUnmasked() {
let sut = makeSut(maskText: true, maskImages: true)
defer { teardown(sut) }

let other = composeViewClass("SampleAppcom.example.compose.ui.windowless.PlainView1")
let window = makeWindow(containing: other.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100)))

#expect(sut.integration.collectMaskableRects(in: window) == [])
}
}
#endif
Loading