Skip to content

Commit a93bdf6

Browse files
authored
Add Permissions-Policy (#77)
mark Feature-Policy deprecated fixes #76
1 parent 60e6560 commit a93bdf6

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ 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"`, // Deprecated: This feature is no longer recommended. 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 "".
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 "".
85+
PermissionsPolicy: "fullscreen=(), geolocation=()", // PermissionsPolicy allows the Permissions-Policy header with the value to be set with a custom value. Default is "".
8586
ExpectCTHeader: `enforce, max-age=30, report-uri="https://www.example.com/ct-report"`,
8687

8788
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.
@@ -117,6 +118,7 @@ l := secure.New(secure.Options{
117118
PublicKey: "",
118119
ReferrerPolicy: "",
119120
FeaturePolicy: "",
121+
PermissionsPolicy: "",
120122
ExpectCTHeader: "",
121123
IsDevelopment: false,
122124
})

secure.go

+24-15
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@ import (
1111
type secureCtxKey string
1212

1313
const (
14-
stsHeader = "Strict-Transport-Security"
15-
stsSubdomainString = "; includeSubDomains"
16-
stsPreloadString = "; preload"
17-
frameOptionsHeader = "X-Frame-Options"
18-
frameOptionsValue = "DENY"
19-
contentTypeHeader = "X-Content-Type-Options"
20-
contentTypeValue = "nosniff"
21-
xssProtectionHeader = "X-XSS-Protection"
22-
xssProtectionValue = "1; mode=block"
23-
cspHeader = "Content-Security-Policy"
24-
cspReportOnlyHeader = "Content-Security-Policy-Report-Only"
25-
hpkpHeader = "Public-Key-Pins"
26-
referrerPolicyHeader = "Referrer-Policy"
27-
featurePolicyHeader = "Feature-Policy"
28-
expectCTHeader = "Expect-CT"
14+
stsHeader = "Strict-Transport-Security"
15+
stsSubdomainString = "; includeSubDomains"
16+
stsPreloadString = "; preload"
17+
frameOptionsHeader = "X-Frame-Options"
18+
frameOptionsValue = "DENY"
19+
contentTypeHeader = "X-Content-Type-Options"
20+
contentTypeValue = "nosniff"
21+
xssProtectionHeader = "X-XSS-Protection"
22+
xssProtectionValue = "1; mode=block"
23+
cspHeader = "Content-Security-Policy"
24+
cspReportOnlyHeader = "Content-Security-Policy-Report-Only"
25+
hpkpHeader = "Public-Key-Pins"
26+
referrerPolicyHeader = "Referrer-Policy"
27+
featurePolicyHeader = "Feature-Policy"
28+
permissionsPolicyHeader = "Permissions-Policy"
29+
expectCTHeader = "Expect-CT"
2930

3031
ctxDefaultSecureHeaderKey = secureCtxKey("SecureResponseHeader")
3132
cspNonceSize = 16
@@ -79,7 +80,10 @@ type Options struct {
7980
// ReferrerPolicy allows sites to control when browsers will pass the Referer header to other sites. Default is "".
8081
ReferrerPolicy string
8182
// FeaturePolicy allows to selectively enable and disable use of various browser features and APIs. Default is "".
83+
// Deprecated: This header has been renamed to Permissions-Policy.
8284
FeaturePolicy string
85+
// PermissionsPolicy allows to selectively enable and disable use of various browser features and APIs. Default is "".
86+
PermissionsPolicy string
8387
// SSLHost is the host name that is used to redirect http requests to https. Default is "", which indicates to use the same host.
8488
SSLHost string
8589
// AllowedHosts is a list of fully qualified domain names that are allowed. Default is empty list, which allows any and all host names.
@@ -426,6 +430,11 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He
426430
responseHeader.Set(featurePolicyHeader, s.opt.FeaturePolicy)
427431
}
428432

433+
// Permissions Policy header.
434+
if len(s.opt.PermissionsPolicy) > 0 {
435+
responseHeader.Set(permissionsPolicyHeader, s.opt.PermissionsPolicy)
436+
}
437+
429438
// Expect-CT header.
430439
if len(s.opt.ExpectCTHeader) > 0 {
431440
responseHeader.Set(expectCTHeader, s.opt.ExpectCTHeader)

secure_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,20 @@ func TestFeaturePolicy(t *testing.T) {
11261126
expect(t, res.Header().Get("Feature-Policy"), "vibrate 'none';")
11271127
}
11281128

1129+
func TestPermissionsPolicy(t *testing.T) {
1130+
s := New(Options{
1131+
PermissionsPolicy: "geolocation=(self)",
1132+
})
1133+
1134+
res := httptest.NewRecorder()
1135+
req, _ := http.NewRequest("GET", "/foo", nil)
1136+
1137+
s.Handler(myHandler).ServeHTTP(res, req)
1138+
1139+
expect(t, res.Code, http.StatusOK)
1140+
expect(t, res.Header().Get("Permissions-Policy"), "geolocation=(self)")
1141+
}
1142+
11291143
func TestExpectCT(t *testing.T) {
11301144
s := New(Options{
11311145
ExpectCTHeader: `enforce, max-age=30, report-uri="https://www.example.com/ct-report"`,

0 commit comments

Comments
 (0)