Skip to content

Commit 374f2a5

Browse files
committed
Protect Signature creation from components containing '\0'
1 parent 8528d4c commit 374f2a5

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
</ItemGroup>
6060
<ItemGroup>
6161
<Compile Include="CheckoutFixture.cs" />
62+
<Compile Include="SignatureFixture.cs" />
6263
<Compile Include="FilterBranchFixture.cs" />
6364
<Compile Include="RemoveFixture.cs" />
6465
<Compile Include="RemoteFixture.cs" />
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using LibGit2Sharp.Tests.TestHelpers;
3+
using Xunit;
4+
using Xunit.Extensions;
5+
6+
namespace LibGit2Sharp.Tests
7+
{
8+
public class SignatureFixture : BaseFixture
9+
{
10+
[Theory]
11+
[InlineData("\0Leading zero")]
12+
[InlineData("Trailing zero\0")]
13+
[InlineData("Zero \0inside")]
14+
[InlineData("\0")]
15+
[InlineData("\0\0\0")]
16+
public void CreatingASignatureWithANameContainingZerosThrows(string name)
17+
{
18+
Assert.Throws<ArgumentException>(() => new Signature(name, "[email protected]", DateTimeOffset.Now));
19+
}
20+
21+
[Theory]
22+
[InlineData("\0[email protected]")]
23+
[InlineData("[email protected]\0")]
24+
[InlineData("Zero@\0inside.com")]
25+
[InlineData("\0")]
26+
[InlineData("\0\0\0")]
27+
public void CreatingASignatureWithAnEmailContainingZerosThrows(string email)
28+
{
29+
Assert.Throws<ArgumentException>(() => new Signature("Me", email, DateTimeOffset.Now));
30+
}
31+
}
32+
}

LibGit2Sharp/Core/Ensure.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ public static void ArgumentNotNullOrEmptyString(string argumentValue, string arg
3939
}
4040
}
4141

42+
/// <summary>
43+
/// Checks a string argument to ensure it doesn't contain a zero byte.
44+
/// </summary>
45+
/// <param name="argumentValue">The argument value to check.</param>
46+
/// <param name="argumentName">The name of the argument.</param>
47+
public static void ArgumentDoesNotContainZeroByte(string argumentValue, string argumentName)
48+
{
49+
if (string.IsNullOrEmpty(argumentValue))
50+
{
51+
return;
52+
}
53+
54+
int zeroPos = -1;
55+
for (var i = 0; i < argumentValue.Length; i++)
56+
{
57+
if (argumentValue[i] == '\0')
58+
{
59+
zeroPos = i;
60+
break;
61+
}
62+
}
63+
64+
if (zeroPos == -1)
65+
{
66+
return;
67+
}
68+
69+
throw new ArgumentException(
70+
string.Format("Zero bytes ('\\0') are not allowed. A zero byte has been found at position {0}.", zeroPos), argumentName);
71+
}
72+
4273
private static readonly Dictionary<GitErrorCode, Func<string, GitErrorCode, GitErrorCategory, LibGit2SharpException>>
4374
GitErrorsToLibGit2SharpExceptions =
4475
new Dictionary<GitErrorCode, Func<string, GitErrorCode, GitErrorCategory, LibGit2SharpException>>

LibGit2Sharp/Signature.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ internal Signature(IntPtr signaturePtr)
3535
/// <param name="when">The when.</param>
3636
public Signature(string name, string email, DateTimeOffset when)
3737
{
38+
Ensure.ArgumentDoesNotContainZeroByte(name, "name");
39+
Ensure.ArgumentDoesNotContainZeroByte(email, "email");
40+
3841
this.name = name;
3942
this.email = email;
4043
this.when = when;

0 commit comments

Comments
 (0)