Skip to content

Commit 7c84acf

Browse files
committed
fix(ios): fail closed on unknown snapshot frontier completeness
1 parent 7966e9d commit 7c84acf

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

‎apple/snapshot-bridge/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ allows at most 32 native requests within the existing capture deadline,
6060
checks foreground ownership on every request, and returns a failure when it
6161
cannot complete a continuation. Budget exhaustion and malformed continuations
6262
use non-launch failure codes, so the route falls back without launch re-polling.
63-
Unchanged native dictionaries and child arrays are reused.
63+
At each native fragment boundary, an absent or invalid child count means unknown
64+
completeness and fails closed. Natural leaves above that boundary need no
65+
continuation evidence. Unchanged native dictionaries and child arrays are reused.

‎apple/snapshot-bridge/SnapshotBridgeCapture.m‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ @interface SnapshotTreeCapture : NSObject
1414
@property(nonatomic) NSUInteger requests;
1515
@property(nonatomic) BOOL truncated;
1616
- (nullable NSDictionary *)read:(id)element depth:(NSUInteger)depth error:(NSError **)error;
17-
- (nullable NSDictionary *)materialize:(NSDictionary *)tree depth:(NSUInteger)depth error:(NSError **)error;
17+
- (nullable NSDictionary *)materialize:(NSDictionary *)tree depth:(NSUInteger)depth nativeLevels:(NSUInteger)nativeLevels error:(NSError **)error;
1818
@end
1919

2020
@implementation SnapshotTreeCapture
@@ -43,7 +43,7 @@ - (nullable NSDictionary *)read:(id)element depth:(NSUInteger)depth error:(NSErr
4343
}
4444
}
4545

