Skip to content

Commit

Permalink
Set coi headers directly in lib/http-server.js; Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlowGrowth committed May 19, 2021
1 parent 402a808 commit ef7b79b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 1 addition & 4 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ function listen(port) {
password: argv.password || process.env.NODE_HTTP_SERVER_PASSWORD
};

options.headers = {};

if (argv.cors && argv.coi) {
logger.info(colors.red('Error: conflicting arguments: --cors and --coi'));
process.exit(1);
Expand All @@ -147,8 +145,7 @@ function listen(port) {
}
}
else if (argv.coi) {
options.headers['Cross-Origin-Embedder-Policy'] = 'require-corp';
options.headers['Cross-Origin-Opener-Policy'] = 'same-origin';
options.coi = true;
}

if (ssl) {
Expand Down
5 changes: 5 additions & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ function HttpServer(options) {
} : null));
}

if (options.coi) {
this.headers['Cross-Origin-Embedder-Policy'] = 'require-corp';
this.headers['Cross-Origin-Opener-Policy'] = 'same-origin';
}

if (options.robots) {
before.push(function (req, res) {
if (req.url === '/robots.txt') {
Expand Down
20 changes: 20 additions & 0 deletions test/http-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ vows.describe('http-server').addBatch({
server.close();
}
},
'When cross origin isolation is enabled,\n': {
topic: function () {
var server = httpServer.createServer({
root: root,
coi: true
});

server.listen(8080);
this.callback(null, server);
},
'and a page is requested': {
topic: function () {
request('http://127.0.0.1:8080/', this.callback);
},
'response should have cross origin isolation headers set': function (err, res) {
assert.equal(res.headers['Cross-Origin-Embedder-Policy'], 'require-corp');
assert.equal(res.headers['Cross-Origin-Opener-Policy'], 'same-origin');
}
}
},
'When gzip and brotli compression is enabled and a compressed file is available': {
topic: function () {
var server = httpServer.createServer({
Expand Down

0 comments on commit ef7b79b

Please sign in to comment.