-
Notifications
You must be signed in to change notification settings - Fork 130
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
Enforce requiring API key to initialise notifier #280
Conversation
Alter the API key annotation to Nonnull rather than Nullable. If the API key is set as nil, the value is ignored and a warning logged.
Gates all methods in Bugsnag.m so that they no-op and log a warning if the notifier has not been initialised. Also avoids initialising the notifier if the API key is not supplied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to have some tests for the behaviour.
Source/Bugsnag.m
Outdated
bsg_g_bugsnag_notifier = | ||
[[BugsnagNotifier alloc] initWithConfiguration:configuration]; | ||
[bsg_g_bugsnag_notifier start]; | ||
if (configuration != nil && configuration.apiKey != nil) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be nicer to have a configuration based validation method. [configuration isValid]
for example, rather than the Bugsnag class having to know what that means.
By tests I mean, that |
Added a test to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a couple of comments, a larger question I have is: it looks like the bugsnagStarted
method only does a nil check on the notifier and so it is possible that the notifier won't be started as it is assigned to the variable before being started, does this matter?
Source/BugsnagConfiguration.m
Outdated
@@ -231,4 +246,9 @@ - (NSDictionary *)sessionApiHeaders { | |||
kHeaderApiSentAt: [BSG_RFC3339DateTool stringFromDate:[NSDate new]] | |||
}; | |||
} | |||
|
|||
- (BOOL)hasValidApiKey { | |||
return _apiKey != nil && ![@"" isEqualToString:_apiKey]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we just use return [_apiKey length] == 0;
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flipped the boolean but yeah, [_apiKey length] > 0
is the ticket here.
|
||
- (NSString *)apiKey { | ||
return _apiKey; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we remove this as it is just the default generated by @synthesize
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it needs to be specified since setApiKey
is overridden.
Source/BugsnagConfiguration.h
Outdated
@@ -69,7 +69,7 @@ typedef NSDictionary *_Nullable (^BugsnagBeforeNotifyHook)( | |||
/** | |||
* The API key of a Bugsnag project | |||
*/ | |||
@property(readwrite, retain, nullable) NSString *apiKey; | |||
@property(readwrite, retain, nonnull) NSString *apiKey; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a breaking change? Do we need to increment the major version number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change in Swift at least, as anyone fetching the value would need to update their code with the appropriate number of ?
and !
. Its also not enforced, as BugsnagConfiguration().apiKey
could be nil.
We should not make this change just yet and instead design/think more about the interface, such as automatic API key detection from Info.plist
or having a recommended constructor which takes an non-null API key as an argument. This change is also not required for this fix.
Source/BugsnagConfiguration.m
Outdated
} | ||
|
||
- (void)setApiKey:(NSString *)apiKey { | ||
if (apiKey != nil) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (apiKey.length > 0)
would be more comprehensive here and catch both the nil and the empty string case.
Source/BugsnagConfiguration.m
Outdated
@@ -231,4 +246,9 @@ - (NSDictionary *)sessionApiHeaders { | |||
kHeaderApiSentAt: [BSG_RFC3339DateTool stringFromDate:[NSDate new]] | |||
}; | |||
} | |||
|
|||
- (BOOL)hasValidApiKey { | |||
return _apiKey != nil && ![@"" isEqualToString:_apiKey]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flipped the boolean but yeah, [_apiKey length] > 0
is the ticket here.
XCTAssertFalse([config hasValidApiKey]); | ||
|
||
config.apiKey = @"5adf89e0aaa"; | ||
XCTAssertTrue([config hasValidApiKey]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config.apiKey = ""
should also be false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's covered by L122, as config defaults to @""
@martin308 good catch with I have addressed all other inline feedback. |
@@ -161,7 +177,7 @@ + (void)clearTabWithName:(NSString *)tabName { | |||
} | |||
|
|||
+ (BOOL)bugsnagStarted { | |||
if (self.notifier == nil) { | |||
if (!self.notifier.started) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this handle the notifier being nil
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, as nil will be coerced to false.
Since we already had it through synthesized properties, keeps it in case its used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The merge invalidated the last review due to conflicts. I'm approving the merge conflict resolution here, not the code changes 👍
Goal
Avoid instantiating the Notifier in an invalid state (without an API key).
Changeset
Changed
Bugsnag.m
with a check to see if the notifier has been initialised, and NOP if not.Tests
Added a unit test for API key setter override.
Discussion
Much of the discussion for this changeset took place here: bugsnag/bugsnag-react-native#237
Review
For the submitter, initial self-review:
For the pull request reviewer(s), this changeset has been reviewed for: