This repository was archived by the owner on Jan 16, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -1016,8 +1016,9 @@ func main() {
1016
1016
}
1017
1017
}
1018
1018
}()
1019
- http .Handle ("/" , s )
1020
- log .Fatal (http .ListenAndServe (s .v .GetString (ConfigBindAddress ), s ))
1019
+ ss := httputil .HSTS (s )
1020
+ http .Handle ("/" , ss )
1021
+ log .Fatal (http .ListenAndServe (s .v .GetString (ConfigBindAddress ), ss ))
1021
1022
}
1022
1023
1023
1024
// removeInternal removes the internal packages from the given package
Original file line number Diff line number Diff line change
1
+ package httputil
2
+
3
+ import "net/http"
4
+
5
+ func HSTS (next http.Handler ) http.Handler {
6
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
7
+ // This enforces the use of HTTPS for 1 year, including present and future subdomains.
8
+ // Chrome and Mozilla Firefox maintain an HSTS preload list
9
+ // that automatically informs the browser that the website can only be accessed through HTTPS.
10
+ // issue : https://github.com/golang/go/issues/26162
11
+ w .Header ().Set ("Strict-Transport-Security" , "max-age=31536000; includeSubDomains; preload" )
12
+ next .ServeHTTP (w , r )
13
+ })
14
+ }
Original file line number Diff line number Diff line change
1
+ package httputil
2
+
3
+ import (
4
+ "io"
5
+ "net/http"
6
+ "net/http/httptest"
7
+ "testing"
8
+ )
9
+
10
+ func TestHSTS (t * testing.T ) {
11
+ req := httptest .NewRequest (http .MethodGet , "/" , nil )
12
+ respRecorder := httptest .NewRecorder ()
13
+ handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
14
+ io .WriteString (w , "" )
15
+ })
16
+ handlerWithMiddlewareHSTS := HSTS (handler )
17
+ handlerWithMiddlewareHSTS .ServeHTTP (respRecorder , req )
18
+ want := "max-age=31536000; includeSubDomains; preload"
19
+ got := respRecorder .Header ().Get ("Strict-Transport-Security" )
20
+ if got != want {
21
+ t .Error ("middlewareHSTS do not add HSTS header" )
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments