Skip to content

Commit bc00133

Browse files
Ssathe/fix casting for xml attr type (#3217)
* Tried a fix in XMLSignatureConsant.cs so that we dont get type instead of Type. * Identified places to change and created app context switches * Added default switch value * Updated the test Signature Values to be returned as per the switch value * Reverted the changes made to XMLSignatureConstants and PublicApis * Updated the tests to consider the switch value now * Added new test case to test Signature with capitalized Type * Reverted changes made to type in RoleDescriptorTemplate * NIT repairs as per the comments
1 parent d912369 commit bc00133

File tree

9 files changed

+126
-11
lines changed

9 files changed

+126
-11
lines changed

src/Microsoft.IdentityModel.Tokens/AppContextSwitches.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ internal static class AppContextSwitches
9191
private static bool? _doNotScrubExceptions;
9292
internal static bool DoNotScrubExceptions => _doNotScrubExceptions ??= (AppContext.TryGetSwitch(DoNotScrubExceptionsSwitch, out bool doNotScrubExceptions) && doNotScrubExceptions);
9393

94+
/// <summary>
95+
/// When enabled, the XML type attribute will be capitalized (XML) for saml configurations.
96+
/// </summary>
97+
internal const string UseCapitalizedXMLTypeAttrSwitch = "Switch.Microsoft.IdentityModel.UseCapitalizedXMLTypeAttr";
98+
private static bool? _useCapitalizedXMLTypeAttr;
99+
internal static bool UseCapitalizedXMLTypeAttr => _useCapitalizedXMLTypeAttr ??= (AppContext.TryGetSwitch(UseCapitalizedXMLTypeAttrSwitch, out bool useCapitalizedXMLTypeAttr) && useCapitalizedXMLTypeAttr);
100+
94101
/// <summary>
95102
/// Used for testing to reset all switches to its default value.
96103
/// </summary>
@@ -113,6 +120,9 @@ internal static void ResetAllSwitches()
113120

114121
_doNotScrubExceptions = null;
115122
AppContext.SetSwitch(DoNotScrubExceptionsSwitch, false);
123+
124+
_useCapitalizedXMLTypeAttr = null;
125+
AppContext.SetSwitch(UseCapitalizedXMLTypeAttrSwitch, false);
116126
}
117127
}
118128
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const Microsoft.IdentityModel.Tokens.AppContextSwitches.UseCapitalizedXMLTypeAttrSwitch = "Switch.Microsoft.IdentityModel.UseCapitalizedXMLTypeAttr" -> string
2+
static Microsoft.IdentityModel.Tokens.AppContextSwitches.UseCapitalizedXMLTypeAttr.get -> bool

src/Microsoft.IdentityModel.Xml/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const Microsoft.IdentityModel.Xml.XmlSignatureConstants.Attributes.Id = "Id" ->
88
const Microsoft.IdentityModel.Xml.XmlSignatureConstants.Attributes.NcName = "NCName" -> string
99
const Microsoft.IdentityModel.Xml.XmlSignatureConstants.Attributes.Nil = "nil" -> string
1010
const Microsoft.IdentityModel.Xml.XmlSignatureConstants.Attributes.PrefixList = "PrefixList" -> string
11-
const Microsoft.IdentityModel.Xml.XmlSignatureConstants.Attributes.Type = "type" -> string
1211
const Microsoft.IdentityModel.Xml.XmlSignatureConstants.Attributes.URI = "URI" -> string
1312
const Microsoft.IdentityModel.Xml.XmlSignatureConstants.Elements.CanonicalizationMethod = "CanonicalizationMethod" -> string
1413
const Microsoft.IdentityModel.Xml.XmlSignatureConstants.Elements.DigestMethod = "DigestMethod" -> string
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
static Microsoft.IdentityModel.Xml.XmlSignatureConstants.Attributes.Type.get -> string

src/Microsoft.IdentityModel.Xml/XmlSignatureConstants.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using Microsoft.IdentityModel.Tokens;
5+
46
namespace Microsoft.IdentityModel.Xml
57
{
68
/// <summary>
@@ -31,7 +33,11 @@ public static class Attributes
3133
public const string NcName = "NCName";
3234
public const string Nil = "nil";
3335
public const string PrefixList = "PrefixList";
34-
public const string Type = "type";
36+
// Change the non-constant field `Type` to a property to resolve CA2211 diagnostic.
37+
public static string Type
38+
{
39+
get => AppContextSwitches.UseCapitalizedXMLTypeAttr ? "Type" : "type";
40+
}
3541
public const string URI = "URI";
3642
}
3743

