rio/internal/repo/repo.go

234 lines
5.3 KiB
Go
Raw Normal View History

2024-01-01 13:19:19 +00:00
package repo
2023-12-31 12:26:56 +00:00
//go:generate mockgen -destination mock_repo_test.go -package repo code.gitea.io/sdk/gitea Client
2023-12-31 12:26:56 +00:00
import (
"encoding/json"
2023-12-31 12:26:56 +00:00
"errors"
"slices"
2023-12-31 12:26:56 +00:00
"strings"
"time"
"git.polynom.me/rio/internal/constants"
"git.polynom.me/rio/internal/context"
"git.polynom.me/rio/internal/gitea"
2024-01-01 13:19:19 +00:00
2023-12-31 12:26:56 +00:00
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
var (
ForbiddenHeaders = []string{
"content-length",
"content-type",
"date",
"location",
"strict-transport-security",
"set-cookie",
}
)
2023-12-31 12:26:56 +00:00
var (
2023-12-31 23:38:39 +00:00
pathCache = cache.New(1*time.Hour, 1*time.Hour)
// Caching the existence of an user
userCache = cache.New(24*time.Hour, 12*time.Hour)
2023-12-31 12:26:56 +00:00
)
type PageCacheEntry struct {
Repository gitea.Repository
2023-12-31 23:38:39 +00:00
Path string
2023-12-31 12:26:56 +00:00
}
func makePageCacheKey(domain, path string) string {
return domain + "/" + path
}
func lookupRepositoryAndCache(username, reponame, branchName, host, domain, path, cname string, giteaClient *gitea.GiteaClient) (*gitea.Repository, error) {
log.Debugf("CNAME: %s", cname)
2023-12-31 12:26:56 +00:00
log.Debugf("Looking up repository %s/%s", username, reponame)
repo, err := giteaClient.GetRepository(username, reponame)
2023-12-31 12:26:56 +00:00
if err != nil {
return nil, err
}
if !giteaClient.HasBranch(username, reponame, branchName) {
return nil, errors.New("Specified branch does not exist")
}
2023-12-31 12:26:56 +00:00
// Check if the CNAME file matches
if cname != "" {
log.Debug("Checking CNAME")
2023-12-31 12:26:56 +00:00
file, _, err := giteaClient.GetFile(
username,
reponame,
constants.PagesBranch,
2023-12-31 12:26:56 +00:00
"CNAME",
nil,
2023-12-31 12:26:56 +00:00
)
if err != nil {
log.Errorf(
"Could not verify CNAME of %s/%s@%s: %v\n",
username,
reponame,
constants.PagesBranch,
err,
)
2023-12-31 12:26:56 +00:00
return nil, err
}
cnameContent := strings.Trim(
string(file[:]),
"\n",
)
log.Debugf("CNAME Content: %s", cnameContent)
2024-01-06 20:26:09 +00:00
if cnameContent != host {
log.Warnf("CNAME mismatch: Repo '%s', Host '%s'", cnameContent, host)
2023-12-31 12:26:56 +00:00
return nil, errors.New("CNAME mismatch")
}
}
// Cache data
pathCache.Set(
makePageCacheKey(domain, path),
PageCacheEntry{
repo,
path,
},
cache.DefaultExpiration,
)
return &repo, nil
2023-12-31 12:26:56 +00:00
}
2024-01-06 20:26:09 +00:00
// host is the domain name we're accessed from. cname is the domain that host is pointing
// if, if we're accessed via a CNAME. If not, then cname is "".
func RepoFromPath(username, host, cname, path string, giteaClient *gitea.GiteaClient) (*gitea.Repository, string, error) {
2023-12-31 12:26:56 +00:00
domain := host
// Guess the repository
key := makePageCacheKey(domain, path)
entry, found := pathCache.Get(key)
if found {
pageEntry := entry.(PageCacheEntry)
return &pageEntry.Repository, pageEntry.Path, nil
}
// Allow specifying the repository name in the TXT record
reponame := ""
if cname != "" {
repoLookup, err := giteaClient.LookupRepoTXT(host)
if err == nil && repoLookup != "" {
log.Infof(
"TXT lookup for %s resulted in choosing repository %s",
2024-01-06 20:26:09 +00:00
host,
repoLookup,
)
reponame = repoLookup
}
2023-12-31 12:26:56 +00:00
}
pathParts := strings.Split(path, "/")
2024-01-06 20:30:44 +00:00
log.Debugf("reponame='%s' len(pathParts)='%d'", reponame, len(pathParts))
if reponame == "" && len(pathParts) > 1 {
2023-12-31 12:26:56 +00:00
log.Debugf("Trying repository %s", pathParts[0])
modifiedPath := strings.Join(pathParts[1:], "/")
repo, err := lookupRepositoryAndCache(
username,
pathParts[0],
constants.PagesBranch,
2023-12-31 12:26:56 +00:00
host,
domain,
modifiedPath,
cname,
giteaClient,
)
if err == nil {
return repo, modifiedPath, nil
}
}
if reponame == "" {
reponame = domain
2023-12-31 21:41:51 +00:00
}
log.Debugf("Trying repository %s/%s", username, reponame)
2023-12-31 12:26:56 +00:00
repo, err := lookupRepositoryAndCache(
username,
reponame,
constants.PagesBranch,
2023-12-31 12:26:56 +00:00
host,
domain,
path,
cname,
giteaClient,
)
return repo, path, err
}
// Checks if the username exists as an organisation or an user on the Gitea
// instance, so that an attacker can't just request certificates for random
// usernames.
func CanRequestCertificate(username string, giteaClient *gitea.GiteaClient) bool {
if _, found := userCache.Get(username); found {
return true
}
hasUser := giteaClient.HasUser(username)
if hasUser {
userCache.Set(username, true, cache.DefaultExpiration)
}
return hasUser
}
2024-01-06 16:42:08 +00:00
func filterHeaders(headers map[string]string) map[string]string {
newHeaders := make(map[string]string)
for key, value := range headers {
if slices.Contains[[]string, string](ForbiddenHeaders, strings.ToLower(key)) {
continue
}
newHeaders[key] = value
2024-01-06 16:42:08 +00:00
}
return newHeaders
}
func GetRepositoryInformation(owner, repoName string, ctx *context.GlobalContext) *context.RepositoryInformation {
res := ctx.Cache.GetRepositoryInformation(owner, repoName)
if res != nil {
return res
}
fetchedConfig, _, err := ctx.Gitea.GetFile(
owner,
repoName,
2024-01-06 16:42:08 +00:00
constants.PagesBranch,
"rio.json",
nil,
2024-01-06 16:42:08 +00:00
)
if err != nil {
log.Errorf("Failed to request rio.json for %s/%s:%v", owner, repoName, err)
return nil
}
2024-01-06 16:42:08 +00:00
var payload map[string]interface{}
err = json.Unmarshal(fetchedConfig, &payload)
if err != nil {
log.Errorf("Failed to unmarshal rio.json for %s/%s:%v", owner, repoName, err)
return nil
}
2024-01-06 16:42:08 +00:00
headers, found := payload["headers"]
if !found {
log.Warnf("Did not find headers key in rio.json for %s/%s", owner, repoName)
headers = make(map[string]string)
2024-01-06 16:42:08 +00:00
}
info := context.RepositoryInformation{
Headers: filterHeaders(headers.(map[string]string)),
}
ctx.Cache.SetRepositoryInformation(owner, repoName, info)
return &info
2024-01-06 16:42:08 +00:00
}