Skip to content

Commit fd2cd1c

Browse files
authored
SQL: supplement input checks on received request parameters (#52229) (#52276)
* SQL: supplement input checks on received request parameters (#52229) * Add more checks around parameter conversions This commit adds two necessary verifications on received parameters: - it checks the validity of the parameter's data type: if the declared data type is resolved to an ES or Java type; - it checks if the returned converter is non-null (i.e. a conversion is possible) and generates an appropriate exception otherwise. (cherry picked from commit eda30ac)
1 parent 5cf25e1 commit fd2cd1c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

x-pack/plugin/sql/qa/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/RestSqlIT.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,20 @@ public void testErrorMessageForTranslatingSQLCommandStatement() throws IOExcepti
3939
containsString("Cannot generate a query DSL for a special SQL command " +
4040
"(e.g.: DESCRIBE, SHOW), sql statement: [SHOW FUNCTIONS]"));
4141
}
42+
43+
public void testErrorMessageForInvalidParamDataType() throws IOException {
44+
expectBadRequest(() -> runTranslateSql(
45+
"{\"query\":\"SELECT null WHERE 0 = ? \", \"mode\": \"odbc\", \"params\":[{\"type\":\"invalid\", \"value\":\"irrelevant\"}]}"
46+
),
47+
containsString("Cannot cast value [irrelevant] of type [KEYWORD] to parameter type [UNSUPPORTED]")
48+
);
49+
}
50+
51+
public void testErrorMessageForInvalidParamSpec() throws IOException {
52+
expectBadRequest(() -> runTranslateSql(
53+
"{\"query\":\"SELECT null WHERE 0 = ? \", \"mode\": \"odbc\", \"params\":[{\"type\":\"SHAPE\", \"value\":false}]}"
54+
),
55+
containsString("Cannot cast value [false] of type [BOOLEAN] to parameter type [SHAPE]")
56+
);
57+
}
4258
}

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/ExpressionBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128

129129
import static java.util.Collections.emptyList;
130130
import static java.util.Collections.singletonList;
131+
import static org.elasticsearch.xpack.sql.type.DataTypeConversion.canConvert;
131132
import static org.elasticsearch.xpack.sql.type.DataTypeConversion.conversionFor;
132133
import static org.elasticsearch.xpack.sql.util.DateUtils.asDateOnly;
133134
import static org.elasticsearch.xpack.sql.util.DateUtils.asTimeOnly;
@@ -716,6 +717,10 @@ public Literal visitParamLiteral(ParamLiteralContext ctx) {
716717
}
717718
// otherwise we need to make sure that xcontent-serialized value is converted to the correct type
718719
try {
720+
if (canConvert(sourceType, dataType) == false) {
721+
throw new ParsingException(source, "Cannot cast value [{}] of type [{}] to parameter type [{}]", param.value, sourceType,
722+
dataType);
723+
}
719724
return new Literal(source, conversionFor(sourceType, dataType).convert(param.value), dataType);
720725
} catch (SqlIllegalArgumentException ex) {
721726
throw new ParsingException(ex, source, "Unexpected actual parameter type [{}] for type [{}]", sourceType, param.type);

0 commit comments

Comments
 (0)