Skip to content

Commit

Permalink
Bugfix validate https redirect url (#915)
Browse files Browse the repository at this point in the history
* verify result in the response

* use string startswith instead of regexp to validate redirecturl

* built in method

* rewrite

* fix

* removed unused reqexp
  • Loading branch information
simen-rekkedal authored Nov 29, 2024
1 parent e269bfb commit 2badac0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Authentication/Helpers/AuthenticationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Security.Policy;
using System.Text.RegularExpressions;
using Altinn.Platform.Authentication.Core.Constants;
using Altinn.Platform.Authentication.Core.Models;
Expand Down Expand Up @@ -306,19 +307,21 @@ public static bool DoesSystemIdStartWithOrgnumber(RegisteredSystem newSystem)
/// <param name="redirectUrls">the redirect url for a system</param>
/// <returns>true if the url matches the expression</returns>
public static bool IsValidRedirectUrl(List<Uri> redirectUrls)
{
string pattern = @"^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$";
Regex expression = new Regex(pattern, RegexOptions.Compiled, TimeSpan.FromSeconds(1));

{
foreach (Uri redirectUri in redirectUrls)
{
if (!redirectUri.IsWellFormedOriginalString() || !expression.IsMatch(redirectUri.OriginalString))
if (!IsValidAbsoluteUriWithHttps(redirectUri))
{
return false;
}
}

return true;

static bool IsValidAbsoluteUriWithHttps(Uri uri)
{
return uri.IsAbsoluteUri && uri.Scheme == Uri.UriSchemeHttps;
}
}

/// <summary>
Expand Down

0 comments on commit 2badac0

Please sign in to comment.