test/Microsoft.IdentityModel.TestUtils/Default.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ public static Signature Signature
10211021
{
10221022
KeyInfo = KeyInfo,
10231023
SignedInfo = SignedInfo,
1024-
SignatureValue = "kzGIa0ZwE1Y7CYZ3hZHdFLGEQ6LvTdoKYSr+jClEdoL8l0bRf0Mkp7zsp0uCPyoZHKVBatU7otEmbciu9FWNMSXmpiDj9eSL/eNqpJ0sRkaNPyM3AqR2zy7TG2481K4SWZfo5EahrSat0glEUC6i3sxojjLb8DRq8ETYO1JsNhLOHQjKWlBEBZ04rAcz/kWXt0N1CQne4+GozQtiaMDvN/PXeqwiEYHbS1Gr5G16wHdiFZNYylH2pW14+t5t/eIZX8c/VJNT5uM09KHeBSMEn7Uksp2qx1brKP1K9SULzke0Pgx+lIJZgVndGbviGd5UP4ufovexs4F5TkhI7Pel6A=="
1024+
SignatureValue = AppContextSwitches.UseCapitalizedXMLTypeAttr ? "OaTq3jGqbPLUVROvhiqV+PneMwdu6iZgVv7vbW++wEk4tSXoqEUkY+b/M2ZzHFy0M/k33migp3s0w+Ff1vNHRI0uT8Zs1D+EdI/Oz4Pu3FwPA/UK+8qe+JTRAOhdN5H7Wv4c0p1nrWJlVlT5WWCUe2uRSpojS2+D+KC1gG/DiDqK5gWgQt/7Z0HV8ml6C0PTqXWvZcYc1u49Y3tNEPOUuSXGzSZOAfhEAMdQ6+qC+126wcbSFK5ww1aOI2K6Nk3u8sxJUXHdUXs92DKvLemcaHXw0yDNUNi/izVldy3yu6VEDEflCJkj1+yvB52U+EpvG/7IGwY66QceVbu/1FFLFA==" : "kzGIa0ZwE1Y7CYZ3hZHdFLGEQ6LvTdoKYSr+jClEdoL8l0bRf0Mkp7zsp0uCPyoZHKVBatU7otEmbciu9FWNMSXmpiDj9eSL/eNqpJ0sRkaNPyM3AqR2zy7TG2481K4SWZfo5EahrSat0glEUC6i3sxojjLb8DRq8ETYO1JsNhLOHQjKWlBEBZ04rAcz/kWXt0N1CQne4+GozQtiaMDvN/PXeqwiEYHbS1Gr5G16wHdiFZNYylH2pW14+t5t/eIZX8c/VJNT5uM09KHeBSMEn7Uksp2qx1brKP1K9SULzke0Pgx+lIJZgVndGbviGd5UP4ufovexs4F5TkhI7Pel6A=="
10251025
};
10261026
return signature;
10271027
}
@@ -1035,7 +1035,7 @@ public static Signature SignatureReferenceWithoutPrefix
10351035
{
10361036
KeyInfo = KeyInfo,
10371037
SignedInfo = SignedInfoReferenceWithoutPrefix,
1038-
SignatureValue = "OaTq3jGqbPLUVROvhiqV+PneMwdu6iZgVv7vbW++wEk4tSXoqEUkY+b/M2ZzHFy0M/k33migp3s0w+Ff1vNHRI0uT8Zs1D+EdI/Oz4Pu3FwPA/UK+8qe+JTRAOhdN5H7Wv4c0p1nrWJlVlT5WWCUe2uRSpojS2+D+KC1gG/DiDqK5gWgQt/7Z0HV8ml6C0PTqXWvZcYc1u49Y3tNEPOUuSXGzSZOAfhEAMdQ6+qC+126wcbSFK5ww1aOI2K6Nk3u8sxJUXHdUXs92DKvLemcaHXw0yDNUNi/izVldy3yu6VEDEflCJkj1+yvB52U+EpvG/7IGwY66QceVbu/1FFLFA=="
1038+
SignatureValue = AppContextSwitches.UseCapitalizedXMLTypeAttr ? "OaTq3jGqbPLUVROvhiqV+PneMwdu6iZgVv7vbW++wEk4tSXoqEUkY+b/M2ZzHFy0M/k33migp3s0w+Ff1vNHRI0uT8Zs1D+EdI/Oz4Pu3FwPA/UK+8qe+JTRAOhdN5H7Wv4c0p1nrWJlVlT5WWCUe2uRSpojS2+D+KC1gG/DiDqK5gWgQt/7Z0HV8ml6C0PTqXWvZcYc1u49Y3tNEPOUuSXGzSZOAfhEAMdQ6+qC+126wcbSFK5ww1aOI2K6Nk3u8sxJUXHdUXs92DKvLemcaHXw0yDNUNi/izVldy3yu6VEDEflCJkj1+yvB52U+EpvG/7IGwY66QceVbu/1FFLFA==" : "kzGIa0ZwE1Y7CYZ3hZHdFLGEQ6LvTdoKYSr+jClEdoL8l0bRf0Mkp7zsp0uCPyoZHKVBatU7otEmbciu9FWNMSXmpiDj9eSL/eNqpJ0sRkaNPyM3AqR2zy7TG2481K4SWZfo5EahrSat0glEUC6i3sxojjLb8DRq8ETYO1JsNhLOHQjKWlBEBZ04rAcz/kWXt0N1CQne4+GozQtiaMDvN/PXeqwiEYHbS1Gr5G16wHdiFZNYylH2pW14+t5t/eIZX8c/VJNT5uM09KHeBSMEn7Uksp2qx1brKP1K9SULzke0Pgx+lIJZgVndGbviGd5UP4ufovexs4F5TkhI7Pel6A=="
10391039
};
10401040
return signature;
10411041
}
@@ -1049,7 +1049,7 @@ public static Signature SignatureReferenceWithId
10491049
{
10501050
KeyInfo = KeyInfo,
10511051
SignedInfo = SignedInfoReferenceWithId,
1052-
SignatureValue = "BOMo5aCr+YIjOq+lmPj8be8/6u8iXJFXuJskeWaYk1iNadUhhUPcSHeFv8XmOBIXV7Yrvk2WiVoBKawJh79iqRrVpJmdpHTxuukUua6iijxEEhwjYGLneleVgBzDTnk2os21WThYSEXmhi52z4Or0eq29vObOlRN3c2VlqDba8avu8jMNqZuKWsptxLDS1q0JfE8zu7Srs9y2GD7SULbWYpsl2VIO3ZCV+0/YWnBHQ09Ee1QKP18HMNr3jgrmpNj165olYKnn+Vr2YDEBSuNX1mxdw2bqAbpEeWITmmIkW2KDivxOtL2lOZEC6QnEVidWr1oyFUb+srKAlmksiy3wA=="
1052+
SignatureValue = AppContextSwitches.UseCapitalizedXMLTypeAttr ? "fqbb3WVUTLu/ihWXHUYgPWO5rgnm9AuwAT8YeiWiood/z+ObWpTwxs42be4HIDac9U94hR05rfLOR+0WxmlzhJp7/fye50VHMKex5kAAp9aCMAzCvDkfNzhMUN3WOHGEFOs4tmxrR0TBV6j+KNnjyDs3AUtdzZnZB+QmOJAlZubdOzWk/D0CGSXSgMmqYgmvH/GZGQWxQtbGMFuB29VCR7moegGN/9VAo/K7Z22xmfUWNKWVHB0OUC8FI36sadVnnUvcKnUo3M3pnQwbEWYz/+rMSYYrboM4dOKEqxZCgFXKou08Pz0MtNe2VwketLbJrKSmuEJOgVnXrzPTwlVSpw==" : "BOMo5aCr+YIjOq+lmPj8be8/6u8iXJFXuJskeWaYk1iNadUhhUPcSHeFv8XmOBIXV7Yrvk2WiVoBKawJh79iqRrVpJmdpHTxuukUua6iijxEEhwjYGLneleVgBzDTnk2os21WThYSEXmhi52z4Or0eq29vObOlRN3c2VlqDba8avu8jMNqZuKWsptxLDS1q0JfE8zu7Srs9y2GD7SULbWYpsl2VIO3ZCV+0/YWnBHQ09Ee1QKP18HMNr3jgrmpNj165olYKnn+Vr2YDEBSuNX1mxdw2bqAbpEeWITmmIkW2KDivxOtL2lOZEC6QnEVidWr1oyFUb+srKAlmksiy3wA=="
10531053
};
10541054
return signature;
10551055
}

