Skip to content

Commit fdd9f7d

Browse files
committed
Detect input manipulation in c.t.x.io.binary.BinaryStreamReader.
1 parent 15cb057 commit fdd9f7d

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

xstream-distribution/src/content/changes.html

+2-1
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
@@ -41,6 +41,7 @@ <h2>Minor changes</h2>
4141
<li>GHPR:#334: Fix remaining buffer size calculation in QuickWriter (by Higuchi Yuta).</li>
4242
<li>GHI:#342: Optimize internal handling of children in DomReader avoiding O(n²) access times for siblings (by Shiang-Yun Yang).</li>
4343
<li>GHI:#359: Add KEYS file with public keys to verify signed artifacts.</li>
44+
<li>Detect input manipulation in c.t.x.io.binary.BinaryStreamReader.</li>
4445
</ul>
4546

4647
<h2>API changes</h2>

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

+12-6
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 XStream Committers.
3+
* Copyright (C) 2006, 2007, 2011, 2013, 2024 XStream Committers.
44
* All rights reserved.
55
*
66
* The software in this package is published under the terms of the BSD
@@ -15,6 +15,7 @@
1515
import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamReader;
1616
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
1717
import com.thoughtworks.xstream.io.StreamException;
18+
import com.thoughtworks.xstream.security.InputManipulationException;
1819

1920
import java.io.DataInputStream;
2021
import java.io.IOException;
@@ -150,15 +151,20 @@ public void moveUp() {
150151
private Token readToken() {
151152
if (pushback == null) {
152153
try {
153-
Token token = tokenFormatter.read(in);
154-
switch (token.getType()) {
154+
boolean mapping = false;
155+
do {
156+
final Token token = tokenFormatter.read(in);
157+
switch (token.getType()) {
155158
case Token.TYPE_MAP_ID_TO_VALUE:
156159
idRegistry.put(token.getId(), token.getValue());
157-
return readToken(); // Next one please.
160+
mapping ^= true;
161+
continue; // Next one please.
158162
default:
159163
return token;
160-
}
161-
} catch (IOException e) {
164+
}
165+
} while (mapping);
166+
throw new InputManipulationException("Binary stream will never have two mapping tokens in sequence");
167+
} catch (final IOException e) {
162168
throw new StreamException(e);
163169
}
164170
} else {

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

+16-1
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, 2021 XStream Committers.
3+
* Copyright (C) 2006, 2007, 2011, 2015, 2016, 2021, 2024 XStream Committers.
44
* All rights reserved.
55
*
66
* The software in this package is published under the terms of the BSD
@@ -17,10 +17,12 @@
1717
import com.thoughtworks.xstream.io.copy.HierarchicalStreamCopier;
1818
import com.thoughtworks.xstream.io.xml.AbstractXMLReaderTest;
1919
import com.thoughtworks.xstream.io.xml.MXParserDriver;
20+
import com.thoughtworks.xstream.security.InputManipulationException;
2021

2122
import java.io.ByteArrayOutputStream;
2223
import java.io.StringReader;
2324
import java.io.ByteArrayInputStream;
25+
import java.io.InputStream;
2426

2527
public class BinaryStreamTest extends AbstractXMLReaderTest {
2628

@@ -89,4 +91,17 @@ public void testIsXXEVulnerableWithExternalGeneralEntity() throws Exception {
8991
}
9092
}
9193

94+
public void testHandleMaliciousInputsOfIdMappingTokens() {
95+
// Insert two successive id mapping tokens into the stream
96+
final byte[] byteArray = new byte[8];
97+
byteArray[0] = byteArray[4] = 10;
98+
byteArray[1] = byteArray[5] = -127;
99+
100+
final InputStream in = new ByteArrayInputStream(byteArray);
101+
try {
102+
new BinaryStreamReader(in);
103+
fail("Thrown " + InputManipulationException.class.getName() + " expected");
104+
} catch (final InputManipulationException e) {
105+
}
106+
}
92107
}

0 commit comments

Comments
 (0)