From 0235268ccd6c6bfee15d3080885f96f2be649202 Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Thu, 19 Nov 2026 04:29:47 +0000 Subject: [PATCH] feat: keep v0 logo and screenshot paths inside the repository --- fields.go | 23 +++++++- ...tion_en_screenshots_outside_repository.yml | 56 +++++++++++++++++++ .../v0/invalid/logo_outside_repository.yml | 54 ++++++++++++++++++ .../monochromeLogo_outside_repository.yml | 54 ++++++++++++++++++ v0_test.go | 10 ++++ 5 files changed, 194 insertions(+), 3 deletions(-) create mode 100644 testdata/v0/invalid/description_en_screenshots_outside_repository.yml create mode 100644 testdata/v0/invalid/logo_outside_repository.yml create mode 100644 testdata/v0/invalid/monochromeLogo_outside_repository.yml diff --git a/fields.go b/fields.go index d0c71ba..8642538 100644 --- a/fields.go +++ b/fields.go @@ -3,6 +3,7 @@ package publiccode import ( "fmt" "net/url" + "path" "regexp" "strings" @@ -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) @@ -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) @@ -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) @@ -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) { diff --git a/testdata/v0/invalid/description_en_screenshots_outside_repository.yml b/testdata/v0/invalid/description_en_screenshots_outside_repository.yml new file mode 100644 index 0000000..c254fde --- /dev/null +++ b/testdata/v0/invalid/description_en_screenshots_outside_repository.yml @@ -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 diff --git a/testdata/v0/invalid/logo_outside_repository.yml b/testdata/v0/invalid/logo_outside_repository.yml new file mode 100644 index 0000000..0b79324 --- /dev/null +++ b/testdata/v0/invalid/logo_outside_repository.yml @@ -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 diff --git a/testdata/v0/invalid/monochromeLogo_outside_repository.yml b/testdata/v0/invalid/monochromeLogo_outside_repository.yml new file mode 100644 index 0000000..468833b --- /dev/null +++ b/testdata/v0/invalid/monochromeLogo_outside_repository.yml @@ -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 diff --git a/v0_test.go b/v0_test.go index fa70d28..68eb950 100644 --- a/v0_test.go +++ b/v0_test.go @@ -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}, }, @@ -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{ @@ -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},