1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-31 18:30:58 +00:00

fix: partial secure cache

This commit is contained in:
Michael Kriese 2024-11-21 22:49:12 +01:00 committed by Kwonunn
parent ea79e3de41
commit 1082b31367
6 changed files with 143 additions and 31 deletions

41
act/artifactcache/mac.go Normal file
View file

@ -0,0 +1,41 @@
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package artifactcache
import (
"crypto/hmac"
"crypto/sha256"
"errors"
"hash"
"github.com/julienschmidt/httprouter"
)
var (
ErrValidation = errors.New("repo validation error")
cachePrefixPath = "repo:/run:/time:/mac:/"
)
func (h *Handler) validateMac(params httprouter.Params) (string, error) {
repo := params.ByName("repo")
run := params.ByName("run")
time := params.ByName("time")
messageMAC := params.ByName("mac")
mac := computeMac(h.secret, repo, run, time)
expectedMAC := mac.Sum(nil)
if hmac.Equal([]byte(messageMAC), expectedMAC) {
return repo, nil
}
return repo, ErrValidation
}
func computeMac(key, repo, run, time string) hash.Hash {
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(repo))
mac.Write([]byte(run))
mac.Write([]byte(time))
return mac
}