Skip to content
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

Harden ValidateSignature against invalid input #2468

Merged
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
16 changes: 14 additions & 2 deletions src/Stripe.net/Services/Events/EventUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,22 @@ public static void ValidateSignature(string json, string stripeSignatureHeader,

private static ILookup<string, string> ParseStripeSignature(string stripeSignatureHeader)
{
(string Key, string Value) ParseItem(string item)
{
string[] parts = item.Trim().Split(new[] { '=' }, 2);
kamil-stripe marked this conversation as resolved.
Show resolved Hide resolved
if (parts.Length != 2)
{
throw new StripeException(
"The signature header format is unexpected.");
}

return (parts[0], parts[1]);
}

return stripeSignatureHeader.Trim()
.Split(',')
.Select(item => item.Trim().Split(new[] { '=' }, 2))
.ToLookup(item => item[0], item => item[1]);
.Select(item => ParseItem(item))
.ToLookup(item => item.Key, item => item.Value);
}

private static bool IsSignaturePresent(string signature, IEnumerable<string> signatures)
Expand Down
14 changes: 14 additions & 0 deletions src/StripeTests/Services/Events/EventUtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,19 @@ public void CanDisableThrowOnApiVersionMismatch()
var evt = EventUtility.ParseEvent(this.json, false);
Assert.Equal("2017-05-25", evt.ApiVersion);
}

[Theory]
[InlineData("t=,v1=2220f87ef101a04665f11cdf770523143f875572008577fa0f20882ddb9cc3c7,v0=63f3a72374a733066c4be69ed7f8e5ac85c22c9f0a6a612ab9a025a9e4ee7eef")]
[InlineData("t,v1=,v0=")]
[InlineData("t,v1=,v0")]
[InlineData("t,v1=,")]
[InlineData("t,,")]
[InlineData(",,")]
[InlineData("t")]
public void ValidateSignatureHandlesIncorrectHeaderValues(string headerValue)
{
Assert.Throws<StripeException>(() =>
EventUtility.ValidateSignature("{}", headerValue, string.Empty));
}
}
}