@@ -13,12 +13,15 @@ const vary = require('vary');
13
13
* - {String|Number} maxAge `Access-Control-Max-Age` in seconds
14
14
* - {Boolean} credentials `Access-Control-Allow-Credentials`
15
15
* - {Boolean} keepHeadersOnError Add set headers to `err.header` if an error is thrown
16
+ * - {Boolean} secureContext `Cross-Origin-Opener-Policy` & `Cross-Origin-Embedder-Policy` headers.', default is false
17
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/Planned_changes
16
18
* @return {Function } cors middleware
17
19
* @api public
18
20
*/
19
21
module . exports = function ( options ) {
20
22
const defaults = {
21
23
allowMethods : 'GET,HEAD,PUT,POST,DELETE,PATCH' ,
24
+ secureContext : false ,
22
25
} ;
23
26
24
27
options = {
@@ -43,7 +46,7 @@ module.exports = function(options) {
43
46
}
44
47
45
48
options . keepHeadersOnError = options . keepHeadersOnError === undefined || ! ! options . keepHeadersOnError ;
46
-
49
+
47
50
return async function cors ( ctx , next ) {
48
51
// If the Origin header is not present terminate this set of steps.
49
52
// The request is outside the scope of this specification.
@@ -91,6 +94,11 @@ module.exports = function(options) {
91
94
set ( 'Access-Control-Expose-Headers' , options . exposeHeaders ) ;
92
95
}
93
96
97
+ if ( options . secureContext ) {
98
+ set ( 'Cross-Origin-Opener-Policy' , 'same-origin' ) ;
99
+ set ( 'Cross-Origin-Embedder-Policy' , 'require-corp' ) ;
100
+ }
101
+
94
102
if ( ! options . keepHeadersOnError ) {
95
103
return await next ( ) ;
96
104
}
@@ -133,6 +141,11 @@ module.exports = function(options) {
133
141
ctx . set ( 'Access-Control-Allow-Methods' , options . allowMethods ) ;
134
142
}
135
143
144
+ if ( options . secureContext ) {
145
+ set ( 'Cross-Origin-Opener-Policy' , 'same-origin' ) ;
146
+ set ( 'Cross-Origin-Embedder-Policy' , 'require-corp' ) ;
147
+ }
148
+
136
149
let allowHeaders = options . allowHeaders ;
137
150
if ( ! allowHeaders ) {
138
151
allowHeaders = ctx . get ( 'Access-Control-Request-Headers' ) ;
0 commit comments