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

JWT with JSON body seen as plaintext JWT #687

Closed
maximedn opened this issue Nov 5, 2021 · 4 comments
Closed

JWT with JSON body seen as plaintext JWT #687

maximedn opened this issue Nov 5, 2021 · 4 comments
Labels
stale Stale issues pending deletion due to inactivity

Comments

@maximedn
Copy link

maximedn commented Nov 5, 2021

Hi,

We are facing a this behavior when using the latest release of jjwt (0.9.1):

When parsing the claims of a token that has a JSON body using Jwts.parser().[...].parseClaimsJws(token), the token is seen as a plaintext JWT. Here is what the token contains:

Header:

{
  "alg": "HS256",
  "typ": "JWT"
}

Payload

{
  "iss": "XXX",
  "scope": "XXX",
  "onBehalfOf": "",
  "aud": " ",
  "locale": "",
  "iat": 1636109922773,
  "exp": 1636110522773,
  "jti": "ID"
}

Apparently, the token payload contains an extra \r\n at the end, which makes it interpreted as a plaintext jwt, see this line:

if (!payload.isEmpty() && payload.charAt(0) == '{' && payload.charAt(payload.length() - 1) == '}') { //likely to be json, parse it:

Is it a normal behavior ? Should the JWT be seen as plaintext when it doesn't end exactly with } ?

Thank you !

@lhazlewood
Copy link
Contributor

lhazlewood commented Nov 5, 2021

Hi Maxime!

Out of curiosity, what made you believe 0.9.1 was the latest JJWT release? It's actually 0.11.2 (at the time I'm writing this) per https://github.com/jwtk/jjwt#install

That said, I'm not sure that 0.11.2 will solve your problem.

Compact JWT bodies can represent both plaintext as well as JSON, so we have to use heuristics (the line you quoted) to do our best to try and figure out if it might be JSON or not. Based on this issue, we need to update our code to ignore any whitespace before or after the first and last { and } characters respectively. (Or potentially even just try to parse the string no matter what, and if not valid JSON, fall back to a String or byte array).

And while we need to do that, I do have to say:

The entire point of compact JWTs is to save space: whatever library that produced your JWT should not be using whitespace anywhere in the JSON structure (other than in JSON member names and values themselves as necessary) - it should be as flat and minimal as possible with no extra whitespace anywhere as that only defeats the purpose of the notion of 'compact'. Could you reach out to whoever produced that JWT to see if they can fix it on their end as well?

@bmarwell
Copy link

bmarwell commented Apr 14, 2022

Hi @lhazlewood,

I just found out that a JWT body created by JJWT (+compact) will have a leading whitespace character (\n).

// Edit: It was JSON-B on OpenLiberty. They need an extra "sstrip()" call.

bmarwell added a commit to bmarwell/jjwt that referenced this issue Apr 16, 2022
bmarwell added a commit to bmarwell/jjwt that referenced this issue Apr 19, 2022
bmarwell added a commit to bmarwell/jjwt that referenced this issue Apr 22, 2022
bmarwell added a commit to bmarwell/jjwt that referenced this issue Apr 22, 2022
@stale
Copy link

stale bot commented Jul 10, 2022

This issue has been automatically marked as stale due to inactivity for 60 or more days. It will be closed in 7 days if no further activity occurs.

@stale stale bot added the stale Stale issues pending deletion due to inactivity label Jul 10, 2022
@lhazlewood
Copy link
Contributor

lhazlewood commented Sep 6, 2023

This was resolved via 760c542 by

if (!hasContentType(header) // If there is a content type set, then the application using JJWT is expected
// to convert the byte payload themselves based on this content type
// https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.10 :
//
// "This parameter is ignored by JWS implementations; any processing of this
// parameter is performed by the JWS application."
//
&& isLikelyJson(payload)) { // likely to be json, parse it:
Map<String, ?> claimsMap = deserialize(payload, "claims");

and
https://github.com/jwtk/jjwt/blob/a6792d938fd89b96fb38d22268962cfb8e742552/impl/src/main/java/io/jsonwebtoken/impl/DefaultJwtParser.java#L230C11-L230C11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Stale issues pending deletion due to inactivity
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants