@@ -2,6 +2,7 @@ package core
22
33import (
44 "errors"
5+ "fmt"
56 "os"
67 "os/exec"
78 "path"
@@ -52,31 +53,47 @@ func CleanUpProcessGroup(cmd *exec.Cmd) {
5253}
5354
5455func ValidateGitopiaRemoteURL (remoteURL string ) (remoteUserId string , remoteRepositoryName string , err error ) {
55- if strings .HasPrefix (remoteURL , GITOPIA_PREFIX ) {
56- s := strings .TrimPrefix (remoteURL , GITOPIA_PREFIX )
57- sp := strings .Split (s , "/" )
56+ if ! strings .HasPrefix (remoteURL , GITOPIA_PREFIX ) {
57+ return "" , "" , fmt .Errorf ("invalid gitopia remote url: must start with '%s', got '%s'" , GITOPIA_PREFIX , remoteURL )
58+ }
59+
60+ s := strings .TrimPrefix (remoteURL , GITOPIA_PREFIX )
61+ sp := strings .Split (s , "/" )
62+
63+ if len (sp ) != 2 {
64+ return "" , "" , fmt .Errorf ("invalid gitopia remote url: expected format 'gitopia://user/repository', got '%s' (found %d parts after prefix)" , remoteURL , len (sp ))
65+ }
66+
67+ remoteUserId = sp [0 ]
68+ remoteRepositoryName = sp [1 ]
5869
59- if len (sp ) != 2 {
60- return "" , "" , ErrInvalidGitopiaRemoteURL
70+ if remoteUserId == "" {
71+ return "" , "" , fmt .Errorf ("invalid gitopia remote url: user ID cannot be empty in '%s'" , remoteURL )
72+ }
73+
74+ if remoteRepositoryName == "" {
75+ return "" , "" , fmt .Errorf ("invalid gitopia remote url: repository name cannot be empty in '%s'" , remoteURL )
76+ }
77+
78+ // Try to parse as bech32 address first
79+ _ , err = sdk .AccAddressFromBech32 (remoteUserId )
80+ if err != nil {
81+ // If not a valid bech32 address, validate as username
82+ if len (remoteUserId ) < 3 {
83+ return "" , "" , fmt .Errorf ("invalid gitopia remote url: user ID '%s' is too short (minimum 3 characters)" , remoteUserId )
84+ }
85+ if len (remoteUserId ) > 39 {
86+ return "" , "" , fmt .Errorf ("invalid gitopia remote url: user ID '%s' is too long (maximum 39 characters)" , remoteUserId )
87+ }
88+
89+ valid , regexErr := regexp .MatchString ("^[a-zA-Z0-9]+(?:[-]?[a-zA-Z0-9])*$" , remoteUserId )
90+ if regexErr != nil {
91+ return "" , "" , fmt .Errorf ("invalid gitopia remote url: error validating user ID '%s': %v" , remoteUserId , regexErr )
6192 }
62- remoteUserId = sp [0 ]
63- remoteRepositoryName = sp [1 ]
64-
65- _ , err := sdk .AccAddressFromBech32 (remoteUserId )
66- if err != nil {
67- if len (remoteUserId ) < 3 || len (remoteUserId ) > 39 {
68- return "" , "" , ErrInvalidGitopiaRemoteURL
69- }
70- valid , err := regexp .MatchString ("^[a-zA-Z0-9]+(?:[-]?[a-zA-Z0-9])*$" , remoteUserId )
71- if err != nil {
72- return "" , "" , ErrInvalidGitopiaRemoteURL
73- }
74- if ! valid {
75- return "" , "" , ErrInvalidGitopiaRemoteURL
76- }
93+ if ! valid {
94+ return "" , "" , fmt .Errorf ("invalid gitopia remote url: user ID '%s' contains invalid characters (only alphanumeric and hyphens allowed)" , remoteUserId )
7795 }
78- return remoteUserId , remoteRepositoryName , nil
7996 }
8097
81- return "" , "" , ErrInvalidGitopiaRemoteURL
98+ return remoteUserId , remoteRepositoryName , nil
8299}
0 commit comments