|
5 | 5 | */ |
6 | 6 | package org.elasticsearch.xpack.sql.parser; |
7 | 7 |
|
| 8 | +import com.google.common.base.Joiner; |
8 | 9 | import org.elasticsearch.test.ESTestCase; |
9 | 10 | import org.elasticsearch.xpack.sql.expression.NamedExpression; |
10 | 11 | import org.elasticsearch.xpack.sql.expression.Order; |
|
22 | 23 | import java.util.ArrayList; |
23 | 24 | import java.util.List; |
24 | 25 |
|
| 26 | +import static java.util.Collections.nCopies; |
25 | 27 | import static java.util.stream.Collectors.toList; |
26 | 28 | import static org.hamcrest.Matchers.hasEntry; |
27 | 29 | import static org.hamcrest.Matchers.hasSize; |
@@ -136,6 +138,88 @@ public void testMultiMatchQuery() { |
136 | 138 | assertThat(mmqp.optionMap(), hasEntry("fuzzy_rewrite", "scoring_boolean")); |
137 | 139 | } |
138 | 140 |
|
| 141 | + public void testLimitToPreventStackOverflowFromLargeUnaryBooleanExpression() { |
| 142 | + // Create expression in the form of NOT(NOT(NOT ... (b) ...) |
| 143 | + |
| 144 | + // 40 elements is ok |
| 145 | + new SqlParser().createExpression( |
| 146 | + Joiner.on("").join(nCopies(40, "NOT(")).concat("b").concat(Joiner.on("").join(nCopies(40, ")")))); |
| 147 | + |
| 148 | + // 100 elements parser's "circuit breaker" is triggered |
| 149 | + ParsingException e = expectThrows(ParsingException.class, () -> new SqlParser().createExpression( |
| 150 | + Joiner.on("").join(nCopies(100, "NOT(")).concat("b").concat(Joiner.on("").join(nCopies(100, ")"))))); |
| 151 | + assertEquals("expression is too large to parse, (tree's depth exceeds 100)", e.getErrorMessage()); |
| 152 | + } |
| 153 | + |
| 154 | + public void testLimitToPreventStackOverflowFromLargeBinaryBooleanExpression() { |
| 155 | + // Create expression in the form of a = b OR a = b OR ... a = b |
| 156 | + |
| 157 | + // 50 elements is ok |
| 158 | + new SqlParser().createExpression(Joiner.on(" OR ").join(nCopies(50, "a = b"))); |
| 159 | + |
| 160 | + // 100 elements parser's "circuit breaker" is triggered |
| 161 | + ParsingException e = expectThrows(ParsingException.class, () -> |
| 162 | + new SqlParser().createExpression(Joiner.on(" OR ").join(nCopies(100, "a = b")))); |
| 163 | + assertEquals("expression is too large to parse, (tree's depth exceeds 100)", e.getErrorMessage()); |
| 164 | + } |
| 165 | + |
| 166 | + public void testLimitToPreventStackOverflowFromLargeUnaryArithmeticExpression() { |
| 167 | + // Create expression in the form of abs(abs(abs ... (i) ...) |
| 168 | + |
| 169 | + // 50 elements is ok |
| 170 | + new SqlParser().createExpression( |
| 171 | + Joiner.on("").join(nCopies(50, "abs(")).concat("i").concat(Joiner.on("").join(nCopies(50, ")")))); |
| 172 | + |
| 173 | + // 101 elements parser's "circuit breaker" is triggered |
| 174 | + ParsingException e = expectThrows(ParsingException.class, () -> new SqlParser().createExpression( |
| 175 | + Joiner.on("").join(nCopies(101, "abs(")).concat("i").concat(Joiner.on("").join(nCopies(101, ")"))))); |
| 176 | + assertEquals("expression is too large to parse, (tree's depth exceeds 100)", e.getErrorMessage()); |
| 177 | + } |
| 178 | + |
| 179 | + public void testLimitToPreventStackOverflowFromLargeBinaryArithmeticExpression() { |
| 180 | + // Create expression in the form of a + a + a + ... + a |
| 181 | + |
| 182 | + // 100 elements is ok |
| 183 | + new SqlParser().createExpression(Joiner.on(" + ").join(nCopies(100, "a"))); |
| 184 | + |
| 185 | + // 101 elements parser's "circuit breaker" is triggered |
| 186 | + ParsingException e = expectThrows(ParsingException.class, () -> |
| 187 | + new SqlParser().createExpression(Joiner.on(" + ").join(nCopies(101, "a")))); |
| 188 | + assertEquals("expression is too large to parse, (tree's depth exceeds 100)", e.getErrorMessage()); |
| 189 | + } |
| 190 | + |
| 191 | + public void testLimitToPreventStackOverflowFromLargeSubselectTree() { |
| 192 | + // Test with queries in the form of `SELECT * FROM (SELECT * FROM (... t) ...) |
| 193 | + |
| 194 | + // 100 elements is ok |
| 195 | + new SqlParser().createStatement( |
| 196 | + Joiner.on(" (").join(nCopies(100, "SELECT * FROM")) |
| 197 | + .concat("t") |
| 198 | + .concat(Joiner.on("").join(nCopies(99, ")")))); |
| 199 | + |
| 200 | + // 101 elements parser's "circuit breaker" is triggered |
| 201 | + ParsingException e = expectThrows(ParsingException.class, () -> new SqlParser().createStatement( |
| 202 | + Joiner.on(" (").join(nCopies(101, "SELECT * FROM")) |
| 203 | + .concat("t") |
| 204 | + .concat(Joiner.on("").join(nCopies(100, ")"))))); |
| 205 | + assertEquals("expression is too large to parse, (tree's depth exceeds 100)", e.getErrorMessage()); |
| 206 | + } |
| 207 | + |
| 208 | + public void testLimitToPreventStackOverflowFromLargeComplexSubselectTree() { |
| 209 | + // Test with queries in the form of `SELECT true OR true OR .. FROM (SELECT true OR true OR... FROM (... t) ...) |
| 210 | + |
| 211 | + new SqlParser().createStatement( |
| 212 | + Joiner.on(" (").join(nCopies(20, "SELECT ")). |
| 213 | + concat(Joiner.on(" OR ").join(nCopies(50, "true"))).concat(" FROM") |
| 214 | + .concat("t").concat(Joiner.on("").join(nCopies(19, ")")))); |
| 215 | + |
| 216 | + ParsingException e = expectThrows(ParsingException.class, () -> new SqlParser().createStatement( |
| 217 | + Joiner.on(" (").join(nCopies(20, "SELECT ")). |
| 218 | + concat(Joiner.on(" OR ").join(nCopies(100, "true"))).concat(" FROM") |
| 219 | + .concat("t").concat(Joiner.on("").join(nCopies(19, ")"))))); |
| 220 | + assertEquals("expression is too large to parse, (tree's depth exceeds 100)", e.getErrorMessage()); |
| 221 | + } |
| 222 | + |
139 | 223 | private LogicalPlan parseStatement(String sql) { |
140 | 224 | return new SqlParser().createStatement(sql); |
141 | 225 | } |
|
0 commit comments