Skip to content

Protect readLine() against DoS #22

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<grpc.version>1.35.0</grpc.version>

<air.javadoc.lint>-missing</air.javadoc.lint>
<versions.java-security-toolkit>1.2.0</versions.java-security-toolkit>
<versions.java-security-toolkit>1.2.1</versions.java-security-toolkit>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This library holds security tools for protecting Java API calls.

License: MIT ✅ | Open source ✅ | More facts

</properties>

<modules>
Expand Down Expand Up @@ -2214,6 +2214,7 @@
<dependency>
<groupId>io.github.pixee</groupId>
<artifactId>java-security-toolkit</artifactId>

<version>${versions.java-security-toolkit}</version>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import io.github.pixee.security.BoundedLineReader;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -53,7 +54,7 @@ private TestingAtop(InputStream dataStream, ZonedDateTime date)
this.date = date;
this.reader = new BufferedReader(new InputStreamReader(dataStream));
try {
line = reader.readLine();
line = BoundedLineReader.readLine(reader, 5_000_000);
}
catch (IOException e) {
line = null;
Expand All @@ -74,7 +75,7 @@ public String next()
}
String currentLine = line;
try {
line = reader.readLine();
line = BoundedLineReader.readLine(reader, 5_000_000);
}
catch (IOException e) {
line = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.common.collect.ImmutableList;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import io.github.pixee.security.BoundedLineReader;
import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -115,7 +116,7 @@ public Page getNextPage()
handle -> ((DruidColumnHandle) handle).getColumnName().equals("errorMessage"));
try {
String readLine;
while ((readLine = responseStream.readLine()) != null) {
while ((readLine = BoundedLineReader.readLine(responseStream, 5_000_000)) != null) {
// if read a blank line,it means read finish
if (readLine.isEmpty()) {
finished = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.druid.segment;

import com.facebook.presto.spi.PrestoException;
import io.github.pixee.security.BoundedLineReader;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -80,7 +81,7 @@ private void loadSmooshFileMetadata()
try {
byte[] metadata = indexFileSource.readFile(SMOOSH_METADATA_FILE_NAME);
BufferedReader in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(metadata)));
String line = in.readLine();
String line = BoundedLineReader.readLine(in, 5_000_000);
if (line == null) {
throw new PrestoException(DRUID_SEGMENT_LOAD_ERROR, format("Malformed metadata file: first line should be version,maxChunkSize,numChunks, got null."));
}
Expand All @@ -94,7 +95,7 @@ private void loadSmooshFileMetadata()
}

while (true) {
line = in.readLine();
line = BoundedLineReader.readLine(in, 5_000_000);
if (line == null) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.collect.Iterables;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import io.github.pixee.security.BoundedLineReader;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -319,7 +320,7 @@ public List<String> readFields()
if (reader == null) {
return null;
}
String line = reader.readLine();
String line = BoundedLineReader.readLine(reader, 5_000_000);
if (line != null) {
fields = LINE_SPLITTER.splitToList(line);
if (!newReader || meetsPredicate(fields)) {
Expand Down
4 changes: 4 additions & 0 deletions presto-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@
<artifactId>ratis-common</artifactId>
<optional>true</optional>
</dependency>
<dependency>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This library holds security tools for protecting Java API calls.

License: MIT ✅ | Open source ✅ | More facts

<groupId>io.github.pixee</groupId>
<artifactId>java-security-toolkit</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.airlift.slice.InputStreamSliceInput;
import io.airlift.slice.SliceInput;
import io.airlift.units.DataSize;
import io.github.pixee.security.BoundedLineReader;

import javax.annotation.concurrent.ThreadSafe;

Expand Down Expand Up @@ -161,7 +162,7 @@ public PagesResponse handle(Request request, Response response)
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getInputStream(), UTF_8))) {
// Get up to 1000 lines for debugging
for (int i = 0; i < 1000; i++) {
String line = reader.readLine();
String line = BoundedLineReader.readLine(reader, 5_000_000);
// Don't output more than 100KB
if (line == null || body.length() + line.length() > 100 * 1024) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import io.airlift.units.Duration;
import io.github.pixee.security.BoundedLineReader;
import io.github.pixee.security.SystemCommand;
import org.apache.spark.SparkEnv$;
import org.apache.spark.SparkFiles;
Expand Down Expand Up @@ -443,7 +444,7 @@ public void run()
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8))) {
String line;
boolean aborted = false;
while ((line = reader.readLine()) != null) {
while ((line = BoundedLineReader.readLine(reader, 5_000_000)) != null) {
if (!aborted && line.startsWith("*** Aborted")) {
aborted = true;
}
Expand Down