Skip to content

Commit be5607e

Browse files
authored
Merge pull request #50 from 0xbaadf00d/feature/2583-disablehttpcloning
Disable HTTP cloning
2 parents 5a6f7ed + e54dec7 commit be5607e

File tree

7 files changed

+40
-12
lines changed

7 files changed

+40
-12
lines changed

conf/app.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ PULL_REQUEST_QUEUE_LENGTH = 1000
2424
; Preferred Licenses to place at the top of the List
2525
; Name must match file name in conf/license or custom/conf/license
2626
PREFERRED_LICENSES = Apache License 2.0,MIT License
27+
; Disable ability to interact with repositories by HTTP protocol
28+
DISABLE_HTTP_GIT = false
2729

2830
[repository.editor]
2931
; List of file extensions that should have line wraps in the CodeMirror editor

modules/context/repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ func RepoAssignment(args ...bool) macaron.Handler {
254254
ctx.Data["IsRepositoryWriter"] = ctx.Repo.IsWriter()
255255

256256
ctx.Data["DisableSSH"] = setting.SSH.Disabled
257+
ctx.Data["DisableHTTP"] = setting.Repository.DisableHTTPGit
257258
ctx.Data["CloneLink"] = repo.CloneLink()
258259
ctx.Data["UncycloCloneLink"] = repo.UncycloCloneLink()
259260

modules/setting/setting.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ var (
117117
MirrorQueueLength int
118118
PullRequestQueueLength int
119119
PreferredLicenses []string
120+
DisableHTTPGit bool
120121

121122
// Repository editor settings
122123
Editor struct {
@@ -491,6 +492,7 @@ func NewContext() {
491492

492493
// Determine and create root git repository path.
493494
sec = Cfg.Section("repository")
495+
Repository.DisableHTTPGit = sec.Key("DISABLE_HTTP_GIT").MustBool()
494496
RepoRootPath = sec.Key("ROOT").MustString(path.Join(homeDir, "gogs-repositories"))
495497
forcePathSeparator(RepoRootPath)
496498
if !filepath.IsAbs(RepoRootPath) {

routers/repo/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ func HTTPBackend(ctx *context.Context, cfg *serviceConfig) http.HandlerFunc {
479479
for _, route := range routes {
480480
r.URL.Path = strings.ToLower(r.URL.Path) // blue: In case some repo name has upper case name
481481
if m := route.reg.FindStringSubmatch(r.URL.Path); m != nil {
482+
if setting.Repository.DisableHTTPGit {
483+
w.WriteHeader(http.StatusForbidden)
484+
w.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
485+
return
486+
}
482487
if route.method != r.Method {
483488
if r.Proto == "HTTP/1.1" {
484489
w.WriteHeader(http.StatusMethodNotAllowed)

templates/repo/bare.tmpl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@
1616
<div class="item">
1717
<h3>{{.i18n.Tr "repo.clone_this_repo"}} <small>{{.i18n.Tr "repo.clone_helper" "http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository" | Str2html}}</small></h3>
1818
<div class="ui action small input">
19-
<button class="ui basic clone button" id="repo-clone-https" data-link="{{.CloneLink.HTTPS}}">
20-
{{if UseHTTPS}}HTTPS{{else}}HTTP{{end}}
21-
</button>
19+
{{if not $.DisableHTTP}}
20+
<button class="ui basic clone button" id="repo-clone-https" data-link="{{.CloneLink.HTTPS}}">
21+
{{if UseHTTPS}}HTTPS{{else}}HTTP{{end}}
22+
</button>
23+
{{end}}
2224
{{if not $.DisableSSH}}
2325
<button class="ui basic clone button" id="repo-clone-ssh" data-link="{{.CloneLink.SSH}}">
2426
SSH
2527
</button>
2628
{{end}}
27-
<input id="repo-clone-url" value="{{$.CloneLink.HTTPS}}" readonly>
29+
{{if not $.DisableHTTP}}
30+
<input id="repo-clone-url" value="{{$.CloneLink.HTTPS}}" readonly>
31+
{{else}}
32+
<input id="repo-clone-url" value="{{$.CloneLink.SSH}}" readonly>
33+
{{end}}
2834
<button class="ui basic button poping up clipboard" id="clipboard-btn" data-original="{{.i18n.Tr "repo.copy_link"}}" data-success="{{.i18n.Tr "repo.copy_link_success"}}" data-error="{{.i18n.Tr "repo.copy_link_error"}}" data-content="{{.i18n.Tr "repo.copy_link"}}" data-variation="inverted tiny" data-clipboard-target="#repo-clone-url">
2935
<i class="octicon octicon-clippy"></i>
3036
</button>

templates/repo/home.tmpl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,21 @@
5151
<!-- Only show colne panel in repository home page -->
5252
{{if eq $n 0}}
5353
<div class="ui action small input" id="clone-panel">
54-
<button class="ui basic clone button" id="repo-clone-https" data-link="{{.CloneLink.HTTPS}}">
55-
{{if UseHTTPS}}HTTPS{{else}}HTTP{{end}}
56-
</button>
54+
{{if not $.DisableHTTP}}
55+
<button class="ui basic clone button" id="repo-clone-https" data-link="{{.CloneLink.HTTPS}}">
56+
{{if UseHTTPS}}HTTPS{{else}}HTTP{{end}}
57+
</button>
58+
{{end}}
5759
{{if not $.DisableSSH}}
5860
<button class="ui basic clone button" id="repo-clone-ssh" data-link="{{.CloneLink.SSH}}">
5961
SSH
6062
</button>
6163
{{end}}
62-
<input id="repo-clone-url" value="{{$.CloneLink.HTTPS}}" readonly>
64+
{{if not $.DisableHTTP}}
65+
<input id="repo-clone-url" value="{{$.CloneLink.HTTPS}}" readonly>
66+
{{else}}
67+
<input id="repo-clone-url" value="{{$.CloneLink.SSH}}" readonly>
68+
{{end}}
6369
<button class="ui basic icon button poping up clipboard" id="clipboard-btn" data-original="{{.i18n.Tr "repo.copy_link"}}" data-success="{{.i18n.Tr "repo.copy_link_success"}}" data-error="{{.i18n.Tr "repo.copy_link_error"}}" data-content="{{.i18n.Tr "repo.copy_link"}}" data-variation="inverted tiny" data-clipboard-target="#repo-clone-url">
6470
<i class="octicon octicon-clippy"></i>
6571
</button>

templates/repo/wiki/view.tmpl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,21 @@
2929
</div>
3030
<div class="ui six wide column">
3131
<div class="ui action small input" id="clone-panel">
32-
<button class="ui basic clone button" id="repo-clone-https" data-link="{{.UncycloCloneLink.HTTPS}}">
33-
{{if UseHTTPS}}HTTPS{{else}}HTTP{{end}}
34-
</button>
32+
{{if not $.DisableHTTP}}
33+
<button class="ui basic clone button" id="repo-clone-https" data-link="{{.UncycloCloneLink.HTTPS}}">
34+
{{if UseHTTPS}}HTTPS{{else}}HTTP{{end}}
35+
</button>
36+
{{end}}
3537
{{if not $.DisableSSH}}
3638
<button class="ui basic clone button" id="repo-clone-ssh" data-link="{{.UncycloCloneLink.SSH}}">
3739
SSH
3840
</button>
3941
{{end}}
40-
<input id="repo-clone-url" value="{{$.UncycloCloneLink.HTTPS}}" readonly>
42+
{{if not $.DisableHTTP}}
43+
<input id="repo-clone-url" value="{{$.UncycloCloneLink.HTTPS}}" readonly>
44+
{{else}}
45+
<input id="repo-clone-url" value="{{$.UncycloCloneLink.SSH}}" readonly>
46+
{{end}}
4147
<button class="ui basic icon button poping up clipboard" id="clipboard-btn" data-original="{{.i18n.Tr "repo.copy_link"}}" data-success="{{.i18n.Tr "repo.copy_link_success"}}" data-error="{{.i18n.Tr "repo.copy_link_error"}}" data-content="{{.i18n.Tr "repo.copy_link"}}" data-variation="inverted tiny" data-clipboard-target="#repo-clone-url">
4248
<i class="octicon octicon-clippy"></i>
4349
</button>

0 commit comments

Comments
 (0)