diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java index f1b7684aff..b52970cc65 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java @@ -969,7 +969,7 @@ public final long readFieldNameHashCodeUnquote() { if (ch == '\\') { nameEscape = true; - ch = chars[offset++]; + ch = charAfterEscape(offset++); switch (ch) { case 'u': { ch = (char) hexDigit4(chars, offset, end); @@ -977,7 +977,7 @@ public final long readFieldNameHashCodeUnquote() { break; } case 'x': { - ch = char2(chars[offset], chars[offset + 1]); + ch = char2(chars, offset, end); offset += 2; break; } @@ -1022,7 +1022,7 @@ public final long readFieldNameHashCodeUnquote() { for (int i = 0; ; ++i) { if (ch == '\\') { nameEscape = true; - ch = chars[offset++]; + ch = charAfterEscape(offset++); switch (ch) { case 'u': { ch = (char) hexDigit4(chars, offset, end); @@ -1030,7 +1030,7 @@ public final long readFieldNameHashCodeUnquote() { break; } case 'x': { - ch = char2(chars[offset], chars[offset + 1]); + ch = char2(chars, offset, end); offset += 2; break; } @@ -1484,7 +1484,7 @@ private final long readFieldNameHashCode0() { if (c == '\\') { nameEscape = true; - c = chars[++offset]; + c = charAfterEscape(++offset); switch (c) { case 'u': { c = (char) hexDigit4(chars, offset + 1, end); @@ -1492,7 +1492,7 @@ private final long readFieldNameHashCode0() { break; } case 'x': { - c = char2(chars[offset + 1], chars[offset + 2]); + c = char2(chars, offset + 1, end); offset += 2; break; } @@ -1550,7 +1550,7 @@ private final long readFieldNameHashCode0() { char c = chars[offset]; if (c == '\\') { nameEscape = true; - c = chars[++offset]; + c = charAfterEscape(++offset); switch (c) { case 'u': { c = (char) hexDigit4(chars, offset + 1, end); @@ -1558,7 +1558,7 @@ private final long readFieldNameHashCode0() { break; } case 'x': { - c = char2(chars[offset + 1], chars[offset + 2]); + c = char2(chars, offset + 1, end); offset += 2; break; } @@ -1709,7 +1709,7 @@ public final long readValueHashCode0() { if (ch == '\\') { nameEscape = true; - ch = chars[++offset]; + ch = charAfterEscape(++offset); switch (ch) { case 'u': { ch = (char) hexDigit4(chars, offset + 1, end); @@ -1717,7 +1717,7 @@ public final long readValueHashCode0() { break; } case 'x': { - ch = char2(chars[offset + 1], chars[offset + 2]); + ch = char2(chars, offset + 1, end); offset += 2; break; } @@ -1746,7 +1746,7 @@ public final long readValueHashCode0() { char c = chars[offset]; if (c == '\\') { nameEscape = true; - c = chars[++offset]; + c = charAfterEscape(++offset); switch (c) { case 'u': { c = (char) hexDigit4(chars, offset + 1, end); @@ -1754,7 +1754,7 @@ public final long readValueHashCode0() { break; } case 'x': { - c = char2(chars[offset + 1], chars[offset + 2]); + c = char2(chars, offset + 1, end); offset += 2; break; } @@ -1829,7 +1829,7 @@ public final long getNameHashCodeLCase() { char c = chars[offset]; if (c == '\\') { - c = chars[++offset]; + c = charAfterEscape(++offset); switch (c) { case 'u': { c = (char) hexDigit4(chars, offset + 1, end); @@ -1837,7 +1837,7 @@ public final long getNameHashCodeLCase() { break; } case 'x': { - c = char2(chars[offset + 1], chars[offset + 2]); + c = char2(chars, offset + 1, end); offset += 2; break; } @@ -1881,7 +1881,7 @@ public final long getNameHashCodeLCase() { char c = chars[offset]; if (c == '\\') { - c = chars[++offset]; + c = charAfterEscape(++offset); switch (c) { case 'u': { c = (char) hexDigit4(chars, offset + 1, end); @@ -1889,7 +1889,7 @@ public final long getNameHashCodeLCase() { break; } case 'x': { - c = char2(chars[offset + 1], chars[offset + 2]); + c = char2(chars, offset + 1, end); offset += 2; break; } @@ -1938,7 +1938,7 @@ public final String getFieldName() { char c = chars[offset]; if (c == '\\') { - c = chars[++offset]; + c = charAfterEscape(++offset); switch (c) { case 'u': { c = (char) hexDigit4(chars, offset + 1, end); @@ -1946,7 +1946,7 @@ public final String getFieldName() { break; } case 'x': { - c = char2(chars[offset + 1], chars[offset + 2]); + c = char2(chars, offset + 1, end); offset += 2; break; } @@ -2000,8 +2000,9 @@ public final String readFieldName() { int c = chars[offset]; if (c == '\\') { nameEscape = true; - c = chars[offset + 1]; + c = charAfterEscape(offset + 1); offset += (c == 'u' ? 6 : (c == 'x' ? 4 : 2)); + checkEscapeEnd(offset); continue; } @@ -3075,7 +3076,7 @@ public final String getString() { char c = chars[offset]; if (c == '\\') { - c = chars[++offset]; + c = charAfterEscape(++offset); switch (c) { case 'u': { c = (char) hexDigit4(chars, offset + 1, end); @@ -3083,7 +3084,7 @@ public final String getString() { break; } case 'x': { - c = char2(chars[offset + 1], chars[offset + 2]); + c = char2(chars, offset + 1, end); offset += 2; break; } @@ -3116,8 +3117,9 @@ protected final void readString0() { char c = chars[offset]; if (c == '\\') { valueEscape = true; - c = chars[offset + 1]; + c = charAfterEscape(offset + 1); offset += (c == 'u' ? 6 : (c == 'x' ? 4 : 2)); + checkEscapeEnd(offset); continue; } @@ -3135,12 +3137,12 @@ protected final void readString0() { for (int i = 0; ; ++i) { char c = this.chars[offset]; if (c == '\\') { - c = this.chars[++offset]; + c = charAfterEscape(++offset); if (c == 'u') { c = (char) hexDigit4(chars, offset + 1, end); offset += 4; } else if (c == 'x') { - c = char2(chars[offset + 1], chars[offset + 2]); + c = char2(chars, offset + 1, end); offset += 2; } else if (c != '\\' && c != '"') { c = char1(c); @@ -3296,7 +3298,7 @@ public String readString() { char c = chars[offset]; if (c == '\\') { - c = chars[++offset]; + c = charAfterEscape(++offset); if (c == 'u') { c = (char) hexDigit4(chars, offset + 1, end); offset += 4; @@ -3352,6 +3354,19 @@ private char char2(char[] chars, int offset, int end) { } } + private char charAfterEscape(int offset) { + if (offset >= end) { + throw error("invalid escape character EOI"); + } + return chars[offset]; + } + + private void checkEscapeEnd(int offset) { + if (offset >= end) { + throw error("invalid escape character EOI"); + } + } + @Override public final boolean skipName() { this.offset = skipName(this, chars, offset, end); @@ -3367,13 +3382,14 @@ private static int skipName(JSONReaderUTF16 jsonReader, char[] chars, int offset for (; ; ) { char ch = chars[offset++]; if (ch == '\\') { - ch = chars[offset]; + ch = jsonReader.charAfterEscape(offset); if (ch == 'u') { hexDigit4(chars, offset + 1, end); } else if (ch == 'x') { - char2(chars[offset + 1], chars[offset + 2]); + jsonReader.char2(chars, offset + 1, end); } offset += (ch == 'u' ? 5 : (ch == 'x' ? 3 : 1)); + jsonReader.checkEscapeEnd(offset); continue; } @@ -3501,9 +3517,9 @@ private static int skipString(JSONReaderUTF16 jsonReader, char[] chars, int offs char ch = offset == end ? EOI : chars[offset++]; for (; ; ) { if (ch == '\\') { - ch = chars[offset++]; + ch = jsonReader.charAfterEscape(offset++); offset = skipEscape(jsonReader, chars, offset, end, ch); - ch = chars[offset++]; + ch = jsonReader.charAfterEscape(offset++); continue; } @@ -3550,7 +3566,7 @@ private static int skipEscape(JSONReaderUTF16 jsonReader, char[] chars, int offs return offset + 4; } if (ch == 'x') { - char2(chars[offset], chars[offset + 1]); + jsonReader.char2(chars, offset, end); return offset + 2; } if (ch != '\\' && ch != '"') { @@ -3563,8 +3579,14 @@ private static int skipStringEscaped(JSONReaderUTF16 jsonReader, char[] bytes, i int ch = bytes[offset++]; for (; ; ) { if (ch == '\\') { + if (offset == bytes.length) { + throw jsonReader.error("invalid escape character EOI"); + } ch = bytes[offset++]; offset = skipEscape(jsonReader, bytes, offset, jsonReader.end, ch); + if (offset >= bytes.length) { + throw jsonReader.error("invalid escape character EOI"); + } ch = bytes[offset++]; continue; } diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java index ca7521c047..99b2d31a1a 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java @@ -580,7 +580,7 @@ public long readFieldNameHashCodeUnquote() { if (ch == '\\') { nameEscape = true; - ch = (char) bytes[offset++]; + ch = (char) byteAfterEscape(offset++); switch (ch) { case 'u': { ch = hexDigit4(bytes, offset, end); @@ -588,7 +588,7 @@ public long readFieldNameHashCodeUnquote() { break; } case 'x': { - ch = char2(bytes[offset], bytes[offset + 1]); + ch = char2(bytes, offset, end); offset += 2; break; } @@ -665,7 +665,7 @@ public long readFieldNameHashCodeUnquote() { for (int i = 0; ; ++i) { if (ch == '\\') { nameEscape = true; - ch = bytes[offset++]; + ch = byteAfterEscape(offset++); switch (ch) { case 'u': { ch = hexDigit4(bytes, offset, end); @@ -673,7 +673,7 @@ public long readFieldNameHashCodeUnquote() { break; } case 'x': { - ch = char2(bytes[offset], bytes[offset + 1]); + ch = char2(bytes, offset, end); offset += 2; break; } @@ -2619,7 +2619,7 @@ protected long readFieldNameHashCode0() { if (ch == '\\') { nameEscape = true; - ch = bytes[++offset]; + ch = byteAfterEscape(++offset); switch (ch) { case 'u': { ch = hexDigit4(bytes, offset + 1, end); @@ -2627,7 +2627,7 @@ protected long readFieldNameHashCode0() { break; } case 'x': { - ch = char2(bytes[offset + 1], bytes[offset + 2]); + ch = char2(bytes, offset + 1, end); offset += 2; break; } @@ -2666,7 +2666,7 @@ protected long readFieldNameHashCode0() { ch = bytes[offset]; if (ch == '\\') { nameEscape = true; - ch = bytes[++offset]; + ch = byteAfterEscape(++offset); switch (ch) { case 'u': { ch = hexDigit4(bytes, offset + 1, end); @@ -2674,7 +2674,7 @@ protected long readFieldNameHashCode0() { break; } case 'x': { - ch = char2(bytes[offset + 1], bytes[offset + 2]); + ch = char2(bytes, offset + 1, end); offset += 2; break; } @@ -2853,7 +2853,7 @@ private long readValueHashCode0() { if (ch == '\\') { nameEscape = true; - ch = bytes[++offset]; + ch = byteAfterEscape(++offset); switch (ch) { case 'u': { ch = hexDigit4(bytes, offset + 1, end); @@ -2861,7 +2861,7 @@ private long readValueHashCode0() { break; } case 'x': { - ch = char2(bytes[offset + 1], bytes[offset + 2]); + ch = char2(bytes, offset + 1, end); offset += 2; break; } @@ -2893,7 +2893,7 @@ private long readValueHashCode0() { ch = bytes[offset]; if (ch == '\\') { nameEscape = true; - ch = bytes[++offset]; + ch = byteAfterEscape(++offset); switch (ch) { case 'u': { ch = hexDigit4(bytes, offset + 1, end); @@ -2901,7 +2901,7 @@ private long readValueHashCode0() { break; } case 'x': { - ch = char2(bytes[offset + 1], bytes[offset + 2]); + ch = char2(bytes, offset + 1, end); offset += 2; break; } @@ -3017,7 +3017,7 @@ public long getNameHashCodeLCase() { int ch = bytes[offset]; if (ch == '\\') { - ch = bytes[++offset]; + ch = byteAfterEscape(++offset); switch (ch) { case 'u': { ch = hexDigit4(bytes, offset + 1, end); @@ -3025,7 +3025,7 @@ public long getNameHashCodeLCase() { break; } case 'x': { - ch = char2(bytes[offset + 1], bytes[offset + 2]); + ch = char2(bytes, offset + 1, end); offset += 2; break; } @@ -3089,7 +3089,7 @@ public long getNameHashCodeLCase() { for (; ; ) { int ch = bytes[offset]; if (ch == '\\') { - ch = bytes[++offset]; + ch = byteAfterEscape(++offset); switch (ch) { case 'u': { ch = hexDigit4(bytes, offset + 1, end); @@ -3097,7 +3097,7 @@ public long getNameHashCodeLCase() { break; } case 'x': { - ch = char2(bytes[offset + 1], bytes[offset + 2]); + ch = char2(bytes, offset + 1, end); offset += 2; break; } @@ -3248,7 +3248,7 @@ public String getFieldName() { } if (ch == '\\') { - ch = (char) bytes[++offset]; + ch = byteAfterEscape(++offset); switch (ch) { case 'u': { ch = hexDigit4(bytes, offset + 1, end); @@ -3256,7 +3256,7 @@ public String getFieldName() { break; } case 'x': { - ch = char2(bytes[offset + 1], bytes[offset + 2]); + ch = char2(bytes, offset + 1, end); offset += 2; break; } @@ -3310,8 +3310,9 @@ public String readFieldName() { int ch = bytes[offset]; if (ch == '\\') { nameEscape = true; - ch = bytes[offset + 1]; + ch = byteAfterEscape(offset + 1); offset += (ch == 'u' ? 6 : (ch == 'x' ? 4 : 2)); + checkEscapeEnd(offset); continue; } @@ -4148,7 +4149,7 @@ public final void readString(ValueConsumer consumer, boolean quoted) { int c = bytes[offset]; if (c == '\\') { valueEscape = true; - c = bytes[++offset]; + c = byteAfterEscape(++offset); switch (c) { case 'u': { offset += 4; @@ -4162,6 +4163,7 @@ public final void readString(ValueConsumer consumer, boolean quoted) { break; } offset++; + checkEscapeEnd(offset); continue; } @@ -4203,7 +4205,7 @@ public final void readString(ValueConsumer consumer, boolean quoted) { for (int i = 0, end = this.end; ; ++i) { int c = bytes[offset]; if (c == '\\') { - c = bytes[++offset]; + c = byteAfterEscape(++offset); switch (c) { case 'u': { c = hexDigit4(bytes, offset + 1, end); @@ -4211,7 +4213,7 @@ public final void readString(ValueConsumer consumer, boolean quoted) { break; } case 'x': { - c = char2(bytes[offset + 1], bytes[offset + 2]); + c = char2(bytes, offset + 1, end); offset += 2; break; } @@ -4332,8 +4334,9 @@ protected void readString0() { int c = bytes[offset]; if (c == '\\') { valueEscape = true; - c = bytes[offset + 1]; + c = byteAfterEscape(offset + 1); offset += (c == 'u' ? 6 : (c == 'x' ? 4 : 2)); + checkEscapeEnd(offset); continue; } @@ -4379,7 +4382,7 @@ protected void readString0() { for (int i = 0; ; ++i) { int c = bytes[offset]; if (c == '\\') { - c = bytes[++offset]; + c = byteAfterEscape(++offset); switch (c) { case 'u': { c = hexDigit4(bytes, offset + 1, end); @@ -4387,7 +4390,7 @@ protected void readString0() { break; } case 'x': { - c = char2(bytes[offset + 1], bytes[offset + 2]); + c = char2(bytes, offset + 1, end); offset += 2; break; } @@ -4490,6 +4493,26 @@ protected void readString0() { stringValue = str; } + private int byteAfterEscape(int offset) { + if (offset >= end) { + throw error("invalid escape character EOI"); + } + return bytes[offset]; + } + + private char char2(byte[] bytes, int offset, int end) { + if (offset + 1 >= end) { + throw error("invalid escape character EOI"); + } + return char2(bytes[offset], bytes[offset + 1]); + } + + private void checkEscapeEnd(int offset) { + if (offset >= end) { + throw error("invalid escape character EOI"); + } + } + @Override public final boolean skipName() { this.offset = skipName(this, bytes, offset, end); @@ -4685,13 +4708,13 @@ private static int skipString(JSONReaderUTF8 jsonReader, byte[] bytes, int offse * rejects; the advances themselves are unchanged. */ private static int skipEscape(JSONReaderUTF8 jsonReader, byte[] bytes, int slashIndex, int end) { - int ch = bytes[slashIndex + 1]; + int ch = jsonReader.byteAfterEscape(slashIndex + 1); if (ch == 'u') { hexDigit4(bytes, slashIndex + 2, end); return slashIndex + 6; } if (ch == 'x') { - char2(bytes[slashIndex + 2], bytes[slashIndex + 3]); + jsonReader.char2(bytes, slashIndex + 2, end); return slashIndex + 4; } if (ch != '\\' && ch != '"' && ch != 'n') { @@ -4705,7 +4728,7 @@ private static int skipStringEscaped(JSONReaderUTF8 jsonReader, byte[] bytes, in offset++; for (; ; ) { if (ch == '\\') { - ch = bytes[offset++]; + ch = jsonReader.byteAfterEscape(offset++); if (ch == 'u') { hexDigit4(bytes, offset, jsonReader.end); offset += 4; @@ -4715,7 +4738,7 @@ private static int skipStringEscaped(JSONReaderUTF8 jsonReader, byte[] bytes, in } else if (ch != '\\' && ch != '"') { jsonReader.char1(ch); } - ch = bytes[offset++]; + ch = jsonReader.byteAfterEscape(offset++); continue; } if (ch == quote) { @@ -5095,7 +5118,7 @@ public final String getString() { } if (c == '\\') { - c = (char) bytes[++offset]; + c = (char) byteAfterEscape(++offset); switch (c) { case 'u': { c = hexDigit4(bytes, offset + 1, end); @@ -5103,7 +5126,7 @@ public final String getString() { break; } case 'x': { - c = char2(bytes[offset + 1], bytes[offset + 2]); + c = char2(bytes, offset + 1, end); offset += 2; break; } @@ -5317,12 +5340,12 @@ public String readString() { if (c == quote) { break LOOP; } else { - c = bytes[offset++]; + c = byteAfterEscape(offset++); if (c == 'u') { c = (char) hexDigit4(bytes, offset, end); offset += 4; } else if (c == 'x') { - c = char2(bytes[offset], bytes[offset + 1]); + c = char2(bytes, offset, end); offset += 2; } else { c = char1(c); diff --git a/core/src/test/java/com/alibaba/fastjson2/JSONReaderUTF16Test.java b/core/src/test/java/com/alibaba/fastjson2/JSONReaderUTF16Test.java new file mode 100644 index 0000000000..8a9f4df970 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/JSONReaderUTF16Test.java @@ -0,0 +1,100 @@ +package com.alibaba.fastjson2; + +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class JSONReaderUTF16Test { + @Test + public void truncatedEscape() { + String[] inputs = { + "\"\\", + "{\"a\":\"\\", + "[\"\\", + "\"ab\\", + "\"中\\" + }; + + for (String input : inputs) { + assertThrows(JSONException.class, () -> JSON.parse(input)); + assertThrows(JSONException.class, () -> JSON.parse(input.toCharArray())); + } + } + + @Test + public void truncatedEscapePublicPaths() { + assertThrows(JSONException.class, () -> JSON.parse("{\"a\\".toCharArray())); + String[] numberInputs = { + "{\"a\":\"\\", + "{\"a\":\"\\u", + "{\"a\":\"\\x" + }; + for (String input : numberInputs) { + assertThrows(JSONException.class, () -> JSON.parseObject(input.toCharArray(), Bean.class)); + } + + assertThrows(JSONException.class, () -> JSON.parseObject("{\"x\":\"\\".toCharArray(), Bean.class)); + assertThrows( + JSONException.class, + () -> JSON.parseObject("{a\\".toCharArray(), Object.class, JSONReader.Feature.AllowUnQuotedFieldNames) + ); + assertThrows( + JSONException.class, + () -> JSON.parseObject("{a\\x".toCharArray(), Object.class, JSONReader.Feature.AllowUnQuotedFieldNames) + ); + assertThrows(JSONException.class, () -> JSON.parseObject("\"ab\\".toCharArray(), Value.class)); + + for (String input : new String[]{"\"a\\u", "\"a\\x"}) { + try (JSONReader jsonReader = JSONReader.of(input.toCharArray())) { + assertThrows(JSONException.class, jsonReader::skipName); + } + } + } + + @Test + public void truncatedEscapeInSubRange() { + char[] chars = "\"ab\\\"".toCharArray(); + try (JSONReader jsonReader = JSONReader.of(chars, 0, 4)) { + assertThrows(JSONException.class, jsonReader::readString); + } + + char[] unquotedName = "{a\\x12:1}".toCharArray(); + try (JSONReader jsonReader = JSONReader.of(unquotedName, 0, 4)) { + jsonReader.getContext().config(JSONReader.Feature.AllowUnQuotedFieldNames); + assertThrows(JSONException.class, jsonReader::readObject); + } + } + + @Test + public void truncatedEscapeUTF8() { + assertThrows(JSONException.class, () -> JSON.parse("\"中\\".getBytes(StandardCharsets.UTF_8))); + assertThrows(JSONException.class, () -> JSON.parse("{\"中\\".getBytes(StandardCharsets.UTF_8))); + assertThrows( + JSONException.class, + () -> JSON.parseObject("{\"a\":\"中\\".getBytes(StandardCharsets.UTF_8), Bean.class) + ); + assertThrows( + JSONException.class, + () -> JSON.parseObject("{\"x\":\"中\\".getBytes(StandardCharsets.UTF_8), Bean.class) + ); + assertThrows( + JSONException.class, + () -> JSON.parseObject("\"中\\".getBytes(StandardCharsets.UTF_8), Value.class) + ); + + byte[] bytes = "\"中\\\"".getBytes(StandardCharsets.UTF_8); + try (JSONReader jsonReader = JSONReader.of(bytes, 0, 5)) { + assertThrows(JSONException.class, jsonReader::readString); + } + } + + public static class Bean { + public Number a; + } + + public enum Value { + A + } +}