PushCertificateIdent: Use Instant/ZoneId instead of old Date API Errorprone suggests to replace the old java.util.Date API with the new java.time.Instant and related classes. Timezone handling is problematic: existing code converts the "+-hhmm" text into minutes (without checking ranges) and formats it back to hhmm for java.util.TimeZone when needed. The first step accepts "+0099" as 99 minutes (and becomes GMT+0139). The second accepts hours in the [-23, 23] range, even when no timezone is beyond [-12, 14] (and the new java.time.ZoneOffset only accepts [-18, 18]). We could try to replicate the old parsing, but the results are as arbitrary as simply defaulting to UTC for invalid timezones. It is not even clear if this timezone is useful. The spec of the git protocol [1] says: > pusher ident > Identify the GPG key in "Human Readable Name <email@address>" format. [1] https://git-scm.com/docs/pack-protocol#_push_certificate Change-Id: I9f0e6113322e20a8be1099d33726846ac84b9f74
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushCertificateIdentTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushCertificateIdentTest.java index 6290b79..2d39fad 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushCertificateIdentTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushCertificateIdentTest.java
@@ -139,48 +139,66 @@ public void incompleteCasesMatchPersonIdent() throws Exception { } @Test - public void timezoneRange_hours() { - int HOUR_TO_MS = 60 * 60 * 1000; + public void timezoneRange_hours_ok() { + PushCertificateIdent ident = PushCertificateIdent + .parse("A U. Thor <a_u_thor@example.com> 1218123387 +1000"); + assertEquals(10 * 60, ident.getTimeZoneOffset()); + assertEquals(10 * 60 * 60 * 1000, + ident.getTimeZone().getOffset(1218123387)); + } - // java.util.TimeZone: Hours must be between 0 to 23 - PushCertificateIdent hourLimit = PushCertificateIdent + /* + * Real timezones are in the [-12, 14] range of hours. + * + * Before 7.2, PushCertificateIdent used java.util.Timezone, which accepted + * hours between 0 and 24. Now java.util.ZoneOffset accepts only hours + * between 0 and 18. + * + * Invalid timezones default to UTC. + */ + @Test + public void getTimeZone_hours_unrealistic_utc() { + PushCertificateIdent ident = PushCertificateIdent .parse("A U. Thor <a_u_thor@example.com> 1218123387 +2300"); - assertEquals(1380, hourLimit.getTimeZoneOffset()); - assertEquals(23 * HOUR_TO_MS, - hourLimit.getTimeZone().getOffset(1218123387)); - - PushCertificateIdent hourDubious = PushCertificateIdent - .parse("A U. Thor <a_u_thor@example.com> 1218123387 +2400"); - assertEquals(1440, hourDubious.getTimeZoneOffset()); - assertEquals(0, hourDubious.getTimeZone().getOffset(1218123387)); + assertEquals(0, ident.getTimeZoneOffset()); + assertEquals(0, ident.getTimeZone().getOffset(1218123387)); } @Test - public void timezoneRange_minutes() { - PushCertificateIdent hourLimit = PushCertificateIdent + public void getTimeZone_hours_tooBig_utc() { + PushCertificateIdent ident = PushCertificateIdent + .parse("A U. Thor <a_u_thor@example.com> 1218123387 +2500"); + assertEquals(0, ident.getTimeZoneOffset()); + assertEquals(0, ident.getTimeZone().getOffset(1218123387)); + } + + @Test + public void getTimeZone_minutes_ok() { + PushCertificateIdent ident = PushCertificateIdent .parse("A U. Thor <a_u_thor@example.com> 1218123387 +0059"); - assertEquals(59, hourLimit.getTimeZoneOffset()); + assertEquals(59, ident.getTimeZoneOffset()); assertEquals(59 * 60 * 1000, - hourLimit.getTimeZone().getOffset(1218123387)); + ident.getTimeZone().getOffset(1218123387)); + } - // This becomes one hour and one minute (!) - PushCertificateIdent hourDubious = PushCertificateIdent + @Test + public void getTimeZone_minutes_tooBig_utc() { + // Before 7.2, this was converted into minutes and then to GMT+0101 + // This is an invalid timezone and now becomes UTC. + PushCertificateIdent ident = PushCertificateIdent .parse("A U. Thor <a_u_thor@example.com> 1218123387 +0061"); - assertEquals(61, hourDubious.getTimeZoneOffset()); - assertEquals(61 * 60 * 1000, - hourDubious.getTimeZone().getOffset(1218123387)); + assertEquals(0, ident.getTimeZoneOffset()); + assertEquals(0, ident.getTimeZone().getOffset(1218123387)); + } - PushCertificateIdent weirdCase = PushCertificateIdent - .parse("A U. Thor <a_u_thor@example.com> 1218123387 +0099"); - assertEquals(99, weirdCase.getTimeZoneOffset()); - assertEquals(99 * 60 * 1000, - weirdCase.getTimeZone().getOffset(1218123387)); - - PushCertificateIdent weirdCase2 = PushCertificateIdent + @Test + public void getTimeZone_minutes_tooBigMoreThanOneHour_utc() { + // Before 7.2, this was converted into 60+99 minutes and then to + // GMT+0239. This is an invalid timezone and now becomes UTC. + PushCertificateIdent ident = PushCertificateIdent .parse("A U. Thor <a_u_thor@example.com> 1218123387 +0199"); - assertEquals(60 + 99, weirdCase2.getTimeZoneOffset()); - assertEquals((60 + 99) * 60 * 1000, - weirdCase2.getTimeZone().getOffset(1218123387)); + assertEquals(0, ident.getTimeZoneOffset()); + assertEquals(0, ident.getTimeZone().getOffset(1218123387)); } private static void assertMatchesPersonIdent(String raw,
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PushCertificateIdent.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PushCertificateIdent.java index bd76b34..256843d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PushCertificateIdent.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PushCertificateIdent.java
@@ -11,14 +11,19 @@ package org.eclipse.jgit.transport; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.time.Instant.EPOCH; +import static java.time.ZoneOffset.UTC; import static org.eclipse.jgit.util.RawParseUtils.lastIndexOfTrim; -import java.text.SimpleDateFormat; +import java.time.DateTimeException; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.Locale; import java.util.TimeZone; -import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.util.MutableInteger; import org.eclipse.jgit.util.RawParseUtils; @@ -66,15 +71,17 @@ public static PushCertificateIdent parse(String str) { int tzBegin = raw.length - 1; tzBegin = lastIndexOfTrim(raw, ' ', tzBegin); if (tzBegin < 0 || raw[tzBegin] != ' ') { - return new PushCertificateIdent(str, str, 0, 0); + return new PushCertificateIdent(str, str, EPOCH, UTC); } int whenBegin = tzBegin++; - int tz = RawParseUtils.parseTimeZoneOffset(raw, tzBegin, p); + // Don't parse the timezone yet: if tz is missing, this could + // be the timestamp + int hhmm = RawParseUtils.parseBase10(raw, tzBegin, p); boolean hasTz = p.value != tzBegin; whenBegin = lastIndexOfTrim(raw, ' ', whenBegin); if (whenBegin < 0 || raw[whenBegin] != ' ') { - return new PushCertificateIdent(str, str, 0, 0); + return new PushCertificateIdent(str, str, EPOCH, UTC); } int idEnd = whenBegin++; long when = RawParseUtils.parseLongBase10(raw, whenBegin, p); @@ -85,7 +92,7 @@ public static PushCertificateIdent parse(String str) { } else { // If either tz or when are non-numeric, mimic parsePersonIdent behavior and // set them both to zero. - tz = 0; + hhmm = 0; when = 0; if (hasTz && !hasWhen) { // Only one trailing numeric field; assume User ID ends before this @@ -97,14 +104,22 @@ public static PushCertificateIdent parse(String str) { } } String id = new String(raw, 0, idEnd, UTF_8); - - return new PushCertificateIdent(str, id, when * 1000L, tz); + return new PushCertificateIdent(str, id, Instant.ofEpochSecond(when), + asZoneId(hhmm)); } + private static final DateTimeFormatter OFFSET_FORMATTER = DateTimeFormatter + .ofPattern("Z", Locale.US); + + private static final DateTimeFormatter TO_STRING_FORMATTER = DateTimeFormatter + .ofPattern("EEE MMM d HH:mm:ss yyyy Z").withLocale(Locale.US); + private final String raw; private final String userId; - private final long when; - private final int tzOffset; + + private final Instant when; + + private final ZoneId tzOffset; /** * Construct a new identity from an OpenPGP User ID. @@ -115,19 +130,38 @@ public static PushCertificateIdent parse(String str) { * local time. * @param tzOffset * timezone offset; see {@link #getTimeZoneOffset()}. + * @deprecated Use {@link #PushCertificateIdent(String,Instant,ZoneId)} + * instead. */ + @Deprecated(since = "8.0") public PushCertificateIdent(String userId, long when, int tzOffset) { + this(userId, Instant.ofEpochMilli(when), + ZoneOffset.ofHoursMinutes(tzOffset / 60, tzOffset % 60)); + } + + /** + * Construct a new identity from an OpenPGP User ID. + * + * @param userId + * OpenPGP User ID; any UTF-8 string. + * @param when + * local time. + * @param tzOffset + * timezone offset. + */ + public PushCertificateIdent(String userId, Instant when, ZoneId tzOffset) { this.userId = userId; this.when = when; this.tzOffset = tzOffset; - StringBuilder sb = new StringBuilder(userId).append(' ').append(when / 1000) - .append(' '); - PersonIdent.appendTimezone(sb, tzOffset); + StringBuilder sb = new StringBuilder(userId).append(' ') + .append(when.getEpochSecond()).append(' ') + .append(OFFSET_FORMATTER + .format(tzOffset.getRules().getOffset(when))); raw = sb.toString(); } - private PushCertificateIdent(String raw, String userId, long when, - int tzOffset) { + private PushCertificateIdent(String raw, String userId, Instant when, + ZoneId tzOffset) { this.raw = raw; this.userId = userId; this.when = when; @@ -203,9 +237,34 @@ public String getEmailAddress() { * Get the timestamp of the identity. * * @return the timestamp of the identity. + * + * @deprecated Use getWhenAsInstant() instead. */ + @Deprecated(since = "8.0") public Date getWhen() { - return new Date(when); + return Date.from(when); + } + + /** + * Get the timestamp of the identity. + * + * @return the timestamp of the identity + */ + public Instant getWhenAsInstant() { + return when; + } + + /** + * Get this person's declared time zone + * + * @return this person's declared time zone; null if the timezone is + * unknown. + * + * @deprecated Use {@link #getZoneId()} instead. + */ + @Deprecated(since = "8.0") + public TimeZone getTimeZone() { + return TimeZone.getTimeZone(tzOffset); } /** @@ -214,8 +273,8 @@ public Date getWhen() { * @return this person's declared time zone; null if the timezone is * unknown. */ - public TimeZone getTimeZone() { - return PersonIdent.getTimeZone(tzOffset); + public ZoneId getZoneId() { + return tzOffset; } /** @@ -223,9 +282,12 @@ public TimeZone getTimeZone() { * * @return this person's declared time zone as minutes east of UTC. If the * timezone is to the west of UTC it is negative. + * + * @deprecated Not used. This offset can be read from the ZoneId. */ + @Deprecated(since = "8.0") public int getTimeZoneOffset() { - return tzOffset; + return tzOffset.getRules().getOffset(when).getTotalSeconds() / 60; } @Override @@ -242,12 +304,24 @@ public int hashCode() { @SuppressWarnings("nls") @Override public String toString() { - SimpleDateFormat fmt; - fmt = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.US); - fmt.setTimeZone(getTimeZone()); - return getClass().getSimpleName() - + "[raw=\"" + raw + "\"," - + " userId=\"" + userId + "\"," - + " " + fmt.format(Long.valueOf(when)) + "]"; + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append("["); + sb.append("raw=\"").append(raw).append("\", "); + sb.append("userId=\"").append(userId).append("\", "); + sb.append(TO_STRING_FORMATTER.withZone(getZoneId()).format(when)); + sb.append("]"); + return sb.toString(); + } + + private static ZoneId asZoneId(int hhmm) { + // Before 7.2, hhmm was converted to minutes and then back to hhmm. + // That accepted invalid timezones like +0061 (which became 61 minutes + // and then GMT+0101). That is as arbitrary as defaulting to UTC. + try { + return ZoneOffset.ofHoursMinutes(hhmm / 100, hhmm % 100); + } catch (DateTimeException e) { + return UTC; + } } }