Handle stored dates that are in the future (#2914)

This is a correctness fix for system profile submission checking, periodic cache cleanup, and failed signed feed expiration check.
This commit is contained in:
Zorg authored and GitHub committed 2026-09-14 20:48:10 -07:00
1 parent 5fddc926a3
commit efc9a22635
3 files changed
+40 -22

No files matched your search

+21 -18
View File
@@ -58,28 +58,31 @@ static NSTimeInterval OLD_ITEM_DELETION_INTERVAL = 86400 * 10; // 10 days
{
NSMutableArray<NSString *> *filePathsToRemove = [NSMutableArray array];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:directory]) {
NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:directory];
NSDate *currentDate = [NSDate date];
for (NSString *filename in directoryEnumerator)
{
NSDictionary<NSString *, id> *fileAttributes = [fileManager attributesOfItemAtPath:[directory stringByAppendingPathComponent:filename] error:NULL];
if (fileAttributes != nil)
{
NSDate *lastModificationDate = [fileAttributes objectForKey:NSFileModificationDate];
if ([currentDate timeIntervalSinceDate:lastModificationDate] >= OLD_ITEM_DELETION_INTERVAL)
{
[filePathsToRemove addObject:[directory stringByAppendingPathComponent:filename]];
NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:directory];
NSDate *currentDate = [NSDate date];
for (NSString *filename in directoryEnumerator) {
NSString *filePath = [directory stringByAppendingPathComponent:filename];
NSDictionary<NSString *, id> *fileAttributes = [fileManager attributesOfItemAtPath:filePath error:NULL];
if (fileAttributes != nil) {
NSDate *lastModificationDate = [fileAttributes objectForKey:NSFileModificationDate];
NSTimeInterval timeIntervalSinceLastModificationDate = [currentDate timeIntervalSinceDate:lastModificationDate];
if (timeIntervalSinceLastModificationDate >= OLD_ITEM_DELETION_INTERVAL) {
[filePathsToRemove addObject:[directory stringByAppendingPathComponent:filename]];
} else if (timeIntervalSinceLastModificationDate < 0) {
// Reset the modification date if it's far out in the future
NSError *resetModificationDateError = nil;
if (![fileManager setAttributes:@{NSFileModificationDate: currentDate} ofItemAtPath:filePath error:&resetModificationDateError]) {
SULog(SULogLevelError, @"Failed to reset modification date of file modified in future: %@", resetModificationDateError.localizedDescription);
}
}
[directoryEnumerator skipDescendants];
}
for (NSString *filename in filePathsToRemove)
{
[fileManager removeItemAtPath:filename error:NULL];
}
[directoryEnumerator skipDescendants];
}
for (NSString *filename in filePathsToRemove) {
[fileManager removeItemAtPath:filename error:NULL];
}
}
+10 -3
View File
@@ -1249,14 +1249,21 @@ static NSString *escapeURLComponent(NSString *str) {
BOOL sendingSystemProfile = [self sendsSystemProfile];
// Let's only send the system profiling information once per week at most, so we normalize daily-checkers vs. biweekly-checkers and the such.
NSDate *currentDate = [NSDate date];
if (sendingSystemProfile) {
NSDate *lastSubmitDate = [_host objectForUserDefaultsKey:SULastProfileSubmitDateKey ofClass:NSDate.class];
if (!lastSubmitDate) {
lastSubmitDate = [NSDate distantPast];
}
const NSTimeInterval oneWeek = 60 * 60 * 24 * 7;
NSTimeInterval timeSinceLastSubmission = [lastSubmitDate timeIntervalSinceNow] * -1;
if (timeSinceLastSubmission < oneWeek) {
NSTimeInterval timeSinceLastSubmission = [currentDate timeIntervalSinceDate:lastSubmitDate];
if (timeSinceLastSubmission < 0) {
// Ignore dates far out in the future
sendingSystemProfile = NO;
[_host setObject:currentDate forUserDefaultsKey:SULastProfileSubmitDateKey];
} else if (timeSinceLastSubmission < oneWeek) {
sendingSystemProfile = NO;
}
}
@@ -1272,7 +1279,7 @@ static NSString *escapeURLComponent(NSString *str) {
if (sendingSystemProfile)
{
parameters = [parameters arrayByAddingObjectsFromArray:[self systemProfileArray]];
[_host setObject:[NSDate date] forUserDefaultsKey:SULastProfileSubmitDateKey];
[_host setObject:currentDate forUserDefaultsKey:SULastProfileSubmitDateKey];
}
if ([parameters count] == 0) { return baseFeedURL; }
+9 -1
View File
@@ -171,7 +171,15 @@
[hostForFailedSigningValidationDate setObject:currentDate forUserDefaultsKey:initialFailedFeedSigningValidationDateKey];
canRecoverFromSigningValidationFailure = NO;
} else {
canRecoverFromSigningValidationFailure = ([currentDate timeIntervalSinceDate:firstFailedSigningValidationDate] >= failureExpirationInterval);
NSTimeInterval timeIntervalSinceFirstFailedValidationDate = [currentDate timeIntervalSinceDate:firstFailedSigningValidationDate];
if (timeIntervalSinceFirstFailedValidationDate < 0) {
// Record first signing validation date if stored date is far out in future
[hostForFailedSigningValidationDate setObject:currentDate forUserDefaultsKey:initialFailedFeedSigningValidationDateKey];
canRecoverFromSigningValidationFailure = NO;
} else {
canRecoverFromSigningValidationFailure = (timeIntervalSinceFirstFailedValidationDate >= failureExpirationInterval);
}
}
}