Skip to content

Commit d056bf3

Browse files
mrsdizzielafriks
authored andcommitted
Clean up ref name rules (#6437)
* Clean up ref name rules Clean up checks on reference names to better conform to the guideline here: https://git-scm.com/docs/git-check-ref-format This fixes half of #6321 * Update branch create integration test According to: https://git-scm.com/docs/git-check-ref-format And: git check-ref-format "master/feature=test1" This is a valid branch name and we should not be testing for it to fail.
1 parent b4941f7 commit d056bf3

File tree

4 files changed

+132
-4
lines changed

4 files changed

+132
-4
lines changed

integrations/repo_branch_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestCreateBranch(t *testing.T) {
5858
OldRefSubURL: "branch/master",
5959
NewBranch: "feature=test1",
6060
ExpectedStatus: http.StatusFound,
61-
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.git_ref_name_error"),
61+
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature=test1"),
6262
},
6363
{
6464
OldRefSubURL: "branch/master",

modules/validation/binding.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ const (
1919

2020
var (
2121
// GitRefNamePattern is regular expression with unallowed characters in git reference name
22-
GitRefNamePattern = regexp.MustCompile("[^\\d\\w-_\\./]")
22+
// They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere.
23+
// They cannot have question-mark ?, asterisk *, or open bracket [ anywhere
24+
GitRefNamePattern = regexp.MustCompile(`[\000-\037\177 \\~^:?*[]+`)
2325
)
2426

2527
// AddBindingRules adds additional binding rules
@@ -44,7 +46,8 @@ func addGitRefNameBindingRule() {
4446
// Additional rules as described at https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
4547
if strings.HasPrefix(str, "/") || strings.HasSuffix(str, "/") ||
4648
strings.HasSuffix(str, ".") || strings.Contains(str, "..") ||
47-
strings.Contains(str, "//") {
49+
strings.Contains(str, "//") || strings.Contains(str, "@{") ||
50+
str == "@" {
4851
errs.Add([]string{name}, ErrGitRefName, "GitRefName")
4952
return false, errs
5053
}

modules/validation/refname_test.go

+124
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ var gitRefNameValidationTestCases = []validationTestCase{
2525
},
2626
expectedErrors: binding.Errors{},
2727
},
28+
{
29+
description: "Reference name has allowed special characters",
30+
data: TestForm{
31+
BranchName: "debian/1%1.6.0-2",
32+
},
33+
expectedErrors: binding.Errors{},
34+
},
2835
{
2936
description: "Reference name contains backslash",
3037
data: TestForm{
@@ -129,6 +136,123 @@ var gitRefNameValidationTestCases = []validationTestCase{
129136
},
130137
},
131138
},
139+
{
140+
description: "Reference name is single @",
141+
data: TestForm{
142+
BranchName: "@",
143+
},
144+
expectedErrors: binding.Errors{
145+
binding.Error{
146+
FieldNames: []string{"BranchName"},
147+
Classification: ErrGitRefName,
148+
Message: "GitRefName",
149+
},
150+
},
151+
},
152+
{
153+
description: "Reference name has @{",
154+
data: TestForm{
155+
BranchName: "branch@{",
156+
},
157+
expectedErrors: binding.Errors{
158+
binding.Error{
159+
FieldNames: []string{"BranchName"},
160+
Classification: ErrGitRefName,
161+
Message: "GitRefName",
162+
},
163+
},
164+
},
165+
{
166+
description: "Reference name has unallowed special character ~",
167+
data: TestForm{
168+
BranchName: "~debian/1%1.6.0-2",
169+
},
170+
expectedErrors: binding.Errors{
171+
binding.Error{
172+
FieldNames: []string{"BranchName"},
173+
Classification: ErrGitRefName,
174+
Message: "GitRefName",
175+
},
176+
},
177+
},
178+
{
179+
description: "Reference name has unallowed special character *",
180+
data: TestForm{
181+
BranchName: "*debian/1%1.6.0-2",
182+
},
183+
expectedErrors: binding.Errors{
184+
binding.Error{
185+
FieldNames: []string{"BranchName"},
186+
Classification: ErrGitRefName,
187+
Message: "GitRefName",
188+
},
189+
},
190+
},
191+
{
192+
description: "Reference name has unallowed special character ?",
193+
data: TestForm{
194+
BranchName: "?debian/1%1.6.0-2",
195+
},
196+
expectedErrors: binding.Errors{
197+
binding.Error{
198+
FieldNames: []string{"BranchName"},
199+
Classification: ErrGitRefName,
200+
Message: "GitRefName",
201+
},
202+
},
203+
},
204+
{
205+
description: "Reference name has unallowed special character ^",
206+
data: TestForm{
207+
BranchName: "^debian/1%1.6.0-2",
208+
},
209+
expectedErrors: binding.Errors{
210+
binding.Error{
211+
FieldNames: []string{"BranchName"},
212+
Classification: ErrGitRefName,
213+
Message: "GitRefName",
214+
},
215+
},
216+
},
217+
{
218+
description: "Reference name has unallowed special character :",
219+
data: TestForm{
220+
BranchName: "debian:jessie",
221+
},
222+
expectedErrors: binding.Errors{
223+
binding.Error{
224+
FieldNames: []string{"BranchName"},
225+
Classification: ErrGitRefName,
226+
Message: "GitRefName",
227+
},
228+
},
229+
},
230+
{
231+
description: "Reference name has unallowed special character (whitespace)",
232+
data: TestForm{
233+
BranchName: "debian jessie",
234+
},
235+
expectedErrors: binding.Errors{
236+
binding.Error{
237+
FieldNames: []string{"BranchName"},
238+
Classification: ErrGitRefName,
239+
Message: "GitRefName",
240+
},
241+
},
242+
},
243+
{
244+
description: "Reference name has unallowed special character [",
245+
data: TestForm{
246+
BranchName: "debian[jessie",
247+
},
248+
expectedErrors: binding.Errors{
249+
binding.Error{
250+
FieldNames: []string{"BranchName"},
251+
Classification: ErrGitRefName,
252+
Message: "GitRefName",
253+
},
254+
},
255+
},
132256
}
133257

134258
func Test_GitRefNameValidation(t *testing.T) {

routers/repo/branch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/base"
1515
"code.gitea.io/gitea/modules/context"
1616
"code.gitea.io/gitea/modules/log"
17+
"code.gitea.io/gitea/modules/util"
1718
)
1819

1920
const (
@@ -250,5 +251,5 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
250251
}
251252

252253
ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
253-
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + form.NewBranchName)
254+
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName))
254255
}

0 commit comments

Comments
 (0)