Skip to content

Commit

Permalink
fix: Session Redact wrong clipping order (#4651)
Browse files Browse the repository at this point in the history
Fix an error where SR clipping happens in the wrong order.
  • Loading branch information
brustolin authored Dec 20, 2024
1 parent c810e58 commit bd8b1bf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
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 {
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

0 comments on commit bd8b1bf

Please sign in to comment.