Skip to content

Commit c0de0c2

Browse files
committed
Check if hash present, before sending a request
1 parent 37c9eaf commit c0de0c2

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

cmd/csaf_checker/processor.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ type reporter interface {
8484
report(*processor, *Domain)
8585
}
8686

87-
var (
88-
// errContinue indicates that the current check should continue.
89-
errContinue = errors.New("continue")
90-
)
87+
// errContinue indicates that the current check should continue.
88+
var errContinue = errors.New("continue")
9189

9290
type whereType byte
9391

@@ -167,7 +165,6 @@ func (m *topicMessages) hasErrors() bool {
167165

168166
// newProcessor returns an initialized processor.
169167
func newProcessor(cfg *config) (*processor, error) {
170-
171168
var validator csaf.RemoteValidator
172169

173170
if cfg.RemoteValidator != "" {
@@ -240,7 +237,6 @@ func (p *processor) reset() {
240237
// Then it calls the report method on each report from the given "reporters" parameter for each domain.
241238
// It returns a pointer to the report and nil, otherwise an error.
242239
func (p *processor) run(domains []string) (*Report, error) {
243-
244240
report := Report{
245241
Date: ReportTime{Time: time.Now().UTC()},
246242
Version: util.SemVersion,
@@ -297,7 +293,6 @@ func (p *processor) run(domains []string) (*Report, error) {
297293

298294
// fillMeta fills the report with extra informations from provider metadata.
299295
func (p *processor) fillMeta(domain *Domain) error {
300-
301296
if p.pmd == nil {
302297
return nil
303298
}
@@ -323,7 +318,6 @@ func (p *processor) fillMeta(domain *Domain) error {
323318
// domainChecks compiles a list of checks which should be performed
324319
// for a given domain.
325320
func (p *processor) domainChecks(domain string) []func(*processor, string) error {
326-
327321
// If we have a direct domain url we dont need to
328322
// perform certain checks.
329323
direct := strings.HasPrefix(domain, "https://")
@@ -393,7 +387,6 @@ func (p *processor) markChecked(s string, mask whereType) bool {
393387
}
394388

395389
func (p *processor) checkRedirect(r *http.Request, via []*http.Request) error {
396-
397390
url := r.URL.String()
398391
p.checkTLS(url)
399392
if p.redirects == nil {
@@ -495,7 +488,6 @@ func (p *processor) usedAuthorizedClient() bool {
495488

496489
// rolieFeedEntries loads the references to the advisory files for a given feed.
497490
func (p *processor) rolieFeedEntries(feed string) ([]csaf.AdvisoryFile, error) {
498-
499491
client := p.httpClient()
500492
res, err := client.Get(feed)
501493
p.badDirListings.use()
@@ -546,7 +538,6 @@ func (p *processor) rolieFeedEntries(feed string) ([]csaf.AdvisoryFile, error) {
546538
var files []csaf.AdvisoryFile
547539

548540
rfeed.Entries(func(entry *csaf.Entry) {
549-
550541
// Filter if we have date checking.
551542
if accept := p.cfg.Range; accept != nil {
552543
if t := time.Time(entry.Updated); !t.IsZero() && !accept.Contains(t) {
@@ -759,14 +750,20 @@ func (p *processor) integrity(
759750
// Check hashes
760751
p.badIntegrities.use()
761752

762-
for _, x := range []struct {
753+
type hash struct {
763754
ext string
764755
url func() string
765756
hash []byte
766-
}{
767-
{"SHA256", f.SHA256URL, s256.Sum(nil)},
768-
{"SHA512", f.SHA512URL, s512.Sum(nil)},
769-
} {
757+
}
758+
hashes := []hash{}
759+
if f.SHA256URL() != "" {
760+
hashes = append(hashes, hash{"SHA256", f.SHA256URL, s256.Sum(nil)})
761+
}
762+
if f.SHA512URL() != "" {
763+
hashes = append(hashes, hash{"SHA512", f.SHA512URL, s512.Sum(nil)})
764+
}
765+
766+
for _, x := range hashes {
770767
hu, err := url.Parse(x.url())
771768
if err != nil {
772769
lg(ErrorType, "Bad URL %s: %v", x.url(), err)
@@ -918,7 +915,6 @@ func (p *processor) checkIndex(base string, mask whereType) error {
918915
// of the fields' values and if they are sorted properly. Then it passes the files to the
919916
// "integrity" functions. It returns error if some test fails, otherwise nil.
920917
func (p *processor) checkChanges(base string, mask whereType) error {
921-
922918
bu, err := url.Parse(base)
923919
if err != nil {
924920
return err
@@ -978,8 +974,7 @@ func (p *processor) checkChanges(base string, mask whereType) error {
978974
}
979975
path := r[pathColumn]
980976

981-
times, files =
982-
append(times, t),
977+
times, files = append(times, t),
983978
append(files, csaf.DirectoryAdvisoryFile{Path: path})
984979
}
985980
return times, files, nil
@@ -1152,7 +1147,6 @@ func (p *processor) checkMissing(string) error {
11521147
// checkInvalid goes over all found adivisories URLs and checks
11531148
// if file name conforms to standard.
11541149
func (p *processor) checkInvalid(string) error {
1155-
11561150
p.badDirListings.use()
11571151
var invalids []string
11581152

@@ -1174,7 +1168,6 @@ func (p *processor) checkInvalid(string) error {
11741168
// checkListing goes over all found adivisories URLs and checks
11751169
// if their parent directory is listable.
11761170
func (p *processor) checkListing(string) error {
1177-
11781171
p.badDirListings.use()
11791172

11801173
pgs := pages{}
@@ -1209,7 +1202,6 @@ func (p *processor) checkListing(string) error {
12091202
// checkWhitePermissions checks if the TLP:WHITE advisories are
12101203
// available with unprotected access.
12111204
func (p *processor) checkWhitePermissions(string) error {
1212-
12131205
var ids []string
12141206
for id, open := range p.labelChecker.whiteAdvisories {
12151207
if !open {
@@ -1235,7 +1227,6 @@ func (p *processor) checkWhitePermissions(string) error {
12351227
// According to the result, the respective error messages added to
12361228
// badProviderMetadata.
12371229
func (p *processor) checkProviderMetadata(domain string) bool {
1238-
12391230
p.badProviderMetadata.use()
12401231

12411232
client := p.httpClient()
@@ -1282,7 +1273,6 @@ func (p *processor) checkSecurity(domain string, legacy bool) (int, string) {
12821273

12831274
// checkSecurityFolder checks the security.txt in a given folder.
12841275
func (p *processor) checkSecurityFolder(folder string) string {
1285-
12861276
client := p.httpClient()
12871277
path := folder + "security.txt"
12881278
res, err := client.Get(path)
@@ -1349,7 +1339,6 @@ func (p *processor) checkSecurityFolder(folder string) string {
13491339
// and serves the "provider-metadata.json".
13501340
// It returns an empty string if all checks are passed, otherwise the errormessage.
13511341
func (p *processor) checkDNS(domain string) string {
1352-
13531342
client := p.httpClient()
13541343
path := "https://csaf.data.security." + domain
13551344
res, err := client.Get(path)
@@ -1359,7 +1348,6 @@ func (p *processor) checkDNS(domain string) string {
13591348
if res.StatusCode != http.StatusOK {
13601349
return fmt.Sprintf("Fetching %s failed. Status code %d (%s)",
13611350
path, res.StatusCode, res.Status)
1362-
13631351
}
13641352
hash := sha256.New()
13651353
defer res.Body.Close()
@@ -1378,7 +1366,6 @@ func (p *processor) checkDNS(domain string) string {
13781366
// available under the /.well-known/csaf/ directory. Returns the errormessage if
13791367
// an error was encountered, or an empty string otherwise
13801368
func (p *processor) checkWellknown(domain string) string {
1381-
13821369
client := p.httpClient()
13831370
path := "https://" + domain + "/.well-known/csaf/provider-metadata.json"
13841371

@@ -1408,7 +1395,6 @@ func (p *processor) checkWellknown(domain string) string {
14081395
// The function returns nil, unless errors outside the checks were found.
14091396
// In that case, errors are returned.
14101397
func (p *processor) checkWellknownSecurityDNS(domain string) error {
1411-
14121398
warningsW := p.checkWellknown(domain)
14131399
// Security check for well known (default) and legacy location
14141400
warningsS, sDMessage := p.checkSecurity(domain, false)
@@ -1461,7 +1447,6 @@ func (p *processor) checkWellknownSecurityDNS(domain string) error {
14611447
// As a result of these a respective error messages are passed to badPGP method
14621448
// in case of errors. It returns nil if all checks are passed.
14631449
func (p *processor) checkPGPKeys(_ string) error {
1464-
14651450
p.badPGPs.use()
14661451

14671452
src, err := p.expr.Eval("$.public_openpgp_keys", p.pmd)
@@ -1520,7 +1505,6 @@ func (p *processor) checkPGPKeys(_ string) error {
15201505
defer res.Body.Close()
15211506
return crypto.NewKeyFromArmoredReader(res.Body)
15221507
}()
1523-
15241508
if err != nil {
15251509
p.badPGPs.error("Reading public OpenPGP key %s failed: %v", u, err)
15261510
continue

0 commit comments

Comments
 (0)