Announcement Announcement Module
Collapse
No announcement yet.
BadLdapGrammarException when Quotes in DN Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • BadLdapGrammarException when Quotes in DN

    There's a similar thread here but I didn't want to wake up a 4 year old topic.

    We're using FilterBasedLdapUserSearch and for some users it's exploding with BadLdapGrammarException. It seems that DnParserImpl can't handle the value that is returned.

    This happens for users with a forward slash in the username. When this forward slash is present the CN gets wrapped in double quotes, and the parser fails on the first double quote character:

    Code:
    Caused by: org.springframework.ldap.BadLdapGrammarException: Failed to parse DN; nested exception is org.springframework.ldap.core.ParseException: Encountered "\"" at line 1, column 1.
    Was expecting one of:
        <ATTRIBUTE_TYPE_STRING> ...
        <LDAP_OID> ...
        " " ...
        
    	at org.springframework.ldap.core.DistinguishedName.parse(DistinguishedName.java:187)
    	at org.springframework.ldap.core.DistinguishedName.<init>(DistinguishedName.java:140)
    	at org.springframework.security.ldap.SpringSecurityLdapTemplate$3.executeWithContext(SpringSecurityLdapTemplate.java:210)
    	at org.springframework.ldap.core.LdapTemplate.executeWithContext(LdapTemplate.java:807)
    	at org.springframework.ldap.core.LdapTemplate.executeReadOnly(LdapTemplate.java:793)
    	at org.springframework.security.ldap.SpringSecurityLdapTemplate.searchForSingleEntry(SpringSecurityLdapTemplate.java:193)
    	at org.springframework.security.ldap.search.FilterBasedLdapUserSearch.searchForUser(FilterBasedLdapUserSearch.java:118)
    	at org.springframework.security.providers.ldap.authenticator.BindAuthenticator.authenticate(BindAuthenticator.java:82)
    	at org.springframework.security.providers.ldap.LdapAuthenticationProvider.authenticate(LdapAuthenticationProvider.java:229)

    Anyone got an idea what to do here? Most users can login fine. Unfortunately I can't tell our customer to just change all their usernames to eliminate the slash.

    Thanks,

    Jeff

  • #2
    Not sure this is even worth mentioning, but here is a unit test that reproduces the parse exception:


    Code:
    	public void testQuotedDn() throws Exception {
    		DistinguishedName dn = new DistinguishedName("\"CN=Pinhead\\, Zippy PH/HU\",OU=AA,OU=A,OU=Users 135200,DC=dom1,DC=local");
    		assertEquals("cn", dn.getLdapRdn(0).getKey());
    		assertEquals("Pinhead, Zippy PH/HU", dn.getLdapRdn(0).getValue());
    	}

    Comment


    • #3
      Are you sure that this occurs with Spring LDAP 1.3.0?

      JNDI CompositeName quotes DNs that contain what it believes are illegal characters. This could be such a case. However, since 1.3.0-RC1, we try to detect whether a CompositeName is passed in to our classes and parse it accordingly. See this bug for more info.

      Comment


      • #4
        Ah, I think I see it now. Spring Security calls the DistinguishedName constructor directly, but the DN is already invalid at that time. The base has been prepended on a quoted name (probably quoted by CompositeName). DistinguishedName can handle a quoted name, but not a quoted and prepended name.

        Comment


        • #5
          I downloaded the source for 1.3.0.RELEASE, hacked on DistinguishedName.unmangleCompositeName to make it remove the quotes even when they aren't at the beginning and end.

          That got us past the first issue, exposed the next one:

          Code:
          Caused by: javax.naming.NamingException: [LDAP: error code 1 - 000020D6: SvcErr: DSID-031006CC, problem 5012 (DIR_ERROR), data 0
          ; remaining name 'cn=Pinhead\, Zippy PH/HU,ou=AA,ou=A,ou=Users 135200,dc=dom1,dc=local'
          	at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3081)
          	at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2987)
          	at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2794)
          	at com.sun.jndi.ldap.LdapCtx.c_lookup(LdapCtx.java:1011)
          	at com.sun.jndi.toolkit.ctx.ComponentContext.c_resolveIntermediate_nns(ComponentContext.java:152)
          	at com.sun.jndi.toolkit.ctx.AtomicContext.c_resolveIntermediate_nns(AtomicContext.java:342)
          	at com.sun.jndi.toolkit.ctx.ComponentContext.p_resolveIntermediate(ComponentContext.java:381)
          	at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:205)
          	at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:121)
          	at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:109)
          	at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:123)
          	at org.springframework.security.ldap.authentication.BindAuthenticator.bindWithDn(BindAuthenticator.java:115)
          	... 36 more

          Comment


          • #6
            ...and solved the next problem, which turns out to be a bug in Spring Security's BindAuthenticator:

            In Spring Security 3.0, BindAuthenticator.java line 115 the call to ctx.getAttributes should be using fullDn instead of userDn; by using userDn the encoding in LdapEncoder never gets used.

            Side Note: I learned a lot about the internals of both Spring-LDAP and Spring-Security while hunting this one down. In a few places (BindAuthenticator.java is an example) I found the dividing line between Spring-LDAP and Spring-Security to be less than ideal.

            Maybe a generic BindAuthenticator could live in Spring-LDAP, and a wrapper adapting it to the Spring-Security Authenticator interface could live in Spring-Security. Just a thought.

            Comment


            • #7
              Yes, it seems the problem is in Spring Security. It would be great if you could create an issue in their JIRA with the patch and other info, such as the places in BindAuthenticator that should be cleaned up.

              We don't consider this a bug in Spring LDAP.

              Comment


              • #8
                Here's the BindAuthenticator bug:

                http://jira.springframework.org/browse/SEC-1444

                There may be another as well, if I end up adding a second Spring-Security bug I will post it here.

                Comment

                Working...
                X