Skip to content

Commit c8a9390

Browse files
committed
Detect input manipulation in c.t.x.io.binary.BinaryStreamReader.
1 parent 8472df8 commit c8a9390

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

xstream-distribution/src/content/changes.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<html>
22
<!--
33
Copyright (C) 2005, 2006 Joe Walnes.
4-
Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 XStream committers.
4+
Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 XStream committers.
55
All rights reserved.
66
77
The software in this package is published under the terms of the BSD
@@ -116,6 +116,7 @@ <h2>Minor changes</h2>
116116
<li>GHPR:#334: Fix remaining buffer size calculation in QuickWriter (by Higuchi Yuta).</li>
117117
<li>GHI:#342: Optimize internal handling of children in DomReader avoiding O(n²) access times for siblings (by Shiang-Yun Yang).</li>
118118
<li>GHI:#359: Add KEYS file with public keys to verify signed artifacts.</li>
119+
<li>Detect input manipulation in c.t.x.io.binary.BinaryStreamReader.</li>
119120
</ul>
120121

121122
<h2>API changes</h2>

xstream/src/java/com/thoughtworks/xstream/io/binary/BinaryStreamReader.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2006 Joe Walnes.
3-
* Copyright (C) 2006, 2007, 2011, 2013, 2014, 2015, 2018 XStream Committers.
3+
* Copyright (C) 2006, 2007, 2011, 2013, 2014, 2015, 2018, 2024 XStream Committers.
44
* All rights reserved.
55
*
66
* The software in this package is published under the terms of the BSD
@@ -22,6 +22,7 @@
2222
import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamReader;
2323
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
2424
import com.thoughtworks.xstream.io.StreamException;
25+
import com.thoughtworks.xstream.security.InputManipulationException;
2526

2627

2728
/**
@@ -165,14 +166,19 @@ public int getLevel() {
165166
private Token readToken() {
166167
if (pushback == null) {
167168
try {
168-
final Token token = tokenFormatter.read(in);
169-
switch (token.getType()) {
170-
case Token.TYPE_MAP_ID_TO_VALUE:
171-
idRegistry.put(token.getId(), token.getValue());
172-
return readToken(); // Next one please.
173-
default:
174-
return token;
175-
}
169+
boolean mapping = false;
170+
do {
171+
final Token token = tokenFormatter.read(in);
172+
switch (token.getType()) {
173+
case Token.TYPE_MAP_ID_TO_VALUE:
174+
idRegistry.put(token.getId(), token.getValue());
175+
mapping ^= true;
176+
continue; // Next one please.
177+
default:
178+
return token;
179+
}
180+
} while (mapping);
181+
throw new InputManipulationException("Binary stream will never have two mapping tokens in sequence");
176182
} catch (final IOException e) {
177183
throw new StreamException(e);
178184
}

xstream/src/test/com/thoughtworks/xstream/io/binary/BinaryStreamTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2006 Joe Walnes.
3-
* Copyright (C) 2006, 2007, 2011, 2015, 2016, 2018, 2019, 2021 XStream Committers.
3+
* Copyright (C) 2006, 2007, 2011, 2015, 2016, 2018, 2019, 2021, 2024 XStream Committers.
44
* All rights reserved.
55
*
66
* The software in this package is published under the terms of the BSD
@@ -13,13 +13,15 @@
1313

1414
import java.io.ByteArrayInputStream;
1515
import java.io.ByteArrayOutputStream;
16+
import java.io.InputStream;
1617
import java.io.StringReader;
1718

1819
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
1920
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
2021
import com.thoughtworks.xstream.io.copy.HierarchicalStreamCopier;
2122
import com.thoughtworks.xstream.io.xml.AbstractReaderTest;
2223
import com.thoughtworks.xstream.io.xml.MXParserDriver;
24+
import com.thoughtworks.xstream.security.InputManipulationException;
2325

2426

2527
public class BinaryStreamTest extends AbstractReaderTest {
@@ -79,4 +81,19 @@ public void testHandlesMoreThan256Ids() {
7981
}
8082
}
8183
}
84+
85+
@SuppressWarnings("resource")
86+
public void testHandleMaliciousInputsOfIdMappingTokens() {
87+
// Insert two successive id mapping tokens into the stream
88+
final byte[] byteArray = new byte[8];
89+
byteArray[0] = byteArray[4] = 10;
90+
byteArray[1] = byteArray[5] = -127;
91+
92+
final InputStream in = new ByteArrayInputStream(byteArray);
93+
try {
94+
new BinaryStreamReader(in);
95+
fail("Thrown " + InputManipulationException.class.getName() + " expected");
96+
} catch (final InputManipulationException e) {
97+
}
98+
}
8299
}

0 commit comments

Comments
 (0)