[backend/federation] Don't recompute clock skew timespan on every invocation in HttpSignature.VerifyAsync

This commit also tightens the maximum allowed clock skew to a much more reasonable 5 minutes.
This commit is contained in:
Laura Hausmann 2024-10-30 18:06:06 +01:00
parent fbdab96f9d
commit de29780321
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -10,6 +10,8 @@ namespace Iceshrimp.Backend.Core.Federation.Cryptography;
public static class HttpSignature public static class HttpSignature
{ {
public static readonly TimeSpan MaxClockSkew = TimeSpan.FromMinutes(5);
public static async Task<bool> VerifyAsync( public static async Task<bool> VerifyAsync(
HttpRequest request, HttpSignatureHeader signature, HttpRequest request, HttpSignatureHeader signature,
IEnumerable<string> requiredHeaders, string key IEnumerable<string> requiredHeaders, string key
@ -53,10 +55,10 @@ public static class HttpSignature
if (created == null && !datePresent) if (created == null && !datePresent)
throw new GracefulException(HttpStatusCode.Forbidden, "Neither date nor (created) are present, refusing"); throw new GracefulException(HttpStatusCode.Forbidden, "Neither date nor (created) are present, refusing");
var dateCheck = datePresent && DateTime.Now - DateTime.Parse(date!) > TimeSpan.FromHours(12); var dateCheck = datePresent && DateTime.Now - DateTime.Parse(date!) > MaxClockSkew;
var createdCheck = created != null && var createdCheck = created != null &&
DateTime.UtcNow - (DateTime.UnixEpoch + TimeSpan.FromSeconds(long.Parse(created))) > DateTime.UtcNow - (DateTime.UnixEpoch + TimeSpan.FromSeconds(long.Parse(created))) >
TimeSpan.FromHours(12); MaxClockSkew;
if (dateCheck || createdCheck) if (dateCheck || createdCheck)
throw new GracefulException(HttpStatusCode.Forbidden, "Request signature is too old"); throw new GracefulException(HttpStatusCode.Forbidden, "Request signature is too old");