Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit a9538cb

Browse files
authored
Merge #624 and #605 from mainline (#656)
* Issue 623, fix security vulnerability regarding to depedencies commons-codec and Guava * Issue 580, Using UTC as default timezone for date_format function if not provided
1 parent 3fd35b1 commit a9538cb

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

build.gradle

+11-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ configurations {
206206
extendsFrom = extendsFrom.findAll { it != configurations.antlr }
207207
}
208208
}
209+
configurations.all {
210+
// enforce 1.1.3, https://www.whitesourcesoftware.com/vulnerability-database/WS-2019-0379
211+
resolutionStrategy.force 'commons-codec:commons-codec:1.13'
212+
}
209213

210214
check.dependsOn jacocoTestReport
211215

@@ -248,7 +252,13 @@ dependencies {
248252
compile group: 'org.locationtech.spatial4j', name: 'spatial4j', version:'0.7'
249253
compile group: "org.elasticsearch.plugin", name: 'parent-join-client', version: "${es_version}"
250254
compile group: "org.elasticsearch.plugin", name: 'reindex-client', version: "${es_version}"
251-
compile group: 'com.google.guava', name: 'guava', version:'15.0'
255+
constraints {
256+
implementation('commons-codec:commons-codec:1.13') {
257+
because 'https://www.whitesourcesoftware.com/vulnerability-database/WS-2019-0379'
258+
}
259+
}
260+
// https://github.com/google/guava/wiki/CVE-2018-10237
261+
compile group: 'com.google.guava', name: 'guava', version:'25.0-jre'
252262
compile group: 'org.json', name: 'json', version:'20180813'
253263
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
254264

src/main/java/com/amazon/opendistroforelasticsearch/sql/query/maker/Maker.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@
7676

7777
public abstract class Maker {
7878

79+
/**
80+
* UTC.
81+
*/
82+
private static final ZoneId UTC = ZoneId.of("UTC");
83+
7984
public static final Object NONE = new Object();
8085

8186
public static final Set<String> queryFunctions = Sets.newHashSet(
@@ -409,7 +414,8 @@ private ToXContent makeForDateFormat(SQLMethodInvokeExpr nameExpr, SQLCharExpr v
409414
if (params.size() > 2) {
410415
zoneId = ZoneId.of(removeSingleQuote(params.get(2).toString())).toString();
411416
} else {
412-
zoneId = ZoneId.systemDefault().toString();
417+
// Using UTC, if there is no Zone provided.
418+
zoneId = UTC.getId();
413419
}
414420

415421
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery(field).format(format).timeZone(zoneId);

src/main/java/com/amazon/opendistroforelasticsearch/sql/utils/SQLFunctions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ private Tuple<String, String> date_format(SQLExpr field, String pattern, String
527527
String name = nextId("date_format");
528528
if (valueName == null) {
529529
return new Tuple<>(name, "def " + name + " = DateTimeFormatter.ofPattern('" + pattern + "').withZone("
530-
+ (zoneId != null ? "ZoneId.of('" + zoneId + "')" : "ZoneId.systemDefault()")
530+
+ (zoneId != null ? "ZoneId.of('" + zoneId + "')" : "ZoneId.of(\"UTC\")")
531531
+ ").format(Instant.ofEpochMilli(" + getPropertyOrValue(field) + ".toInstant().toEpochMilli()))");
532532
} else {
533533
return new Tuple<>(name, exprString(field) + "; "

src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/DateFormatIT.java

+36-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
import java.util.stream.Collectors;
3434
import java.util.stream.IntStream;
3535

36+
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.rows;
37+
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.schema;
38+
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.verifyDataRows;
39+
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.verifySchema;
3640
import static org.hamcrest.Matchers.contains;
3741
import static org.hamcrest.Matchers.is;
3842

@@ -112,6 +116,17 @@ public void and() throws SqlParseException{
112116
);
113117
}
114118

119+
@Test
120+
public void andWithDefaultTimeZone() throws SqlParseException {
121+
assertThat(
122+
dateQuery(SELECT_FROM +
123+
"WHERE date_format(insert_time, 'yyyy-MM-dd HH:mm:ss') >= '2014-08-17 16:13:12' " +
124+
"AND date_format(insert_time, 'yyyy-MM-dd HH:mm:ss') <= '2014-08-17 16:13:13'",
125+
"yyyy-MM-dd HH:mm:ss"),
126+
contains("2014-08-17 16:13:12")
127+
);
128+
}
129+
115130
@Test
116131
public void or() throws SqlParseException {
117132
assertThat(
@@ -152,6 +167,17 @@ public void sortByAliasedDateFormat() throws IOException {
152167
is(new DateTime("2014-08-24T00:00:41.221Z", DateTimeZone.UTC)));
153168
}
154169

170+
@Test
171+
public void selectDateTimeWithDefaultTimeZone() throws SqlParseException {
172+
JSONObject response = executeJdbcRequest("SELECT date_format(insert_time, 'yyyy-MM-dd') as date " +
173+
" FROM " + TestsConstants.TEST_INDEX_ONLINE +
174+
" WHERE date_format(insert_time, 'yyyy-MM-dd HH:mm:ss') >= '2014-08-17 16:13:12' " +
175+
" AND date_format(insert_time, 'yyyy-MM-dd HH:mm:ss') <= '2014-08-17 16:13:13'");
176+
177+
verifySchema(response, schema("date", "", "text"));
178+
verifyDataRows(response, rows("2014-08-17"));
179+
}
180+
155181
@Test
156182
public void groupByAndSort() throws IOException {
157183
JSONObject aggregations = executeQuery(
@@ -203,17 +229,19 @@ private void checkAggregations(JSONObject aggregations, String key, Ordering<Com
203229
}
204230

205231
private Set<Object> dateQuery(String sql) throws SqlParseException {
232+
return dateQuery(sql, TestsConstants.SIMPLE_DATE_FORMAT);
233+
}
234+
235+
private Set<Object> dateQuery(String sql, String format) throws SqlParseException {
206236
try {
207237
JSONObject response = executeQuery(sql);
208-
return getResult(response, "insert_time");
238+
return getResult(response, "insert_time", DateTimeFormat.forPattern(format));
209239
} catch (IOException e) {
210240
throw new SqlParseException(String.format("Unable to process query '%s'", sql));
211241
}
212242
}
213243

214-
private Set<Object> getResult(JSONObject response, String fieldName) {
215-
DateTimeFormatter formatter = DateTimeFormat.forPattern(TestsConstants.SIMPLE_DATE_FORMAT);
216-
244+
private Set<Object> getResult(JSONObject response, String fieldName, DateTimeFormatter formatter) {
217245
JSONArray hits = getHits(response);
218246
Set<Object> result = new TreeSet<>(); // Using TreeSet so order is maintained
219247
for (int i = 0; i < hits.length(); i++) {
@@ -227,4 +255,8 @@ private Set<Object> getResult(JSONObject response, String fieldName) {
227255

228256
return result;
229257
}
258+
259+
private JSONObject executeJdbcRequest(String query) {
260+
return new JSONObject(executeQuery(query, "jdbc"));
261+
}
230262
}

0 commit comments

Comments
 (0)