Skip to content

Commit 837dcd5

Browse files
authored
Make Expressions max length configurable (#3422)
The maximum length of a single template expression was hard-coded to 10000 characters in feign.template.Expressions, throwing IllegalArgumentException for anything longer with no way to opt out. Make the limit configurable through the "feign.template.expression.maxLength" system property (default 10000), and allow disabling the check entirely by setting it to a non-positive value. Fixes #2916 Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>
1 parent 247fb46 commit 837dcd5

2 files changed

Lines changed: 57 additions & 4 deletions

File tree

core/src/main/java/feign/template/Expressions.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525

2626
public final class Expressions {
2727

28-
private static final int MAX_EXPRESSION_LENGTH = 10000;
28+
/**
29+
* System property controlling the maximum allowed length of a single expression. Defaults to
30+
* {@link #DEFAULT_MAX_EXPRESSION_LENGTH}. Setting it to {@code 0} (or any non-positive value)
31+
* disables the length check entirely.
32+
*/
33+
static final String MAX_EXPRESSION_LENGTH_PROPERTY = "feign.template.expression.maxLength";
34+
35+
private static final int DEFAULT_MAX_EXPRESSION_LENGTH = 10000;
2936

3037
private static final String PATH_STYLE_OPERATOR = ";";
3138

@@ -73,10 +80,16 @@ public static Expression create(final String value) {
7380
throw new IllegalArgumentException("an expression is required.");
7481
}
7582

76-
/* Check if the expression is too long */
77-
if (expression.length() > MAX_EXPRESSION_LENGTH) {
83+
/*
84+
* Check if the expression is too long. The limit is configurable through the
85+
* "feign.template.expression.maxLength" system property and can be disabled by setting it to a
86+
* non-positive value.
87+
*/
88+
final int maxExpressionLength =
89+
Integer.getInteger(MAX_EXPRESSION_LENGTH_PROPERTY, DEFAULT_MAX_EXPRESSION_LENGTH);
90+
if (maxExpressionLength > 0 && expression.length() > maxExpressionLength) {
7891
throw new IllegalArgumentException(
79-
"expression is too long. Max length: " + MAX_EXPRESSION_LENGTH);
92+
"expression is too long. Max length: " + maxExpressionLength);
8093
}
8194

8295
/* create a new regular expression matcher for the expression */

core/src/test/java/feign/template/ExpressionsTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,53 @@
1616
package feign.template;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatNoException;
1920
import static org.assertj.core.api.Assertions.assertThatObject;
21+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2022

2123
import java.util.Collections;
24+
import org.junit.jupiter.api.AfterEach;
2225
import org.junit.jupiter.api.Test;
2326

2427
class ExpressionsTest {
2528

29+
@AfterEach
30+
void clearMaxExpressionLengthProperty() {
31+
System.clearProperty(Expressions.MAX_EXPRESSION_LENGTH_PROPERTY);
32+
}
33+
34+
@Test
35+
void tooLongExpressionFailsWithDefaultLimit() {
36+
String tooLong = "{" + "a".repeat(10001) + "}";
37+
assertThatThrownBy(() -> Expressions.create(tooLong))
38+
.isInstanceOf(IllegalArgumentException.class)
39+
.hasMessageContaining("expression is too long");
40+
}
41+
42+
@Test
43+
void maxExpressionLengthIsConfigurable() {
44+
System.setProperty(Expressions.MAX_EXPRESSION_LENGTH_PROPERTY, "5");
45+
assertThatThrownBy(() -> Expressions.create("{foobar}"))
46+
.isInstanceOf(IllegalArgumentException.class)
47+
.hasMessageContaining("Max length: 5");
48+
}
49+
50+
@Test
51+
void lengthCheckCanBeDisabled() {
52+
// An expression well beyond the default 10000 limit, expressed as a name plus a regular
53+
// expression value modifier so the disabled length check is exercised in isolation.
54+
String longExpression = "{name:" + "a".repeat(15000) + "}";
55+
assertThatThrownBy(() -> Expressions.create(longExpression))
56+
.as("guarded by default limit")
57+
.isInstanceOf(IllegalArgumentException.class)
58+
.hasMessageContaining("expression is too long");
59+
60+
System.setProperty(Expressions.MAX_EXPRESSION_LENGTH_PROPERTY, "0");
61+
assertThatNoException()
62+
.as("length check disabled")
63+
.isThrownBy(() -> Expressions.create(longExpression));
64+
}
65+
2666
@Test
2767
void simpleExpression() {
2868
Expression expression = Expressions.create("{foo}");

0 commit comments

Comments
 (0)