Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/allow cross origin isolated #685

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ Using `npx` you can run the script without installing it first:

`-s` or `--silent` Suppress log messages from output

`--cors` Enable CORS via the `Access-Control-Allow-Origin` header
`--cors` Enable CORS via the `Access-Control-Allow-Origin` header. Cannot be specified alongside `--coi`.

`--coi` Enable cross origin isolation via the `Cross-Origin-Embedder-Policy:required-corp` and `Cross-Origin-Opener-Policy:same-origin` headers. Cannot be specified alongside `--cors`.

`-o [path]` Open browser window after starting the server. Optionally provide a URL path to open. e.g.: -o /other/dir/

Expand Down
10 changes: 10 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ if (argv.h || argv.help) {
' -s --silent Suppress log messages from output',
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
' Optionally provide CORS headers list separated by commas',
' --coi Enable cross origin isolation via the "Cross-Origin-Embedder-Policy:required-corp"',
' and "Cross-Origin-Opener-Policy:same-origin" headers.',
' -o [path] Open browser window after starting the server.',
' Optionally provide a URL path to open the browser window to.',
' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',
Expand Down Expand Up @@ -131,12 +133,20 @@ function listen(port) {
password: argv.password || process.env.NODE_HTTP_SERVER_PASSWORD
};

if (argv.cors && argv.coi) {
logger.info(colors.red('Error: conflicting arguments: --cors and --coi'));
process.exit(1);
}

if (argv.cors) {
options.cors = true;
if (typeof argv.cors === 'string') {
options.corsHeaders = argv.cors;
}
}
else if (argv.coi) {
options.coi = true;
}

if (ssl) {
options.https = {
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