mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-23 00:32:55 +01:00
Merge pull request #365 from csaf-poc/filename-id
Check that filename matches /document/tracking/id
This commit is contained in:
commit
4461bd6892
7 changed files with 52 additions and 0 deletions
|
|
@ -557,6 +557,11 @@ func (w *worker) mirrorFiles(tlpLabel csaf.TLPLabel, files []csaf.AdvisoryFile)
|
||||||
continue
|
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 {
|
if err := w.extractCategories(label, advisory); err != nil {
|
||||||
log.Printf("error: %s: %v\n", file, err)
|
log.Printf("error: %s: %v\n", file, err)
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -512,6 +512,12 @@ func (p *processor) integrity(
|
||||||
p.invalidAdvisories.error("CSAF file %s has %d validation errors.", u, len(errors))
|
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.
|
// Validate against remote validator.
|
||||||
if p.validator != nil {
|
if p.validator != nil {
|
||||||
if rvr, err := p.validator.Validate(doc); err != nil {
|
if rvr, err := p.validator.Validate(doc); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -439,6 +439,11 @@ nextAdvisory:
|
||||||
continue
|
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
|
// Validate against remote validator
|
||||||
if d.validator != nil {
|
if d.validator != nil {
|
||||||
rvr, err := d.validator.Validate(doc)
|
rvr, err := d.validator.Validate(doc)
|
||||||
|
|
|
||||||
|
|
@ -196,6 +196,11 @@ func (c *controller) upload(r *http.Request) (any, error) {
|
||||||
return nil, err
|
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.
|
// Check if we have to search for dynamic categories.
|
||||||
var dynamicCategories []string
|
var dynamicCategories []string
|
||||||
if catExprs := c.cfg.DynamicCategories(); len(catExprs) > 0 {
|
if catExprs := c.cfg.DynamicCategories(); len(catExprs) > 0 {
|
||||||
|
|
|
||||||
|
|
@ -243,6 +243,11 @@ func (p *processor) uploadRequest(filename string) (*http.Request, error) {
|
||||||
writeStrings("Errors:", errs)
|
writeStrings("Errors:", errs)
|
||||||
return nil, errors.New("local schema check failed")
|
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)
|
body := new(bytes.Buffer)
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,7 @@ func main() {
|
||||||
func run(opts *options, files []string) error {
|
func run(opts *options, files []string) error {
|
||||||
|
|
||||||
var validator csaf.RemoteValidator
|
var validator csaf.RemoteValidator
|
||||||
|
eval := util.NewPathEval()
|
||||||
|
|
||||||
if opts.RemoteValidator != "" {
|
if opts.RemoteValidator != "" {
|
||||||
validatorOptions := csaf.RemoteValidatorOptions{
|
validatorOptions := csaf.RemoteValidatorOptions{
|
||||||
|
|
@ -109,6 +110,13 @@ func run(opts *options, files []string) error {
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("%q passes the schema validation.\n", file)
|
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.
|
// Validate against remote validator.
|
||||||
if validator != nil {
|
if validator != nil {
|
||||||
rvr, err := validator.Validate(doc)
|
rvr, err := validator.Validate(doc)
|
||||||
|
|
|
||||||
18
util/file.go
18
util/file.go
|
|
@ -9,6 +9,7 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -38,6 +39,23 @@ func ConformingFileName(fname string) bool {
|
||||||
return fname == CleanFileName(fname)
|
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.
|
// PathExists returns true if path exits.
|
||||||
func PathExists(path string) (bool, error) {
|
func PathExists(path string) (bool, error) {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue