security: [BUG] SAML login fails when username contains '\h' due to JwtToken.

What is the bug? SAML login fails when username contains ‘\h’. it passes in all other cases such as username containing ‘\a’ or ‘\x’ . The error is coming from: (https://github.com/opensearch-project/security/blob/main/src/main/java/com/amazon/dlic/auth/http/saml/AuthTokenProcessorHandler.java#319)

We tried logging in using two usernames :

  1. First username had a ‘\h’ in it. We got a 500 internal error with this.
  2. Second username had a ‘\a’ in it. We were able to login using this.

https://github.com/opensearch-project/security/blob/main/src/main/java/com/amazon/dlic/auth/http/saml/AuthTokenProcessorHandler.java#L297 extracts similar jwtSubjectKey for usernames with ‘\h’ and ‘\a’.

But the encoded JWT at : https://github.com/opensearch-project/security/blob/main/src/main/java/com/amazon/dlic/auth/http/saml/AuthTokenProcessorHandler.java#L319

return different types of values for sub. For username with ‘\h’, it returns (in encoded form) :

{2 items
"header":{1 item
"alg":"HS512"
}
"payload":{5 items
"nbf":1678363196
"exp":1678366796
"sub":"abcdhhabcd"
"saml_nif":"email"
"saml_si":"318941483"
}
}

For username with ‘\a’, it returns (in encoded form) :

{2 items
"header":{1 item
"alg":"HS512"
}
"payload":{5 items
"nbf":1678363358
"exp":1678366958
"sub":"abcd\ahabcd"
"saml_nif":"email"
"saml_si":"693676161"
}
}

It is still unknown for how many characters will the issue repeat. How can one reproduce the bug? Steps to reproduce the behavior:

  1. Create a SAML domain, and try logging in using a username with ‘\h’ and you will see 500 internal error.
  2. Now repeat same thing with ‘\a’ or ‘\x’. This should work.

What is the expected behavior? User should be able to login even when username contains ‘\h’.

What is your host/environment?

  • OS_1.3

Do you have any screenshots? If applicable, add screenshots to help explain your problem.

Do you have any additional context? Add any other context about the problem.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (15 by maintainers)

Most upvoted comments

@reta Thanks for following up! I will raise PRs on the release branches.

@cwperks new releases just landed [1], we should be able to fix this issue in time for 2.16.0, thanks!

[1] https://cxf.apache.org/download.html

JWT is Base64Url encoded and Bases64URL doesn’t accept backslash character, although most of libraries don’t enforce this rule: https://datatracker.ietf.org/doc/html/rfc4648#section-5 If escaped backslash is used, it seems like both cxf and nimbus jwt libraries work with it `

    //nimbus jwt
    JWSHeader header= new JWSHeader(JWSAlgorithm.HS512);
    JWTClaimsSet jwtClaimsSet = new JWTClaimsSet.Builder()
            .claim("2", "\\h")
            .claim("4", "\\\\h")
            .claim("6", "\\\\\\h")
            .claim("8", "\\\\\\\\h")
            .build();

    JWSSigner signer = new MACSigner("someSecretKeyThatsVeryHardToBreakAndHasAtLeast512BitsOrMoreToSupportHMAC512");
    SignedJWT jwt = new SignedJWT(header, jwtClaimsSet);
    jwt.sign(signer);
    jwt.serialize();
}

` This code would produce jwt: eyJhbGciOiJIUzUxMiJ9.eyI4IjoiXFxcXFxcXFxoIiwiMiI6IlxcaCIsIjQiOiJcXFxcaCIsIjYiOiJcXFxcXFxoIn0.T6KZHLgFEU1gnEXUhW0kc44PrpJB_TjDZuM62M0eAwaiFGX_K6sfr06SkcdB38wNuoAEziTBarQfdVaHQpvAiA which seems to get parsed ok.

image Regarding characters \t \n \x \a \h, I think java will try to either escape backslash during deserialization/serialization, or maybe try to treat it as special characters (tab, new-line etc).

I don’t think I have permissions to create an issue. I’ll go through the self serve signup page to request an account.

@martin-gaievski I think I finally got to the bottom of this. This is an issue with the cxf library that the security plugin uses to issue JWTs for SAML auth.

CXF thinks that \h is a special character that needs to be escaped: https://github.com/apache/cxf/blob/main/rt/rs/extensions/json-basic/src/main/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriter.java#L52

PR where it was introduced: https://github.com/apache/cxf/pull/819/files#diff-3396d540d530ac37f53c476b87ef6fdb86343aa6ff211826418438171e05c764

I can’t find an official list of characters that need to be escaped in JSON, but https://stackoverflow.com/a/27516892 seems to indicate that it isn’t one. I will open up a PR in cxf to solicit comments.

@reta Fancy seeing you in that codebase! Do you know if h can be removed from the ESCAPED_CHARS set or if there is documentation about what the \h character is?