Skip to content

Trim tokens in error messages to 256 byte to prevent attacks #322

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

Merged
merged 2 commits into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2819,7 +2819,8 @@ protected void _reportInvalidToken(String matchedPart, String msg) throws IOExce
* regular Java identifier character rules. It's just a heuristic,
* nothing fancy here.
*/
while (true) {
final int maxTokenLength = 256;
while (sb.length() < maxTokenLength) {
if (_inputPtr >= _inputEnd) {
if (!_loadMore()) {
break;
Expand All @@ -2832,6 +2833,9 @@ protected void _reportInvalidToken(String matchedPart, String msg) throws IOExce
++_inputPtr;
sb.append(c);
}
if (sb.length() == maxTokenLength) {
sb.append("...");
}
_reportError("Unrecognized token '"+sb.toString()+"': was expecting "+msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3510,7 +3510,8 @@ protected void _reportInvalidToken(String matchedPart, String msg) throws IOExce
* regular Java identifier character rules. It's just a heuristic,
* nothing fancy here (nor fast).
*/
while (true) {
final int maxTokenLength = 256;
while (sb.length() < maxTokenLength) {
if (_inputPtr >= _inputEnd && !_loadMore()) {
break;
}
Expand All @@ -3521,6 +3522,9 @@ protected void _reportInvalidToken(String matchedPart, String msg) throws IOExce
}
sb.append(c);
}
if (sb.length() == maxTokenLength) {
sb.append("...");
}
_reportError("Unrecognized token '"+sb.toString()+"': was expecting "+msg);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.fasterxml.jackson.core.json;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;

/**
* Test size of parser error messages
*/
public class TestMaxErrorSize
extends com.fasterxml.jackson.core.BaseTest
{
public void testLongErrorMessage()
throws Exception
{
final String DOC = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
assertTrue(DOC.length() > 256);
JsonParser jp = createParserUsingReader(DOC);
try {
jp.nextToken();
fail("Expected an exception for unrecognized token");
} catch (JsonParseException jpe) {
String msg = jpe.getMessage();
final String expectedPrefix = "Unrecognized token '";
final String expectedSuffix = "...': was expecting ('true', 'false' or 'null')";
assertTrue(msg.startsWith(expectedPrefix));
assertTrue(msg.contains(expectedSuffix));
msg = msg.substring(expectedPrefix.length(), msg.indexOf(expectedSuffix));
assertEquals(256, msg.length());
}
jp.close();

jp = createParser(MODE_INPUT_STREAM, DOC);
try {
jp.nextToken();
fail("Expected an exception for unrecognized token");
} catch (JsonParseException jpe) {
String msg = jpe.getMessage();
final String expectedPrefix = "Unrecognized token '";
final String expectedSuffix = "...': was expecting ('true', 'false' or 'null')";
assertTrue(msg.startsWith(expectedPrefix));
assertTrue(msg.contains(expectedSuffix));
msg = msg.substring(expectedPrefix.length(), msg.indexOf(expectedSuffix));
assertEquals(256, msg.length());
}
jp.close();
}

public void testShortErrorMessage()
throws Exception
{
final String DOC = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
assertTrue(DOC.length() < 256);
JsonParser jp = createParserUsingReader(DOC);
try {
jp.nextToken();
fail("Expected an exception for unrecognized token");
} catch (JsonParseException jpe) {
String msg = jpe.getMessage();
final String expectedPrefix = "Unrecognized token '";
final String expectedSuffix = "': was expecting ('true', 'false' or 'null')";
assertTrue(msg.startsWith(expectedPrefix));
assertTrue(msg.contains(expectedSuffix));
msg = msg.substring(expectedPrefix.length(), msg.indexOf(expectedSuffix));
assertEquals(DOC.length(), msg.length());
}
jp.close();

jp = createParser(MODE_INPUT_STREAM, DOC);
try {
jp.nextToken();
fail("Expected an exception for unrecognized token");
} catch (JsonParseException jpe) {
String msg = jpe.getMessage();
final String expectedPrefix = "Unrecognized token '";
final String expectedSuffix = "': was expecting ('true', 'false' or 'null')";
assertTrue(msg.startsWith(expectedPrefix));
assertTrue(msg.contains(expectedSuffix));
msg = msg.substring(expectedPrefix.length(), msg.indexOf(expectedSuffix));
assertEquals(DOC.length(), msg.length());
}
jp.close();
}
}