Skip to content

Commit f18b06f

Browse files
authored
add check (#25)
* add_check * fix
1 parent f5f709d commit f18b06f

File tree

5 files changed

+98
-16
lines changed

5 files changed

+98
-16
lines changed

config.yml.example

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
LFS_BUCKET: ***********
2-
CDN_DOMAIN: ***********
3-
OBS_REGION: ***********
4-
OBS_ACCESS_KEY_ID: ***********
5-
OBS_SECRET_ACCESS_KEY: ***********
6-
CLIENT_ID: ***********
7-
CLIENT_SECRET: ***********
8-
PATH_PREFIX: ***********
1+
{
2+
"LFS_BUCKET": ***********
3+
"CDN_DOMAIN": ***********
4+
"OBS_REGION": ***********
5+
"OBS_ACCESS_KEY_ID": ***********
6+
"OBS_SECRET_ACCESS_KEY": ***********
7+
"CLIENT_ID": ***********
8+
"CLIENT_SECRET": ***********
9+
"PATH_PREFIX": ***********
10+
"VALIDATE_REGEXP": {
11+
"OWNER_REGEXP": "^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$",
12+
"REPONAME_REGEXP": "^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$",
13+
"USERNAME_REGEXP": "^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$",
14+
"PASSWORD_REGEXP": "^[a-zA-Z0-9!@_#$%^&*()\\-=+,?.,]*$"
15+
}
16+
}

config/config.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ import (
77
)
88

99
type Config struct {
10-
Prefix string `json:"PATH_PREFIX"`
11-
LfsBucket string `json:"LFS_BUCKET"`
12-
ClientId string `json:"CLIENT_ID"`
13-
ClientSecret string `json:"CLIENT_SECRET"`
14-
CdnDomain string `json:"CDN_DOMAIN"`
15-
ObsRegion string `json:"OBS_REGION"`
16-
ObsAccessKeyId string `json:"OBS_ACCESS_KEY_ID"`
17-
ObsSecretAccessKey string `json:"OBS_SECRET_ACCESS_KEY"`
10+
Prefix string `json:"PATH_PREFIX"`
11+
LfsBucket string `json:"LFS_BUCKET"`
12+
ClientId string `json:"CLIENT_ID"`
13+
ClientSecret string `json:"CLIENT_SECRET"`
14+
CdnDomain string `json:"CDN_DOMAIN"`
15+
ObsRegion string `json:"OBS_REGION"`
16+
ObsAccessKeyId string `json:"OBS_ACCESS_KEY_ID"`
17+
ObsSecretAccessKey string `json:"OBS_SECRET_ACCESS_KEY"`
18+
ValidateConfig ValidateConfig `json:"VALIDATE_REGEXP"`
19+
}
20+
21+
type ValidateConfig struct {
22+
OwnerRegexp string `json:"OWNER_REGEXP" required:"true"`
23+
RepoNameRegexp string `json:"REPONAME_REGEXP" required:"true"`
24+
UsernameRegexp string `json:"USERNAME_REGEXP" required:"true"`
25+
PasswordRegexp string `json:"PASSWORD_REGEXP" required:"true"`
1826
}
1927

2028
// LoadConfig loads the configuration file from the specified path and deletes the file if needed

main.go

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ func main() {
8888
return
8989
}
9090

91+
if err := server.Init(cfg.ValidateConfig); err != nil {
92+
logrus.Errorf("load ValidateConfig, err:%s", err.Error())
93+
94+
return
95+
}
96+
9197
if err := auth.Init(cfg); err != nil {
9298
logrus.Errorf("load gitee config, err:%s", err.Error())
9399

server/server.go

+19
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) {
132132
userInRepo.Operation = req.Operation
133133
userInRepo.Owner = chi.URLParam(r, "owner")
134134
userInRepo.Repo = chi.URLParam(r, "repo")
135+
136+
if !validatecfg.ownerRegexp.MatchString(userInRepo.Owner) || !validatecfg.reponameRegexp.MatchString(userInRepo.Repo) {
137+
w.WriteHeader(http.StatusBadRequest)
138+
must(json.NewEncoder(w).Encode(batch.ErrorResponse{
139+
Message: "invalid owner or reponame format",
140+
}))
141+
return
142+
}
143+
135144
if err = auth.CheckRepoOwner(userInRepo); req.Operation == "upload" || err != nil {
136145
err := s.dealWithAuthError(userInRepo, w, r)
137146
if err != nil {
@@ -170,6 +179,15 @@ func (s *server) dealWithAuthError(userInRepo auth.UserInRepo, w http.ResponseWr
170179
if username, password, ok := r.BasicAuth(); ok {
171180
userInRepo.Username = username
172181
userInRepo.Password = password
182+
183+
if !validatecfg.usernameRegexp.MatchString(userInRepo.Username) ||
184+
!validatecfg.passwordRegexp.MatchString(userInRepo.Password) {
185+
w.WriteHeader(http.StatusBadRequest)
186+
must(json.NewEncoder(w).Encode(batch.ErrorResponse{
187+
Message: "invalid username or password format",
188+
}))
189+
return errors.New("invalid username or password format")
190+
}
173191
err = s.isAuthorized(userInRepo)
174192
} else {
175193
err = errors.New("unauthorized: cannot get password")
@@ -190,6 +208,7 @@ func (s *server) dealWithAuthError(userInRepo auth.UserInRepo, w http.ResponseWr
190208
}))
191209
return err
192210
}
211+
193212
return nil
194213
}
195214

server/validate.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package server
2+
3+
import (
4+
"fmt"
5+
"github.com/metalogical/BigFiles/config"
6+
"regexp"
7+
)
8+
9+
type validateConfig struct {
10+
ownerRegexp *regexp.Regexp
11+
reponameRegexp *regexp.Regexp
12+
usernameRegexp *regexp.Regexp
13+
passwordRegexp *regexp.Regexp
14+
}
15+
16+
var validatecfg validateConfig
17+
18+
func Init(cfg config.ValidateConfig) error {
19+
var err error
20+
validatecfg.ownerRegexp, err = regexp.Compile(cfg.OwnerRegexp)
21+
if err != nil {
22+
return fmt.Errorf("failed to compile owner regexp: %w", err)
23+
}
24+
25+
validatecfg.reponameRegexp, err = regexp.Compile(cfg.RepoNameRegexp)
26+
if err != nil {
27+
return fmt.Errorf("failed to compile repo name regexp: %w", err)
28+
}
29+
30+
validatecfg.usernameRegexp, err = regexp.Compile(cfg.UsernameRegexp)
31+
if err != nil {
32+
return fmt.Errorf("failed to compile username regexp: %w", err)
33+
}
34+
35+
validatecfg.passwordRegexp, err = regexp.Compile(cfg.PasswordRegexp)
36+
if err != nil {
37+
return fmt.Errorf("failed to compile password regexp: %w", err)
38+
}
39+
40+
return nil
41+
}

0 commit comments

Comments
 (0)