46-
- (nullable NSDictionary *)materialize:(NSDictionary *)tree depth:(NSUInteger)depth error:(NSError **)error
46+
- (nullable NSDictionary *)materialize:(NSDictionary *)tree depth:(NSUInteger)depth nativeLevels:(NSUInteger)nativeLevels error:(NSError **)error
4747
{
4848
if (![tree isKindOfClass:NSDictionary.class] ||
4949
![tree[attributesKey] isKindOfClass:NSDictionary.class] ||
@@ -59,7 +59,15 @@ - (nullable NSDictionary *)materialize:(NSDictionary *)tree depth:(NSUInteger)de
5959
self.remainingNodes--;
6060
NSArray *children = tree[childrenKey];
6161
NSNumber *childCount = tree[childCountKey];
62-
BOOL withheld = [childCount isKindOfClass:NSNumber.class] && childCount.unsignedIntegerValue > children.count;
62+
BOOL knownChildCount = [childCount isKindOfClass:NSNumber.class] && childCount.doubleValue >= 0 &&
63+
childCount.doubleValue == (double)childCount.unsignedIntegerValue;
64+
if (nativeLevels <= 1 && !knownChildCount && depth <= 1) self.truncated = YES;
65+
if (depth > 1 && nativeLevels <= 1 && !knownChildCount) {
66+
if (error) *error = [NSError errorWithDomain:@"agent-device.snapshot" code:6
67+
userInfo:@{NSLocalizedDescriptionKey: @"snapshot boundary child count unavailable"}];
68+
return nil;
69+
}
70+
BOOL withheld = knownChildCount && childCount.unsignedIntegerValue > children.count;
6371
if (depth <= 1 || self.remainingNodes == 0) {
6472
self.truncated |= children.count > 0 || withheld;
6573
if (children.count == 0) return tree;
@@ -76,6 +84,7 @@ - (nullable NSDictionary *)materialize:(NSDictionary *)tree depth:(NSUInteger)de
7684
}
7785
NSDictionary *continuation = [self read:element depth:depth error:error];
7886
if (!continuation) return nil;
87+
nativeLevels = MIN(depth, self.acceptedDepth);
7988
children = continuation[childrenKey];
8089
if (![children isKindOfClass:NSArray.class] || children.count < MIN(childCount.unsignedIntegerValue, self.remainingNodes)) {
8190
if (error) *error = [NSError errorWithDomain:@"agent-device.snapshot" code:4
@@ -92,7 +101,7 @@ - (nullable NSDictionary *)materialize:(NSDictionary *)tree depth:(NSUInteger)de
92101
if (!materialized) materialized = [[children subarrayWithRange:NSMakeRange(0, index)] mutableCopy];
93102
break;
94103
}
95-
NSDictionary *node = [self materialize:child depth:depth - 1 error:error];
104+
NSDictionary *node = [self materialize:child depth:depth - 1 nativeLevels:(nativeLevels > 0 ? nativeLevels - 1 : 0) error:error];
96105
if (!node) return nil;
97106
if (node != child && !materialized) materialized = [[children subarrayWithRange:NSMakeRange(0, index)] mutableCopy];
98107
[materialized addObject:node];
@@ -114,7 +123,7 @@ - (nullable NSDictionary *)materialize:(NSDictionary *)tree depth:(NSUInteger)de
114123
capture.remainingNodes = maxNodes;
115124
capture.maximumNodes = maxNodes;
116125
NSDictionary *tree = [capture read:element depth:maxDepth + 1 error:error];
117-
NSDictionary *result = tree ? [capture materialize:tree depth:maxDepth + 1 error:error] : nil;
126+
NSDictionary *result = tree ? [capture materialize:tree depth:maxDepth + 1 nativeLevels:capture.acceptedDepth error:error] : nil;
118127
*truncated = capture.truncated;
119128
return result;
120129
}

‎packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ - (id)userTestingSnapshotForElement:(id)element options:(NSDictionary *)options
8585
@"UIAccessibilitySnapshotKeyChildrenCount": @(i < 6 ? 1 : 0),
8686
@"UIAccessibilitySnapshotKeyChildren": tree ? @[tree] : @[]} mutableCopy];
8787
}
88-
if ([captureScenario isEqual:@"depth-missing-element"]) {
88+
if ([@[@"depth-missing-element", @"depth-missing-count", @"depth-invalid-count", @"depth-fractional-count", @"depth-nan-count", @"depth-negative-count"] containsObject:captureScenario] || ([captureScenario isEqual:@"depth-continuation-count"] && level > 0)) {
8989
NSMutableDictionary *frontier = tree;
9090
while ([frontier[@"UIAccessibilitySnapshotKeyChildren"] count]) frontier = [frontier[@"UIAccessibilitySnapshotKeyChildren"] firstObject];
91-
[frontier removeObjectForKey:@"UIAccessibilitySnapshotKeyElement"];
91+
if ([captureScenario isEqual:@"depth-missing-element"]) [frontier removeObjectForKey:@"UIAccessibilitySnapshotKeyElement"];
92+
else if ([captureScenario isEqual:@"depth-missing-count"] || [captureScenario isEqual:@"depth-continuation-count"]) [frontier removeObjectForKey:@"UIAccessibilitySnapshotKeyChildrenCount"];
93+
else frontier[@"UIAccessibilitySnapshotKeyChildrenCount"] = [captureScenario isEqual:@"depth-fractional-count"] ? @0.5 : [captureScenario isEqual:@"depth-nan-count"] ? @(NAN) : [captureScenario isEqual:@"depth-negative-count"] ? @(-1) : [NSNull null];
9294
}
9395
if ([captureScenario isEqual:@"depth-incomplete"] && level > 0) tree[@"UIAccessibilitySnapshotKeyChildren"] = @[];
9496
if ([captureScenario isEqual:@"depth-owner-change"] && level > 0) primaryApplication = replacementApplication;
@@ -160,7 +162,7 @@ int main(int argc, const char *argv[])
160162
if ([scenario hasPrefix:@"api-depth-"]) {
161163
require(levels == depth + 1, @"native levels must include the root exactly once");
162164
NSDictionary *tree = nil;
163-
for (NSUInteger i = 0; i < levels; i++) tree = @{@"UIAccessibilitySnapshotKeyAttributes": @{}, @"UIAccessibilitySnapshotKeyChildren": tree ? @[tree] : @[]};
165+
for (NSUInteger i = 0; i < levels; i++) tree = @{@"UIAccessibilitySnapshotKeyAttributes": @{}, @"UIAccessibilitySnapshotKeyChildrenCount": [scenario isEqual:@"api-depth-unknown"] ? [NSNull null] : @(tree ? 1 : 0), @"UIAccessibilitySnapshotKeyChildren": tree ? @[tree] : @[]};
164166
return tree;
165167
}
166168
if ([element isEqual:@"root"]) return root;
@@ -171,7 +173,7 @@ int main(int argc, const char *argv[])
171173
else {
172174
NSUInteger count = 0;
173175
for (NSDictionary *node = result; node; node = [node[@"UIAccessibilitySnapshotKeyChildren"] firstObject]) count++;
174-
require(count == depth + 1 && !truncated && requests == 1, @"every requested depth must include root plus permitted descendants");
176+
require(count == depth + 1 && truncated == [scenario isEqual:@"api-depth-unknown"] && requests == 1, @"every requested depth must include root plus permitted descendants");
175177
}
176178
return 0;
177179
}
@@ -191,9 +193,9 @@ int main(int argc, const char *argv[])
191193
expectedCaptures = [scenario isEqual:@"wide-continuation"] ? 2 : 1;
192194
} else if ([scenario hasPrefix:@"depth-"]) {
193195
expectedCaptures = [scenario isEqual:@"depth-bound"] ? 5 : [scenario isEqual:@"depth-nodes"] ? 2 : 3;
194-
if ([scenario isEqual:@"depth-missing-element"] || [scenario isEqual:@"depth-incomplete"]) {
196+
if ([@[@"depth-missing-element", @"depth-incomplete", @"depth-missing-count", @"depth-invalid-count", @"depth-continuation-count", @"depth-fractional-count", @"depth-nan-count", @"depth-negative-count"] containsObject:scenario]) {
195197
expectedCode = @"snapshot-tree-malformed";
196-
if ([scenario isEqual:@"depth-missing-element"]) expectedCaptures = 2;
198+
if (![scenario isEqual:@"depth-incomplete"] && ![scenario isEqual:@"depth-continuation-count"]) expectedCaptures = 2;
197199
}
198200
if ([scenario isEqual:@"depth-owner-change"]) {
199201
replacementApplication = system;

‎packages/platform-apple/src/snapshot-source/native-runtime.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe.skipIf(process.platform !== 'darwin')('native snapshot capture', () =>
4141
'request-budget',
4242
'runtime-budget',
4343
'api-depth-0',
44+
'api-depth-unknown',
4445
'api-depth-1',
4546
'api-depth-4',
4647
'api-depth-128',
@@ -55,6 +56,12 @@ describe.skipIf(process.platform !== 'darwin')('native snapshot capture', () =>
5556
'depth-recovery',
5657
'depth-wrapper',
5758
'depth-missing-element',
59+
'depth-missing-count',
60+
'depth-invalid-count',
61+
'depth-fractional-count',
62+
'depth-nan-count',
63+
'depth-negative-count',
64+
'depth-continuation-count',
5865
'depth-incomplete',
5966
'depth-bound',
6067
'depth-nodes',

0 commit comments

Comments
 (0)