Skip to content

Commit 328a2a9

Browse files
authored
Add ability to set Cross-Origin-Opener-Policy header (#80)
* Add ability to set Cross-Origin-Opener-Policy header * Document CrossOriginOpenerPolicy
1 parent 1a8629f commit 328a2a9

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ s := secure.New(secure.Options{
8383
ReferrerPolicy: "same-origin", // ReferrerPolicy allows the Referrer-Policy header with the value to be set with a custom value. Default is "".
8484
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 "".
8585
PermissionsPolicy: "fullscreen=(), geolocation=()", // PermissionsPolicy allows the Permissions-Policy header with the value to be set with a custom value. Default is "".
86+
CrossOriginOpenerPolicy: "same-origin", // CrossOriginOpenerPolicy allows the Cross-Origin-Opener-Policy header with the value to be set with a custom value. Default is "".
8687
ExpectCTHeader: `enforce, max-age=30, report-uri="https://www.example.com/ct-report"`,
8788

8889
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.
@@ -119,6 +120,7 @@ l := secure.New(secure.Options{
119120
ReferrerPolicy: "",
120121
FeaturePolicy: "",
121122
PermissionsPolicy: "",
123+
CrossOriginOpenerPolicy: "",
122124
ExpectCTHeader: "",
123125
IsDevelopment: false,
124126
})

secure.go

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
featurePolicyHeader = "Feature-Policy"
2828
permissionsPolicyHeader = "Permissions-Policy"
2929
expectCTHeader = "Expect-CT"
30+
coopHeader = "Cross-Origin-Opener-Policy"
3031

3132
ctxDefaultSecureHeaderKey = secureCtxKey("SecureResponseHeader")
3233
cspNonceSize = 16
@@ -84,6 +85,9 @@ type Options struct {
8485
FeaturePolicy string
8586
// PermissionsPolicy allows to selectively enable and disable use of various browser features and APIs. Default is "".
8687
PermissionsPolicy string
88+
// CrossOriginOpenerPolicy allows you to ensure a top-level document does not share a browsing context group with cross-origin documents. Default is "".
89+
// Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy
90+
CrossOriginOpenerPolicy string
8791
// SSLHost is the host name that is used to redirect http requests to https. Default is "", which indicates to use the same host.
8892
SSLHost string
8993
// AllowedHosts is a list of fully qualified domain names that are allowed. Default is empty list, which allows any and all host names.
@@ -435,6 +439,11 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He
435439
responseHeader.Set(permissionsPolicyHeader, s.opt.PermissionsPolicy)
436440
}
437441

442+
// Cross Origin Opener Policy header.
443+
if len(s.opt.CrossOriginOpenerPolicy) > 0 {
444+
responseHeader.Set(coopHeader, s.opt.CrossOriginOpenerPolicy)
445+
}
446+
438447
// Expect-CT header.
439448
if len(s.opt.ExpectCTHeader) > 0 {
440449
responseHeader.Set(expectCTHeader, s.opt.ExpectCTHeader)

secure_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,20 @@ func TestPermissionsPolicy(t *testing.T) {
11401140
expect(t, res.Header().Get("Permissions-Policy"), "geolocation=(self)")
11411141
}
11421142

1143+
func TestCrossOriginOpenerPolicy(t *testing.T) {
1144+
s := New(Options{
1145+
CrossOriginOpenerPolicy: "same-origin",
1146+
})
1147+
1148+
res := httptest.NewRecorder()
1149+
req, _ := http.NewRequest("GET", "/foo", nil)
1150+
1151+
s.Handler(myHandler).ServeHTTP(res, req)
1152+
1153+
expect(t, res.Code, http.StatusOK)
1154+
expect(t, res.Header().Get("Cross-Origin-Opener-Policy"), "same-origin")
1155+
}
1156+
11431157
func TestExpectCT(t *testing.T) {
11441158
s := New(Options{
11451159
ExpectCTHeader: `enforce, max-age=30, report-uri="https://www.example.com/ct-report"`,

0 commit comments

Comments
 (0)