Skip to content
Open
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
19 changes: 17 additions & 2 deletions src/main/java/org/owasp/esapi/reference/DefaultValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,11 @@ public String getValidRedirectLocation(String context, String input, boolean all
* {@inheritDoc}
* <p>
* This implementation reads until a newline or the specified number of
* characters.
* characters. A line may end with LF, CR, or CRLF. A CRLF pair is only
* consumed as one terminator when the stream supports
* {@link InputStream#mark(int)}, and doing so moves the stream's mark.
* Wrap the stream in a {@link java.io.BufferedInputStream} if it does not
* support marks.
*/
@Override
public String safeReadLine(InputStream in, int max) throws ValidationException {
Expand All @@ -1281,7 +1285,18 @@ public String safeReadLine(InputStream in, int max) throws ValidationException {
}
break;
}
if (c == '\n' || c == '\r') {
if (c == '\n') {
break;
}
if (c == '\r') {
// Treat CRLF as one line terminator. Without this, the LF is
// left in the stream and the next call returns an empty line.
if (in.markSupported()) {
in.mark(1);
if (in.read() != '\n') {
in.reset();
}
}
break;
}
count++;
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/owasp/esapi/reference/ValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,29 @@ public void testSafeReadLine() {
}
}

@Test
public void testSafeReadLineLineTerminators() throws Exception {
Validator instance = ESAPI.validator();
byte[] bytes = "one\r\ntwo\nthree\rfour\r\n\r\nsix".getBytes(PREFERRED_ENCODING);
ByteArrayInputStream s = new ByteArrayInputStream(bytes);
assertEquals("one", instance.safeReadLine(s, 20));
assertEquals("two", instance.safeReadLine(s, 20));
assertEquals("three", instance.safeReadLine(s, 20));
assertEquals("four", instance.safeReadLine(s, 20));
assertEquals("", instance.safeReadLine(s, 20));
assertEquals("six", instance.safeReadLine(s, 20));
assertNull(instance.safeReadLine(s, 20));

// Same lines as BufferedReader.readLine()
BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes), PREFERRED_ENCODING));
ByteArrayInputStream t = new ByteArrayInputStream(bytes);
String expected;
while ((expected = br.readLine()) != null) {
assertEquals(expected, instance.safeReadLine(t, 20));
}
assertNull(instance.safeReadLine(t, 20));
}

@Test
public void testIssue82_SafeString_Bad_Regex() {
Validator instance = ESAPI.validator();
Expand Down