test/Microsoft.IdentityModel.TestUtils/TestSets.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ public static SignedInfoTestSet ReferenceDigestValueNotBase64
862862
{
863863
var digestValue = Guid.NewGuid().ToString();
864864
var reference = Default.ReferenceWithNullTokenStreamNS;
865+
string typeAttr = AppContextSwitches.UseCapitalizedXMLTypeAttr ? "Type" : "type";
865866
reference.DigestValue = digestValue;
866867
var signedInfo = Default.SignedInfoNS;
867868
signedInfo.References.Clear();
@@ -883,7 +884,8 @@ public static SignedInfoTestSet ReferenceDigestValueNotBase64
883884
SecurityAlgorithms.EnvelopedSignature,
884885
SecurityAlgorithms.ExclusiveC14n,
885886
Default.ReferenceDigestMethod,
886-
digestValue))
887+
digestValue,
888+
typeAttr))
887889
};
888890
}
889891
}
@@ -974,6 +976,7 @@ public static SignedInfoTestSet UnknownReferenceTransform
974976
reference.CanonicalizingTransfrom = new ExclusiveCanonicalizationTransform();
975977
signedInfo.References.Clear();
976978
signedInfo.References.Add(reference);
979+
string typeAttr = AppContextSwitches.UseCapitalizedXMLTypeAttr ? "Type" : "type";
977980
return new SignedInfoTestSet
978981
{
979982
SignedInfo = signedInfo,
@@ -990,7 +993,8 @@ public static SignedInfoTestSet UnknownReferenceTransform
990993
unknownTransform,
991994
SecurityAlgorithms.ExclusiveC14n,
992995
SecurityAlgorithms.Sha256Digest,
993-
Default.ReferenceDigestValue))
996+
Default.ReferenceDigestValue,
997+
typeAttr))
994998

