From 4fe679fab1deda241cb804fd7a516603b73427e6 Mon Sep 17 00:00:00 2001 From: Tanner Bennett <8371943+NSExceptional@users.noreply.github.com> Date: Thu, 6 Aug 2026 17:45:13 -0500 Subject: [PATCH] Fix crash when a pin-failure report file fails to write (#351) TSKBackgroundReporter writes the JSON report to a temp file and then uploads it with -uploadTaskWithRequest:fromFile:. A write failure was only treated as fatal in DEBUG builds; in production the code fell through and still handed the non-existent file to the upload task. CFNetwork validates the file synchronously at task-creation time and throws an uncaught NSInvalidArgumentException ("Cannot read file at ..."), crashing the app. Return early when the report file cannot be written so we never create an upload task for a file that does not exist. Firebase appeared in the reported stack trace only because FirebasePerformance swizzles uploadTaskWithRequest:fromFile:; it was not the cause. --- TrustKit/Reporting/TSKBackgroundReporter.m | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/TrustKit/Reporting/TSKBackgroundReporter.m b/TrustKit/Reporting/TSKBackgroundReporter.m index c9c1d997..f6b37c23 100644 --- a/TrustKit/Reporting/TSKBackgroundReporter.m +++ b/TrustKit/Reporting/TSKBackgroundReporter.m @@ -203,6 +203,12 @@ - (void) pinValidationFailedForHostname:(nonnull NSString *)serverHostname [NSException raise:@"TSKBackgroundReporter runtime error" format:@"Report cannot be saved to file: %@", [error description]]; #endif + // In production, if the report could not be written, bail out here. Otherwise we would go on to + // hand a non-existent file to -uploadTaskWithRequest:fromFile:, which throws an uncaught + // NSInvalidArgumentException ("Cannot read file at ...") and crashes the app. + // https://github.com/datatheorem/TrustKit/issues/351 + TSKLog(@"Report for %@ could not be saved to file; skipping upload: %@", serverHostname, [error description]); + return; } TSKLog(@"Report for %@ created at: %@", serverHostname, [tmpFileURL path]);