Skip to content

Commit 17f2285

Browse files
Add filename conformity check
* Add util function to check a filename for confirming to csaf-v2.0-csd02. * Add code to reject bad filenames in provider, checker, aggregator and uploader.
1 parent f6fa366 commit 17f2285

File tree

6 files changed

+100
-10
lines changed

6 files changed

+100
-10
lines changed

cmd/csaf_aggregator/mirror.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,13 @@ func (w *worker) mirrorFiles(tlpLabel *csaf.TLPLabel, files []string) error {
419419
log.Printf("error: %s\n", err)
420420
continue
421421
}
422-
filename := util.CleanFileName(filepath.Base(u.Path))
422+
423+
// Ignore not confirming filenames.
424+
filename := filepath.Base(u.Path)
425+
if !util.ConfirmingFileName(filename) {
426+
log.Printf("Not confirming filename %q. Ignoring.\n", filename)
427+
continue
428+
}
423429

424430
var advisory interface{}
425431

cmd/csaf_checker/processor.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"log"
2323
"net/http"
2424
"net/url"
25+
"path/filepath"
2526
"regexp"
2627
"sort"
2728
"strconv"
@@ -204,6 +205,7 @@ func (p *processor) checkDomain(domain string) error {
204205
(*processor).checkSecurity,
205206
(*processor).checkCSAFs,
206207
(*processor).checkMissing,
208+
(*processor).checkInvalid,
207209
(*processor).checkListing,
208210
(*processor).checkWellknownMetadataReporter,
209211
(*processor).checkDNSPathReporter,
@@ -724,7 +726,29 @@ func (p *processor) checkMissing(string) error {
724726
return nil
725727
}
726728

727-
// checkListing wents over all found adivisories URLs and checks,
729+
// checkInvalid wents over all found adivisories URLs and checks
730+
// if file name confirms to standard.
731+
func (p *processor) checkInvalid(string) error {
732+
733+
p.badDirListings.use()
734+
var invalids []string
735+
736+
for f := range p.alreadyChecked {
737+
if !util.ConfirmingFileName(filepath.Base(f)) {
738+
invalids = append(invalids, f)
739+
}
740+
}
741+
742+
if len(invalids) > 0 {
743+
sort.Strings(invalids)
744+
p.badDirListings.add("advisories with invalid file names: %s",
745+
strings.Join(invalids, ", "))
746+
}
747+
748+
return nil
749+
}
750+
751+
// checkListing wents over all found adivisories URLs and checks
728752
// if their parent directory is listable.
729753
func (p *processor) checkListing(string) error {
730754

cmd/csaf_provider/actions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func (c *controller) loadCSAF(r *http.Request) (string, []byte, error) {
3939
}
4040
defer file.Close()
4141

42+
if !util.ConfirmingFileName(handler.Filename) {
43+
return "", nil, errors.New("given csaf filename is not confirming")
44+
}
45+
4246
var buf bytes.Buffer
4347
if _, err := io.Copy(&buf, c.cfg.uploadLimiter(file)); err != nil {
4448
return "", nil, err

cmd/csaf_uploader/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ func (p *processor) uploadRequest(filename string) (*http.Request, error) {
277277
// It prints the response messages.
278278
func (p *processor) process(filename string) error {
279279

280+
if bn := filepath.Base(filename); !util.ConfirmingFileName(bn) {
281+
return fmt.Errorf("%q is not a confirming file name", bn)
282+
}
283+
280284
req, err := p.uploadRequest(filename)
281285
if err != nil {
282286
return err

util/file.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,25 @@ import (
1919
"time"
2020
)
2121

22-
var (
23-
twoOrMoreDots = regexp.MustCompile(`\.{2,}`)
24-
stripSlashes = strings.NewReplacer(`/`, ``, `\`, ``)
25-
)
26-
27-
// CleanFileName removes the "/" "\" charachters and replace the two or more
28-
// occurences of "." with only one from the passed string.
22+
var invalidRune = regexp.MustCompile(`[^+\-a-z0-9]+`) // invalid runes + `_`
23+
24+
// CleanFileName replaces invalid runes with an underscore and
25+
// afterwards collapses multiple underscores into one.
26+
// If the filename does not end with '.json' it will be appended.
27+
// The filename is converted to lower case.
28+
// https://docs.oasis-open.org/csaf/csaf/v2.0/csd02/csaf-v2.0-csd02.html#51-filename
29+
// specifies valid runes as 'a' to 'z', '0' to '9' and '+', '-', '_'.
2930
func CleanFileName(s string) string {
30-
return twoOrMoreDots.ReplaceAllString(stripSlashes.Replace(s), `.`)
31+
s = strings.ToLower(s)
32+
if strings.HasSuffix(s, ".json") {
33+
s = s[:len(s)-len(".json")]
34+
}
35+
return invalidRune.ReplaceAllString(s, "_") + ".json"
36+
}
37+
38+
// ConfirmingFileName checks if the given filename is confirming the standard.
39+
func ConfirmingFileName(fname string) bool {
40+
return fname == CleanFileName(fname)
3141
}
3242

3343
// PathExists returns true if path exits.

util/file_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,48 @@ import (
55
"testing"
66
)
77

8+
func TestCleanFileName(t *testing.T) {
9+
for _, x := range [][2]string{
10+
{`HELLO`, `hello.json`},
11+
{`hello`, `hello.json`},
12+
{`cisco-sa-20190513-secureboot.json`, `cisco-sa-20190513-secureboot.json`},
13+
{``, `.json`},
14+
{`..`, `_.json`},
15+
{`../..`, `_.json`},
16+
{`abc.html`, `abc_html.json`},
17+
{`abc_.htm__l`, `abc_htm_l.json`},
18+
{`foo+BAR`, `foo+bar.json`},
19+
} {
20+
if got := CleanFileName(x[0]); got != x[1] {
21+
t.Errorf("%q: Expected %q but got %q.", x[0], x[1], got)
22+
}
23+
}
24+
}
25+
26+
func TestConfirmingFileName(t *testing.T) {
27+
for _, x := range []struct {
28+
s string
29+
b bool
30+
}{
31+
{`HELLO`, false},
32+
{`hello`, false},
33+
{`cisco-sa-20190513-secureboot.json`, true},
34+
{`example_company_-_2019-yh3234.json`, true},
35+
{`rhba-2019_0024.json`, true},
36+
{`2022__01-a.json`, false},
37+
{``, false},
38+
{`..`, false},
39+
{`../..`, false},
40+
{`abc.html`, false},
41+
{`abc_.htm__l`, false},
42+
{`foo+BAR`, false},
43+
} {
44+
if got := ConfirmingFileName(x.s); got != x.b {
45+
t.Errorf("%q: Expected %t but got %t.", x.s, x.b, got)
46+
}
47+
}
48+
}
49+
850
func TestNWriter(t *testing.T) {
951

1052
msg := []byte("Gruß!\n")

0 commit comments

Comments
 (0)