995999
};
9961000
}

test/Microsoft.IdentityModel.TestUtils/XmlGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,12 @@ public static string Generate(SignedInfo signedInfo)
354354

355355
public static string ReferenceTemplate
356356
{
357-
get => "<{0}Reference Id=\"{1}\" type=\"{2}\" URI=\"{3}\" xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"{4}\" /><Transform Algorithm=\"{5}\" /></Transforms><DigestMethod Algorithm=\"{6}\" /><DigestValue>{7}</DigestValue></{0}Reference>";
357+
get => "<{0}Reference Id=\"{1}\" {2}=\"{3}\" URI=\"{4}\" xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\"><Transforms><Transform Algorithm=\"{5}\" /><Transform Algorithm=\"{6}\" /></Transforms><DigestMethod Algorithm=\"{7}\" /><DigestValue>{8}</DigestValue></{0}Reference>";
358358
}
359359

360-
public static string ReferenceXml(string prefix, string id, string type, string referenceUri, string envelopingAlgorithm, string c14nAlgorithm, string digestAlgorithm, string digestValue)
360+
public static string ReferenceXml(string prefix, string id, string type, string referenceUri, string envelopingAlgorithm, string c14nAlgorithm, string digestAlgorithm, string digestValue, string typeAttr = "type")
361361
{
362-
return string.Format(ReferenceTemplate, prefix, id, type, referenceUri, envelopingAlgorithm, c14nAlgorithm, digestAlgorithm, digestValue);
362+
return string.Format(ReferenceTemplate, prefix, id, typeAttr, type, referenceUri, envelopingAlgorithm, c14nAlgorithm, digestAlgorithm, digestValue);
363363
}
364364

365365
// Always assumes two transforms

test/Microsoft.IdentityModel.Xml.Tests/DSigSerializerTests.cs

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Xml;
1010
using Microsoft.IdentityModel.TestUtils;
1111
using Microsoft.IdentityModel.Tokens;
12-
using Microsoft.IdentityModel.Xml;
1312
using Xunit;
1413

1514
#pragma warning disable CS3016 // Arrays as attribute arguments is not CLS-compliant
@@ -166,6 +165,27 @@ public void ReadSignature(DSigSerializerTheoryData theoryData)
166165
TestUtilities.AssertFailIfErrors(context);
167166
}
168167

168+
[Theory, MemberData(nameof(ReadSignatureTheoryDataTypeCapitalized), DisableDiscoveryEnumeration = true)]
169+
public void ReadSignatureTypeCapitalized(DSigSerializerTheoryData theoryData)
170+
{
171+
var context = TestUtilities.WriteHeader($"{this}.ReadSignature", theoryData);
172+
bool switchValue;
173+
AppContext.TryGetSwitch("Switch.Microsoft.IdentityModel.UseCapitalizedXMLTypeAttr", out switchValue);
174+
AppContext.SetSwitch("Switch.Microsoft.IdentityModel.UseCapitalizedXMLTypeAttr", true);
175+
try
176+
{
177+
var signature = theoryData.Serializer.ReadSignature(XmlUtilities.CreateDictionaryReader(theoryData.Xml));
178+
theoryData.ExpectedException.ProcessNoException(context);
179+
IdentityComparer.AreEqual(signature, theoryData.Signature, context);
180+
}
181+
catch (Exception ex)
182+
{
183+
theoryData.ExpectedException.ProcessException(ex, context);
184+
}
185+
AppContext.SetSwitch("Switch.Microsoft.IdentityModel.UseCapitalizedXMLTypeAttr", switchValue);
186+
TestUtilities.AssertFailIfErrors(context);
187+
}
188+
169189
public static TheoryData<DSigSerializerTheoryData> ReadSignatureTheoryData
170190
{
171191
get
@@ -236,6 +256,79 @@ public static TheoryData<DSigSerializerTheoryData> ReadSignatureTheoryData
236256
}
237257
}
238258

