From 5b9aaf5e24aa625f7643f6bb9347ecdbfecec664 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sat, 3 Feb 2024 15:51:23 +0100 Subject: [PATCH] fix: Fix CNAME parsing --- internal/repo/repo.go | 14 +++++++ internal/repo/repo_test.go | 81 +++++++++++++++++++++++++++++++------- 2 files changed, 80 insertions(+), 15 deletions(-) diff --git a/internal/repo/repo.go b/internal/repo/repo.go index eb6a4de..40feec7 100644 --- a/internal/repo/repo.go +++ b/internal/repo/repo.go @@ -190,8 +190,22 @@ func GetRepositoryInformation(owner, repoName string, ctx *context.GlobalContext headers = make(map[string]string) } + cname, found := payload["CNAME"] + if found { + switch cname.(type) { + case string: + // NOOP + default: + log.Warnf("CNAME attribute is not a string for %s/%s", owner, repoName) + cname = "" + } + } else { + cname = "" + } + info := context.RepositoryInformation{ Headers: filterHeaders(headers.(map[string]string)), + CNAME: cname.(string), } ctx.Cache.SetRepositoryInformation(owner, repoName, info) return &info diff --git a/internal/repo/repo_test.go b/internal/repo/repo_test.go index 8a236f2..2153ff9 100644 --- a/internal/repo/repo_test.go +++ b/internal/repo/repo_test.go @@ -6,7 +6,9 @@ import ( "testing" "time" + "git.polynom.me/rio/internal/context" "git.polynom.me/rio/internal/gitea" + log "github.com/sirupsen/logrus" ) @@ -67,8 +69,15 @@ func TestPickingCorrectRepositoryDefault(t *testing.T) { return "", nil }, } + ctx := &context.GlobalContext{ + Gitea: &client, + Cache: &context.CacheContext{ + RepositoryInformationCache: context.MakeRepoInfoCache(), + RepositoryPathCache: context.MakeRepoPathCache(), + }, + } - res, path, err := RepoFromPath("example-user", "example-user.pages.example.org", "", "index.html", &client) + res, path, err := RepoFromPath("example-user", "example-user.pages.example.org", "", "index.html", ctx) if err != nil { t.Fatalf("An error occured: %v", err) } @@ -118,8 +127,15 @@ func TestPickingCorrectRepositoryDefaultSubdirectory(t *testing.T) { return "", nil }, } + ctx := &context.GlobalContext{ + Gitea: &client, + Cache: &context.CacheContext{ + RepositoryInformationCache: context.MakeRepoInfoCache(), + RepositoryPathCache: context.MakeRepoPathCache(), + }, + } - res, path, err := RepoFromPath("example-user", "example-user.pages.example.org", "", "assets/index.css", &client) + res, path, err := RepoFromPath("example-user", "example-user.pages.example.org", "", "assets/index.css", ctx) if err != nil { t.Fatalf("An error occured: %v", err) } @@ -173,8 +189,15 @@ func TestPickingCorrectRepositorySubdirectoryNoPagesBranch(t *testing.T) { return "", nil }, } + ctx := &context.GlobalContext{ + Gitea: &client, + Cache: &context.CacheContext{ + RepositoryInformationCache: context.MakeRepoInfoCache(), + RepositoryPathCache: context.MakeRepoPathCache(), + }, + } - res, path, err := RepoFromPath("example-user", "example-user.pages.example.org", "", "blog/post1.html", &client) + res, path, err := RepoFromPath("example-user", "example-user.pages.example.org", "", "blog/post1.html", ctx) if err != nil { t.Fatalf("An error occured: %v", err) } @@ -211,8 +234,8 @@ func TestPickingNoRepositoryInvalidCNAME(t *testing.T) { return false }, GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) { - if username == "example-user" && repositoryName == "example-user.pages.example.org" && branch == "pages" && path == "CNAME" { - return []byte("some-other-domain.local"), true, nil + if username == "example-user" && repositoryName == "example-user.pages.example.org" && branch == "pages" && path == "rio.json" { + return []byte("{\"CNAME\": \"some-other-domain.local\"}"), true, nil } t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path) @@ -225,8 +248,15 @@ func TestPickingNoRepositoryInvalidCNAME(t *testing.T) { return "", nil }, } + ctx := &context.GlobalContext{ + Gitea: &client, + Cache: &context.CacheContext{ + RepositoryInformationCache: context.MakeRepoInfoCache(), + RepositoryPathCache: context.MakeRepoPathCache(), + }, + } - _, _, err := RepoFromPath("example-user", "example-user.pages.example.org", "example-user.local", "index.html", &client) + _, _, err := RepoFromPath("example-user", "example-user.pages.example.org", "example-user.local", "index.html", ctx) if err == nil { t.Fatal("gitea.Repository returned even though CNAME validation should fail") } @@ -254,8 +284,8 @@ func TestPickingRepositoryValidCNAME(t *testing.T) { return false }, GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) { - if username == "example-user" && repositoryName == "example-user.local" && branch == "pages" && path == "CNAME" { - return []byte("example-user.local"), true, nil + if username == "example-user" && repositoryName == "example-user.local" && branch == "pages" && path == "rio.json" { + return []byte("{\"CNAME\": \"example-user.local\"}"), true, nil } t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path) @@ -268,8 +298,15 @@ func TestPickingRepositoryValidCNAME(t *testing.T) { return "", nil }, } + ctx := &context.GlobalContext{ + Gitea: &client, + Cache: &context.CacheContext{ + RepositoryInformationCache: context.MakeRepoInfoCache(), + RepositoryPathCache: context.MakeRepoPathCache(), + }, + } - repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", &client) + repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", ctx) if err != nil { t.Fatalf("Error returned: %v", err) } @@ -301,8 +338,8 @@ func TestPickingRepositoryValidCNAMEWithTXTLookup(t *testing.T) { return false }, GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) { - if username == "example-user" && repositoryName == "some-different-repository" && branch == "pages" && path == "CNAME" { - return []byte("example-user.local"), true, nil + if username == "example-user" && repositoryName == "some-different-repository" && branch == "pages" && path == "rio.json" { + return []byte("{\"CNAME\": \"example-user.local\"}"), true, nil } t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path) @@ -318,8 +355,15 @@ func TestPickingRepositoryValidCNAMEWithTXTLookup(t *testing.T) { return "", nil }, } + ctx := &context.GlobalContext{ + Gitea: &client, + Cache: &context.CacheContext{ + RepositoryInformationCache: context.MakeRepoInfoCache(), + RepositoryPathCache: context.MakeRepoPathCache(), + }, + } - repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", &client) + repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", ctx) if err != nil { t.Fatalf("Error returned: %v", err) } @@ -350,8 +394,8 @@ func TestPickingRepositoryValidCNAMEWithTXTLookupAndSubdirectory(t *testing.T) { return false }, GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) { - if username == "example-user" && repositoryName == "some-different-repository" && branch == "pages" && path == "CNAME" { - return []byte("example-user.local"), true, nil + if username == "example-user" && repositoryName == "some-different-repository" && branch == "pages" && path == "rio.json" { + return []byte("{\"CNAME\": \"example-user.local\"}"), true, nil } t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path) @@ -367,8 +411,15 @@ func TestPickingRepositoryValidCNAMEWithTXTLookupAndSubdirectory(t *testing.T) { return "", nil }, } + ctx := &context.GlobalContext{ + Gitea: &client, + Cache: &context.CacheContext{ + RepositoryInformationCache: context.MakeRepoInfoCache(), + RepositoryPathCache: context.MakeRepoPathCache(), + }, + } - repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "blog/index.html", &client) + repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "blog/index.html", ctx) if err != nil { t.Fatalf("Error returned: %v", err) }