summaryrefslogtreecommitdiffstats
path: root/services/secrets/validation.go
blob: 3db5b9645249b6a63b405270f45f50090405ebb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package secrets

import (
	"regexp"

	"code.gitea.io/gitea/modules/util"
)

// https://docs.github.com/en/actions/security-guides/encrypted-secrets#naming-your-secrets
var (
	namePattern            = regexp.MustCompile("(?i)^[A-Z_][A-Z0-9_]*$")
	forbiddenPrefixPattern = regexp.MustCompile("(?i)^GIT(EA|HUB)_")

	ErrInvalidName = util.NewInvalidArgumentErrorf("invalid secret name")
)

func ValidateName(name string) error {
	if !namePattern.MatchString(name) || forbiddenPrefixPattern.MatchString(name) {
		return ErrInvalidName
	}
	return nil
}