Skip to content

Commit b5069f3

Browse files
authoredDec 13, 2022
linting (#89)
1 parent bedd69e commit b5069f3

File tree

7 files changed

+186
-149
lines changed

7 files changed

+186
-149
lines changed
 

‎.golangci.yaml

+34-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,37 @@ run:
22
timeout: 10m
33

44
linters:
5-
disable-all: false
5+
enable-all: true
6+
disable:
7+
# Deprecated linters
8+
- varcheck
9+
- exhaustivestruct
10+
- ifshort
11+
- structcheck
12+
- golint
13+
- maligned
14+
- interfacer
15+
- nosnakecase
16+
- deadcode
17+
- scopelint
18+
- rowserrcheck
19+
- sqlclosecheck
20+
- structcheck
21+
- wastedassign
22+
# Ignoring
23+
- lll
24+
- varnamelen
25+
- paralleltest
26+
- testpackage
27+
- goerr113
28+
- exhaustruct
29+
- nestif
30+
- funlen
31+
- goconst
32+
- nlreturn
33+
- gochecknoglobals
34+
- cyclop
35+
- gocyclo
36+
- gocognit
37+
- maintidx
38+
- contextcheck

‎csp.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func withCSPNonce(r *http.Request, nonce string) *http.Request {
3535

3636
func cspRandNonce() string {
3737
var buf [cspNonceSize]byte
38+
3839
_, err := io.ReadFull(rand.Reader, buf[:])
3940
if err != nil {
4041
panic("CSP Nonce rand.Reader failed" + err.Error())

‎csp_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package secure
22

33
import (
4+
"context"
45
"encoding/base64"
56
"fmt"
67
"net/http"
@@ -22,15 +23,14 @@ func TestCSPNonce(t *testing.T) {
2223
}{
2324
{Options{ContentSecurityPolicy: csp}, []string{"Content-Security-Policy"}},
2425
{Options{ContentSecurityPolicyReportOnly: csp}, []string{"Content-Security-Policy-Report-Only"}},
25-
{Options{ContentSecurityPolicy: csp, ContentSecurityPolicyReportOnly: csp},
26-
[]string{"Content-Security-Policy", "Content-Security-Policy-Report-Only"}},
26+
{Options{ContentSecurityPolicy: csp, ContentSecurityPolicyReportOnly: csp}, []string{"Content-Security-Policy", "Content-Security-Policy-Report-Only"}},
2727
}
2828

2929
for _, c := range cases {
3030
s := New(c.options)
3131

3232
res := httptest.NewRecorder()
33-
req, _ := http.NewRequest("GET", "/foo", nil)
33+
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil)
3434

3535
s.Handler(cspHandler).ServeHTTP(res, req)
3636

@@ -54,7 +54,7 @@ func TestCSPNonce(t *testing.T) {
5454
}
5555

5656
func TestWithCSPNonce(t *testing.T) {
57-
req, _ := http.NewRequest("GET", "/foo", nil)
57+
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil)
5858

5959
nonce := "jdgKGHkbnd+/"
6060

‎cspbuilder/builder.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
const (
8-
// Fetch Directives
8+
// Fetch Directives.
99
ChildSrc = "child-src"
1010
ConnectSrc = "connect-src"
1111
DefaultSrc = "default-src"
@@ -24,20 +24,20 @@ const (
2424
StyleSrcElem = "style-src-elem"
2525
WorkerSrc = "worker-src"
2626

27-
// Document Directives
27+
// Document Directives.
2828
BaseURI = "base-uri"
2929
Sandbox = "sandbox"
3030

31-
// Navigation directives
31+
// Navigation directives.
3232
FormAction = "form-action"
3333
FrameAncestors = "frame-ancestors"
3434
NavigateTo = "navigate-to"
3535

36-
// Reporting directives
36+
// Reporting directives.
3737
ReportURI = "report-uri"
3838
ReportTo = "report-to"
3939

40-
// Other directives
40+
// Other directives.
4141
RequireTrustedTypesFor = "require-trusted-types-for"
4242
TrustedTypes = "trusted-types"
4343
UpgradeInsecureRequests = "upgrade-insecure-requests"
@@ -53,6 +53,7 @@ func (builder *Builder) MustBuild() string {
5353
if err != nil {
5454
panic(err)
5555
}
56+
5657
return policy
5758
}
5859

‎cspbuilder/directive_builder.go

+17-26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func buildDirectiveSandbox(sb *strings.Builder, values []string) error {
1111
sb.WriteString(Sandbox)
1212
return nil
1313
}
14+
1415
if len(values) > 1 {
1516
return fmt.Errorf("too many values set for directive %s", Sandbox)
1617
}
@@ -44,15 +45,13 @@ func buildDirectiveSandbox(sb *strings.Builder, values []string) error {
4445
return nil
4546
}
4647

47-
func buildDirectiveFrameAncestors(
48-
sb *strings.Builder,
49-
values []string,
50-
) error {
48+
func buildDirectiveFrameAncestors(sb *strings.Builder, values []string) error {
5149
if len(values) == 0 {
5250
return fmt.Errorf("no values set for directive %s", FrameAncestors)
5351
}
5452

5553
sb.WriteString(FrameAncestors)
54+
5655
for _, val := range values {
5756
if strings.HasPrefix(val, "'") && strings.HasSuffix(val, "'") {
5857
switch val {
@@ -62,19 +61,19 @@ func buildDirectiveFrameAncestors(
6261
return fmt.Errorf("unallowed value %s for directive %s", val, FrameAncestors)
6362
}
6463
}
64+
6565
sb.WriteString(" ")
6666
sb.WriteString(val)
6767
}
68+
6869
return nil
6970
}
7071

71-
func buildDirectiveReportTo(
72-
sb *strings.Builder,
73-
values []string,
74-
) error {
72+
func buildDirectiveReportTo(sb *strings.Builder, values []string) error {
7573
if len(values) == 0 {
7674
return fmt.Errorf("no values set for directive %s", ReportTo)
7775
}
76+
7877
if len(values) > 1 {
7978
return fmt.Errorf("too many values set for directive %s", ReportTo)
8079
}
@@ -86,10 +85,7 @@ func buildDirectiveReportTo(
8685
return nil
8786
}
8887

89-
func buildDirectiveRequireTrustedTypesFor(
90-
sb *strings.Builder,
91-
values []string,
92-
) error {
88+
func buildDirectiveRequireTrustedTypesFor(sb *strings.Builder, values []string) error {
9389
const allowedValue = "'script'"
9490
if len(values) != 1 || values[0] != allowedValue {
9591
return fmt.Errorf("value for directive %s must be %s", RequireTrustedTypesFor, allowedValue)
@@ -102,14 +98,12 @@ func buildDirectiveRequireTrustedTypesFor(
10298
return nil
10399
}
104100

105-
func buildDirectiveTrustedTypes(
106-
sb *strings.Builder,
107-
values []string,
108-
) error {
101+
func buildDirectiveTrustedTypes(sb *strings.Builder, values []string) error {
109102
sb.WriteString(TrustedTypes)
110103

111104
for _, val := range values {
112105
sb.WriteString(" ")
106+
113107
switch val {
114108
case "'none'":
115109
if len(values) != 1 {
@@ -120,43 +114,40 @@ func buildDirectiveTrustedTypes(
120114
case "*":
121115
// nothing to do
122116
default:
123-
// value is policyname
117+
// value is policy name
124118
regex := regexp.MustCompile(`^[A-Za-z0-9\-#=_/@\.%]*$`)
125119
if !regex.MatchString(val) {
126120
return fmt.Errorf("unallowed value %s for directive %s", val, TrustedTypes)
127121
}
128122
}
123+
129124
sb.WriteString(val)
130125
}
131126

132127
return nil
133128
}
134129

135-
func buildDirectiveUpgradeInsecureRequests(
136-
sb *strings.Builder,
137-
values []string,
138-
) error {
130+
func buildDirectiveUpgradeInsecureRequests(sb *strings.Builder, values []string) error {
139131
if len(values) != 0 {
140132
return fmt.Errorf("directive %s must not contain values", UpgradeInsecureRequests)
141133
}
142134

143135
sb.WriteString(UpgradeInsecureRequests)
136+
144137
return nil
145138
}
146139

147-
func buildDirectiveDefault(
148-
sb *strings.Builder,
149-
directive string,
150-
values []string,
151-
) error {
140+
func buildDirectiveDefault(sb *strings.Builder, directive string, values []string) error {
152141
if len(values) == 0 {
153142
return fmt.Errorf("no values set for directive %s", directive)
154143
}
155144

156145
sb.WriteString(directive)
146+
157147
for i := range values {
158148
sb.WriteString(" ")
159149
sb.WriteString(values[i])
160150
}
151+
161152
return nil
162153
}

‎secure.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func defaultBadRequestHandler(w http.ResponseWriter, r *http.Request) {
5050
// Options is a struct for specifying configuration options for the secure.Secure middleware.
5151
type Options struct {
5252
// If BrowserXssFilter is true, adds the X-XSS-Protection header with the value `1; mode=block`. Default is false.
53-
BrowserXssFilter bool //nolint:stylecheck
53+
BrowserXssFilter bool //nolint:stylecheck,revive
5454
// If ContentTypeNosniff is true, adds the X-Content-Type-Options header with the value `nosniff`. Default is false.
5555
ContentTypeNosniff bool
5656
// If ForceSTSHeader is set to true, the STS header will be added even when the connection is HTTP. Default is false.
@@ -77,7 +77,7 @@ type Options struct {
7777
// ContentSecurityPolicyReportOnly allows the Content-Security-Policy-Report-Only header value to be set with a custom value. Default is "".
7878
ContentSecurityPolicyReportOnly string
7979
// CustomBrowserXssValue allows the X-XSS-Protection header value to be set with a custom value. This overrides the BrowserXssFilter option. Default is "".
80-
CustomBrowserXssValue string //nolint:stylecheck
80+
CustomBrowserXssValue string //nolint:stylecheck,revive
8181
// Passing a template string will replace `$NONCE` with a dynamic nonce value of 16 bytes for each request which can be later retrieved using the Nonce function.
8282
// Eg: script-src $NONCE -> script-src 'nonce-a2ZobGFoZg=='
8383
// CustomFrameOptionsValue allows the X-Frame-Options header value to be set with a custom value. This overrides the FrameDeny option. Default is "".
@@ -165,6 +165,7 @@ func New(options ...Options) *Secure {
165165
if err != nil {
166166
panic(fmt.Sprintf("Error parsing AllowedHost: %s", err))
167167
}
168+
168169
s.cRegexAllowedHosts = append(s.cRegexAllowedHosts, regex)
169170
}
170171
}
@@ -211,8 +212,6 @@ func (s *Secure) HandlerForRequestOnly(h http.Handler) http.Handler {
211212
// Let secure process the request. If it returns an error,
212213
// that indicates the request should not continue.
213214
responseHeader, r, err := s.processRequest(w, r)
214-
215-
// If there was an error, do not continue.
216215
if err != nil {
217216
return
218217
}
@@ -299,6 +298,7 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He
299298

300299
// Resolve the host for the request, using proxy headers if present.
301300
host := r.Host
301+
302302
for _, header := range s.opt.HostsProxyHeaders {
303303
if h := r.Header.Get(header); h != "" {
304304
host = h
@@ -309,6 +309,7 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He
309309
// Allowed hosts check.
310310
if len(s.opt.AllowedHosts) > 0 && !s.opt.IsDevelopment {
311311
isGoodHost := false
312+
312313
if s.opt.AllowedHostsAreRegex {
313314
for _, allowedHost := range s.cRegexAllowedHosts {
314315
if match := allowedHost.MatchString(host); match {
@@ -324,6 +325,7 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He
324325
}
325326
}
326327
}
328+
327329
if !isGoodHost {
328330
s.badHostHandler.ServeHTTP(w, r)
329331
return nil, nil, fmt.Errorf("bad host name: %s", host)
@@ -353,29 +355,33 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He
353355
}
354356

355357
http.Redirect(w, r, url.String(), status)
358+
356359
return nil, nil, fmt.Errorf("redirecting to HTTPS")
357360
}
358361

359362
if s.opt.SSLForceHost {
360-
var SSLHost = host
363+
tempSSLHost := host
364+
361365
if s.opt.SSLHostFunc != nil {
362366
if h := (*s.opt.SSLHostFunc)(host); len(h) > 0 {
363-
SSLHost = h
367+
tempSSLHost = h
364368
}
365369
} else if len(s.opt.SSLHost) > 0 {
366-
SSLHost = s.opt.SSLHost
370+
tempSSLHost = s.opt.SSLHost
367371
}
368-
if SSLHost != host {
372+
373+
if tempSSLHost != host {
369374
url := r.URL
370375
url.Scheme = "https"
371-
url.Host = SSLHost
376+
url.Host = tempSSLHost
372377

373378
status := http.StatusMovedPermanently
374379
if s.opt.SSLTemporaryRedirect {
375380
status = http.StatusTemporaryRedirect
376381
}
377382

378383
http.Redirect(w, r, url.String(), status)
384+
379385
return nil, nil, fmt.Errorf("redirecting to HTTPS")
380386
}
381387
}
@@ -485,6 +491,7 @@ func (s *Secure) isSSL(r *http.Request) bool {
485491
}
486492
}
487493
}
494+
488495
return ssl
489496
}
490497

@@ -507,12 +514,14 @@ func (s *Secure) ModifyResponseHeaders(res *http.Response) error {
507514

508515
responseHeader := res.Request.Context().Value(s.ctxSecureHeaderKey)
509516
if responseHeader != nil {
510-
for header, values := range responseHeader.(http.Header) {
517+
headers, _ := responseHeader.(http.Header)
518+
for header, values := range headers {
511519
if len(values) > 0 {
512520
res.Header.Set(header, strings.Join(values, ","))
513521
}
514522
}
515523
}
516524
}
525+
517526
return nil
518527
}

‎secure_test.go

+105-103
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.