Skip to content

Commit

Permalink
revert: Block writes on invalid API key based on Identity/Config resp…
Browse files Browse the repository at this point in the history
…onses
  • Loading branch information
einsteinx2 committed Apr 19, 2024
1 parent 5085ce4 commit 7b98acc
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 45 deletions.
11 changes: 0 additions & 11 deletions mParticle-Apple-SDK/MPBackendController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1317,12 +1317,6 @@ - (void)logCrash:(NSString *)message stackTrace:(NSString *)stackTrace plCrashR
}

- (void)logBaseEvent:(MPBaseEvent *)event completionHandler:(void (^)(MPBaseEvent *event, MPExecStatus execStatus))completionHandler {
if (![MPStateMachine canWriteMessagesToDB]) {
MPILogError(@"Not saving message for event to prevent excessive local database growth because API Key appears to be invalid based on server response");
completionHandler(event, MPExecStatusFail);
return;
}

[MPListenerController.sharedInstance onAPICalled:_cmd parameter1:event];

if (event.shouldBeginSession) {
Expand Down Expand Up @@ -1578,11 +1572,6 @@ - (void)startWithKey:(NSString *)apiKey secret:(NSString *)secret firstRun:(BOOL
}

- (void)saveMessage:(MPMessage *)message updateSession:(BOOL)updateSession {
if (![MPStateMachine canWriteMessagesToDB]) {
MPILogError(@"Not saving message for event to prevent excessive local database growth because API Key appears to be invalid based on server response");
return;
}

NSTimeInterval lastEventTimestamp = message.timestamp ?: [[NSDate date] timeIntervalSince1970];
if (MPStateMachine.runningInBackground) {
self.timeOfLastEventInBackground = lastEventTimestamp;
Expand Down
17 changes: 2 additions & 15 deletions mParticle-Apple-SDK/Network/MPNetworkCommunication.m
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,6 @@ - (NSNumber *)maxAgeForCache:(nonnull NSString *)cache {
return maxAge;
}

- (void)checkResponseCodeToDisableEventLogging:(NSInteger)responseCode {
if (responseCode == HTTPStatusCodeBadRequest || responseCode == HTTPStatusCodeUnauthorized || responseCode == HTTPStatusCodeForbidden) {
[MPStateMachine setCanWriteMessagesToDB:NO];
MPILogError(@"API Key appears to be invalid based on server response, disabling event logging to prevent excessive local database growth");
} else {
[MPStateMachine setCanWriteMessagesToDB:YES];
}
}

#pragma mark Public methods
- (NSObject<MPConnectorProtocol> *_Nonnull)makeConnector {
if (MPNetworkCommunication.connectorFactory) {
Expand Down Expand Up @@ -463,9 +454,7 @@ - (void)requestConfig:(nullable NSObject<MPConnectorProtocol> *)connector withCo

NSInteger responseCode = [httpResponse statusCode];
MPILogVerbose(@"Config Response Code: %ld, Execution Time: %.2fms", (long)responseCode, ([[NSDate date] timeIntervalSince1970] - start) * 1000.0);

[self checkResponseCodeToDisableEventLogging:responseCode];


if (responseCode == HTTPStatusCodeNotModified) {
MPIUserDefaults *userDefaults = [MPIUserDefaults standardUserDefaults];
[userDefaults setConfiguration:[userDefaults getConfiguration] eTag:userDefaults[kMPHTTPETagHeaderKey] requestTimestamp:[[NSDate date] timeIntervalSince1970] currentAge:ageString maxAge:maxAge];
Expand Down Expand Up @@ -875,9 +864,7 @@ - (void)identityApiRequestWithURL:(NSURL*)url identityRequest:(MPIdentityHTTPBas


MPILogVerbose(@"Identity response code: %ld", (long)responseCode);

[self checkResponseCodeToDisableEventLogging:[httpResponse statusCode]];


if (success) {
@try {
NSError *serializationError = nil;
Expand Down
4 changes: 0 additions & 4 deletions mParticle-Apple-SDK/Persistence/MPPersistenceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1557,10 +1557,6 @@ - (void)saveIntegrationAttributes:(nonnull MPIntegrationAttributes *)integration
}

- (void)saveMessage:(MPMessage *)message {
if (![MPStateMachine canWriteMessagesToDB]) {
MPILogError(@"Not saving message for event to prevent excessive local database growth because API Key appears to be invalid based on server response");
return;
}
if (!message.shouldUploadEvent) {
MPILogDebug(@"Not saving message for event because shouldUploadEvent was set to NO, message id: %lld, type: %@", message.messageId, message.messageType);
return;
Expand Down
2 changes: 0 additions & 2 deletions mParticle-Apple-SDK/Utils/MPStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@
+ (BOOL)runningInBackground;
+ (void)setRunningInBackground:(BOOL)background;
+ (BOOL)isAppExtension;
+ (BOOL)canWriteMessagesToDB;
+ (void)setCanWriteMessagesToDB:(BOOL)canWriteMessagesToDB;
- (void)configureCustomModules:(nullable NSArray<NSDictionary *> *)customModuleSettings;
- (void)configureRampPercentage:(nullable NSNumber *)rampPercentage;
- (void)configureTriggers:(nullable NSDictionary *)triggerDictionary;
Expand Down
13 changes: 0 additions & 13 deletions mParticle-Apple-SDK/Utils/MPStateMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

static MPEnvironment runningEnvironment = MPEnvironmentAutoDetect;
static BOOL runningInBackground = NO;
static BOOL _canWriteMessagesToDB = YES;

@interface MParticle ()
+ (dispatch_queue_t)messageQueue;
Expand Down Expand Up @@ -348,18 +347,6 @@ + (BOOL)isAppExtension {
#endif
}

+ (BOOL)canWriteMessagesToDB {
@synchronized(self) {
return _canWriteMessagesToDB;
}
}

+ (void)setCanWriteMessagesToDB:(BOOL)canWriteMessagesToDB {
@synchronized(self) {
_canWriteMessagesToDB = canWriteMessagesToDB;
}
}

#pragma mark Public accessors
- (MPConsumerInfo *)consumerInfo {
if (_consumerInfo) {
Expand Down

0 comments on commit 7b98acc

Please sign in to comment.