Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package publiccode
import (
"fmt"
"net/url"
"path"
"regexp"
"strings"

Expand Down Expand Up @@ -50,7 +51,7 @@ func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseU
}

if publiccodev0.Logo != nil && *publiccodev0.Logo != "" {
if _, err := isRelativePathOrURL(*publiccodev0.Logo, "logo"); err != nil {
if err := isRepositoryPathOrURL(*publiccodev0.Logo, "logo"); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u := parser.toAbsoluteURL(*publiccodev0.Logo, baseURL, network)
Expand All @@ -69,7 +70,7 @@ func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseU
"This key is DEPRECATED and will be removed in the future. Use 'logo' instead",
))

if _, err := isRelativePathOrURL(*publiccodev0.MonochromeLogo, "monochromeLogo"); err != nil {
if err := isRepositoryPathOrURL(*publiccodev0.MonochromeLogo, "monochromeLogo"); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u := parser.toAbsoluteURL(*publiccodev0.MonochromeLogo, baseURL, network)
Expand Down Expand Up @@ -223,7 +224,7 @@ func validateFieldsV0(publiccode PublicCode, parser *Parser, network bool, baseU

for i, v := range desc.Screenshots {
keyName := fmt.Sprintf("description.%s.screenshots[%d]", lang, i)
if _, err := isRelativePathOrURL(v, keyName); err != nil {
if err := isRepositoryPathOrURL(v, keyName); err != nil {
vr = append(vr, err)
} else if !parser.disableExternalChecks {
u := parser.toAbsoluteURL(v, baseURL, network)
Expand Down Expand Up @@ -470,6 +471,22 @@ func validateFieldsV1(publiccode PublicCode, parser *Parser, network bool, baseU
return vr
}

// isRepositoryPathOrURL is isRelativePathOrURL with the Standard's rule
// that a path must not refer to a file outside the repository.
func isRepositoryPathOrURL(content string, keyName string) error {
isURL := urlSchemeRegexp.MatchString(content)

if !isURL && !strings.HasPrefix(content, "/") {
if cleaned := path.Clean(content); cleaned == ".." || strings.HasPrefix(cleaned, "../") {
return newValidationError(keyName, "refers to a file outside the repository")
}
}

_, err := isRelativePathOrURL(content, keyName)

return err
}

// isRelativePathOrURL checks whether the field contains either a relative filename
// or an HTTP URL.
func isRelativePathOrURL(content string, keyName string) (bool, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
publiccodeYmlVersion: "0"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"
softwareVersion: "dev"
releaseDate: "2017-04-15"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Feature 1

# Should NOT validate: screenshot refers to a file outside the repository
screenshots:
- ../../other/repo/main/screenshot.png

legal:
license: AGPL-3.0-or-later

maintenance:
type: "community"

contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- en
54 changes: 54 additions & 0 deletions testdata/v0/invalid/logo_outside_repository.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
publiccodeYmlVersion: "0"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"
releaseDate: "2017-04-15"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

# Should NOT validate: logo refers to a file outside the repository
logo: "img/../../logo.png"

description:
en:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "community"

contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- en
54 changes: 54 additions & 0 deletions testdata/v0/invalid/monochromeLogo_outside_repository.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
publiccodeYmlVersion: "0"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"
releaseDate: "2017-04-15"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

# Should NOT validate: monochromeLogo refers to a file outside the repository
monochromeLogo: "../logo.png"

description:
en:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "community"

contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- en
10 changes: 10 additions & 0 deletions v0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ func TestInvalidTestcasesV0(t *testing.T) {
"logo_absolute_path.yml": ValidationResults{
ValidationError{"", "logo", "is an absolute path. Only relative paths or HTTP(s) URLs allowed", 18, 1},
},
"logo_outside_repository.yml": ValidationResults{
ValidationError{"", "logo", "refers to a file outside the repository", 18, 1},
},
"logo_file_scheme.yml": ValidationResults{
ValidationError{"", "logo", "is a file:// URL. Only relative paths or HTTP(s) URLs allowed", 18, 1},
},
Expand Down Expand Up @@ -255,6 +258,10 @@ func TestInvalidTestcasesV0(t *testing.T) {
ValidationWarning{"", "monochromeLogo", "This key is DEPRECATED and will be removed in the future. Use 'logo' instead", 18, 1},
ValidationError{"", "monochromeLogo", "no such file: " + cwd + "/testdata/v0/invalid/no_such_file.png", 18, 1},
},
"monochromeLogo_outside_repository.yml": ValidationResults{
ValidationWarning{"", "monochromeLogo", "This key is DEPRECATED and will be removed in the future. Use 'logo' instead", 18, 1},
ValidationError{"", "monochromeLogo", "refers to a file outside the repository", 18, 1},
},

// organisation
"organisation_wrong_uri.yml": ValidationResults{
Expand Down Expand Up @@ -440,6 +447,9 @@ func TestInvalidTestcasesV0(t *testing.T) {
9,
},
},
"description_en_screenshots_outside_repository.yml": ValidationResults{
ValidationError{"", "description.en.screenshots[0]", "refers to a file outside the repository", 42, 9},
},
"description_en_awards_wrong_type.yml": ValidationResults{
ValidationError{"", "description.en.awards", "wrong type for this field", 40, 1},
ValidationError{"", "description", "description must contain more than 0 items", 18, 1},
Expand Down