Skip to content

Commit fc0b221

Browse files
committed
Limit Mockttp standalone server access to the same origins as the HTK server
1 parent d885863 commit fc0b221

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

src/httptoolkit-server.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { GraphQLScalarType } from 'graphql';
66
import { HtkConfig } from './config';
77
import { reportError } from './error-tracking';
88
import { buildInterceptors, Interceptor } from './interceptors';
9-
import { IS_PROD_BUILD } from './util';
9+
import { ALLOWED_ORIGINS } from './util';
1010

1111
const packageJson = require('../package.json');
1212

@@ -160,26 +160,7 @@ export class HttpToolkitServer extends events.EventEmitter {
160160
// and override the port from 4000 to something less likely to conflict.
161161
port: { port: 45457, host: 'localhost' },
162162
playground: false,
163-
cors: {
164-
origin: IS_PROD_BUILD
165-
? [
166-
// Prod builds only allow HTTPS app.httptoolkit.tech usage. This
167-
// ensures that no other sites/apps can communicate with your server
168-
// whilst you have the app open. If they could (requires an HTTP mitm),
169-
// they would be able to start proxies & interceptors. It's not remote
170-
// execution, but it's definitely not desirable.
171-
/^https:\/\/app\.httptoolkit\.tech$/
172-
]
173-
: [
174-
// Dev builds can use the main site, or local sites, even if those
175-
// use HTTP. Note that HTTP here could technically open you to the risk
176-
// above, but it'd require a DNS MitM too (to stop local.httptoolkit.tech
177-
// resolving to localhost and never hitting the network).
178-
/^https?:\/\/localhost(:\d+)?$/,
179-
/^http:\/\/local\.httptoolkit\.tech(:\d+)?$/,
180-
/^https:\/\/app\.httptoolkit\.tech$/
181-
]
182-
}
163+
cors: { origin: ALLOWED_ORIGINS }
183164
});
184165
}
185166
};

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import updateCommand from '@oclif/plugin-update/lib/commands/update';
1010
import { HttpToolkitServer } from './httptoolkit-server';
1111
import { checkBrowserConfig } from './browsers';
1212
import { reportError } from './error-tracking';
13-
import { delay } from './util';
13+
import { delay, ALLOWED_ORIGINS } from './util';
1414

1515
const canAccess = util.promisify(fs.access);
1616
const mkDir = util.promisify(fs.mkdir);
@@ -67,7 +67,8 @@ export async function runHTK(options: {
6767
serverDefaults: {
6868
cors: false,
6969
https: httpsConfig
70-
}
70+
},
71+
corsOptions: { origin: ALLOWED_ORIGINS }
7172
});
7273
standalone.start({
7374
port: 45456,

src/util.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,23 @@ export function delay(durationMs: number): Promise<void> {
22
return new Promise((resolve) => setTimeout(resolve, durationMs));
33
}
44

5-
export const IS_PROD_BUILD = !!process.env.HTTPTOOLKIT_SERVER_BINPATH;
5+
export const IS_PROD_BUILD = !!process.env.HTTPTOOLKIT_SERVER_BINPATH;
6+
7+
export const ALLOWED_ORIGINS = IS_PROD_BUILD
8+
? [
9+
// Prod builds only allow HTTPS app.httptoolkit.tech usage. This
10+
// ensures that no other sites/apps can communicate with your server
11+
// whilst you have the app open. If they could (requires an HTTP mitm),
12+
// they would be able to start proxies & interceptors. It's not remote
13+
// execution, but it's definitely not desirable.
14+
/^https:\/\/app\.httptoolkit\.tech$/
15+
]
16+
: [
17+
// Dev builds can use the main site, or local sites, even if those
18+
// use HTTP. Note that HTTP here could technically open you to the risk
19+
// above, but it'd require a DNS MitM too (to stop local.httptoolkit.tech
20+
// resolving to localhost and never hitting the network).
21+
/^https?:\/\/localhost(:\d+)?$/,
22+
/^http:\/\/local\.httptoolkit\.tech(:\d+)?$/,
23+
/^https:\/\/app\.httptoolkit\.tech$/
24+
]

0 commit comments

Comments
 (0)