From de86c5e6ef57cf8fb9dd36abe1d83d3ade4cb12f Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sat, 14 Jan 2023 08:16:30 +0100 Subject: [PATCH] [BRANDING] add X-Forgejo-* headers --- modules/httpcache/httpcache.go | 1 + modules/httpcache/httpcache_test.go | 3 +++ routers/api/v1/api.go | 2 +- routers/api/v1/repo/file.go | 7 ++++++- services/mailer/mail.go | 10 ++++++++++ services/webhook/deliver.go | 4 ++++ 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/modules/httpcache/httpcache.go b/modules/httpcache/httpcache.go index 750233d4a7..882f207ef7 100644 --- a/modules/httpcache/httpcache.go +++ b/modules/httpcache/httpcache.go @@ -31,6 +31,7 @@ func AddCacheControlToHeader(h http.Header, maxAge time.Duration, additionalDire // to remind users they are using non-prod setting. h.Add("X-Gitea-Debug", "RUN_MODE="+setting.RunMode) + h.Add("X-Forgejo-Debug", "RUN_MODE="+setting.RunMode) } h.Set("Cache-Control", strings.Join(append(directives, additionalDirectives...), ", ")) diff --git a/modules/httpcache/httpcache_test.go b/modules/httpcache/httpcache_test.go index 49e54d147e..e7965d6af8 100644 --- a/modules/httpcache/httpcache_test.go +++ b/modules/httpcache/httpcache_test.go @@ -30,6 +30,9 @@ func countFormalHeaders(h http.Header) (c int) { if strings.HasPrefix(k, "X-Gitea-") { continue } + if strings.HasPrefix(k, "X-Forgejo-") { + continue + } c++ } return c diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index b0a864d199..38cd8e87bd 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -611,7 +611,7 @@ func Routes(ctx gocontext.Context) *web.Route { // setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option AllowedMethods: setting.CORSConfig.Methods, AllowCredentials: setting.CORSConfig.AllowCredentials, - AllowedHeaders: []string{"Authorization", "X-Gitea-OTP"}, + AllowedHeaders: []string{"Authorization", "X-Gitea-OTP", "X-Forgejo-OTP"}, MaxAge: int(setting.CORSConfig.MaxAge.Seconds()), })) } diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index aba5b1f9e7..3dcd28ccd0 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -33,7 +33,10 @@ import ( files_service "code.gitea.io/gitea/services/repository/files" ) -const giteaObjectTypeHeader = "X-Gitea-Object-Type" +const ( + giteaObjectTypeHeader = "X-Gitea-Object-Type" + forgejoObjectTypeHeader = "X-Forgejo-Object-Type" +) // GetRawFile get a file by path on a repository func GetRawFile(ctx *context.APIContext) { @@ -80,6 +83,7 @@ func GetRawFile(ctx *context.APIContext) { } ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) + ctx.RespHeader().Set(forgejoObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) if err := common.ServeBlob(ctx.Context, blob, lastModified); err != nil { ctx.Error(http.StatusInternalServerError, "ServeBlob", err) @@ -129,6 +133,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) { } ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) + ctx.RespHeader().Set(forgejoObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) // LFS Pointer files are at most 1024 bytes - so any blob greater than 1024 bytes cannot be an LFS file if blob.Size() > 1024 { diff --git a/services/mailer/mail.go b/services/mailer/mail.go index eece83ca43..23de3dc531 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -373,6 +373,16 @@ func generateAdditionalHeaders(ctx *mailCommentContext, reason string, recipient "X-Gitea-Issue-ID": strconv.FormatInt(ctx.Issue.Index, 10), "X-Gitea-Issue-Link": ctx.Issue.HTMLURL(), + "X-Forgejo-Reason": reason, + "X-Forgejo-Sender": ctx.Doer.DisplayName(), + "X-Forgejo-Recipient": recipient.DisplayName(), + "X-Forgejo-Recipient-Address": recipient.Email, + "X-Forgejo-Repository": repo.Name, + "X-Forgejo-Repository-Path": repo.FullName(), + "X-Forgejo-Repository-Link": repo.HTMLURL(), + "X-Forgejo-Issue-ID": strconv.FormatInt(ctx.Issue.Index, 10), + "X-Forgejo-Issue-Link": ctx.Issue.HTMLURL(), + "X-GitHub-Reason": reason, "X-GitHub-Sender": ctx.Doer.DisplayName(), "X-GitHub-Recipient": recipient.DisplayName(), diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index 74a69c297c..af45369d53 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -116,6 +116,10 @@ func Deliver(ctx context.Context, t *webhook_model.HookTask) error { event := t.EventType.Event() eventType := string(t.EventType) + req.Header.Add("X-Forgejo-Delivery", t.UUID) + req.Header.Add("X-Forgejo-Event", event) + req.Header.Add("X-Forgejo-Event-Type", eventType) + req.Header.Add("X-Forgejo-Signature", signatureSHA256) req.Header.Add("X-Gitea-Delivery", t.UUID) req.Header.Add("X-Gitea-Event", event) req.Header.Add("X-Gitea-Event-Type", eventType)