GeezerVison is showing github.com — Removing HPKP and Expect-CT (#92) · unrolled/secure@58f2e47
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1-4Lines changed: 1 addition & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -53,7 +53,7 @@ Content-Security-Policy: script-src 'nonce-a2ZobGFoZg==' | 53 | 53 | ~~~ | 54 | 54 |
| 55 | 55 | ### Set the `IsDevelopment` option to `true` when developing! | 56 | | -When `IsDevelopment` is true, the AllowedHosts, SSLRedirect, STS header, and HPKP header will not be in effect. This allows you to work in development/test mode and not have any annoying redirects to HTTPS (ie. development can happen on HTTP), or block `localhost` has a bad host. | | 56 | +When `IsDevelopment` is true, the AllowedHosts, SSLRedirect, and STS header will not be in effect. This allows you to work in development/test mode and not have any annoying redirects to HTTPS (ie. development can happen on HTTP), or block `localhost` has a bad host. | 57 | 57 |
| 58 | 58 | ### Available options | 59 | 59 | Secure comes with a variety of configuration options (Note: these are not the default option values. See the defaults below.): | @@ -80,12 +80,10 @@ s := secure.New(secure.Options{ | 80 | 80 | BrowserXssFilter: true, // If BrowserXssFilter is true, adds the X-XSS-Protection header with the value `1; mode=block`. Default is false. | 81 | 81 | CustomBrowserXssValue: "1; report=https://example.com/xss-report", // CustomBrowserXssValue allows the X-XSS-Protection header value to be set with a custom value. This overrides the BrowserXssFilter option. Default is "". | 82 | 82 | ContentSecurityPolicy: "default-src 'self'", // ContentSecurityPolicy allows the Content-Security-Policy header value to be set with a custom value. Default is "". 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. | 83 | | - PublicKey: `pin-sha256="base64+primary=="; pin-sha256="base64+backup=="; max-age=5184000; includeSubdomains; report-uri="https://www.example.com/hpkp-report"`, // Deprecated: This feature is no longer recommended. PublicKey implements HPKP to prevent MITM attacks with forged certificates. Default is "". | 84 | 83 | ReferrerPolicy: "same-origin", // ReferrerPolicy allows the Referrer-Policy header with the value to be set with a custom value. Default is "". | 85 | 84 | FeaturePolicy: "vibrate 'none';", // Deprecated: this header has been renamed to PermissionsPolicy. FeaturePolicy allows the Feature-Policy header with the value to be set with a custom value. Default is "". | 86 | 85 | PermissionsPolicy: "fullscreen=(), geolocation=()", // PermissionsPolicy allows the Permissions-Policy header with the value to be set with a custom value. Default is "". | 87 | 86 | CrossOriginOpenerPolicy: "same-origin", // CrossOriginOpenerPolicy allows the Cross-Origin-Opener-Policy header with the value to be set with a custom value. Default is "". | 88 | | - ExpectCTHeader: `enforce, max-age=30, report-uri="https://www.example.com/ct-report"`, | 89 | 87 |
| 90 | 88 | IsDevelopment: true, // This will cause the AllowedHosts, SSLRedirect, and STSSeconds/STSIncludeSubdomains options to be ignored during development. When deploying to production, be sure to set this to false. | 91 | 89 | }) | @@ -123,7 +121,6 @@ l := secure.New(secure.Options{ | 123 | 121 | FeaturePolicy: "", | 124 | 122 | PermissionsPolicy: "", | 125 | 123 | CrossOriginOpenerPolicy: "", | 126 | | - ExpectCTHeader: "", | 127 | 124 | IsDevelopment: false, | 128 | 125 | }) | 129 | 126 | ~~~ | |
Collapse file Copy file name to clipboardExpand all lines: secure.go -16Lines changed: 0 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,6 @@ const ( | 26 | 26 | referrerPolicyHeader = "Referrer-Policy" | 27 | 27 | featurePolicyHeader = "Feature-Policy" | 28 | 28 | permissionsPolicyHeader = "Permissions-Policy" | 29 | | - expectCTHeader = "Expect-CT" | 30 | 29 | coopHeader = "Cross-Origin-Opener-Policy" | 31 | 30 |
| 32 | 31 | ctxDefaultSecureHeaderKey = secureCtxKey("SecureResponseHeader") | @@ -82,9 +81,6 @@ type Options struct { | 82 | 81 | // Eg: script-src $NONCE -> script-src 'nonce-a2ZobGFoZg==' | 83 | 82 | // CustomFrameOptionsValue allows the X-Frame-Options header value to be set with a custom value. This overrides the FrameDeny option. Default is "". | 84 | 83 | CustomFrameOptionsValue string | 85 | | - // PublicKey implements HPKP to prevent MITM attacks with forged certificates. Default is "". | 86 | | - // Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible. | 87 | | - PublicKey string | 88 | 84 | // ReferrerPolicy allows sites to control when browsers will pass the Referer header to other sites. Default is "". | 89 | 85 | ReferrerPolicy string | 90 | 86 | // FeaturePolicy allows to selectively enable and disable use of various browser features and APIs. Default is "". | @@ -112,8 +108,6 @@ type Options struct { | 112 | 108 | SSLProxyHeaders map[string]string | 113 | 109 | // STSSeconds is the max-age of the Strict-Transport-Security header. Default is 0, which would NOT include the header. | 114 | 110 | STSSeconds int64 | 115 | | - // ExpectCTHeader allows the Expect-CT header value to be set with a custom value. Default is "". | 116 | | - ExpectCTHeader string | 117 | 111 | // SecureContextKey allows a custom key to be specified for context storage. | 118 | 112 | SecureContextKey string | 119 | 113 | } | @@ -434,11 +428,6 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He | 434 | 428 | responseHeader.Set(xssProtectionHeader, xssProtectionValue) | 435 | 429 | } | 436 | 430 |
| 437 | | - // HPKP header. | 438 | | - if len(s.opt.PublicKey) > 0 && ssl && !s.opt.IsDevelopment { | 439 | | - responseHeader.Set(hpkpHeader, s.opt.PublicKey) | 440 | | - } | 441 | | - | 442 | 431 | // Content Security Policy header. | 443 | 432 | if len(s.opt.ContentSecurityPolicy) > 0 { | 444 | 433 | if s.opt.nonceEnabled { | @@ -477,11 +466,6 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He | 477 | 466 | responseHeader.Set(coopHeader, s.opt.CrossOriginOpenerPolicy) | 478 | 467 | } | 479 | 468 |
| 480 | | - // Expect-CT header. | 481 | | - if len(s.opt.ExpectCTHeader) > 0 { | 482 | | - responseHeader.Set(expectCTHeader, s.opt.ExpectCTHeader) | 483 | | - } | 484 | | - | 485 | 469 | return responseHeader, r, nil | 486 | 470 | } | 487 | 471 |
| |
Collapse file Copy file name to clipboardExpand all lines: secure_test.go -130Lines changed: 0 additions & 130 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -969,121 +969,6 @@ func TestInlineSecureForRequestOnly(t *testing.T) { | 969 | 969 | expect(t, res.Header().Get("X-Frame-Options"), "") | 970 | 970 | } | 971 | 971 |
| 972 | | -// https://developer.mozilla.org/en-US/docs/Web/Security/Public_Key_Pinning | 973 | | -const hpkp = `pin-sha256="cUPcTAZWKaASuYWhhneDttWpY3oBAkE3h2+soZS7sWs="; pin-sha256="M8HztCzM3elUxkcjR2S5P4hhyBNf6lHkmjAHKhpGPWE="; max-age=5184000; includeSubdomains; report-uri="https://www.example.net/hpkp-report"` | 974 | | - | 975 | | -func TestHPKP(t *testing.T) { | 976 | | - s := New(Options{ | 977 | | - PublicKey: hpkp, | 978 | | - }) | 979 | | - | 980 | | - res := httptest.NewRecorder() | 981 | | - req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil) | 982 | | - req.URL.Scheme = "https" | 983 | | - | 984 | | - s.Handler(myHandler).ServeHTTP(res, req) | 985 | | - | 986 | | - expect(t, res.Code, http.StatusOK) | 987 | | - expect(t, res.Header().Get("Public-Key-Pins"), hpkp) | 988 | | -} | 989 | | - | 990 | | -func TestHPKPForRequestOnly(t *testing.T) { | 991 | | - s := New(Options{ | 992 | | - PublicKey: hpkp, | 993 | | - }) | 994 | | - | 995 | | - res := httptest.NewRecorder() | 996 | | - req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil) | 997 | | - req.URL.Scheme = "https" | 998 | | - | 999 | | - s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) | 1000 | | - | 1001 | | - expect(t, res.Code, http.StatusOK) | 1002 | | - expect(t, res.Header().Get("Public-Key-Pins"), "") | 1003 | | -} | 1004 | | - | 1005 | | -func TestHPKPNotSet(t *testing.T) { | 1006 | | - s := New() | 1007 | | - | 1008 | | - res := httptest.NewRecorder() | 1009 | | - req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil) | 1010 | | - | 1011 | | - s.Handler(myHandler).ServeHTTP(res, req) | 1012 | | - | 1013 | | - expect(t, res.Code, http.StatusOK) | 1014 | | - expect(t, res.Header().Get("Public-Key-Pins"), "") | 1015 | | -} | 1016 | | - | 1017 | | -func TestHPKPNotSetForRequestOnly(t *testing.T) { | 1018 | | - s := New() | 1019 | | - | 1020 | | - res := httptest.NewRecorder() | 1021 | | - req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil) | 1022 | | - | 1023 | | - s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) | 1024 | | - | 1025 | | - expect(t, res.Code, http.StatusOK) | 1026 | | - expect(t, res.Header().Get("Public-Key-Pins"), "") | 1027 | | -} | 1028 | | - | 1029 | | -func TestHPKPInDevMode(t *testing.T) { | 1030 | | - s := New(Options{ | 1031 | | - PublicKey: hpkp, | 1032 | | - IsDevelopment: true, | 1033 | | - }) | 1034 | | - | 1035 | | - res := httptest.NewRecorder() | 1036 | | - req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil) | 1037 | | - | 1038 | | - s.Handler(myHandler).ServeHTTP(res, req) | 1039 | | - | 1040 | | - expect(t, res.Code, http.StatusOK) | 1041 | | - expect(t, res.Header().Get("Public-Key-Pins"), "") | 1042 | | -} | 1043 | | - | 1044 | | -func TestHPKPInDevModeForRequestOnly(t *testing.T) { | 1045 | | - s := New(Options{ | 1046 | | - PublicKey: hpkp, | 1047 | | - IsDevelopment: true, | 1048 | | - }) | 1049 | | - | 1050 | | - res := httptest.NewRecorder() | 1051 | | - req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil) | 1052 | | - | 1053 | | - s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) | 1054 | | - | 1055 | | - expect(t, res.Code, http.StatusOK) | 1056 | | - expect(t, res.Header().Get("Public-Key-Pins"), "") | 1057 | | -} | 1058 | | - | 1059 | | -func TestHPKPNonSSL(t *testing.T) { | 1060 | | - s := New(Options{ | 1061 | | - PublicKey: hpkp, | 1062 | | - }) | 1063 | | - | 1064 | | - res := httptest.NewRecorder() | 1065 | | - req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil) | 1066 | | - | 1067 | | - s.Handler(myHandler).ServeHTTP(res, req) | 1068 | | - | 1069 | | - expect(t, res.Code, http.StatusOK) | 1070 | | - expect(t, res.Header().Get("Public-Key-Pins"), "") | 1071 | | -} | 1072 | | - | 1073 | | -func TestHPKPNonSSLForRequestOnly(t *testing.T) { | 1074 | | - s := New(Options{ | 1075 | | - PublicKey: hpkp, | 1076 | | - }) | 1077 | | - | 1078 | | - res := httptest.NewRecorder() | 1079 | | - req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil) | 1080 | | - | 1081 | | - s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) | 1082 | | - | 1083 | | - expect(t, res.Code, http.StatusOK) | 1084 | | - expect(t, res.Header().Get("Public-Key-Pins"), "") | 1085 | | -} | 1086 | | - | 1087 | 972 | func TestReferrer(t *testing.T) { | 1088 | 973 | s := New(Options{ | 1089 | 974 | ReferrerPolicy: "same-origin", | @@ -1154,21 +1039,6 @@ func TestCrossOriginOpenerPolicy(t *testing.T) { | 1154 | 1039 | expect(t, res.Header().Get("Cross-Origin-Opener-Policy"), "same-origin") | 1155 | 1040 | } | 1156 | 1041 |
| 1157 | | -func TestExpectCT(t *testing.T) { | 1158 | | - s := New(Options{ | 1159 | | - ExpectCTHeader: `enforce, max-age=30, report-uri="https://www.example.com/ct-report"`, | 1160 | | - }) | 1161 | | - | 1162 | | - res := httptest.NewRecorder() | 1163 | | - req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo", nil) | 1164 | | - req.Host = "www.example.com" | 1165 | | - | 1166 | | - s.Handler(myHandler).ServeHTTP(res, req) | 1167 | | - | 1168 | | - expect(t, res.Code, http.StatusOK) | 1169 | | - expect(t, res.Header().Get("Expect-CT"), `enforce, max-age=30, report-uri="https://www.example.com/ct-report"`) | 1170 | | -} | 1171 | | - | 1172 | 1042 | func TestIsSSL(t *testing.T) { | 1173 | 1043 | s := New(Options{ | 1174 | 1044 | SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, | |
|