Skip to content

Commit cb0e9b5

Browse files
authored
enhance the behavior of verifying keys (#3000)
1 parent 6528948 commit cb0e9b5

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

api/v1/translation/handler_trans.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 VMware, Inc.
2+
* Copyright 2022-2024 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55

@@ -286,7 +286,7 @@ func GetString(c *gin.Context) {
286286
uriPart := struct {
287287
ProductName string `uri:"productName" binding:"alphanum"`
288288
Component string `uri:"component" binding:"component"`
289-
Key string `uri:"key" binding:"key"`
289+
Key string `uri:"key" binding:"nonHTML,key"`
290290
}{}
291291
formPart := struct {
292292
Version string `form:"version" binding:"version"`
@@ -325,7 +325,7 @@ func GetString3(c *gin.Context) {
325325
uriPart := struct {
326326
ProductName string `uri:"productName" binding:"alphanum"`
327327
Component string
328-
Key string `uri:"key" binding:"key"`
328+
Key string `uri:"key" binding:"nonHTML,key"`
329329
}{Component: "default"}
330330
formPart := struct {
331331
Version string `form:"version" binding:"version"`

api/v1/translation/types_trans.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 VMware, Inc.
2+
* Copyright 2022-2024 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55

@@ -14,7 +14,7 @@ type (
1414
ReleaseID
1515
Locale string `form:"locale" binding:"locale"`
1616
Component string `form:"component" binding:"component"`
17-
Key string `form:"key" binding:"key"`
17+
Key string `form:"key" binding:"nonHTML,key"`
1818
Source string `form:"source"`
1919
Pseudo bool `form:"pseudo" binding:"omitempty"`
2020
}

api/v2/translation/handler_trans.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 VMware, Inc.
2+
* Copyright 2022-2024 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55

@@ -237,7 +237,7 @@ func GetBundle(c *gin.Context) {
237237
func GetStrings(c *gin.Context) {
238238
uriPart := BundleID{}
239239
formPart := struct {
240-
Keys string `form:"keys" binding:"required"`
240+
Keys string `form:"keys" binding:"nonHTML,sgtnkeys"`
241241
Pseudo bool `form:"pseudo"`
242242
}{}
243243
if err := api.ExtractParameters(c, &uriPart, &formPart); err != nil {

api/v2/translation/types_trans.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 VMware, Inc.
2+
* Copyright 2022-2024 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55

@@ -28,7 +28,7 @@ type (
2828

2929
StringID struct {
3030
BundleID
31-
Key string `uri:"key" binding:"key"`
31+
Key string `uri:"key" binding:"nonHTML,key"`
3232
}
3333

3434
GetStringReq struct {

api/validator.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 VMware, Inc.
2+
* Copyright 2022-2024 VMware, Inc.
33
* SPDX-License-Identifier: EPL-2.0
44
*/
55

@@ -35,6 +35,8 @@ var (
3535
localesRegex = componentsRegex
3636
patternScopeRegex = regexp.MustCompile(`^(\s*[a-zA-Z]+\s*)(,\s*[a-zA-Z]+\s*)*$`)
3737
asciiCharsRegex = regexp.MustCompile(`\A[[:ascii:]]+\z`)
38+
multiASCIIStringRegex = regexp.MustCompile(`\A([[:ascii:]]+)(,[[:ascii:]]+)*\z`)
39+
hTMLRegex = regexp.MustCompile(`<[/]?([a-zA-Z]+).*?>`)
3840
)
3941

4042
var validatorInfoArray = [][]interface{}{
@@ -49,6 +51,8 @@ var validatorInfoArray = [][]interface{}{
4951
{ComponentsAPIKey, componentsRegex, fmt.Sprintf(letterAndNumberAndValidCharStringError, ComponentsAPIKey)},
5052
{LocalesAPIKey, localesRegex, fmt.Sprintf(letterAndNumberAndValidCharStringError, LocalesAPIKey)},
5153
{KeyAPIKey, asciiCharsRegex, "'{0}' is invalid(only standard ASCII characters are allowed)"},
54+
{"sgtnkeys", multiASCIIStringRegex, "'{0}' is invalid(only standard ASCII characters are allowed)"},
55+
{"nonHTML", func(fl validator.FieldLevel) bool { return !hTMLRegex.MatchString(fl.Field().String()) }, "HTML tags aren't allowed"},
5256
}
5357

5458
var enTranslator ut.Translator
@@ -70,11 +74,20 @@ func InitValidator() {
7074
}
7175

7276
for _, info := range validatorInfoArray {
73-
name, r := info[0].(string), info[1].(*regexp.Regexp)
74-
err := validate.RegisterValidation(name,
75-
func(fl validator.FieldLevel) bool {
76-
return r.MatchString(fl.Field().String())
77-
})
77+
name, verification := info[0].(string), info[1]
78+
var err error
79+
switch actual := verification.(type) {
80+
case *regexp.Regexp:
81+
err = validate.RegisterValidation(name,
82+
func(fl validator.FieldLevel) bool {
83+
return actual.MatchString(fl.Field().String())
84+
})
85+
case func(fl validator.FieldLevel) bool:
86+
err = validate.RegisterValidation(name, actual)
87+
default:
88+
logger.SLog.Fatal("wrong validator method: %v", name)
89+
}
90+
7891
if err == nil {
7992
err = validate.RegisterTranslation(name, enTranslator,
8093
func(ut ut.Translator) error {

0 commit comments

Comments
 (0)