259+
public static TheoryData<DSigSerializerTheoryData> ReadSignatureTheoryDataTypeCapitalized
260+
{
261+
get
262+
{
263+
bool switchValue;
264+
AppContext.TryGetSwitch("Switch.Microsoft.IdentityModel.UseCapitalizedXMLTypeAttr", out switchValue);
265+
AppContext.SetSwitch("Switch.Microsoft.IdentityModel.UseCapitalizedXMLTypeAttr", true);
266+
var signature = Default.Signature;
267+
signature.SignedInfo.References[0] = Default.ReferenceWithNullTokenStream;
268+
269+
// uncomment to view exception displayed to user
270+
// ExpectedException.DefaultVerbose = true;
271+
var theoryData = new TheoryData<DSigSerializerTheoryData>
272+
{
273+
new DSigSerializerTheoryData
274+
{
275+
First = true,
276+
Signature = signature,
277+
TestId = nameof(Default.Signature),
278+
Xml = XmlGenerator.Generate(Default.Signature),
279+
}
280+
};
281+
282+
signature = Default.SignatureReferenceWithId;
283+
signature.SignedInfo.References[0] = Default.ReferenceWithNullTokenStreamAndId;
284+
theoryData.Add(new DSigSerializerTheoryData
285+
{
286+
Signature = signature,
287+
TestId = nameof(Default.SignatureReferenceWithId),
288+
Xml = XmlGenerator.Generate(Default.SignatureReferenceWithId),
289+
});
290+
291+
signature = Default.Signature;
292+
signature.SignedInfo.References[0] = Default.ReferenceWithNullTokenStream;
293+
theoryData.Add(new DSigSerializerTheoryData
294+
{
295+
Signature = signature,
296+
TestId = nameof(Default.Signature) + "ReferenceWithoutPrefix",
297+
Xml = XmlGenerator.Generate(Default.SignatureReferenceWithoutPrefix),
298+
});
299+
300+
signature = Default.Signature;
301+
signature.SignedInfo.References[0] = Default.ReferenceWithNullTokenStream;
302+
signature.SignedInfo.References[0].DigestMethod = $"_{SecurityAlgorithms.Sha256Digest}";
303+
theoryData.Add(new DSigSerializerTheoryData
304+
{
305+
Signature = signature,
306+
TestId = "UnknownDigestAlgorithm",
307+
Xml = XmlGenerator.Generate(Default.Signature).Replace(SecurityAlgorithms.Sha256Digest, $"_{SecurityAlgorithms.Sha256Digest}")
308+
});
309+
310+
signature = Default.Signature;
311+
signature.SignedInfo.References[0] = Default.ReferenceWithNullTokenStream;
312+
signature.SignedInfo.SignatureMethod = $"_{SecurityAlgorithms.RsaSha256Signature}";
313+
theoryData.Add(new DSigSerializerTheoryData
314+
{
315+
Signature = signature,
316+
TestId = "UnknownSignatureAlgorithm",
317+
Xml = XmlGenerator.Generate(Default.Signature).Replace(SecurityAlgorithms.RsaSha256Signature, $"_{SecurityAlgorithms.RsaSha256Signature}")
318+
});
319+
320+
theoryData.Add(new DSigSerializerTheoryData
321+
{
322+
ExpectedException = new ExpectedException(typeof(XmlReadException), "IDX30022:"),
323+
Signature = new Signature(),
324+
TestId = "EmptySignature",
325+
Xml = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></Signature>"
326+
});
327+
AppContext.SetSwitch("Switch.Microsoft.IdentityModel.UseCapitalizedXMLTypeAttr", switchValue);
328+
return theoryData;
329+
}
330+
}
331+
239332
[Theory, MemberData(nameof(WriteSignatureTheoryData), DisableDiscoveryEnumeration = true)]
240333
public void WriteSignature(DSigSerializerTheoryData theoryData)
241334
{

0 commit comments

Comments
 (0)