-
Notifications
You must be signed in to change notification settings - Fork 381
/
validation.go
80 lines (69 loc) · 2.27 KB
/
validation.go
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package types
import (
"net/url"
"regexp"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
MaxWasmSize = 500 * 1024
// MaxLabelSize is the longest label that can be used when Instantiating a contract
MaxLabelSize = 128
// BuildTagRegexp is a docker image regexp.
// We only support max 128 characters, with at least one organization name (subset of all legal names).
//
// Details from https://docs.docker.com/engine/reference/commandline/tag/#extended-description :
//
// An image name is made up of slash-separated name components (optionally prefixed by a registry hostname).
// Name components may contain lowercase characters, digits and separators.
// A separator is defined as a period, one or two underscores, or one or more dashes. A name component may not start or end with a separator.
//
// A tag name must be valid ASCII and may contain lowercase and uppercase letters, digits, underscores, periods and dashes.
// A tag name may not start with a period or a dash and may contain a maximum of 128 characters.
BuildTagRegexp = "^[a-z0-9][a-z0-9._-]*[a-z0-9](/[a-z0-9][a-z0-9._-]*[a-z0-9])+:[a-zA-Z0-9_][a-zA-Z0-9_.-]*$"
MaxBuildTagSize = 128
)
func validateSourceURL(source string) error {
if source != "" {
u, err := url.Parse(source)
if err != nil {
return sdkerrors.Wrap(ErrInvalid, "not an url")
}
if !u.IsAbs() {
return sdkerrors.Wrap(ErrInvalid, "not an absolute url")
}
if u.Scheme != "https" {
return sdkerrors.Wrap(ErrInvalid, "must use https")
}
}
return nil
}
func validateBuilder(buildTag string) error {
if len(buildTag) > MaxBuildTagSize {
return sdkerrors.Wrap(ErrLimit, "longer than 128 characters")
}
if buildTag != "" {
ok, err := regexp.MatchString(BuildTagRegexp, buildTag)
if err != nil || !ok {
return ErrInvalid
}
}
return nil
}
func validateWasmCode(s []byte) error {
if len(s) == 0 {
return sdkerrors.Wrap(ErrEmpty, "is required")
}
if len(s) > MaxWasmSize {
return sdkerrors.Wrapf(ErrLimit, "cannot be longer than %d bytes", MaxWasmSize)
}
return nil
}
func validateLabel(label string) error {
if label == "" {
return sdkerrors.Wrap(ErrEmpty, "is required")
}
if len(label) > MaxLabelSize {
return sdkerrors.Wrap(ErrLimit, "cannot be longer than 128 characters")
}
return nil
}