Skip to content

FIRApp - validate app name using NSCharacterSet #2906

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

Merged
merged 2 commits into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Example/Core/Tests/FIRAppTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ - (void)testConfigureWithMultipleApps {

- (void)testValidName {
XCTAssertNoThrow([FIRApp configureWithName:@"aA1_" options:[FIROptions defaultOptions]]);
XCTAssertNoThrow([FIRApp configureWithName:@"aA1-" options:[FIROptions defaultOptions]]);
XCTAssertNoThrow([FIRApp configureWithName:@"aAē1_" options:[FIROptions defaultOptions]]);
XCTAssertThrows([FIRApp configureWithName:@"aA1%" options:[FIROptions defaultOptions]]);
XCTAssertThrows([FIRApp configureWithName:@"aA1?" options:[FIROptions defaultOptions]]);
XCTAssertThrows([FIRApp configureWithName:@"aA1!" options:[FIROptions defaultOptions]]);
Expand Down
25 changes: 17 additions & 8 deletions Firebase/Core/FIRApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ + (void)configureWithOptions:(FIROptions *)options {
[FIRApp configureWithName:kFIRDefaultAppName options:options];
}

+ (NSCharacterSet *)applicationNameAllowedCharacters {
static NSCharacterSet *applicationNameAllowedCharacters;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSMutableCharacterSet *allowedNameCharacters = [NSMutableCharacterSet alphanumericCharacterSet];
[allowedNameCharacters addCharactersInString:@"-_"];
applicationNameAllowedCharacters = [allowedNameCharacters copy];
});
return applicationNameAllowedCharacters;
}

+ (void)configureWithName:(NSString *)name options:(FIROptions *)options {
if (!name || !options) {
[NSException raise:kFirebaseCoreErrorDomain format:@"Neither name nor options can be nil."];
Expand All @@ -156,14 +167,12 @@ + (void)configureWithName:(NSString *)name options:(FIROptions *)options {
FIRLogDebug(kFIRLoggerCore, @"I-COR000001", @"Configuring the default app.");
} else {
// Validate the app name and ensure it hasn't been configured already.
for (NSUInteger charIndex = 0; charIndex < name.length; charIndex++) {
char character = [name characterAtIndex:charIndex];
if (!((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') ||
(character >= '0' && character <= '9') || character == '_' || character == '-')) {
[NSException raise:kFirebaseCoreErrorDomain
format:@"App name can only contain alphanumeric (A-Z,a-z,0-9), "
@"hyphen (-), and underscore (_) characters"];
}
NSCharacterSet *nameCharacters = [NSCharacterSet characterSetWithCharactersInString:name];

if (![[self applicationNameAllowedCharacters] isSupersetOfSet:nameCharacters]) {
[NSException raise:kFirebaseCoreErrorDomain
format:@"App name can only contain alphanumeric, "
@"hyphen (-), and underscore (_) characters"];
}

@synchronized(self) {
Expand Down