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

[Bug] Parsing a valid saml assertion throws IDX13102 #2543

Open
blushingpenguin opened this issue Mar 28, 2024 · 0 comments
Open

[Bug] Parsing a valid saml assertion throws IDX13102 #2543

blushingpenguin opened this issue Mar 28, 2024 · 0 comments
Labels
SAML related to SAML tokens

Comments

@blushingpenguin
Copy link

Which version of Microsoft.IdentityModel are you using?
7.5.0

Where is the issue?
M.IM.Tokens

Is this a new or an existing app?
New (reproduction)

Repro

using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens.Saml2;

internal class Program
{
    private static void Main()
    {
        string token = @"
<saml2:Assertion Version=""2.0"" ID=""test"" IssueInstant=""2024-03-27T17:49:12.345Z"" xmlns:saml2=""urn:oasis:names:tc:SAML:2.0:assertion"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
  <saml2:Issuer xmlns:saml2=""urn:oasis:names:tc:SAML:2.0:assertion"" Format=""urn:oasis:names:tc:SAML:2.0:nameid-format:entity"">https://example.com/issuer</saml2:Issuer><Signature xmlns=""http://www.w3.org/2000/09/xmldsig#""><SignedInfo><CanonicalizationMethod Algorithm=""http://www.w3.org/2001/10/xml-exc-c14n#"" /><SignatureMethod Algorithm=""http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"" /><Reference URI=""#_gzzLVaFPZNwsRusXqZl9b""><Transforms><Transform Algorithm=""http://www.w3.org/2000/09/xmldsig#enveloped-signature"" /><Transform Algorithm=""http://www.w3.org/2001/10/xml-exc-c14n#"" /></Transforms><DigestMethod Algorithm=""http://www.w3.org/2001/04/xmlenc#sha256"" /><DigestValue>digest</DigestValue></Reference></SignedInfo><SignatureValue>sign</SignatureValue><KeyInfo><X509Data><X509Certificate>cert</X509Certificate></X509Data></KeyInfo></Signature>
  <saml2:Subject xmlns:saml2=""urn:oasis:names:tc:SAML:2.0:assertion"">
    <saml2:NameID Format=""urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"">test@example.com</saml2:NameID>
    <saml2:SubjectConfirmation Method=""urn:oasis:names:tc:SAML:2.0:cm:bearer"">
      <saml2:SubjectConfirmationData InResponseTo=""id1234"" Recipient=""https://example.com/acsurl"" NotOnOrAfter=""2024-03-27T18:49:12.345Z"" />
    </saml2:SubjectConfirmation>
  </saml2:Subject>
  <saml2:Conditions xmlns:saml2=""urn:oasis:names:tc:SAML:2.0:assertion"" NotBefore=""2024-03-27T17:49:12.345Z"" NotOnOrAfter=""2024-03-27T18:49:12.345Z"">
    <saml2:AudienceRestriction>
      <saml2:Audience>https://example.com</saml2:Audience>
    </saml2:AudienceRestriction>
  </saml2:Conditions>
  <saml2:AuthnStatement AuthnInstant=""2024-03-27T17:49:12.345Z"" SessionIndex=""id1234"" SessionNotOnOrAfter=""2024-03-27T18:49:12.345Z"" xmlns:saml2=""urn:oasis:names:tc:SAML:2.0:assertion"">
    <saml2:AuthnContext>
      <saml2:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml2:AuthnContextClassRef>
    </saml2:AuthnContext>
  </saml2:AuthnStatement>
  <saml2:AttributeStatement xmlns:saml2=""urn:oasis:names:tc:SAML:2.0:assertion"">
    <saml2:Attribute Name=""groups"" NameFormat=""urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"">
      <saml2:AttributeValue>
        <name xmlns=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:type=""xs:string"">Test Group</name>
        <id xmlns=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:type=""xs:string"">1234</id>
        <email xmlns=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:type=""xs:string"">group@example.com</email>
      </saml2:AttributeValue>
    </saml2:Attribute>
  </saml2:AttributeStatement>
</saml2:Assertion>";
        try
        {
            IdentityModelEventSource.ShowPII = true;
            var th = new Saml2SecurityTokenHandler();
            th.ReadSaml2Token(token);
        }
        catch(Exception e)
        {
            Console.WriteLine("caught {0}", e);
        }
    }
}

Expected behavior
Assertion parsed without error

Actual behavior

caught Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenReadException: IDX13102: Exception thrown while reading 'AttributeValue' for Saml2SecurityToken. Inner exception: 'System.Xml.XmlException: 'Element' is an invalid XmlNodeType. Line 23, position 131.
   at System.Xml.XmlReader.ReadEndElement()
   at Microsoft.IdentityModel.Tokens.Saml2.Saml2Serializer.ReadAttributeValue(XmlDictionaryReader reader, Saml2Attribute attribute)'.
 ---> System.Xml.XmlException: 'Element' is an invalid XmlNodeType. Line 23, position 131.
   at System.Xml.XmlReader.ReadEndElement()
   at Microsoft.IdentityModel.Tokens.Saml2.Saml2Serializer.ReadAttributeValue(XmlDictionaryReader reader, Saml2Attribute attribute)
   --- End of inner exception stack trace ---
   at Microsoft.IdentityModel.Tokens.Saml2.Saml2Serializer.ReadAttributeValue(XmlDictionaryReader reader, Saml2Attribute attribute)
   at Microsoft.IdentityModel.Tokens.Saml2.Saml2Serializer.ReadAttribute(XmlDictionaryReader reader)
   at Microsoft.IdentityModel.Tokens.Saml2.Saml2Serializer.ReadAttributeStatement(XmlDictionaryReader reader)
   at Microsoft.IdentityModel.Tokens.Saml2.Saml2Serializer.ReadAssertion(XmlReader reader)
   at Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ReadSaml2Token(XmlReader reader)
   at Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ReadSaml2Token(String token)

Possible solution

in Saml2Serializer.ReadAttributeValue, read more whitespace after the element start

            reader.ReadStartElement(Saml2Constants.Elements.AttributeValue, Saml2Constants.Namespace);
            // added block
            while (reader.NodeType == XmlNodeType.Whitespace)
            {
                whiteSpace += reader.Value;
                reader.Read();
            }

or just remove the "helpful" hack and parse the xml normally (not sure why it's in there though, presumably that will break something else).

@jennyf19 jennyf19 added the SAML related to SAML tokens label Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SAML related to SAML tokens
Projects
None yet
Development

No branches or pull requests

2 participants