Skip to content

Commit ad09662

Browse files
committed
Add length limits for description and data
1 parent c82ac1f commit ad09662

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

models/actions/variable.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package actions
66
import (
77
"context"
88
"strings"
9+
"unicode/utf8"
910

1011
"code.gitea.io/gitea/models/db"
1112
"code.gitea.io/gitea/modules/log"
@@ -37,6 +38,11 @@ type ActionVariable struct {
3738
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
3839
}
3940

41+
const (
42+
VariableDataMaxLength = 65536
43+
VariableDescriptionMaxLength = 4096
44+
)
45+
4046
func init() {
4147
db.RegisterModel(new(ActionVariable))
4248
}
@@ -48,6 +54,14 @@ func InsertVariable(ctx context.Context, ownerID, repoID int64, name, data, desc
4854
ownerID = 0
4955
}
5056

57+
if utf8.RuneCountInString(data) > VariableDataMaxLength {
58+
data = string([]rune(data)[:VariableDataMaxLength])
59+
}
60+
61+
if utf8.RuneCountInString(description) > VariableDescriptionMaxLength {
62+
description = string([]rune(description)[:VariableDescriptionMaxLength])
63+
}
64+
5165
variable := &ActionVariable{
5266
OwnerID: ownerID,
5367
RepoID: repoID,

models/secret/secret.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"strings"
10+
"unicode/utf8"
1011

1112
actions_model "code.gitea.io/gitea/models/actions"
1213
"code.gitea.io/gitea/models/db"
@@ -44,6 +45,11 @@ type Secret struct {
4445
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
4546
}
4647

48+
const (
49+
SecretDataMaxLength = 65536
50+
SecretDescriptionMaxLength = 4096
51+
)
52+
4753
// ErrSecretNotFound represents a "secret not found" error.
4854
type ErrSecretNotFound struct {
4955
Name string
@@ -68,10 +74,19 @@ func InsertEncryptedSecret(ctx context.Context, ownerID, repoID int64, name, dat
6874
return nil, fmt.Errorf("%w: ownerID and repoID cannot be both zero, global secrets are not supported", util.ErrInvalidArgument)
6975
}
7076

77+
if len(data) > SecretDataMaxLength {
78+
data = data[:SecretDataMaxLength]
79+
}
80+
81+
if utf8.RuneCountInString(description) > SecretDescriptionMaxLength {
82+
description = string([]rune(description)[:SecretDescriptionMaxLength])
83+
}
84+
7185
encrypted, err := secret_module.EncryptSecret(setting.SecretKey, data)
7286
if err != nil {
7387
return nil, err
7488
}
89+
7590
secret := &Secret{
7691
OwnerID: ownerID,
7792
RepoID: repoID,

routers/web/shared/actions/variables.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func SetVariablesContext(ctx *context.Context, ownerID, repoID int64) {
2323
return
2424
}
2525
ctx.Data["Variables"] = variables
26+
ctx.Data["DataMaxLength"] = actions_model.VariableDataMaxLength
27+
ctx.Data["DescriptionMaxLength"] = actions_model.VariableDescriptionMaxLength
2628
}
2729

2830
func CreateVariable(ctx *context.Context, ownerID, repoID int64, redirectURL string) {

routers/web/shared/secrets/secrets.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func SetSecretsContext(ctx *context.Context, ownerID, repoID int64) {
2222
}
2323

2424
ctx.Data["Secrets"] = secrets
25+
ctx.Data["DataMaxLength"] = secret_model.SecretDataMaxLength
26+
ctx.Data["DescriptionMaxLength"] = secret_model.SecretDescriptionMaxLength
2527
}
2628

2729
func PerformSecretsPost(ctx *context.Context, ownerID, repoID int64, redirectURL string) {

templates/shared/secrets/add_list.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
<input autofocus required
6666
id="secret-name"
6767
name="name"
68-
value="{{.name}}"
6968
pattern="^(?!GITEA_|GITHUB_)[a-zA-Z_][a-zA-Z0-9_]*$"
7069
placeholder="{{ctx.Locale.Tr "secrets.creation.name_placeholder"}}"
7170
>
@@ -75,7 +74,7 @@
7574
<input
7675
id="secret-description"
7776
name="description"
78-
value="{{.description}}"
77+
maxlength="{{.DescriptionMaxLength}}"
7978
placeholder="{{ctx.Locale.Tr "secrets.creation.description_placeholder"}}"
8079
>
8180
</div>
@@ -84,6 +83,7 @@
8483
<textarea required
8584
id="secret-data"
8685
name="data"
86+
maxlength="{{.DataMaxLength}}"
8787
placeholder="{{ctx.Locale.Tr "secrets.creation.value_placeholder"}}"
8888
></textarea>
8989
</div>

templates/shared/variables/variable_list.tmpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
data-modal-header="{{ctx.Locale.Tr "actions.variables.creation"}}"
88
data-modal-dialog-variable-name=""
99
data-modal-dialog-variable-data=""
10+
data-modal-dialog-variable-description=""
1011
>
1112
{{ctx.Locale.Tr "actions.variables.creation"}}
1213
</button>
@@ -76,7 +77,6 @@
7677
<input autofocus required
7778
name="name"
7879
id="dialog-variable-name"
79-
value="{{.name}}"
8080
pattern="^(?!GITEA_|GITHUB_)[a-zA-Z_][a-zA-Z0-9_]*$"
8181
placeholder="{{ctx.Locale.Tr "secrets.creation.name_placeholder"}}"
8282
>
@@ -86,7 +86,7 @@
8686
<input
8787
name="description"
8888
id="dialog-variable-description"
89-
value="{{.description}}"
89+
maxlength="{{.DescriptionMaxLength}}"
9090
placeholder="{{ctx.Locale.Tr "secrets.creation.description_placeholder"}}"
9191
>
9292
</div>
@@ -95,6 +95,7 @@
9595
<textarea required
9696
name="data"
9797
id="dialog-variable-data"
98+
maxlength="{{.DataMaxLength}}"
9899
placeholder="{{ctx.Locale.Tr "secrets.creation.value_placeholder"}}"
99100
></textarea>
100101
</div>

0 commit comments

Comments
 (0)