Skip to content

Commit

Permalink
Merge pull request #445 from libgit2/safer-signature
Browse files Browse the repository at this point in the history
Safe signature
  • Loading branch information
jspahrsummers committed Mar 3, 2015
2 parents 5a911e3 + a712556 commit 3f617c5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
33 changes: 21 additions & 12 deletions ObjectiveGit/GTRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -718,24 +718,33 @@ - (GTSubmodule *)submoduleWithName:(NSString *)name error:(NSError **)error {

#pragma mark User

+ (NSString *)defaultUserName {
NSString *name = NSFullUserName();
if (name.length == 0) name = NSUserName();
if (name.length == 0) name = @"nobody";
return name;
}

+ (NSString *)defaultEmail {
NSString *username = NSUserName();
if (username.length == 0) username = @"nobody";
NSString *domain = NSProcessInfo.processInfo.hostName ?: @"nowhere.local";
return [NSString stringWithFormat:@"%@@%@", username, domain];
}

- (GTSignature *)userSignatureForNow {
GTConfiguration *configuration = [self configurationWithError:NULL];
NSString *name = [configuration stringForKey:@"user.name"];
if (name.length == 0) {
name = NSFullUserName();
if (name.length == 0) name = NSUserName();
if (name.length == 0) name = @"nobody";
}
if (name.length == 0) name = self.class.defaultUserName;

NSString *email = [configuration stringForKey:@"user.email"];
if (email == nil) {
NSString *username = NSUserName();
if (username.length == 0) username = @"nobody";
NSString *domain = NSProcessInfo.processInfo.hostName ?: @"nowhere.local";
email = [NSString stringWithFormat:@"%@@%@", username, domain];
}
if (email.length == 0) email = self.class.defaultEmail;

NSDate *now = [NSDate date];
GTSignature *signature = [[GTSignature alloc] initWithName:name email:email time:now];
if (signature != nil) return signature;

return [[GTSignature alloc] initWithName:name email:email time:[NSDate date]];
return [[GTSignature alloc] initWithName:self.class.defaultUserName email:self.class.defaultEmail time:now];
}

#pragma mark Tagging
Expand Down
50 changes: 50 additions & 0 deletions ObjectiveGitTests/GTRepositorySpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,56 @@
});
});

describe(@"-userSignatureForNow", ^{
static NSString * const userName = @"johnsmith";
static NSString * const email = @"[email protected]";

__block GTConfiguration *configuration;

beforeEach(^{
configuration = [repository configurationWithError:NULL];
expect(configuration).notTo(beNil());
});

it(@"should use the values from the config", ^{
[configuration setString:userName forKey:@"user.name"];
[configuration setString:email forKey:@"user.email"];

GTSignature *signature = [repository userSignatureForNow];
expect(signature.name).to(equal(userName));
expect(signature.email).to(equal(email));
});

describe(@"invalid values", ^{
it(@"should use a default value if the name is empty", ^{
[configuration setString:@"" forKey:@"user.name"];
[configuration setString:email forKey:@"user.email"];

GTSignature *signature = [repository userSignatureForNow];
expect(@(signature.name.length)).to(beGreaterThan(@0));
expect(@(signature.email.length)).to(beGreaterThan(@0));
});

it(@"should use a default value if the email is empty", ^{
[configuration setString:userName forKey:@"user.name"];
[configuration setString:@"" forKey:@"user.email"];

GTSignature *signature = [repository userSignatureForNow];
expect(@(signature.name.length)).to(beGreaterThan(@0));
expect(@(signature.email.length)).to(beGreaterThan(@0));
});

it(@"should use a default value if the email contains angled brackets", ^{
[configuration setString:userName forKey:@"user.name"];
[configuration setString:@"<[email protected]>" forKey:@"user.email"];

GTSignature *signature = [repository userSignatureForNow];
expect(@(signature.name.length)).to(beGreaterThan(@0));
expect(@(signature.email.length)).to(beGreaterThan(@0));
});
});
});

afterEach(^{
[self tearDown];
});
Expand Down

0 comments on commit 3f617c5

Please sign in to comment.