From 270fa6d63b9a25021d9663bd6162f4933b7eb5ff Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 15 Jan 2019 05:05:27 +0800 Subject: [PATCH] Support CORS headers to git smart http protocol (#5719) --- custom/conf/app.ini.sample | 3 ++ .../doc/advanced/config-cheat-sheet.en-us.md | 3 ++ modules/setting/setting.go | 38 ++++++++++--------- routers/repo/http.go | 12 ++++++ 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 31048f7857..a6ca758423 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -31,6 +31,9 @@ PULL_REQUEST_QUEUE_LENGTH = 1000 PREFERRED_LICENSES = Apache License 2.0,MIT License ; Disable the ability to interact with repositories using the HTTP protocol DISABLE_HTTP_GIT = false +; Value for Access-Control-Allow-Origin header, default is not to present +; WARNING: This maybe harmful to you website if you do not give it a right value. +ACCESS_CONTROL_ALLOW_ORIGIN = ; Force ssh:// clone url instead of scp-style uri when default SSH port is used USE_COMPAT_SSH_URI = false diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index a1526cf439..6fe98f809b 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -62,6 +62,9 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. HTTP protocol. - `USE_COMPAT_SSH_URI`: **false**: Force ssh:// clone url instead of scp-style uri when default SSH port is used. +- `ACCESS_CONTROL_ALLOW_ORIGIN`: **\**: Value for Access-Control-Allow-Origin header, + default is not to present. **WARNING**: This maybe harmful to you website if you do not + give it a right value. ### Repository - Pull Request (`repository.pull-request`) - `WORK_IN_PROGRESS_PREFIXES`: **WIP:,\[WIP\]**: List of prefixes used in Pull Request diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 88101937e9..2560c09107 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -201,15 +201,16 @@ var ( // Repository settings Repository = struct { - AnsiCharset string - ForcePrivate bool - DefaultPrivate string - MaxCreationLimit int - MirrorQueueLength int - PullRequestQueueLength int - PreferredLicenses []string - DisableHTTPGit bool - UseCompatSSHURI bool + AnsiCharset string + ForcePrivate bool + DefaultPrivate string + MaxCreationLimit int + MirrorQueueLength int + PullRequestQueueLength int + PreferredLicenses []string + DisableHTTPGit bool + AccessControlAllowOrigin string + UseCompatSSHURI bool // Repository editor settings Editor struct { @@ -237,15 +238,16 @@ var ( WorkInProgressPrefixes []string } `ini:"repository.pull-request"` }{ - AnsiCharset: "", - ForcePrivate: false, - DefaultPrivate: RepoCreatingLastUserVisibility, - MaxCreationLimit: -1, - MirrorQueueLength: 1000, - PullRequestQueueLength: 1000, - PreferredLicenses: []string{"Apache License 2.0,MIT License"}, - DisableHTTPGit: false, - UseCompatSSHURI: false, + AnsiCharset: "", + ForcePrivate: false, + DefaultPrivate: RepoCreatingLastUserVisibility, + MaxCreationLimit: -1, + MirrorQueueLength: 1000, + PullRequestQueueLength: 1000, + PreferredLicenses: []string{"Apache License 2.0,MIT License"}, + DisableHTTPGit: false, + AccessControlAllowOrigin: "", + UseCompatSSHURI: false, // Repository editor settings Editor: struct { diff --git a/routers/repo/http.go b/routers/repo/http.go index ec5fbe6c0d..1728a75fcb 100644 --- a/routers/repo/http.go +++ b/routers/repo/http.go @@ -27,6 +27,18 @@ import ( // HTTP implmentation git smart HTTP protocol func HTTP(ctx *context.Context) { + if len(setting.Repository.AccessControlAllowOrigin) > 0 { + // Set CORS headers for browser-based git clients + ctx.Resp.Header().Set("Access-Control-Allow-Origin", setting.Repository.AccessControlAllowOrigin) + ctx.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, User-Agent") + + // Handle preflight OPTIONS request + if ctx.Req.Method == "OPTIONS" { + ctx.Status(http.StatusOK) + return + } + } + username := ctx.Params(":username") reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git")