From 3d4f9f0f4abe1f42c13863705fb0bc08fd796e70 Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Fri, 31 Jan 2025 14:22:24 +0200 Subject: [PATCH] knotserver: verify signature timestamp --- knotserver/middleware.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/knotserver/middleware.go b/knotserver/middleware.go index 4494186..c9f7182 100644 --- a/knotserver/middleware.go +++ b/knotserver/middleware.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/hex" "net/http" + "time" ) func (h *Handle) VerifySignature(next http.Handler) http.Handler { @@ -20,7 +21,21 @@ func (h *Handle) VerifySignature(next http.Handler) http.Handler { func (h *Handle) verifyHMAC(signature string, r *http.Request) bool { secret := h.c.Secret - message := r.Method + r.URL.Path + r.URL.RawQuery + timestamp := r.Header.Get("X-Timestamp") + if timestamp == "" { + return false + } + + // Verify that the timestamp is not older than a minute + reqTime, err := time.Parse(time.RFC3339, timestamp) + if err != nil { + return false + } + if time.Since(reqTime) > time.Minute { + return false + } + + message := r.Method + r.URL.Path + timestamp mac := hmac.New(sha256.New, []byte(secret)) mac.Write([]byte(message)) -- knotmirror 0.0.1