From a52260ef0163d306bc6d54de0c99ce0634e9ed50 Mon Sep 17 00:00:00 2001 From: D Kitchen Date: Tue, 18 Jul 2023 09:55:52 +0000 Subject: [PATCH] go-staticcheck fixes Nothing significant and all manually tested, these are the recommended fixes from staticcheck. --- helpers.go | 6 +----- sanitize.go | 37 +------------------------------------ sanitize_test.go | 7 ++----- 3 files changed, 4 insertions(+), 46 deletions(-) diff --git a/helpers.go b/helpers.go index 2b03d7e..aa0b7b9 100644 --- a/helpers.go +++ b/helpers.go @@ -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 }, ) } diff --git a/sanitize.go b/sanitize.go index 028f274..2792fb3 100644 --- a/sanitize.go +++ b/sanitize.go @@ -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 '\'': - // "'" is shorter than "'" and apos was not in HTML until HTML5. - esc = "'" - case '<': - esc = "<" - case '>': - esc = ">" - case '"': - // """ is shorter than """. - esc = """ - case '\r': - esc = " " - 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 @@ -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 } } diff --git a/sanitize_test.go b/sanitize_test.go index f782f2f..c81aa8b 100644 --- a/sanitize_test.go +++ b/sanitize_test.go @@ -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") } } @@ -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` }, )