1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 18:15:42 +01:00

Trimmed redirects (#230)

* Changes phrasing of redirects to be clearer. Now omits redirects if they are already listed as part of a larger redirect chain

* Rebuilt how the redirection string is built. Now checks for duplicate redirections after all redirections have been read

* Fixes intendation error

* Fixed redirect output.

* Fixed recording redirects.

Co-authored-by: Jan Höfelmeyer <Jan Höfelmeyer jhoefelmeyer@intevation.de>
Co-authored-by: Sascha L. Teichmann <sascha.teichmann@intevation.de>
This commit is contained in:
JanHoefelmeyer 2022-07-15 07:39:06 +02:00 committed by GitHub
parent 56a047cdde
commit c00b8b37bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 17 deletions

View file

@ -43,7 +43,7 @@ type processor struct {
opts *options
client util.Client
redirects map[string]string
redirects map[string][]string
noneTLS map[string]struct{}
alreadyChecked map[string]whereType
pmdURL string
@ -259,19 +259,19 @@ func (p *processor) markChecked(s string, mask whereType) bool {
func (p *processor) checkRedirect(r *http.Request, via []*http.Request) error {
var path strings.Builder
for i, v := range via {
if i > 0 {
path.WriteString(", ")
}
path.WriteString(v.URL.String())
}
url := r.URL.String()
p.checkTLS(url)
if p.redirects == nil {
p.redirects = map[string]string{}
p.redirects = map[string][]string{}
}
if redirects := p.redirects[url]; len(redirects) == 0 {
redirects = make([]string, len(via))
for i, v := range via {
redirects[i] = v.URL.String()
}
p.redirects[url] = redirects
}
p.redirects[url] = path.String()
if len(via) > 10 {
return errors.New("too many redirections")

View file

@ -11,6 +11,7 @@ package main
import (
"fmt"
"sort"
"strings"
)
type (
@ -76,19 +77,41 @@ func (r *redirectsReporter) report(p *processor, domain *Domain) {
return
}
keys := make([]string, len(p.redirects))
var i int
for k := range p.redirects {
keys[i] = k
i++
keys := keysNotInValues(p.redirects)
first := func(i int) string {
if vs := p.redirects[keys[i]]; len(vs) > 0 {
return vs[0]
}
sort.Strings(keys)
return ""
}
sort.Slice(keys, func(i, j int) bool { return first(i) < first(j) })
for i, k := range keys {
keys[i] = fmt.Sprintf("Redirect %s: %s", k, p.redirects[k])
keys[i] = fmt.Sprintf("Redirect %s -> %s", strings.Join(p.redirects[k], " -> "), k)
}
req.message(WarnType, keys...)
}
// keysNotInValues returns a slice of keys which are not in the values
// of the given map.
func keysNotInValues(m map[string][]string) []string {
values := map[string]bool{}
for _, vs := range m {
for _, v := range vs {
values[v] = true
}
}
keys := make([]string, 0, len(m))
for k := range m {
if !values[k] {
keys = append(keys, k)
}
}
return keys
}
// report tests if an provider-metadata.json are available and sets the
// "message" field value of the "Requirement" struct as a result of that.
func (r *providerMetadataReport) report(p *processor, domain *Domain) {