Skip to content

Commit

Permalink
fix: add header and cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed Aug 4, 2024
1 parent 87e319f commit 3cd4426
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -872,3 +872,4 @@ tach http://example.com
| `--trace` | `false` | Enable performance tracing ([details](#performance-traces)) |
| `--trace-log-dir` | `${cwd}/logs` | The directory to put tracing log files. Defaults to `${cwd}/logs`. |
| `--trace-cat` | [default categories](./src/defaults.ts) | The tracing categories to record. Should be a string of comma-separated category names |
| `--cross-origin-isolated` | `false` | Add HTTP headers to enable [cross-origin isolation](https://developer.mozilla.org/en-US/docs/Web/API/Window/crossOriginIsolated). |
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ $ tach http://example.com
mountPoints,
resolveBareModules: config.resolveBareModules,
cache: config.mode !== 'manual',
crossOriginIsolated: config.crossOriginIsolated,
});
for (const spec of specs) {
servers.set(spec, server);
Expand Down
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface Config {
npmrc?: string;
csvFileStats: string;
csvFileRaw: string;
crossOriginIsolated: boolean;
}

export async function makeConfig(opts: Opts): Promise<Config> {
Expand All @@ -55,6 +56,7 @@ export async function makeConfig(opts: Opts): Promise<Config> {
? parseGithubCheckFlag(opts['github-check'])
: undefined,
remoteAccessibleHost: opts['remote-accessible-host'],
crossOriginIsolated: opts['cross-origin-isolated'],
};

let config: Config;
Expand Down Expand Up @@ -175,6 +177,10 @@ export function applyDefaults(partial: Partial<Config>): Config {
: defaults.resolveBareModules,
root: partial.root !== undefined ? partial.root : defaults.root,
timeout: partial.timeout !== undefined ? partial.timeout : defaults.timeout,
crossOriginIsolated:
partial.crossOriginIsolated !== undefined
? partial.crossOriginIsolated
: defaults.crossOriginIsolated,
};
}

Expand Down
5 changes: 5 additions & 0 deletions src/cross-origin-isolation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {Middleware} from 'koa';

// Enable cross-origin isolation for more precise timers:
Expand Down
1 change: 1 addition & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const mode = 'automatic';
export const resolveBareModules = true;
export const forceCleanNpmInstall = false;
export const measurementExpression = 'window.tachometerResult';
export const crossOriginIsolated = false;
export const traceLogDir = path.join(process.cwd(), 'logs');
export const traceCategories = [
'blink',
Expand Down
1 change: 1 addition & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export interface Opts {
trace: boolean;
'trace-log-dir': string;
'trace-cat': string;
'cross-origin-isolated': boolean;

// Extra arguments not associated with a flag are put here. These are our
// benchmark names/URLs.
Expand Down
7 changes: 5 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {nodeResolve} from 'koa-node-resolve';

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

import * as url from 'url';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
Expand All @@ -32,6 +32,7 @@ export interface ServerOpts {
mountPoints: MountPoint[];
resolveBareModules: boolean;
cache: boolean;
crossOriginIsolated: boolean;
}

export interface MountPoint {
Expand Down Expand Up @@ -92,7 +93,9 @@ export class Server {
this.server = server;
const app = new Koa();

app.use(crossOriginIsolation());
if (opts.crossOriginIsolated) {
app.use(crossOriginIsolation());
}
app.use(bodyParser());
app.use(mount('/submitResults', this.submitResults.bind(this)));
app.use(this.instrumentRequests.bind(this));
Expand Down
5 changes: 5 additions & 0 deletions src/test/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ suite('makeConfig', function () {
csvFileStats: '',
csvFileRaw: '',
githubCheck: undefined,
crossOriginIsolated: false,
benchmarks: [
{
browser: {
Expand Down Expand Up @@ -92,6 +93,7 @@ suite('makeConfig', function () {
csvFileRaw: '',
// TODO(aomarks) Be consistent about undefined vs unset.
githubCheck: undefined,
crossOriginIsolated: false,
benchmarks: [
{
browser: {
Expand Down Expand Up @@ -137,6 +139,7 @@ suite('makeConfig', function () {
csvFileStats: '',
csvFileRaw: '',
githubCheck: undefined,
crossOriginIsolated: false,
benchmarks: [
{
browser: {
Expand Down Expand Up @@ -190,6 +193,7 @@ suite('makeConfig', function () {
remoteAccessibleHost: '',
// TODO(aomarks) Be consistent about undefined vs unset.
githubCheck: undefined,
crossOriginIsolated: false,
benchmarks: [
{
browser: {
Expand Down Expand Up @@ -237,6 +241,7 @@ suite('makeConfig', function () {
remoteAccessibleHost: '',
// TODO(aomarks) Be consistent about undefined vs unset.
githubCheck: undefined,
crossOriginIsolated: false,
benchmarks: [
{
browser: {
Expand Down
75 changes: 46 additions & 29 deletions src/test/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@ import {testData} from './test_helpers.js';
suite('server', () => {
let server: Server;

const defaultOptions = {
host: 'localhost',
ports: [0], // random
root: testData,
resolveBareModules: true,
npmInstalls: [],
mountPoints: [
{
diskPath: testData,
urlPath: '/',
},
],
cache: true,
crossOriginIsolated: false,
};

setup(async () => {
server = await Server.start({
host: 'localhost',
ports: [0], // random
root: testData,
resolveBareModules: true,
npmInstalls: [],
mountPoints: [
{
diskPath: testData,
urlPath: '/',
},
],
cache: true,
...defaultOptions,
});
});

Expand Down Expand Up @@ -88,18 +93,8 @@ suite('server', () => {
await server.close();

server = await Server.start({
host: 'localhost',
ports: [0], // random
root: testData,
resolveBareModules: true,
...defaultOptions,
npmInstalls: [{installDir, packageJson}],
mountPoints: [
{
diskPath: testData,
urlPath: '/',
},
],
cache: true,
});
});

Expand Down Expand Up @@ -138,13 +133,35 @@ suite('server', () => {
assert.equal(session.bytesSent, 0);
});

test('enables cross-origin isolation', async () => {
test('cross-origin isolation is disabled by default', 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'
);
assert.equal(res.headers.get('Cross-Origin-Opener-Policy'), null);
assert.equal(res.headers.get('Cross-Origin-Embedder-Policy'), null);
});

suite('cross origin isolation enabled', async () => {
setup(async () => {
// Close the base server and replace it with a custom server that is
// configured with crossOriginIsolated=true
await server.close();
server = await Server.start({
...defaultOptions,
crossOriginIsolated: true,
});
});

test('cross-origin isolation can be enabled', 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 3cd4426

Please sign in to comment.