Skip to content

Commit

Permalink
go-staticcheck fixes
Browse files Browse the repository at this point in the history
Nothing significant and all manually tested, these are the recommended
fixes from staticcheck.
  • Loading branch information
grafana-dee committed Jul 18, 2023
1 parent 84e9ab4 commit a52260e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 46 deletions.
6 changes: 1 addition & 5 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,7 @@ func (p *Policy) AllowDataURIImages() {
}

_, err := base64.StdEncoding.DecodeString(url.Opaque[len(matched):])
if err != nil {
return false
}

return true
return err == nil
},
)
}
Expand Down
37 changes: 1 addition & 36 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,41 +95,6 @@ func (p *Policy) SanitizeReaderToWriter(r io.Reader, w io.Writer) error {
return p.sanitize(r, w)
}

const escapedURLChars = "'<>\"\r"

func escapeUrlComponent(w stringWriterWriter, val string) error {
i := strings.IndexAny(val, escapedURLChars)
for i != -1 {
if _, err := w.WriteString(val[:i]); err != nil {
return err
}
var esc string
switch val[i] {
case '\'':
// "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
esc = "&#39;"
case '<':
esc = "&lt;"
case '>':
esc = "&gt;"
case '"':
// "&#34;" is shorter than "&quot;".
esc = "&#34;"
case '\r':
esc = "&#13;"
default:
panic("unrecognized escape character")
}
val = val[i+1:]
if _, err := w.WriteString(esc); err != nil {
return err
}
i = strings.IndexAny(val, escapedURLChars)
}
_, err := w.WriteString(val)
return err
}

// Query represents a single part of the query string, a query param
type Query struct {
Key string
Expand Down Expand Up @@ -994,7 +959,7 @@ func (p *Policy) validURL(rawurl string) (string, bool) {
}

for _, urlPolicy := range urlPolicies {
if urlPolicy(u) == true {
if urlPolicy(u) {
return u.String(), true
}
}
Expand Down
7 changes: 2 additions & 5 deletions sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type test struct {
func TestEmpty(t *testing.T) {
p := StrictPolicy()

if "" != p.Sanitize(``) {
if p.Sanitize(``) != `` {
t.Error("Empty string is not empty")
}
}
Expand Down Expand Up @@ -459,10 +459,7 @@ func TestGlobalURLPatternsViaCustomPolicy(t *testing.T) {
"https",
func(url *url.URL) bool {
// Allow YouTube
if url.Host == `www.youtube.com` {
return true
}
return false
return url.Host == `www.youtube.com`
},
)

Expand Down

0 comments on commit a52260e

Please sign in to comment.