commit aca68171072fc8b5ce0145885ec827cdd7776ee4 Author: Karsten Loesing karsten.loesing@gmx.net Date: Mon Dec 11 14:58:00 2017 +0100
Only serve completed dates.
Implements #24580. --- .../metrics/exonerator/ExoneraTorServlet.java | 28 +++++++++++++++++++++- .../metrics/exonerator/QueryServlet.java | 16 +++++++++++++ src/main/resources/ExoneraTor.properties | 2 ++ 3 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/torproject/metrics/exonerator/ExoneraTorServlet.java b/src/main/java/org/torproject/metrics/exonerator/ExoneraTorServlet.java index eb3b367..c26d08e 100644 --- a/src/main/java/org/torproject/metrics/exonerator/ExoneraTorServlet.java +++ b/src/main/java/org/torproject/metrics/exonerator/ExoneraTorServlet.java @@ -15,6 +15,8 @@ import java.io.StringWriter; import java.net.URL; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -76,6 +78,8 @@ public class ExoneraTorServlet extends HttpServlet { String timestampParameter = request.getParameter("timestamp"); String timestampStr = parseTimestampParameter(timestampParameter); final boolean timestampHasError = timestampStr == null; + final boolean timestampTooRecent = !timestampHasError + && checkTimestampTooRecent(timestampStr);
/* Parse lang parameter. */ String langParameter = request.getParameter("lang"); @@ -96,7 +100,7 @@ public class ExoneraTorServlet extends HttpServlet {
/* Only query, if we received valid user input. */ if (null != relayIp && !relayIp.isEmpty() && null != timestampStr - && !timestampStr.isEmpty()) { + && !timestampStr.isEmpty() && !timestampTooRecent) { QueryResponse queryResponse = this.queryBackend(relayIp, timestampStr); if (null != queryResponse) { successfullyConnectedToBackend = true; @@ -175,6 +179,12 @@ public class ExoneraTorServlet extends HttpServlet { this.writeSummaryInvalidTimestamp(out, rb, timestampParameter); this.writeFooter(out, rb, null, null);
+ /* If the timestamp is too recent, print summary with error message and + * exit. */ + } else if (timestampTooRecent) { + this.writeSummaryTimestampTooRecent(out, rb, timestampStr); + this.writeFooter(out, rb, null, null); + /* If we were unable to connect to the database, * write an error message. */ } else if (!successfullyConnectedToBackend) { @@ -307,6 +317,14 @@ public class ExoneraTorServlet extends HttpServlet { return timestampStr; }
+ /** Return whether the timestamp parameter is too recent, which is the case if + * it matches the day before the current system date (in UTC) or is even + * younger. */ + static boolean checkTimestampTooRecent(String timestampParameter) { + return timestampParameter.compareTo(ZonedDateTime.now(ZoneOffset.UTC) + .toLocalDate().minusDays(1).toString()) >= 0; + } + /* Helper method for fetching a query response via URL. */
private QueryResponse queryBackend(String relayIp, String timestampStr) { @@ -458,6 +476,14 @@ public class ExoneraTorServlet extends HttpServlet { escapedTimestampParameter, ""YYYY-MM-DD""); }
+ private void writeSummaryTimestampTooRecent(PrintWriter out, + ResourceBundle rb, String timestampStr) throws IOException { + this.writeSummary(out, rb.getString("summary.heading"), "panel-danger", + rb.getString("summary.invalidparams.timestamptoorecent.title"), + null, rb.getString("summary.invalidparams.timestamptoorecent.body"), + timestampStr); + } + private void writeSummaryNoDataForThisInterval(PrintWriter out, ResourceBundle rb) throws IOException { String contactLink = diff --git a/src/main/java/org/torproject/metrics/exonerator/QueryServlet.java b/src/main/java/org/torproject/metrics/exonerator/QueryServlet.java index 50badd6..7593d1a 100644 --- a/src/main/java/org/torproject/metrics/exonerator/QueryServlet.java +++ b/src/main/java/org/torproject/metrics/exonerator/QueryServlet.java @@ -16,6 +16,8 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; @@ -90,6 +92,11 @@ public class QueryServlet extends HttpServlet { "Invalid timestamp parameter."); return; } + if (this.checkTimestampTooRecent(timestampParameter)) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST, + "Timestamp too recent."); + return; + }
/* Query the database. */ QueryResponse queryResponse = this.queryDatabase(relayIp, timestamp); @@ -228,6 +235,15 @@ public class QueryServlet extends HttpServlet { return timestamp; }
+ + /** Return whether the timestamp parameter is too recent, which is the case if + * it matches the day before the current system date (in UTC) or is even + * younger. */ + private boolean checkTimestampTooRecent(String timestampParameter) { + return timestampParameter.compareTo(ZonedDateTime.now(ZoneOffset.UTC) + .toLocalDate().minusDays(1).toString()) >= 0; + } + /* Helper methods for querying the database. */
private QueryResponse queryDatabase(String relayIp, long timestamp) { diff --git a/src/main/resources/ExoneraTor.properties b/src/main/resources/ExoneraTor.properties index 8488623..2d71486 100644 --- a/src/main/resources/ExoneraTor.properties +++ b/src/main/resources/ExoneraTor.properties @@ -19,6 +19,8 @@ summary.invalidparams.invalidip.title=Invalid IP address parameter summary.invalidparams.invalidip.body=Sorry, %s is not a valid IP address. The expected IP address formats are %s or %s. summary.invalidparams.invalidtimestamp.title=Invalid date parameter summary.invalidparams.invalidtimestamp.body=Sorry, %s is not a valid date. The expected date format is %s. +summary.invalidparams.timestamptoorecent.title=Date parameter too recent +summary.invalidparams.timestamptoorecent.body=Sorry, %s is too recent. The database may not yet contain enough data to correctly answer this request. summary.serverproblem.nodata.title=Server problem summary.serverproblem.nodata.body.text=The database does not contain any data for the requested date. Please try again later. If this problem persists, please %s! summary.serverproblem.nodata.body.link=let us know
tor-commits@lists.torproject.org