Skip to content

Commit 089b741

Browse files
committed
Also add limitng for ReplaceFilter
1 parent 0079405 commit 089b741

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/main/java/com/hubspot/jinjava/lib/filter/ReplaceFilter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
44
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
55
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
6+
import com.hubspot.jinjava.interpret.InvalidInputException;
7+
import com.hubspot.jinjava.interpret.InvalidReason;
68
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
79
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
810
import org.apache.commons.lang3.StringUtils;
@@ -66,6 +68,17 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args)
6668
}
6769

6870
String s = var.toString();
71+
long maxStringLength = interpreter.getConfig().getMaxStringLength();
72+
if (maxStringLength > 0 && s.length() > maxStringLength) {
73+
throw new InvalidInputException(
74+
interpreter,
75+
this,
76+
InvalidReason.LENGTH,
77+
s.length(),
78+
maxStringLength
79+
);
80+
}
81+
6982
String toReplace = args[0];
7083
String replaceWith = args[1];
7184
Integer count = null;

src/test/java/com/hubspot/jinjava/lib/filter/ReplaceFilterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.hubspot.jinjava.lib.filter;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
45

56
import com.hubspot.jinjava.BaseInterpretingTest;
7+
import com.hubspot.jinjava.Jinjava;
8+
import com.hubspot.jinjava.JinjavaConfig;
69
import com.hubspot.jinjava.interpret.InterpretException;
10+
import com.hubspot.jinjava.interpret.InvalidInputException;
711
import com.hubspot.jinjava.objects.SafeString;
812
import org.junit.Before;
913
import org.junit.Test;
@@ -52,4 +56,22 @@ public void replaceBoolean() {
5256
assertThat(filter.filter(true, interpreter, "true", "TRUEEE").toString())
5357
.isEqualTo("TRUEEE");
5458
}
59+
60+
@Test
61+
public void itLimitsLongInput() {
62+
assertThatThrownBy(
63+
() ->
64+
filter.filter(
65+
"123456789OO",
66+
new Jinjava(JinjavaConfig.newBuilder().withMaxStringLength(10).build())
67+
.newInterpreter(),
68+
"O",
69+
"0"
70+
)
71+
)
72+
.isInstanceOf(InvalidInputException.class)
73+
.hasMessageContaining(
74+
"Invalid input for 'replace': input with length '11' exceeds maximum allowed length of '10'"
75+
);
76+
}
5577
}

0 commit comments

Comments
 (0)