diff --git a/Example/Core/Tests/FIRAppTest.m b/Example/Core/Tests/FIRAppTest.m index 1a273e37f01..a72fa1a2171 100644 --- a/Example/Core/Tests/FIRAppTest.m +++ b/Example/Core/Tests/FIRAppTest.m @@ -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]]); diff --git a/Firebase/Core/FIRApp.m b/Firebase/Core/FIRApp.m index d3b6c460ece..7d17127f496 100644 --- a/Firebase/Core/FIRApp.m +++ b/Firebase/Core/FIRApp.m @@ -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."]; @@ -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) {