1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Merge pull request #365 from csaf-poc/filename-id

Check that filename matches /document/tracking/id
This commit is contained in:
JanHoefelmeyer 2023-05-16 08:50:48 +02:00 committed by GitHub
commit 4461bd6892
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 0 deletions

View file

@ -557,6 +557,11 @@ func (w *worker) mirrorFiles(tlpLabel csaf.TLPLabel, files []csaf.AdvisoryFile)
continue
}
if util.CleanFileName(sum.ID) != filename {
log.Printf("ID %q does not match filename %s",
sum.ID, filename)
}
if err := w.extractCategories(label, advisory); err != nil {
log.Printf("error: %s: %v\n", file, err)
continue

View file

@ -512,6 +512,12 @@ func (p *processor) integrity(
p.invalidAdvisories.error("CSAF file %s has %d validation errors.", u, len(errors))
}
if err := util.IDMatchesFilename(p.expr, doc, filepath.Base(u)); err != nil {
p.invalidAdvisories.error("%s: %v\n", u, err)
continue
}
// Validate against remote validator.
if p.validator != nil {
if rvr, err := p.validator.Validate(doc); err != nil {

View file

@ -439,6 +439,11 @@ nextAdvisory:
continue
}
if err := util.IDMatchesFilename(d.eval, doc, filename); err != nil {
log.Printf("Ignoring %s: %s.\n", file.URL(), err)
continue
}
// Validate against remote validator
if d.validator != nil {
rvr, err := d.validator.Validate(doc)

View file

@ -196,6 +196,11 @@ func (c *controller) upload(r *http.Request) (any, error) {
return nil, err
}
if util.CleanFileName(ex.ID) != newCSAF {
return nil, fmt.Errorf("ID %q does not match filename %s",
ex.ID, newCSAF)
}
// Check if we have to search for dynamic categories.
var dynamicCategories []string
if catExprs := c.cfg.DynamicCategories(); len(catExprs) > 0 {

View file

@ -243,6 +243,11 @@ func (p *processor) uploadRequest(filename string) (*http.Request, error) {
writeStrings("Errors:", errs)
return nil, errors.New("local schema check failed")
}
eval := util.NewPathEval()
if err := util.IDMatchesFilename(eval, doc, filepath.Base(filename)); err != nil {
return nil, err
}
}
body := new(bytes.Buffer)

View file

@ -54,6 +54,7 @@ func main() {
func run(opts *options, files []string) error {
var validator csaf.RemoteValidator
eval := util.NewPathEval()
if opts.RemoteValidator != "" {
validatorOptions := csaf.RemoteValidatorOptions{
@ -109,6 +110,13 @@ func run(opts *options, files []string) error {
} else {
fmt.Printf("%q passes the schema validation.\n", file)
}
// Check filename agains ID
if err := util.IDMatchesFilename(eval, doc, filepath.Base(file)); err != nil {
log.Printf("%s: %s.\n", file, err)
continue
}
// Validate against remote validator.
if validator != nil {
rvr, err := validator.Validate(doc)

View file

@ -9,6 +9,7 @@
package util
import (
"fmt"
"io"
"math/rand"
"os"
@ -38,6 +39,23 @@ func ConformingFileName(fname string) bool {
return fname == CleanFileName(fname)
}
// IDMatchesFilename checks that filename can be derived from the value
// of document/tracking/id extracted from doc using eval.
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#51-filename
func IDMatchesFilename(eval *PathEval, doc any, filename string) error {
var id string
if err := eval.Extract(`$.document.tracking.id`, StringMatcher(&id), false, doc); err != nil {
return fmt.Errorf("check that ID matches filename: %v", err)
}
if CleanFileName(id) != filename {
return fmt.Errorf("document/tracking/id %q does not match filename %s",
id, filename)
}
return nil
}
// PathExists returns true if path exits.
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)