Skip to content

Commit e04b6ba

Browse files
FIRApp - validate app name using NSCharacterSet (#2906)
1 parent e6854a8 commit e04b6ba

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Example/Core/Tests/FIRAppTest.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ - (void)testConfigureWithMultipleApps {
208208

209209
- (void)testValidName {
210210
XCTAssertNoThrow([FIRApp configureWithName:@"aA1_" options:[FIROptions defaultOptions]]);
211+
XCTAssertNoThrow([FIRApp configureWithName:@"aA1-" options:[FIROptions defaultOptions]]);
212+
XCTAssertNoThrow([FIRApp configureWithName:@"aAē1_" options:[FIROptions defaultOptions]]);
211213
XCTAssertThrows([FIRApp configureWithName:@"aA1%" options:[FIROptions defaultOptions]]);
212214
XCTAssertThrows([FIRApp configureWithName:@"aA1?" options:[FIROptions defaultOptions]]);
213215
XCTAssertThrows([FIRApp configureWithName:@"aA1!" options:[FIROptions defaultOptions]]);

Firebase/Core/FIRApp.m

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ + (void)configureWithOptions:(FIROptions *)options {
139139
[FIRApp configureWithName:kFIRDefaultAppName options:options];
140140
}
141141

142+
+ (NSCharacterSet *)applicationNameAllowedCharacters {
143+
static NSCharacterSet *applicationNameAllowedCharacters;
144+
static dispatch_once_t onceToken;
145+
dispatch_once(&onceToken, ^{
146+
NSMutableCharacterSet *allowedNameCharacters = [NSMutableCharacterSet alphanumericCharacterSet];
147+
[allowedNameCharacters addCharactersInString:@"-_"];
148+
applicationNameAllowedCharacters = [allowedNameCharacters copy];
149+
});
150+
return applicationNameAllowedCharacters;
151+
}
152+
142153
+ (void)configureWithName:(NSString *)name options:(FIROptions *)options {
143154
if (!name || !options) {
144155
[NSException raise:kFirebaseCoreErrorDomain format:@"Neither name nor options can be nil."];
@@ -156,14 +167,12 @@ + (void)configureWithName:(NSString *)name options:(FIROptions *)options {
156167
FIRLogDebug(kFIRLoggerCore, @"I-COR000001", @"Configuring the default app.");
157168
} else {
158169
// Validate the app name and ensure it hasn't been configured already.
159-
for (NSUInteger charIndex = 0; charIndex < name.length; charIndex++) {
160-
char character = [name characterAtIndex:charIndex];
161-
if (!((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') ||
162-
(character >= '0' && character <= '9') || character == '_' || character == '-')) {
163-
[NSException raise:kFirebaseCoreErrorDomain
164-
format:@"App name can only contain alphanumeric (A-Z,a-z,0-9), "
165-
@"hyphen (-), and underscore (_) characters"];
166-
}
170+
NSCharacterSet *nameCharacters = [NSCharacterSet characterSetWithCharactersInString:name];
171+
172+
if (![[self applicationNameAllowedCharacters] isSupersetOfSet:nameCharacters]) {
173+
[NSException raise:kFirebaseCoreErrorDomain
174+
format:@"App name can only contain alphanumeric, "
175+
@"hyphen (-), and underscore (_) characters"];
167176
}
168177

169178
@synchronized(self) {

0 commit comments

Comments
 (0)