diff --git a/gitrpc/branch.go b/gitrpc/branch.go index 1555bd09d..ba9421d9e 100644 --- a/gitrpc/branch.go +++ b/gitrpc/branch.go @@ -10,6 +10,7 @@ import ( "fmt" "io" + "github.com/harness/gitness/gitrpc/check" "github.com/harness/gitness/gitrpc/rpc" "github.com/rs/zerolog/log" @@ -75,6 +76,11 @@ func (c *Client) CreateBranch(ctx context.Context, params *CreateBranchParams) ( if params == nil { return nil, ErrNoParamsProvided } + + if err := check.BranchName(params.BranchName); err != nil { + return nil, ErrInvalidArgumentf(err.Error()) + } + resp, err := c.refService.CreateBranch(ctx, &rpc.CreateBranchRequest{ Base: mapToRPCWriteRequest(params.WriteParams), Target: params.Target, diff --git a/gitrpc/check/branch.go b/gitrpc/check/branch.go index bcbdd667f..e5c5ca871 100644 --- a/gitrpc/check/branch.go +++ b/gitrpc/check/branch.go @@ -32,11 +32,11 @@ var refnameDisposition = [256]byte{ } func BranchName(branch string) error { - lock := ".lock" - last := '\x00' + const lock = ".lock" + last := byte('\x00') - for i, c := range branch { - ch := c & 255 + for i := 0; i < len(branch); i++ { + ch := branch[i] & 255 disp := refnameDisposition[ch] switch disp { @@ -80,7 +80,7 @@ out: return fmt.Errorf("branch '%s' cannot start with '.'", branch) } if strings.HasSuffix(branch, lock) { - return fmt.Errorf("branch '%s' cannot ends with '%s'", branch, lock) + return fmt.Errorf("branch '%s' cannot end with '%s'", branch, lock) } return nil } diff --git a/gitrpc/check/branch_test.go b/gitrpc/check/branch_test.go index fef785321..dfc2e941b 100644 --- a/gitrpc/check/branch_test.go +++ b/gitrpc/check/branch_test.go @@ -29,6 +29,13 @@ func TestBranchName(t *testing.T) { }, wantErr: false, }, + { + name: "happy path, test utf-8 chars", + args: args{ + branch: "eb/new\u2318branch", + }, + wantErr: false, + }, { name: "branch name empty should return error", args: args{ diff --git a/internal/api/controller/repo/create_branch.go b/internal/api/controller/repo/create_branch.go index 26c55c33d..3e78f36f2 100644 --- a/internal/api/controller/repo/create_branch.go +++ b/internal/api/controller/repo/create_branch.go @@ -9,9 +9,7 @@ import ( "fmt" "github.com/harness/gitness/gitrpc" - "github.com/harness/gitness/gitrpc/check" apiauth "github.com/harness/gitness/internal/api/auth" - "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/auth" "github.com/harness/gitness/types/enum" ) @@ -42,11 +40,6 @@ func (c *Controller) CreateBranch(ctx context.Context, session *auth.Session, in.Target = repo.DefaultBranch } - err = check.BranchName(in.Name) - if err != nil { - return nil, usererror.BadRequest(err.Error()) - } - writeParams, err := CreateRPCWriteParams(ctx, c.urlProvider, session, repo) if err != nil { return nil, fmt.Errorf("failed to create RPC write params: %w", err)