release.gno
4.79 Kb · 141 lines
1package releases
2
3import (
4 "errors"
5 "strings"
6)
7
8// Size caps. A registry is written by anyone who opens a project in it, so
9// every caller-supplied string has a ceiling.
10const (
11 MaxNameLen = 32
12 MaxVersionLen = 32
13 MaxPkgPathLen = 200
14 MaxURLLen = 300
15 MaxNotesLen = 4096
16 MaxReasonLen = 280
17 MaxDescriptionLen = 280
18)
19
20// pathDomain is the prefix every release's package path carries. The stage
21// page links a release to its realm by stripping it, so "gno.land/r/x/v1"
22// becomes the in-site link "/r/x/v1".
23const pathDomain = "gno.land"
24
25var (
26 ErrInvalidName = errors.New("releases: invalid project name")
27 ErrInvalidVersion = errors.New("releases: invalid version")
28 ErrInvalidPkgPath = errors.New("releases: invalid package path")
29 ErrInvalidText = errors.New("releases: text too long or not printable")
30 ErrInvalidStatus = errors.New("releases: status cannot be stored")
31 ErrInvalidSunset = errors.New("releases: sunset height must not be negative")
32 ErrVersionExists = errors.New("releases: version already released")
33 ErrPathExists = errors.New("releases: package path already listed")
34 ErrNotFound = errors.New("releases: no such version")
35 ErrFinal = errors.New("releases: a yanked release cannot change status")
36 ErrVulnerableIsFinal = errors.New("releases: a vulnerable release can only be yanked")
37)
38
39// Release is one version of a project: the package path it was published at,
40// what changed, and where it stands.
41//
42// Project hands Releases out by value, so a caller holding one cannot change
43// the registry by writing to it. The quarantined p/demo/releases this package
44// revives returned a pointer to a range-loop copy from ByVersion, which looked
45// like a handle on the stored release and was not; there is no pointer here
46// to get wrong.
47type Release struct {
48 Version string // "v1", the key inside its project
49 PkgPath string // "gno.land/r/moul/config/v1", unique inside its project
50 URL string // optional link; the realm itself when empty
51 Notes string // the changelog entry, one item per line
52 Status Status // the stored status; Latest is never stored
53 Reason string // why the status is what it is, shown in banners
54 ReleasedAt int64 // block height at which it was listed
55 Sunset int64 // Deprecated only: the height it goes read-only, 0 for never
56 Successor string // optional version to send readers to instead of Latest
57}
58
59// Link is where a reader goes to see this release: its URL when one was
60// given, the realm's own page otherwise.
61func (r Release) Link() string {
62 if r.URL != "" {
63 return r.URL
64 }
65 return strings.TrimPrefix(r.PkgPath, pathDomain)
66}
67
68// ValidName reports whether s can name a project: a lower-case letter, then
69// letters, digits and dashes. It is the realm's render path, so it is kept to
70// characters that need no escaping anywhere.
71func ValidName(s string) bool {
72 if s == "" || len(s) > MaxNameLen || !(s[0] >= 'a' && s[0] <= 'z') {
73 return false
74 }
75 for i := 0; i < len(s); i++ {
76 c := s[i]
77 if !((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-') {
78 return false
79 }
80 }
81 return true
82}
83
84// ValidVersion reports whether s can be a version. Not semver-enforcing, for
85// the reason p/moul/forge gives: gno.land versions as v0, v1 with no minor or
86// patch, and a registry refusing those would refuse its own ecosystem.
87func ValidVersion(s string) bool {
88 if s == "" || len(s) > MaxVersionLen || strings.Contains(s, "..") {
89 return false
90 }
91 for i := 0; i < len(s); i++ {
92 c := s[i]
93 ok := (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
94 (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_' || c == '+'
95 if !ok {
96 return false
97 }
98 }
99 return true
100}
101
102// ValidPkgPath reports whether s looks like a gno.land package path. It does
103// not ask the chain whether the package exists: a Draft is listed before its
104// code is.
105func ValidPkgPath(s string) bool {
106 if len(s) > MaxPkgPathLen || !strings.HasPrefix(s, pathDomain+"/") ||
107 strings.Contains(s, "..") || strings.Contains(s, "//") || strings.HasSuffix(s, "/") {
108 return false
109 }
110 for i := len(pathDomain) + 1; i < len(s); i++ {
111 c := s[i]
112 ok := (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
113 c == '/' || c == '_' || c == '.' || c == '-'
114 if !ok {
115 return false
116 }
117 }
118 return true
119}
120
121// validText caps a free-text field and refuses control characters other than
122// newline and tab. Escaping happens at render time; this only bounds storage.
123func validText(s string, max int, multiline bool) bool {
124 if len(s) > max {
125 return false
126 }
127 for i := 0; i < len(s); i++ {
128 c := s[i]
129 if c == '\n' && multiline {
130 continue
131 }
132 if c < 0x20 && c != '\t' || c == 0x7f {
133 return false
134 }
135 }
136 return true
137}
138
139func validURL(s string) bool {
140 return validText(s, MaxURLLen, false) && !strings.ContainsAny(s, " \t")
141}