Skip to content

Commit

Permalink
fix: enable cross-origin isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed Apr 25, 2022
1 parent 7bf3c57 commit e01f5d9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cross-origin-isolation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Middleware} from 'koa';

// Enable cross-origin isolation for more precise timers:
// https://developer.chrome.com/blog/cross-origin-isolated-hr-timers/
export function crossOriginIsolation(): Middleware {
// Based on https://github.com/fishel-feng/koa-isolated
return async function isolated(ctx, next) {
ctx.set('Cross-Origin-Opener-Policy', 'same-origin');
ctx.set('Cross-Origin-Embedder-Policy', 'require-corp');
await next();
};
}
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {nodeResolve} from 'koa-node-resolve';

import {BenchmarkResponse, Deferred} from './types';
import {NpmInstall} from './versions';
import {crossOriginIsolation} from './cross-origin-isolation';

export interface ServerOpts {
host: string;
Expand Down Expand Up @@ -88,6 +89,7 @@ export class Server {
this.server = server;
const app = new Koa();

app.use(crossOriginIsolation());
app.use(bodyParser());
app.use(mount('/submitResults', this.submitResults.bind(this)));
app.use(this.instrumentRequests.bind(this));
Expand Down
10 changes: 10 additions & 0 deletions src/test/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,14 @@ suite('server', () => {
session = server.endSession();
assert.equal(session.bytesSent, 0);
});

test('enables cross-origin isolation', async () => {
const res = await fetch(`${server.url}/import-bare-module.html`);

assert.equal(res.headers.get('Cross-Origin-Opener-Policy'), 'same-origin');
assert.equal(
res.headers.get('Cross-Origin-Embedder-Policy'),
'require-corp'
);
});
});

0 comments on commit e01f5d9

Please sign in to comment.