diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2023-09-05 17:21:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-05 15:21:02 +0000 |
commit | a99b96cbcd10e4cad3194c142a0fffe371959455 (patch) | |
tree | fcd68e70ecced060c20c43b08414ae4c97fe8d38 /routers/web/shared/actions | |
parent | e9f50676535216b74a467fab4623daf6d0c39fce (diff) | |
download | gitea-a99b96cbcd10e4cad3194c142a0fffe371959455.tar.gz gitea-a99b96cbcd10e4cad3194c142a0fffe371959455.zip |
Refactor secrets modification logic (#26873)
- Share code between web and api
- Add some tests
Diffstat (limited to 'routers/web/shared/actions')
-rw-r--r-- | routers/web/shared/actions/variables.go | 16 |
1 files changed, 3 insertions, 13 deletions
diff --git a/routers/web/shared/actions/variables.go b/routers/web/shared/actions/variables.go index 8d1516c91c..341c18f589 100644 --- a/routers/web/shared/actions/variables.go +++ b/routers/web/shared/actions/variables.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" + secret_service "code.gitea.io/gitea/services/secrets" ) func SetVariablesContext(ctx *context.Context, ownerID, repoID int64) { @@ -33,20 +34,9 @@ func SetVariablesContext(ctx *context.Context, ownerID, repoID int64) { // https://docs.github.com/en/actions/learn-github-actions/variables#naming-conventions-for-configuration-variables // https://docs.github.com/en/actions/security-guides/encrypted-secrets#naming-your-secrets var ( - nameRx = regexp.MustCompile("(?i)^[A-Z_][A-Z0-9_]*$") - forbiddenPrefixRx = regexp.MustCompile("(?i)^GIT(EA|HUB)_") - forbiddenEnvNameCIRx = regexp.MustCompile("(?i)^CI") ) -func NameRegexMatch(name string) error { - if !nameRx.MatchString(name) || forbiddenPrefixRx.MatchString(name) { - log.Error("Name %s, regex match error", name) - return errors.New("name has invalid character") - } - return nil -} - func envNameCIRegexMatch(name string) error { if forbiddenEnvNameCIRx.MatchString(name) { log.Error("Env Name cannot be ci") @@ -58,7 +48,7 @@ func envNameCIRegexMatch(name string) error { func CreateVariable(ctx *context.Context, ownerID, repoID int64, redirectURL string) { form := web.GetForm(ctx).(*forms.EditVariableForm) - if err := NameRegexMatch(form.Name); err != nil { + if err := secret_service.ValidateName(form.Name); err != nil { ctx.JSONError(err.Error()) return } @@ -82,7 +72,7 @@ func UpdateVariable(ctx *context.Context, redirectURL string) { id := ctx.ParamsInt64(":variable_id") form := web.GetForm(ctx).(*forms.EditVariableForm) - if err := NameRegexMatch(form.Name); err != nil { + if err := secret_service.ValidateName(form.Name); err != nil { ctx.JSONError(err.Error()) return } |