mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
refactor(dsql): read a token's expiry from the validator (#6246)
* refactor(dsql): read a token's expiry from the validator DSQL's endpoint worked out a token's expiry for its "Signature expired" hint, and SigV4RequestValidator worked it out again to judge the token, each with its own parsing (a long against an int, its own date formatter). The validator now exposes the expiry it judges, and the hint reports that one, so the two cannot disagree. A date or an expiry that does not parse, or one past what an Instant holds, gives no expiry, and the token is refused as before. * fix(dsql): read a token's expiry seconds as an int, as validate did The shared expiry parsed X-Amz-Expires as a long, so a signed token whose expiry no int holds, which validate used to refuse, was taken: a refactor that widened what is accepted. It reads the seconds as an int again, and the DSQL token helper signs a long so a test can sign one past an int.
This commit is contained in:
1 parent
b0a4d5064b
commit
f08ce36298
4 files changed
+84
-22
No files matched your search
+31
-3
@@ -9,6 +9,7 @@ import javax.crypto.spec.SecretKeySpec;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.DateTimeException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
@@ -126,9 +127,12 @@ public final class SigV4RequestValidator {
|
||||
return false;
|
||||
}
|
||||
|
||||
Instant tokenTime = Instant.from(DATETIME_FMT.parse(dateTime));
|
||||
int expirySeconds = Integer.parseInt(expires);
|
||||
if (Instant.now().minus(expiryGrace).isAfter(tokenTime.plusSeconds(expirySeconds))) {
|
||||
Optional<Instant> expiry = expiry(dateTime, expires);
|
||||
if (expiry.isEmpty()) {
|
||||
LOG.debugv("{0} has a date or expiry that does not parse", logLabel);
|
||||
return false;
|
||||
}
|
||||
if (Instant.now().minus(expiryGrace).isAfter(expiry.get())) {
|
||||
LOG.debugv("{0} expired", logLabel);
|
||||
return false;
|
||||
}
|
||||
@@ -199,6 +203,30 @@ public final class SigV4RequestValidator {
|
||||
|
||||
private record QueryParameter(String name, String value) {}
|
||||
|
||||
/**
|
||||
* When a token stops being valid, before any grace its service allows: its {@code X-Amz-Date}
|
||||
* plus {@code X-Amz-Expires} seconds, or empty when either is missing or does not parse, the
|
||||
* seconds as an {@code int}. A caller that reports the expiry, as Aurora DSQL's hint does, reads
|
||||
* it here, so it reports the one {@link #validate} judged.
|
||||
*/
|
||||
public static Optional<Instant> expiry(String rawQuery) {
|
||||
List<QueryParameter> parameters = decodeQuery(rawQuery);
|
||||
return expiry(findParam(parameters, "X-Amz-Date"), findParam(parameters, "X-Amz-Expires"));
|
||||
}
|
||||
|
||||
private static Optional<Instant> expiry(String dateTime, String expires) {
|
||||
if (dateTime == null || expires == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
try {
|
||||
return Optional.of(Instant.from(DATETIME_FMT.parse(dateTime)).plusSeconds(Integer.parseInt(expires)));
|
||||
} catch (DateTimeException | NumberFormatException ignored) {
|
||||
// A date or an expiry that does not parse sets no expiry, so the token it came with is
|
||||
// refused: one of more seconds than an int holds included, as validate always refused it.
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The value of {@code name} in a token's query string, read as {@link #validate} reads it: the
|
||||
* first parameter whose percent-decoded name matches, its value decoded once. A caller that acts
|
||||
|
||||
@@ -37,6 +37,7 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -362,26 +363,16 @@ public class DsqlEndpoint {
|
||||
* {@code Signature expired: <expiry> is now earlier than <now - 5 s> (<now> - 0 min.)}.
|
||||
*/
|
||||
private static String expiredHint(String rawQuery) {
|
||||
String date = SigV4RequestValidator.queryParameter(rawQuery, "X-Amz-Date");
|
||||
String expires = SigV4RequestValidator.queryParameter(rawQuery, "X-Amz-Expires");
|
||||
if (date == null || expires == null) {
|
||||
return null;
|
||||
}
|
||||
Instant expiry;
|
||||
try {
|
||||
expiry = Instant.from(AMZ_DATE.parse(date)).plusSeconds(Long.parseLong(expires));
|
||||
} catch (RuntimeException ignored) {
|
||||
// A date or expiry that does not parse gets no hint here: the signature check that
|
||||
// follows refuses the token without one, as DSQL does.
|
||||
return null;
|
||||
}
|
||||
// A token without an expiry that parses gets no hint here: the signature check that
|
||||
// follows refuses it without one, as DSQL does.
|
||||
Optional<Instant> expiry = SigV4RequestValidator.expiry(rawQuery);
|
||||
Instant now = Instant.now();
|
||||
Instant earliest = now.minus(EXPIRY_GRACE);
|
||||
if (!expiry.isBefore(earliest)) {
|
||||
if (expiry.isEmpty() || !expiry.get().isBefore(earliest)) {
|
||||
return null;
|
||||
}
|
||||
return "Signature expired: " + AMZ_DATE.format(expiry) + " is now earlier than " + AMZ_DATE.format(earliest)
|
||||
+ " (" + AMZ_DATE.format(now) + " - 0 min.)";
|
||||
return "Signature expired: " + AMZ_DATE.format(expiry.get()) + " is now earlier than "
|
||||
+ AMZ_DATE.format(earliest) + " (" + AMZ_DATE.format(now) + " - 0 min.)";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user