Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public ICollection<Uri> AuthenticatingAuthorities
/// describes the authentication context declaration that follows.
/// [Saml2Core, 2.7.2.2]
/// </summary>
/// <remarks>
/// The check for an absolute URI can be disabled by setting <see cref="AppContextSwitches.AllowRelativeUrisInSaml2AuthnContext"/>.
/// </remarks>
/// <exception cref="ArgumentNullException">if 'value' is null.</exception>
/// <exception cref="ArgumentException">if 'value' is not an absolute Uri.</exception>
public Uri ClassReference
Expand All @@ -78,7 +81,7 @@ public Uri ClassReference
if (value == null)
throw LogArgumentNullException(nameof(value));

if (!value.IsAbsoluteUri)
if (!value.IsAbsoluteUri && !AppContextSwitches.AllowRelativeUrisInSaml2AuthnContext)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, MarkAsNonPII(nameof(ClassReference)), value)));

_classReference = value;
Expand Down
11 changes: 11 additions & 0 deletions src/Microsoft.IdentityModel.Tokens/AppContextSwitches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ internal static class AppContextSwitches
private static bool? _useCapitalizedXMLTypeAttr;
internal static bool UseCapitalizedXMLTypeAttr => _useCapitalizedXMLTypeAttr ??= (AppContext.TryGetSwitch(UseCapitalizedXMLTypeAttrSwitch, out bool useCapitalizedXMLTypeAttr) && useCapitalizedXMLTypeAttr);

/// <summary>
/// When enabled, allows using relative URIs in SAML2 authentication context.
/// </summary>
internal const string AllowRelativeUrisInSaml2AuthnContextSwitch = "Switch.Microsoft.IdentityModel.AllowRelativeUrisInSaml2AuthnContext";
private static bool? _allowRelativeUrisInSaml2AuthnContext;
internal static bool AllowRelativeUrisInSaml2AuthnContext => _allowRelativeUrisInSaml2AuthnContext ??= (AppContext.TryGetSwitch(AllowRelativeUrisInSaml2AuthnContextSwitch, out bool allowRelativeUris) && allowRelativeUris);


/// <summary>
/// Used for testing to reset all switches to its default value.
/// </summary>
Expand All @@ -123,6 +131,9 @@ internal static void ResetAllSwitches()

_useCapitalizedXMLTypeAttr = null;
AppContext.SetSwitch(UseCapitalizedXMLTypeAttrSwitch, false);

_allowRelativeUrisInSaml2AuthnContext = null;
AppContext.SetSwitch(AllowRelativeUrisInSaml2AuthnContextSwitch, false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Xunit;

namespace Microsoft.IdentityModel.Tokens.Saml.Tests
{
[CollectionDefinition("Saml2AuthenticationContextTests", DisableParallelization = true)]
public class Saml2AuthenticationContextCollectionDefinition
{
// This class is used to define a collection for the Saml2AuthenticationContext tests.
// It allows the tests to run sequentially and share setup/teardown logic if needed.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Microsoft.IdentityModel.Tokens.Saml.Tests
{
[Collection("Saml2AuthenticationContextTests")]
public class Saml2AuthenticationContextTests
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Xunit;
using Microsoft.IdentityModel.Tokens.Saml2;

namespace Microsoft.IdentityModel.Tokens.Saml.Tests
{
[Collection("Saml2AuthenticationContextTests")]
public class Saml2AuthenticationContextWithAppContextTests
{
[Fact]
public void Saml2AuthenticationContext_RelativeClassReference_AllowRelativeUris_NoException()
{
try
{
var classRef = new Uri("resource", UriKind.Relative);
AppContext.SetSwitch(AppContextSwitches.AllowRelativeUrisInSaml2AuthnContextSwitch, true);
var authContext = new Saml2AuthenticationContext
{
ClassReference = classRef
};
Assert.Equal(classRef, authContext.ClassReference);
}
finally
{
AppContextSwitches.ResetAllSwitches();
}
}
}
}
Loading