Repository navigation
Expand file tree
/
Copy pathfields.go
More file actions
476 lines (415 loc) · 15.1 KB
/
Copy pathfields.go
File metadata and controls
476 lines (415 loc) · 15.1 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package publiccode
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/alranel/go-vcsurl/v2"
urlutil "github.com/publiccodeyml/libpubliccode/v5/internal"
)
// RFC 3986 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ).
var urlSchemeRegexp = regexp.MustCompile(`^([A-Za-z][A-Za-z0-9+.-]*):`)
type validateFn func(publiccode PublicCode, parser *Parser, network bool, baseURL *url.URL) error
// validateFieldsV0 validates publiccode.yml with additional rules not validatable
// with go-playground/validator
// It returns any error encountered as ValidationResults.
func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseURL *url.URL) error { //nolint:maintidx
publiccodev0, ok := publiccode.(*PublicCodeV0)
if !ok {
return fmt.Errorf("internal: expected *PublicCodeV0, got %T", publiccode) //nolint:err113 // dynamic type name
}
var vr ValidationResults
checksNetwork := network && !parser.disableExternalChecks
if checksNetwork && publiccodev0.URL != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.URL)); !reachable {
vr = append(vr, newValidationErrorf("url", "'%s' not reachable: %s", publiccodev0.URL, err.Error()))
}
if !vcsurl.IsRepo((*url.URL)(publiccodev0.URL)) {
vr = append(vr, newValidationError("url", "is not a valid code repository"))
}
}
if checksNetwork && publiccodev0.LandingURL != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.LandingURL)); !reachable {
vr = append(vr, newValidationErrorf(
"landingURL",
"'%s' not reachable: %s", publiccodev0.LandingURL, err.Error(),
))
}
}
if checksNetwork && publiccodev0.Roadmap != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev0.Roadmap)); !reachable {
vr = append(vr, newValidationErrorf(
"roadmap",
"'%s' not reachable: %s", publiccodev0.Roadmap, err.Error(),
))
}
}
if publiccodev0.Logo != nil && *publiccodev0.Logo != "" {
if _, err := isRelativePathOrURL(*publiccodev0.Logo, "logo"); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u := toAbsoluteURL(*publiccodev0.Logo, baseURL, network)
if u != nil {
validLogo, err := parser.validLogo(*u, network)
if !validLogo {
vr = append(vr, newValidationError("logo", err.Error()))
}
}
}
}
if publiccodev0.MonochromeLogo != nil && *publiccodev0.MonochromeLogo != "" {
vr = append(vr, newValidationWarning(
"monochromeLogo",
"This key is DEPRECATED and will be removed in the future. Use 'logo' instead",
))
if _, err := isRelativePathOrURL(*publiccodev0.MonochromeLogo, "monochromeLogo"); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u := toAbsoluteURL(*publiccodev0.MonochromeLogo, baseURL, network)
if u != nil {
validLogo, err := parser.validLogo(*u, network)
if !validLogo {
vr = append(vr, newValidationError("monochromeLogo", err.Error()))
}
}
}
}
if publiccodev0.IntendedAudience != nil {
// This is not ideal, but we need to revalidate the countries
// here, because otherwise we could get a warning and the advice
// to use uppercase on an invalid country.
if publiccodev0.IntendedAudience.Countries != nil {
for i, c := range *publiccodev0.IntendedAudience.Countries {
if sharedValidate.Var(c, "iso3166_1_alpha2_lower_or_upper") == nil && c == strings.ToLower(c) {
vr = append(vr, newValidationWarningf(
fmt.Sprintf("intendedAudience.countries[%d]", i),
"Lowercase country codes are DEPRECATED. Use uppercase instead ('%s')", strings.ToUpper(c),
))
}
}
}
if publiccodev0.IntendedAudience.Scope != nil {
for i, scope := range *publiccodev0.IntendedAudience.Scope {
if scope == "government" {
vr = append(vr, newValidationWarning(
fmt.Sprintf("intendedAudience.scope[%d]", i),
"The 'government' scope is DEPRECATED and will be removed in the future. It's safe to drop it",
))
}
}
}
if publiccodev0.IntendedAudience.UnsupportedCountries != nil {
vr = append(vr, newValidationWarning(
"intendedAudience.unsupportedCountries",
"This key is DEPRECATED and will be removed in the future. "+
"Use 'intendedAudience.countries' to list the targeted countries instead",
))
for i, c := range *publiccodev0.IntendedAudience.UnsupportedCountries {
if sharedValidate.Var(c, "iso3166_1_alpha2_lower_or_upper") == nil && c == strings.ToLower(c) {
vr = append(vr, newValidationWarningf(
fmt.Sprintf("intendedAudience.unsupportedCountries[%d]", i),
"Lowercase country codes are DEPRECATED. Use uppercase instead ('%s')", strings.ToUpper(c),
))
}
}
}
}
if publiccodev0.Maintenance.Contacts != nil {
for i, contact := range *publiccodev0.Maintenance.Contacts {
if contact.Phone == nil || *contact.Phone == "" {
continue
}
if sharedValidate.Var(*contact.Phone, "e164") != nil {
vr = append(vr, newValidationWarning(
fmt.Sprintf("maintenance.contacts[%d].phone", i),
"Phone numbers SHOULD use the E.164 format, e.g. '+3923113215112'",
))
}
}
}
if publiccodev0.Legal.AuthorsFile != nil {
vr = append(vr, newValidationWarning(
"legal.authorsFile",
"This key is DEPRECATED and will be removed in the future. It's safe to drop it",
))
if _, err := isRelativePathOrURL(*publiccodev0.Legal.AuthorsFile, "legal.authorsFile"); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u := toAbsoluteURL(*publiccodev0.Legal.AuthorsFile, baseURL, network)
if u != nil {
exists, err := parser.fileExists(*u, network)
if !exists {
vr = append(vr, newValidationErrorf(
"legal.authorsFile", "'%s' does not exist: %s", urlutil.DisplayURL(u), err.Error(),
))
}
}
}
}
if publiccodev0.Legal.RepoOwner != nil {
vr = append(vr, newValidationWarning(
"legal.repoOwner",
"This key is DEPRECATED and will be removed in the future. Use 'organisation.name' instead",
))
}
if publiccodev0.InputTypes != nil {
vr = append(vr, newValidationWarning(
"inputTypes",
"This key is DEPRECATED and will be removed in the future. It's safe to drop it",
))
}
if publiccodev0.DependsOn != nil && publiccodev0.DependsOn.Open != nil {
vr = append(vr, newValidationWarning(
"dependsOn.open",
"This key is DEPRECATED and will be removed in the future. It's safe to drop it",
))
}
if publiccodev0.UsedBy != nil {
vr = append(vr, newValidationWarning(
"usedBy",
"This key is DEPRECATED and will be removed in the future. It's safe to drop it",
))
}
if publiccodev0.OutputTypes != nil {
vr = append(vr, newValidationWarning(
"outputTypes",
"This key is DEPRECATED and will be removed in the future. It's safe to drop it",
))
}
for lang, desc := range publiccodev0.Description {
if len(desc.GenericName) > 0 {
vr = append(vr, newValidationWarning(
fmt.Sprintf("description.%s.genericName", lang),
"This key is DEPRECATED and will be removed in the future. It's safe to drop it",
))
}
if checksNetwork && desc.Documentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.Documentation)); !reachable {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.documentation", lang),
"'%s' not reachable: %s", desc.Documentation, err.Error(),
))
}
}
if checksNetwork && desc.APIDocumentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.APIDocumentation)); !reachable {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.apiDocumentation", lang),
"'%s' not reachable: %s", desc.APIDocumentation, err.Error(),
))
}
}
for i, v := range desc.Screenshots {
keyName := fmt.Sprintf("description.%s.screenshots[%d]", lang, i)
if _, err := isRelativePathOrURL(v, keyName); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u := toAbsoluteURL(v, baseURL, network)
if u != nil {
isImage, err := parser.isImageFile(*u, network)
if !isImage {
vr = append(vr, newValidationErrorf(
keyName,
"'%s' is not an image: %s", v, err.Error(),
))
}
}
}
}
if len(desc.Videos) > 0 {
vr = append(vr, newValidationWarning(
fmt.Sprintf("description.%s.videos", lang),
"This key is DEPRECATED and will be removed in the future. "+
"Link videos from the documentation or the website instead",
))
}
for i, v := range desc.Videos {
err := parser.checkOEmbedURL((*url.URL)(v))
if err != nil {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.videos[%d]", lang, i),
"'%s' is not a valid video URL supporting oEmbed: %s", v, err.Error(),
))
}
}
}
it := publiccodev0.IT
if publiccodev0.It != nil {
vr = append(vr, newValidationWarning(
"it",
"Lowercase country codes are DEPRECATED and will be removed in the future. Use 'IT' instead",
))
it = publiccodev0.It
}
if publiccodev0.IT != nil && publiccodev0.It != nil {
vr = append(vr, newValidationError("it", "'IT' key already present. Remove this key"))
it = publiccodev0.IT
}
if it != nil {
if it.Conforme != nil {
vr = append(vr, newValidationWarning(
"IT.conforme",
"This key is DEPRECATED and will be removed in the future. It's safe to drop it",
))
}
if it.Riuso.CodiceIPA != "" {
if sharedValidate.Var(it.Riuso.CodiceIPA, "is_italian_ipa_code") == nil {
vr = append(vr, newValidationWarningf(
"IT.riuso.codiceIPA",
"This key is DEPRECATED and will be removed in the future. "+
"Use 'organisation.uri' and set it to 'urn:x-italian-pa:%s' instead",
it.Riuso.CodiceIPA,
))
}
}
}
if len(vr) == 0 {
return nil
}
return vr
}
// validateFieldsV1 validates publiccode.yml with additional rules not validatable
// with go-playground/validator
// It returns any error encountered as ValidationResults.
func validateFieldsV1(publiccode PublicCode, parser *Parser, network bool, baseURL *url.URL) error {
publiccodev1, ok := publiccode.(*PublicCodeV1)
if !ok {
return fmt.Errorf("internal: expected *PublicCodeV1, got %T", publiccode) //nolint:err113 // dynamic type name
}
var vr ValidationResults
checksNetwork := network && !parser.disableExternalChecks
if checksNetwork && publiccodev1.URL != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev1.URL)); !reachable {
vr = append(vr, newValidationErrorf("url", "'%s' not reachable: %s", publiccodev1.URL, err.Error()))
}
if !vcsurl.IsRepo((*url.URL)(publiccodev1.URL)) {
vr = append(vr, newValidationError("url", "is not a valid code repository"))
}
}
if checksNetwork && publiccodev1.LandingURL != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev1.LandingURL)); !reachable {
vr = append(vr, newValidationErrorf(
"landingURL",
"'%s' not reachable: %s", publiccodev1.LandingURL, err.Error(),
))
}
}
if checksNetwork && publiccodev1.Roadmap != nil {
if reachable, err := parser.isReachable(*(*url.URL)(publiccodev1.Roadmap)); !reachable {
vr = append(vr, newValidationErrorf(
"roadmap",
"'%s' not reachable: %s", publiccodev1.Roadmap, err.Error(),
))
}
}
if publiccodev1.Logo != nil && *publiccodev1.Logo != "" {
if _, err := isRelativePathOrURL(*publiccodev1.Logo, "logo"); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u := toAbsoluteURL(*publiccodev1.Logo, baseURL, network)
if u != nil {
validLogo, err := parser.validLogo(*u, network)
if !validLogo {
vr = append(vr, newValidationError("logo", err.Error()))
}
}
}
}
if publiccodev1.IntendedAudience != nil {
// This is not ideal, but we need to revalidate the countries
// here, because otherwise we could get a warning and the advice
// to use uppercase on an invalid country.
if publiccodev1.IntendedAudience.Countries != nil {
for i, c := range *publiccodev1.IntendedAudience.Countries {
if sharedValidate.Var(c, "iso3166_1_alpha2_lower_or_upper") == nil && c == strings.ToLower(c) {
vr = append(vr, newValidationWarningf(
fmt.Sprintf("intendedAudience.countries[%d]", i),
"Lowercase country codes are DEPRECATED. Use uppercase instead ('%s')", strings.ToUpper(c),
))
}
}
}
if publiccodev1.IntendedAudience.UnsupportedCountries != nil {
for i, c := range *publiccodev1.IntendedAudience.UnsupportedCountries {
if sharedValidate.Var(c, "iso3166_1_alpha2_lower_or_upper") == nil && c == strings.ToLower(c) {
vr = append(vr, newValidationWarningf(
fmt.Sprintf("intendedAudience.unsupportedCountries[%d]", i),
"Lowercase country codes are DEPRECATED. Use uppercase instead ('%s')", strings.ToUpper(c),
))
}
}
}
}
for lang, desc := range publiccodev1.Description {
if checksNetwork && desc.Documentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.Documentation)); !reachable {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.documentation", lang),
"'%s' not reachable: %s", desc.Documentation, err.Error(),
))
}
}
if checksNetwork && desc.APIDocumentation != nil {
if reachable, err := parser.isReachable(*(*url.URL)(desc.APIDocumentation)); !reachable {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.apiDocumentation", lang),
"'%s' not reachable: %s", desc.APIDocumentation, err.Error(),
))
}
}
for i, v := range desc.Screenshots {
keyName := fmt.Sprintf("description.%s.screenshots[%d]", lang, i)
if _, err := isRelativePathOrURL(v, keyName); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u := toAbsoluteURL(v, baseURL, network)
if u != nil {
isImage, err := parser.isImageFile(*u, network)
if !isImage {
vr = append(vr, newValidationErrorf(
keyName,
"'%s' is not an image: %s", v, err.Error(),
))
}
}
}
}
for i, v := range desc.Videos {
err := parser.checkOEmbedURL((*url.URL)(v))
if err != nil {
vr = append(vr, newValidationErrorf(
fmt.Sprintf("description.%s.videos[%d]", lang, i),
"'%s' is not a valid video URL supporting oEmbed: %s", v, err.Error(),
))
}
}
}
if len(vr) == 0 {
return nil
}
return vr
}
// isRelativePathOrURL checks whether the field contains either a relative filename
// or an HTTP URL
//
//nolint:unparam
func isRelativePathOrURL(content string, keyName string) (bool, error) {
if strings.HasPrefix(content, "/") {
return false, newValidationError(keyName, "is an absolute path. Only relative paths or HTTP(s) URLs allowed")
}
// Not url.Parse: it fails on values like "file:///%zz", which would then
// pass as relative paths. URL schemes are case-insensitive (RFC 3986).
if m := urlSchemeRegexp.FindStringSubmatch(content); m != nil {
switch scheme := strings.ToLower(m[1]); scheme {
case "http", "https":
return true, nil
case "file":
return false, newValidationError(keyName, "is a file:// URL. Only relative paths or HTTP(s) URLs allowed")
default:
return false, newValidationErrorf(
keyName, "has an unsupported URL scheme '%s'. Only relative paths or HTTP(s) URLs allowed", scheme,
)
}
}
return true, nil
}