Skip to content

Commit c83fbe9

Browse files
committed
Fix compression being omitted for JWEs with arbitrary content
The JWE content was compressed only when claims was used as payload, but when using arbitrary content, the compression was omitted, while keeping the "zip" header field, leading to decompression failing.
1 parent c673b76 commit c83fbe9

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

impl/src/main/java/io/jsonwebtoken/impl/DefaultJwtBuilder.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -693,14 +693,13 @@ private String encrypt(final Payload content, final Key key, final Provider keyP
693693
Assert.stateNotNull(keyAlgFunction, "KeyAlgorithm function cannot be null.");
694694
assertPayloadEncoding("JWE");
695695

696-
InputStream plaintext;
696+
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
697697
if (content.isClaims()) {
698-
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
699698
writeAndClose("JWE Claims", content, out);
700-
plaintext = Streams.of(out.toByteArray());
701699
} else {
702-
plaintext = content.toInputStream();
700+
writeAndClose("JWE Content", content, out);
703701
}
702+
InputStream plaintext = Streams.of(out.toByteArray());
704703

705704
//only expose (mutable) JweHeader functionality to KeyAlgorithm instances, not the full headerBuilder
706705
// (which exposes this JwtBuilder and shouldn't be referenced by KeyAlgorithms):

impl/src/test/groovy/io/jsonwebtoken/JwtsTest.groovy

+92
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import io.jsonwebtoken.impl.io.Streams
2222
import io.jsonwebtoken.impl.lang.Bytes
2323
import io.jsonwebtoken.impl.lang.Services
2424
import io.jsonwebtoken.impl.security.*
25+
import io.jsonwebtoken.io.CompressionAlgorithm
2526
import io.jsonwebtoken.io.Decoders
2627
import io.jsonwebtoken.io.Deserializer
2728
import io.jsonwebtoken.io.Encoders
@@ -1398,6 +1399,97 @@ class JwtsTest {
13981399
}
13991400
}
14001401

1402+
@Test
1403+
void testJweCompressionWithArbitraryContentString() {
1404+
def codecs = [Jwts.ZIP.DEF, Jwts.ZIP.GZIP]
1405+
1406+
for (CompressionAlgorithm zip : codecs) {
1407+
1408+
for (AeadAlgorithm enc : Jwts.ENC.get().values()) {
1409+
1410+
SecretKey key = enc.key().build()
1411+
1412+
String payload = 'hello, world!'
1413+
1414+
// encrypt and compress:
1415+
String jwe = Jwts.builder()
1416+
.content(payload)
1417+
.compressWith(zip)
1418+
.encryptWith(key, enc)
1419+
.compact()
1420+
1421+
//decompress and decrypt:
1422+
def jwt = Jwts.parser()
1423+
.decryptWith(key)
1424+
.build()
1425+
.parseEncryptedContent(jwe)
1426+
assertEquals payload, new String(jwt.getPayload(), StandardCharsets.UTF_8)
1427+
}
1428+
}
1429+
}
1430+
1431+
@Test
1432+
void testJweCompressionWithArbitraryContentByteArray() {
1433+
def codecs = [Jwts.ZIP.DEF, Jwts.ZIP.GZIP]
1434+
1435+
for (CompressionAlgorithm zip : codecs) {
1436+
1437+
for (AeadAlgorithm enc : Jwts.ENC.get().values()) {
1438+
1439+
SecretKey key = enc.key().build()
1440+
1441+
byte[] payload = new byte[14];
1442+
Randoms.secureRandom().nextBytes(payload)
1443+
1444+
// encrypt and compress:
1445+
String jwe = Jwts.builder()
1446+
.content(payload)
1447+
.compressWith(zip)
1448+
.encryptWith(key, enc)
1449+
.compact()
1450+
1451+
//decompress and decrypt:
1452+
def jwt = Jwts.parser()
1453+
.decryptWith(key)
1454+
.build()
1455+
.parseEncryptedContent(jwe)
1456+
assertArrayEquals payload, jwt.getPayload()
1457+
}
1458+
}
1459+
}
1460+
1461+
@Test
1462+
void testJweCompressionWithArbitraryContentInputStream() {
1463+
def codecs = [Jwts.ZIP.DEF, Jwts.ZIP.GZIP]
1464+
1465+
for (CompressionAlgorithm zip : codecs) {
1466+
1467+
for (AeadAlgorithm enc : Jwts.ENC.get().values()) {
1468+
1469+
SecretKey key = enc.key().build()
1470+
1471+
byte[] payloadBytes = new byte[14];
1472+
Randoms.secureRandom().nextBytes(payloadBytes)
1473+
1474+
ByteArrayInputStream payload = new ByteArrayInputStream(payloadBytes)
1475+
1476+
// encrypt and compress:
1477+
String jwe = Jwts.builder()
1478+
.content(payload)
1479+
.compressWith(zip)
1480+
.encryptWith(key, enc)
1481+
.compact()
1482+
1483+
//decompress and decrypt:
1484+
def jwt = Jwts.parser()
1485+
.decryptWith(key)
1486+
.build()
1487+
.parseEncryptedContent(jwe)
1488+
assertArrayEquals payloadBytes, jwt.getPayload()
1489+
}
1490+
}
1491+
}
1492+
14011493
@Test
14021494
void testPasswordJwes() {
14031495

0 commit comments

Comments
 (0)