Skip to content
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

Fix possible regex matching stack overflow #2150

Merged
merged 2 commits into from
Aug 8, 2023
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
8 changes: 8 additions & 0 deletions core/src/main/java/feign/template/Expressions.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

public final class Expressions {

private static final int MAX_EXPRESSION_LENGTH = 10000;

private static final String PATH_STYLE_OPERATOR = ";";
/**
* Literals may be present and preceded the expression.
Expand Down Expand Up @@ -68,6 +70,12 @@ public static Expression create(final String value) {
throw new IllegalArgumentException("an expression is required.");
}

/* Check if the expression is too long */
if (expression.length() > MAX_EXPRESSION_LENGTH) {
throw new IllegalArgumentException(
"expression is too long. Max length: " + MAX_EXPRESSION_LENGTH);
}

/* create a new regular expression matcher for the expression */
String variableName = null;
String variablePattern = null;
Expand Down
12 changes: 12 additions & 0 deletions core/src/test/java/feign/template/ExpressionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.junit.jupiter.api.Test;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatObject;

public class ExpressionsTest {

Expand All @@ -27,6 +28,17 @@ public void simpleExpression() {
assertThat(expanded).isEqualToIgnoringCase("foo=bar");
}

@Test
public void malformedBodyTemplate() {
String bodyTemplate = "{" + "a".repeat(65536) + "}";

try {
BodyTemplate template = BodyTemplate.create(bodyTemplate);
} catch (Throwable e) {
assertThatObject(e).isNotInstanceOf(StackOverflowError.class);
}
}

@Test
public void androidCompatibility() {
// To match close brace on Android, it must be escaped due to the simpler ICU regex engine
Expand Down