Skip to content

Commit 2d55d8c

Browse files
committed
Review: Disallow parsing of unixtimestamps with non UTC timezone
1 parent 6e63cb4 commit 2d55d8c

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

src/main/java/org/elasticsearch/common/joda/Joda.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,11 @@ public int estimateParsedLength() {
218218

219219
@Override
220220
public int parseInto(DateTimeParserBucket bucket, String text, int position) {
221-
if (text.length() > estimateParsedLength() || pattern.matcher(text).matches() == false) {
222-
return 0;
221+
if (text.length() > estimateParsedLength() ||
222+
// timestamps have to have UTC timezone
223+
bucket.getZone() != DateTimeZone.UTC ||
224+
pattern.matcher(text).matches() == false) {
225+
return -1;
223226
}
224227

225228
int factor = hasMilliSecondPrecision ? 1 : 1000;
@@ -235,7 +238,7 @@ public int parseInto(DateTimeParserBucket bucket, String text, int position) {
235238
bucket.saveField(DateTimeFieldType.millisOfSecond(), dt.getMillisOfSecond());
236239
bucket.setZone(DateTimeZone.UTC);
237240
} catch (Exception e) {
238-
return 0;
241+
return -1;
239242
}
240243
return text.length();
241244
}

src/main/java/org/elasticsearch/index/mapper/internal/TimestampFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper
5757

5858
public static final String NAME = "_timestamp";
5959
public static final String CONTENT_TYPE = "_timestamp";
60-
public static final String DEFAULT_DATE_TIME_FORMAT = "epoch_second_millis||dateOptionalTime";
60+
public static final String DEFAULT_DATE_TIME_FORMAT = "epoch_millis||dateOptionalTime";
6161

6262
public static class Defaults extends DateFieldMapper.Defaults {
6363
public static final String NAME = "_timestamp";

src/test/java/org/elasticsearch/common/joda/DateMathParserTests.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
import org.elasticsearch.ExceptionsHelper;
2424
import org.elasticsearch.test.ElasticsearchTestCase;
2525
import org.joda.time.DateTimeZone;
26+
import org.junit.Test;
2627

28+
import java.util.TimeZone;
2729
import java.util.concurrent.Callable;
28-
import java.util.concurrent.TimeUnit;
2930
import java.util.concurrent.atomic.AtomicBoolean;
3031

3132
import static org.hamcrest.Matchers.equalTo;
3233

3334
public class DateMathParserTests extends ElasticsearchTestCase {
35+
3436
FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime||epoch_millis");
3537
DateMathParser parser = new DateMathParser(formatter);
3638

@@ -195,9 +197,6 @@ public void testRounding() {
195197
public void testTimestamps() {
196198
assertDateMathEquals("1418248078000", "2014-12-10T21:47:58.000");
197199

198-
// timezone does not affect timestamps
199-
assertDateMathEquals("1418248078000", "2014-12-10T21:47:58.000", 0, false, DateTimeZone.forID("-08:00"));
200-
201200
// datemath still works on timestamps
202201
assertDateMathEquals("1418248078000||/m", "2014-12-10T21:47:00.000");
203202

@@ -250,4 +249,10 @@ public Long call() throws Exception {
250249
parser.parse("now/d", now, false, null);
251250
assertTrue(called.get());
252251
}
252+
253+
@Test(expected = ElasticsearchParseException.class)
254+
public void testThatUnixTimestampMayNotHaveTimeZone() {
255+
DateMathParser parser = new DateMathParser(Joda.forPattern("epoch_millis"));
256+
parser.parse("1234567890123", callable(42), false, DateTimeZone.forTimeZone(TimeZone.getTimeZone("CET")));
257+
}
253258
}

src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,15 @@ public void testThatEpochsInSecondsCanBeParsed() {
272272
}
273273

274274
@Test(expected = IllegalArgumentException.class)
275-
public void testForInvalidDates() {
276-
FormatDateTimeFormatter formatter = Joda.forPattern(randomBoolean() ? "epoch_second" : "epoch_millis");
277-
formatter.parser().parseDateTime(randomFrom("invalid date", "12345678901", "123456789", "12345678901234"));
275+
public void testForInvalidDatesInEpochSecond() {
276+
FormatDateTimeFormatter formatter = Joda.forPattern("epoch_second");
277+
formatter.parser().parseDateTime(randomFrom("invalid date", "12345678901", "12345678901234"));
278+
}
279+
280+
@Test(expected = IllegalArgumentException.class)
281+
public void testForInvalidDatesInEpochMillis() {
282+
FormatDateTimeFormatter formatter = Joda.forPattern("epoch_millis");
283+
formatter.parser().parseDateTime(randomFrom("invalid date", "12345678901234"));
278284
}
279285

280286
private long utcTimeInMillis(String time) {

0 commit comments

Comments
 (0)