Skip to content

Commit ff7a281

Browse files
hashworksunrolled
authored andcommitted
Add ability to set Feature-Policy header (#38)
1 parent 9707245 commit ff7a281

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ s := secure.New(secure.Options{
8181
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.
8282
PublicKey: `pin-sha256="base64+primary=="; pin-sha256="base64+backup=="; max-age=5184000; includeSubdomains; report-uri="https://www.example.com/hpkp-report"`, // PublicKey implements HPKP to prevent MITM attacks with forged certificates. Default is "".
8383
ReferrerPolicy: "same-origin" // ReferrerPolicy allows the Referrer-Policy header with the value to be set with a custom value. Default is "".
84+
FeaturePolicy: "vibrate 'none';" // FeaturePolicy allows the Feature-Policy header with the value to be set with a custom value. Default is "".
8485

8586
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.
8687
})

secure.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
cspHeader = "Content-Security-Policy"
2323
hpkpHeader = "Public-Key-Pins"
2424
referrerPolicyHeader = "Referrer-Policy"
25+
featurePolicyHeader = "Feature-Policy"
2526

2627
ctxSecureHeaderKey = secureCtxKey("SecureResponseHeader")
2728
cspNonceSize = 16
@@ -71,6 +72,8 @@ type Options struct {
7172
PublicKey string
7273
// ReferrerPolicy allows sites to control when browsers will pass the Referer header to other sites. Default is "".
7374
ReferrerPolicy string
75+
// FeaturePolicy allows to selectively enable and disable use of various browser features and APIs. Default is "".
76+
FeaturePolicy string
7477
// SSLHost is the host name that is used to redirect http requests to https. Default is "", which indicates to use the same host.
7578
SSLHost string
7679
// AllowedHosts is a list of fully qualified domain names that are allowed. Default is empty list, which allows any and all host names.
@@ -348,6 +351,11 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He
348351
responseHeader.Set(referrerPolicyHeader, s.opt.ReferrerPolicy)
349352
}
350353

354+
// Feature Policy header.
355+
if len(s.opt.FeaturePolicy) > 0 {
356+
responseHeader.Set(featurePolicyHeader, s.opt.FeaturePolicy)
357+
}
358+
351359
return responseHeader, nil
352360
}
353361

secure_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,20 @@ func TestReferrerForRequestOnly(t *testing.T) {
10231023
expect(t, res.Header().Get("Referrer-Policy"), "")
10241024
}
10251025

1026+
func TestFeaturePolicy(t *testing.T) {
1027+
s := New(Options{
1028+
FeaturePolicy: "vibrate 'none';",
1029+
})
1030+
1031+
res := httptest.NewRecorder()
1032+
req, _ := http.NewRequest("GET", "/foo", nil)
1033+
1034+
s.Handler(myHandler).ServeHTTP(res, req)
1035+
1036+
expect(t, res.Code, http.StatusOK)
1037+
expect(t, res.Header().Get("Feature-Policy"), "vibrate 'none';")
1038+
}
1039+
10261040
func TestIsSSL(t *testing.T) {
10271041
s := New(Options{
10281042
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},

0 commit comments

Comments
 (0)