Skip to content

Commit 40056c9

Browse files
committed
fix(ios): hold age restriction check through notification handoff
Signed-off-by: Tom Brow <[email protected]>
1 parent f960500 commit 40056c9

4 files changed

Lines changed: 54 additions & 15 deletions

File tree

‎mobile/ios/BuzzPushKit/Sources/BuzzPushKit/BuzzAgeRestrictionSession.swift‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,29 @@ public final class BuzzAgeRestrictionSession {
3939
/// A missing file, inaccessible container, or unknown OS error means allowed.
4040
/// A shared nonblocking probe conflicts only with a live exclusive holder.
4141
public static func isRestricted(containerURL: URL?) -> Bool {
42-
guard let containerURL else { return false }
43-
let opened = open(containerURL.appendingPathComponent(fileName).path, O_RDONLY)
44-
guard opened >= 0 else { return false }
42+
!handoffIfAllowed(containerURL: containerURL, deliver: {})
43+
}
44+
45+
/// Holds shared authority through synchronous delivery, so confirmed
46+
/// restriction cannot finish its purge between the check and the handoff.
47+
/// Unknown storage failures still deliver normally.
48+
public static func handoffIfAllowed(containerURL: URL?, deliver: () -> Void) -> Bool {
49+
guard let containerURL else { deliver(); return true }
50+
let path = containerURL.appendingPathComponent(fileName).path
51+
var opened = open(path, O_RDONLY)
52+
if opened < 0 && errno == ENOENT {
53+
opened = open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)
54+
}
55+
guard opened >= 0 else { deliver(); return true }
4556
defer { close(opened) }
4657
if flock(opened, LOCK_SH | LOCK_NB) == 0 {
47-
flock(opened, LOCK_UN)
48-
return false
58+
defer { flock(opened, LOCK_UN) }
59+
deliver()
60+
return true
4961
}
50-
return errno == EWOULDBLOCK || errno == EAGAIN
62+
if errno == EWOULDBLOCK || errno == EAGAIN { return false }
63+
deliver()
64+
return true
5165
}
5266

5367
private static func posixError() -> POSIXError {

‎mobile/ios/BuzzPushKit/Tests/BuzzPushKitTests/BuzzAgeRestrictionSessionTests.swift‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,31 @@ struct BuzzAgeRestrictionSessionTests {
4040
#expect(!BuzzAgeRestrictionSession.isRestricted(containerURL: missing))
4141
}
4242

43+
@Test func `Restriction cannot begin between the access check and delivery`() throws {
44+
let url = try directory()
45+
defer { try? FileManager.default.removeItem(at: url) }
46+
let session = BuzzAgeRestrictionSession()
47+
var delivered = false
48+
#expect(BuzzAgeRestrictionSession.handoffIfAllowed(containerURL: url) {
49+
#expect(throws: (any Error).self) { try session.restrict(containerURL: url) }
50+
delivered = true
51+
})
52+
#expect(delivered)
53+
try session.restrict(containerURL: url)
54+
#expect(!BuzzAgeRestrictionSession.handoffIfAllowed(containerURL: url) {
55+
Issue.record("A confirmed restriction must suppress ordinary content")
56+
})
57+
session.release()
58+
}
59+
60+
@Test func `Handoff delivers when restriction storage is unavailable`() {
61+
var deliveries = 0
62+
#expect(BuzzAgeRestrictionSession.handoffIfAllowed(containerURL: nil) { deliveries += 1 })
63+
let missing = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
64+
#expect(BuzzAgeRestrictionSession.handoffIfAllowed(containerURL: missing) { deliveries += 1 })
65+
#expect(deliveries == 2)
66+
}
67+
4368
#if os(macOS)
4469
@Test(.timeLimit(.minutes(1))) func processExitReleasesAuthorityWithoutCleanup() throws {
4570
let url = try directory()

‎mobile/ios/BuzzPushKit/Tests/BuzzPushKitTests/BuzzNotificationHandoffTests.swift‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ struct BuzzNotificationHandoffTests {
2525
content,
2626
restrictedFallback: "Open Buzz to view this message.",
2727
handoffIfAllowed: { deliver in
28-
guard !BuzzAgeRestrictionSession.isRestricted(containerURL: directory) else { return false }
29-
deliver()
30-
return true
28+
BuzzAgeRestrictionSession.handoffIfAllowed(containerURL: directory, deliver: deliver)
3129
},
3230
cleanup: {
3331
// Neither the Intents callback nor its timer needs to fire for delivery.

‎mobile/ios/NotificationService/NotificationService.swift‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ final class NotificationService: UNNotificationServiceExtension {
108108
content,
109109
restrictedFallback: restrictedFallbackContent ?? Self.restrictedFallback(from: content),
110110
handoffIfAllowed: { deliver in
111-
guard !isAgeRestricted() else { return false }
112-
deliver()
113-
return true
111+
BuzzAgeRestrictionSession.handoffIfAllowed(
112+
containerURL: ageRestrictionContainerURL, deliver: deliver)
114113
}
115114
) { [self] in
116115
// The service deadline cannot wait for Intents cleanup. The safe content
@@ -126,11 +125,14 @@ final class NotificationService: UNNotificationServiceExtension {
126125
}
127126
}
128127

129-
private func isAgeRestricted() -> Bool {
130-
let container = appGroupIdentifier.flatMap {
128+
private var ageRestrictionContainerURL: URL? {
129+
appGroupIdentifier.flatMap {
131130
FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: $0)
132131
}
133-
return BuzzAgeRestrictionSession.isRestricted(containerURL: container)
132+
}
133+
134+
private func isAgeRestricted() -> Bool {
135+
BuzzAgeRestrictionSession.isRestricted(containerURL: ageRestrictionContainerURL)
134136
}
135137

136138
private static func restrictedFallback(

0 commit comments

Comments
 (0)