-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds handling for common JSON parsing exceptions and wraps them in a …
…`JwtException` Move the parser error handling logic out of DefaultJwtParser into the new JwtDeserializer and wraps them with developer freiendly exceptions Add check for common JSON parsing exceptions like stack overflow when parsing deeply nested (or malformed) JSON
- Loading branch information
Showing
8 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
impl/src/main/java/io/jsonwebtoken/impl/JwtDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (C) 2021 jsonwebtoken.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.jsonwebtoken.impl; | ||
|
||
import io.jsonwebtoken.MalformedJwtException; | ||
import io.jsonwebtoken.io.DeserializationException; | ||
import io.jsonwebtoken.io.Deserializer; | ||
import io.jsonwebtoken.io.IOException; | ||
import io.jsonwebtoken.lang.Assert; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* A {@link Deserializer} implementation that wraps another Deserializer implementation to adds common JWT related | ||
* error handling. | ||
* @param <T> type of object to deserialize. | ||
* @since 0.11.3 | ||
*/ | ||
class JwtDeserializer<T> implements Deserializer<T> { | ||
|
||
static final String MALFORMED_ERROR = "Malformed JWT JSON: "; | ||
static final String MALFORMED_COMPLEX_ERROR = "Malformed or excessively complex JWT JSON. This could reflect a potential malicious JWT, please investigate the JWT source further. JSON: "; | ||
|
||
private final Deserializer<T> deserializer; | ||
|
||
JwtDeserializer(Deserializer<T> deserializer) { | ||
Assert.notNull(deserializer, "deserializer cannot be null."); | ||
this.deserializer = deserializer; | ||
} | ||
|
||
@Override | ||
public T deserialize(byte[] bytes) throws DeserializationException { | ||
try { | ||
return deserializer.deserialize(bytes); | ||
} catch (DeserializationException e) { | ||
throw new MalformedJwtException(MALFORMED_ERROR + new String(bytes, StandardCharsets.UTF_8), e); | ||
} catch (StackOverflowError e) { | ||
throw new IOException(MALFORMED_COMPLEX_ERROR + new String(bytes, StandardCharsets.UTF_8), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
impl/src/test/groovy/io/jsonwebtoken/impl/JwtDeserializerTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (C) 2021 jsonwebtoken.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.jsonwebtoken.impl | ||
|
||
import io.jsonwebtoken.MalformedJwtException | ||
import io.jsonwebtoken.io.DeserializationException | ||
import io.jsonwebtoken.io.Deserializer | ||
import io.jsonwebtoken.io.IOException | ||
import org.junit.Assert | ||
import org.junit.Test | ||
|
||
import java.nio.charset.StandardCharsets | ||
|
||
import static org.easymock.EasyMock.expect | ||
import static org.easymock.EasyMock.mock | ||
import static org.easymock.EasyMock.replay | ||
import static org.junit.Assert.assertEquals | ||
|
||
class JwtDeserializerTest { | ||
|
||
/** | ||
* It's common for JSON parser's to throw a StackOverflowError when body is deeply nested. Since it's common | ||
* across multiple parsers, JJWT handles the exception when parsing. | ||
*/ | ||
@Test | ||
void testParserStackOverflowError() { | ||
|
||
byte[] jsonBytes = '{"test": "testParserStackOverflowError"}'.getBytes(StandardCharsets.UTF_8) | ||
|
||
// create a Deserializer that will throw a StackOverflowError | ||
Deserializer<Map<String,?>> deserializer = mock(Deserializer) | ||
expect(deserializer.deserialize(jsonBytes)).andThrow(new StackOverflowError("Test exception: testParserStackOverflowError" )) | ||
replay(deserializer) | ||
|
||
try { | ||
new JwtDeserializer<>(deserializer).deserialize(jsonBytes) | ||
Assert.fail("Expected IOException") | ||
} catch (IOException e) { | ||
assertEquals JwtDeserializer.MALFORMED_COMPLEX_ERROR + new String(jsonBytes), e.message | ||
} | ||
} | ||
|
||
/** | ||
* Check that a DeserializationException, is wrapped and rethrown as a MalformedJwtException with a developer friendly message. | ||
*/ | ||
@Test | ||
void testDeserializationExceptionMessage() { | ||
|
||
byte[] jsonBytes = '{"test": "testDeserializationExceptionMessage"}'.getBytes(StandardCharsets.UTF_8) | ||
|
||
// create a Deserializer that will throw a DeserializationException | ||
Deserializer<Map<String,?>> deserializer = mock(Deserializer) | ||
expect(deserializer.deserialize(jsonBytes)).andThrow(new DeserializationException("Test exception: testDeserializationExceptionMessage" )) | ||
replay(deserializer) | ||
|
||
try { | ||
new JwtDeserializer<>(deserializer).deserialize(jsonBytes) | ||
Assert.fail("Expected MalformedJwtException") | ||
} catch (MalformedJwtException e) { | ||
assertEquals JwtDeserializer.MALFORMED_ERROR + new String(jsonBytes), e.message | ||
} | ||
} | ||
} |