From dd8823e03bc883750bd08ec032db40fd9c3ee2b5 Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Wed, 28 Dec 2022 10:08:19 +0100 Subject: [PATCH] fix: Deleting old envelopes for empty DSN The SentryFileManager doesn't append the DSN hash to the basePath if the DSN is nil. We didn't handle this edge case when deleting old envelopes. This is fixed now. --- CHANGELOG.md | 1 + Sources/Sentry/SentryFileManager.m | 17 ++++++++++++++--- .../Helper/SentryFileManagerTests.swift | 10 ++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ab00907b6..6eef8d1254 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ This version adds a dependency on Swift. - Fix ARC issue for FileManager (#2525) - Remove delay for deleting old envelopes (#2541) - Fix strong reference cycle for HttpTransport (#2552) +- Deleting old envelopes for empty DSN (#2562) ### Breaking Changes diff --git a/Sources/Sentry/SentryFileManager.m b/Sources/Sentry/SentryFileManager.m index aff41b6d89..a27c192cb9 100644 --- a/Sources/Sentry/SentryFileManager.m +++ b/Sources/Sentry/SentryFileManager.m @@ -16,6 +16,8 @@ NS_ASSUME_NONNULL_BEGIN +NSString *const EnvelopesPathComponent = @"envelopes"; + @interface SentryFileManager () @@ -173,8 +175,17 @@ - (void)deleteOldEnvelopesFromAllSentryPaths continue; } + // If the options don't have a DSN the sentry path doesn't contain a hash and the envelopes + // folder is stored in the base path. + NSString *envelopesPath; + if ([fullPath hasSuffix:EnvelopesPathComponent]) { + envelopesPath = fullPath; + } else { + envelopesPath = [fullPath stringByAppendingPathComponent:EnvelopesPathComponent]; + } + // Then we will remove all old items from the envelopes subdirectory - [self deleteOldEnvelopesFromPath:[fullPath stringByAppendingPathComponent:@"envelopes"]]; + [self deleteOldEnvelopesFromPath:envelopesPath]; } } @@ -472,7 +483,7 @@ - (void)moveBreadcrumbsToPreviousBreadcrumbs - (void)moveState:(NSString *)stateFilePath toPreviousState:(NSString *)previousStateFilePath { - SENTRY_LOG_DEBUG(@"Moving current app state to previous app state."); + SENTRY_LOG_DEBUG(@"Moving state %@ to previous %@.", stateFilePath, previousStateFilePath); NSFileManager *fileManager = [NSFileManager defaultManager]; // We first need to remove the old previous state file, @@ -664,7 +675,7 @@ - (void)createPathsWithOptions:(SentryOptions *_Nonnull)options [self.sentryPath stringByAppendingPathComponent:@"breadcrumbs.2.state"]; self.timezoneOffsetFilePath = [self.sentryPath stringByAppendingPathComponent:@"timezone.offset"]; - self.envelopesPath = [self.sentryPath stringByAppendingPathComponent:@"envelopes"]; + self.envelopesPath = [self.sentryPath stringByAppendingPathComponent:EnvelopesPathComponent]; } - (BOOL)createDirectoryIfNotExists:(NSString *)path error:(NSError **)error diff --git a/Tests/SentryTests/Helper/SentryFileManagerTests.swift b/Tests/SentryTests/Helper/SentryFileManagerTests.swift index 445d4c43bd..86221d6951 100644 --- a/Tests/SentryTests/Helper/SentryFileManagerTests.swift +++ b/Tests/SentryTests/Helper/SentryFileManagerTests.swift @@ -134,7 +134,17 @@ class SentryFileManagerTests: XCTestCase { } func testDeleteOldEnvelopes() throws { + try givenOldEnvelopes() + + sut = fixture.getSut() + XCTAssertEqual(sut.getAllEnvelopes().count, 0) + } + + func testDeleteOldEnvelopes_WithEmptyDSN() throws { + fixture.options.dsn = nil + sut = fixture.getSut() + try givenOldEnvelopes() sut = fixture.getSut()