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

Fix duplicated query string parameters for VSO/AzDo provider #814

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -91,12 +92,16 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
var challengeUrl = base.BuildChallengeUrl(properties, redirectUri);

// Visual Studio Online/Azure DevOps uses "Assertion" instead of "code"
var challengeUri = new Uri(challengeUrl, UriKind.Absolute);
var challengeUri = new UriBuilder(challengeUrl);
var query = QueryHelpers.ParseQuery(challengeUri.Query);

query["response_type"] = "Assertion";

return QueryHelpers.AddQueryString(Options.AuthorizationEndpoint, query);
// Replace the query with the edit so that the parameters are not duplicated.
// See https://github.com/dotnet/aspnetcore/issues/47054 for more context.
challengeUri.Query = QueryString.Create(query).Value;

return challengeUri.Uri.AbsoluteUri;
}

private static partial class Log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ public async Task Can_Sign_In_Using_Visual_Studio(string claimType, string claim
=> await AuthenticateUserAndAssertClaimValue(claimType, claimValue);

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task BuildChallengeUrl_Generates_Correct_Url(bool usePkce)
[InlineData(false, "")]
[InlineData(true, "")]
[InlineData(false, "?foo=bar")]
[InlineData(true, "?foo=bar")]
public async Task BuildChallengeUrl_Generates_Correct_Url(
bool usePkce,
string authorizationEndpointSuffix)
{
// Arrange
var options = new VisualStudioAuthenticationOptions()
{
UsePkce = usePkce,
};

options.AuthorizationEndpoint += authorizationEndpointSuffix;

var redirectUrl = "https://my-site.local/signin-visualstudio";

// Act
Expand Down Expand Up @@ -66,5 +72,10 @@ public async Task BuildChallengeUrl_Generates_Correct_Url(bool usePkce)
query.ShouldNotContainKey(OAuthConstants.CodeChallengeKey);
query.ShouldNotContainKey(OAuthConstants.CodeChallengeMethodKey);
}

foreach (var parameter in query)
{
parameter.Value.Count.ShouldBe(1, $"Query string parameter {parameter.Key} appears more than once: {parameter.Value}.");
}
}
}