Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevents POST requests when the JSON body couldn't be correctly formed #415

Merged
merged 8 commits into from Dec 17, 2020

Conversation

aboedo
Copy link
Member

@aboedo aboedo commented Dec 4, 2020

We're consistently getting a few POST requests on the backend which have empty body.

Thing is, we never create requests with nil bodies intentionally, so after looking at the code it seems like this might happen if the body dictionary couldn't be parsed into JSON.
It doesn't make sense to still hit the backend if the body of the request couldn't be correctly formed, so this does an early exit before that happens.
Also, when you hit the backend unnecessarily, a panda dies. Don't kill pandas.

@aboedo aboedo self-assigned this Dec 4, 2020
NSParameterAssert(([HTTPMethod isEqualToString:@"GET"] || [HTTPMethod isEqualToString:@"POST"]));
NSParameterAssert(!([HTTPMethod isEqualToString:@"GET"] && requestBody));
NSParameterAssert(!([HTTPMethod isEqualToString:@"POST"] && !requestBody));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a new check, but it looks like we don't have any places where we make POST requests without a body, and I can't think of a reason why you might want to.

Comment on lines 182 to 189
RCLog(@"Error creating request JSON: %@", requestBody);
RCErrorLog(@"Error creating request JSON: %@", requestBody);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a clear case for an error log to me

@@ -179,16 +186,18 @@ - (NSMutableURLRequest *)createRequestWithMethod:(NSString *)HTTPMethod
NSError *JSONParseError;
NSData *body = [NSJSONSerialization dataWithJSONObject:requestBody options:0 error:&JSONParseError];
if (JSONParseError) {
RCLog(@"Error creating request JSON: %@", requestBody);
RCErrorLog(@"Error creating request JSON: %@", requestBody);
return nil;
Copy link
Member Author

@aboedo aboedo Dec 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new behavior: if we can't create a JSON request, don't create the request at all, just call completion with an error

Comment on lines 92 to 97
if (!urlRequest) {
RCErrorLog(@"Could not create request to %@ with body %@", path, requestBody);
completionHandler(-1,
nil,
[RCPurchasesErrorUtils networkErrorWithUnderlyingError:RCPurchasesErrorUtils.unknownError]);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there might be a nicer-looking way of doing this. But this is the essence of the PR

@aboedo aboedo marked this pull request as ready for review December 7, 2020 17:04
Comment on lines -82 to -90
[self assertIsValidRequestWithMethod:httpMethod body:requestBody];

NSMutableDictionary *defaultHeaders = self.defaultHeaders.mutableCopy;
[defaultHeaders addEntriesFromDictionary:headers];

NSMutableURLRequest *urlRequest = [self createRequestWithMethod:httpMethod
path:path
requestBody:requestBody
headers:defaultHeaders];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved some of these checks to happen earlier in the flow, since if they fail then the method doesn't actually do anything

@aboedo aboedo requested a review from vegaro December 7, 2020 17:04
@aboedo aboedo added this to the 3.9.2 milestone Dec 7, 2020
Copy link
Contributor

@vegaro vegaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more killed pandas

@aboedo aboedo merged commit a3c4d6d into develop Dec 17, 2020
@aboedo aboedo deleted the fix/posts_with_empty_body branch December 17, 2020 14:07
@aboedo aboedo mentioned this pull request Dec 17, 2020
@aboedo aboedo mentioned this pull request Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants