Skip to content
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

fix: detect swiftui images not too agressively #181

Merged
merged 8 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- recording: detect swiftui images not too agressively ([#181](https://github.com/PostHog/posthog-ios/pull/181))

## 3.9.0 - 2024-09-06

- chore: add SwiftUI View extensions to capture screen view and views in general (postHogViewEvent, postHogScreenView) ([#180](https://github.com/PostHog/posthog-ios/pull/180))
Expand Down
26 changes: 22 additions & 4 deletions PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
// SwiftUI image types
// https://stackoverflow.com/questions/57554590/how-to-get-all-the-subviews-of-a-window-or-view-in-latest-swiftui-app
// https://stackoverflow.com/questions/58336045/how-to-detect-swiftui-usage-programmatically-in-an-ios-application
private let swiftUIImageTypes = ["_TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView",
"_TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView",
"SwiftUI._UIGraphicsView",
private let swiftUIImageTypes = ["SwiftUI._UIGraphicsView",
"SwiftUI.ImageLayer"].compactMap { NSClassFromString($0) }

private let swiftUIGenericTypes = ["_TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView",
"_TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView"].compactMap { NSClassFromString($0) }

static let dispatchQueue = DispatchQueue(label: "com.posthog.PostHogReplayIntegration",
target: .global(qos: .utility))

Expand Down Expand Up @@ -214,7 +215,15 @@
}

if swiftUIImageTypes.contains(where: { view.isKind(of: $0) }) {
if isAnyInputSensitive(view) {
if isSwiftUIImageSensitive(view) {
maskableWidgets.append(view.toAbsoluteRect(parent))
return
}
}

// this can be anything, so better to be conservative
if swiftUIGenericTypes.contains(where: { view.isKind(of: $0) }) {
if isTextInputSensitive(view) {
maskableWidgets.append(view.toAbsoluteRect(parent))
return
}
Expand Down Expand Up @@ -306,6 +315,15 @@
}
}

private func isSwiftUIImageSensitive(_ view: UIView) -> Bool {
// the raw type _UIGraphicsView is always something like Color.white or similar
// never contains PII and should not be masked
let type = type(of: view)

let rawGraphicsView = String(describing: type) == "_UIGraphicsView"
return (config.sessionReplayConfig.maskAllImages || view.isNoCapture()) && !rawGraphicsView
}

private func isImageViewSensitive(_ view: UIImageView) -> Bool {
var isAsset = false
if let image = view.image {
Expand Down
159 changes: 81 additions & 78 deletions PostHogExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,99 +90,102 @@ struct ContentView: View {
}

var body: some View {
NavigationStack {
List {
Section("General") {
NavigationLink {
ContentView()
} label: {
Text("Infinite navigation")
}.accessibilityIdentifier("ph-no-capture")

Button("Show Sheet") {
showingSheet.toggle()
}
.sheet(isPresented: $showingSheet) {
ContentView()
.postHogScreenView("ContentViewSheet")
}
Button("Show redacted view") {
showingRedactedSheet.toggle()
}
.sheet(isPresented: $showingRedactedSheet) {
RepresentedExampleUIView()
}
ZStack {
Color.white.edgesIgnoringSafeArea(.all)
NavigationStack {
List {
Section("General") {
NavigationLink {
ContentView()
} label: {
Text("Infinite navigation")
}.accessibilityIdentifier("ph-no-capture")

Button("Show Sheet") {
showingSheet.toggle()
}
.sheet(isPresented: $showingSheet) {
ContentView()
.postHogScreenView("ContentViewSheet")
}
Button("Show redacted view") {
showingRedactedSheet.toggle()
}
.sheet(isPresented: $showingRedactedSheet) {
RepresentedExampleUIView()
}

Text("Sensitive text!!").accessibilityIdentifier("ph-no-capture")
Button(action: incCounter) {
Text(String(counter))
}.accessibilityIdentifier("ph-no-capture-id").accessibilityLabel("ph-no-capture")
Text("Sensitive text!!").accessibilityIdentifier("ph-no-capture")
Button(action: incCounter) {
Text(String(counter))
}.accessibilityIdentifier("ph-no-capture-id").accessibilityLabel("ph-no-capture")

TextField("Enter your name", text: $name).accessibilityLabel("ph-no-capture")
Text("Hello, \(name)!")
Button(action: triggerAuthentication) {
Text("Trigger fake authentication!")
TextField("Enter your name", text: $name).accessibilityLabel("ph-no-capture")
Text("Hello, \(name)!")
Button(action: triggerAuthentication) {
Text("Trigger fake authentication!")
}
Button(action: triggerIdentify) {
Text("Trigger identify!")
}.postHogViewSeen("Trigger identify")
}
Button(action: triggerIdentify) {
Text("Trigger identify!")
}.postHogViewSeen("Trigger identify")
}

Section("Feature flags") {
HStack {
Text("Boolean:")
Spacer()
Text("\(featureFlagsModel.boolValue?.description ?? "unknown")")
.foregroundColor(.secondary)
}
HStack {
Text("String:")
Spacer()
Text("\(featureFlagsModel.stringValue ?? "unknown")")
.foregroundColor(.secondary)
}
Section("Feature flags") {
HStack {
Text("Boolean:")
Spacer()
Text("\(featureFlagsModel.boolValue?.description ?? "unknown")")
.foregroundColor(.secondary)
}
HStack {
Text("String:")
Spacer()
Text("\(featureFlagsModel.stringValue ?? "unknown")")
.foregroundColor(.secondary)
}

HStack {
Text("Payload:")
Spacer()
Text("\(featureFlagsModel.payloadValue?.description ?? "unknown")")
.foregroundColor(.secondary)
}
HStack {
Button(action: triggerFlagReload) {
Text("Reload flags")
HStack {
Text("Payload:")
Spacer()
Text("\(featureFlagsModel.payloadValue?.description ?? "unknown")")
.foregroundColor(.secondary)
}
Spacer()
if featureFlagsModel.isReloading {
ProgressView()
HStack {
Button(action: triggerFlagReload) {
Text("Reload flags")
}
Spacer()
if featureFlagsModel.isReloading {
ProgressView()
}
}
}
}

Section("PostHog beers") {
if !api.beers.isEmpty {
ForEach(api.beers) { beer in
HStack(alignment: .center) {
Text(beer.name)
Section("PostHog beers") {
if !api.beers.isEmpty {
ForEach(api.beers) { beer in
HStack(alignment: .center) {
Text(beer.name)
Spacer()
Text("First brewed")
Text(beer.first_brewed).foregroundColor(Color.gray)
}
}
} else {
HStack {
Text("Loading beers...")
Spacer()
Text("First brewed")
Text(beer.first_brewed).foregroundColor(Color.gray)
ProgressView()
}
}
} else {
HStack {
Text("Loading beers...")
Spacer()
ProgressView()
}
}
}
.navigationTitle("PostHog")
}.onAppear {
api.listBeers(completion: { beers in
api.beers = beers
})
}
.navigationTitle("PostHog")
}.onAppear {
api.listBeers(completion: { beers in
api.beers = beers
})
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions PostHogExample/PostHogExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ struct PostHogExampleApp: App {

var body: some Scene {
WindowGroup {
ContentView()
.postHogScreenView() // will infer the class name (ContentView)
// ContentView()
// .postHogScreenView() // will infer the class name (ContentView)
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
VStack {
Color.white
}
}
}
}
Loading