1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-11 17:50:58 +00:00

fix: validate timestamp

This commit is contained in:
Michael Kriese 2024-11-22 01:36:40 +01:00 committed by Kwonunn
parent 1082b31367
commit 21ca8102fa

View file

@ -8,34 +8,56 @@ import (
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"hash" "hash"
"strconv"
"time"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
var ( var (
ErrValidation = errors.New("repo validation error") ErrValidation = errors.New("validation error")
cachePrefixPath = "repo:/run:/time:/mac:/" cachePrefixPath = "repo:/run:/ts:/mac:/"
) )
func (h *Handler) validateMac(params httprouter.Params) (string, error) { func (h *Handler) validateMac(params httprouter.Params) (string, error) {
ts := params.ByName("ts")
repo := params.ByName("repo") repo := params.ByName("repo")
run := params.ByName("run") run := params.ByName("run")
time := params.ByName("time")
messageMAC := params.ByName("mac") messageMAC := params.ByName("mac")
mac := computeMac(h.secret, repo, run, time) // TODO: allow configurable max age
expectedMAC := mac.Sum(nil) if !validateAge(ts) {
return "", ErrValidation
}
expectedMAC := computeMac(h.secret, repo, run, ts).Sum(nil)
if hmac.Equal([]byte(messageMAC), expectedMAC) { if hmac.Equal([]byte(messageMAC), expectedMAC) {
return repo, nil return repo, nil
} }
return repo, ErrValidation return repo, ErrValidation
} }
func computeMac(key, repo, run, time string) hash.Hash { func validateAge(ts string) bool {
tsInt, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return false
}
if tsInt > time.Now().Unix() {
return false
}
return true
}
func computeMac(key, repo, run, ts string) hash.Hash {
mac := hmac.New(sha256.New, []byte(key)) mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(repo)) mac.Write([]byte(repo))
mac.Write([]byte(run)) mac.Write([]byte(run))
mac.Write([]byte(time)) mac.Write([]byte(ts))
return mac return mac
} }
func ComputeMac(key, repo, run, ts string) string {
mac := computeMac(key, repo, run, ts)
return string(mac.Sum(nil))
}