Skip to content

Commit 857084c

Browse files
committed
[java] Reject unescaped control characters in JSON strings
RFC 8259 §7 requires characters U+0000..U+001F to be escaped inside JSON strings. readString() previously appended them silently, so inputs containing a literal newline / tab / etc. inside quotes were accepted.
1 parent 0779019 commit 857084c

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

java/src/org/openqa/selenium/json/JsonInput.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,13 @@ private String readString() {
586586
readEscape(builder);
587587
break;
588588
default:
589+
// RFC 8259 §7: characters U+0000..U+001F MUST be escaped.
590+
if (c < 0x20) {
591+
throw new JsonException(
592+
String.format(
593+
"Illegal unescaped control character U+%04X in string. %s",
594+
(int) c, input));
595+
}
589596
builder.append(c);
590597
}
591598
}

java/test/org/openqa/selenium/json/JsonInputTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@ void shouldBeAbleToReadNonWellFormedDataLongerThanReadBuffer() {
291291
}
292292
}
293293

294+
@Test
295+
void shouldRejectUnescapedControlCharactersInStrings() {
296+
// RFC 8259 §7: characters U+0000..U+001F MUST be escaped in JSON strings.
297+
// A literal newline / tab / etc. inside quotes is not valid JSON.
298+
try (JsonInput input = newInput("\"a\nb\"")) {
299+
assertThatExceptionOfType(JsonException.class)
300+
.isThrownBy(input::nextString)
301+
.withMessageStartingWith("Illegal unescaped control character");
302+
}
303+
304+
// Escaped equivalents are still fine.
305+
try (JsonInput input = newInput("\"a\\nb\"")) {
306+
assertThat(input.nextString()).isEqualTo("a\nb");
307+
}
308+
}
309+
294310
@Test
295311
void nullInputsShouldCoerceAsNullValues() throws IOException {
296312
try (InputStream is = new ByteArrayInputStream(new byte[0]);

0 commit comments

Comments
 (0)