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: Session Redact wrong clipping order #4651

Merged
merged 3 commits into from
Dec 20, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- `SentrySdkInfo.packages` should be an array (#4626)
- Use the same SdkInfo for envelope header and event (#4629)
- Fixes Session replay screenshot provider crash (#4649)
- Session Redact wrong clipping order (#4651)

### Internal

Expand Down
4 changes: 1 addition & 3 deletions Sources/Swift/Tools/SentryViewPhotographer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class SentryViewPhotographer: NSObject, SentryViewScreenshotProvider {
let imageRect = CGRect(origin: .zero, size: size)
context.cgContext.addRect(CGRect(origin: CGPoint.zero, size: size))
context.cgContext.clip(using: .evenOdd)
UIColor.blue.setStroke()

context.cgContext.interpolationQuality = .none
image.draw(at: .zero)
Expand All @@ -79,10 +78,9 @@ class SentryViewPhotographer: NSObject, SentryViewScreenshotProvider {

defer { latestRegion = region }

guard latestRegion?.canReplace(as: region) != true && imageRect.intersects(path.boundingBoxOfPath) else { continue }

switch region.type {
case .redact, .redactSwiftUI:
guard latestRegion?.canReplace(as: region) != true && imageRect.intersects(path.boundingBoxOfPath) else { continue }
(region.color ?? UIImageHelper.averageColor(of: context.currentImage, at: rect.applying(region.transform))).setFill()
context.cgContext.addPath(path)
context.cgContext.fillPath()
Expand Down
6 changes: 3 additions & 3 deletions Sources/Swift/Tools/UIRedactBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,16 @@ class UIRedactBuilder {
}

guard let subLayers = layer.sublayers, subLayers.count > 0 else { return }

if view.clipsToBounds {
let clipToBounds = view.clipsToBounds
if clipToBounds {
/// Because the order in which we process the redacted regions is reversed, we add the end of the clip region first.
/// The beginning will be added after all the subviews have been mapped.
redacting.append(RedactRegion(size: layer.bounds.size, transform: newTransform, type: .clipEnd))
}
for subLayer in subLayers.sorted(by: { $0.zPosition < $1.zPosition }) {
mapRedactRegion(fromLayer: subLayer, relativeTo: layer, redacting: &redacting, rootFrame: rootFrame, transform: newTransform, forceRedact: enforceRedact)
}
if view.clipsToBounds {
if clipToBounds {
redacting.append(RedactRegion(size: layer.bounds.size, transform: newTransform, type: .clipBegin))
}
}
Expand Down
26 changes: 26 additions & 0 deletions Tests/SentryTests/SentryViewPhotographerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,32 @@ class SentryViewPhotographerTests: XCTestCase {
])
}

func testLabelRedactedStackedHierarchy() throws {
brustolin marked this conversation as resolved.
Show resolved Hide resolved
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
label.text = "Test"

let bottomView = UIView(frame: CGRect(x: 5, y: 5, width: 40, height: 40))
bottomView.clipsToBounds = true
bottomView.backgroundColor = .white

let middleView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
middleView.clipsToBounds = true
middleView.backgroundColor = .white

let topView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
topView.clipsToBounds = true
topView.backgroundColor = .white

bottomView.addSubview(middleView)
middleView.addSubview(topView)
topView.addSubview(label)

let image = try XCTUnwrap(prepare(views: [bottomView]))
let pixel = color(at: CGPoint(x: 10, y: 10), in: image)

assertColor(pixel, .black)
}

private func assertColor(_ color: UIColor, in image: UIImage, at points: [CGPoint]) {
points.forEach {
let pixel = self.color(at: $0, in: image)
Expand Down
Loading