Skip to content

Commit 5613b53

Browse files
committed
Allow connections from webpages via private network access CORS
This is required when using the UI in some very recent Chrome releases.
1 parent fd0c9a9 commit 5613b53

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/api-server.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ export class HttpToolkitServerApi extends events.EventEmitter {
288288
this.server = express();
289289
this.server.disable('x-powered-by');
290290

291+
// Allow web pages on non-local URLs (app.httptoolkit.tech, not localhost) to
292+
// send requests to this admin server too. Without this, those requests will
293+
// fail after rejected preflights in recent Chrome (from ~v102, ish? Unclear).
294+
this.server.use((req, res, next) => {
295+
if (req.headers["access-control-request-private-network"]) {
296+
res.setHeader("access-control-allow-private-network", "true");
297+
}
298+
next(null);
299+
});
300+
291301
this.server.use(cors({
292302
origin: ALLOWED_ORIGINS,
293303
maxAge: 86400 // Cache this result for as long as possible

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ export async function runHTK(options: {
121121
corsOptions: {
122122
strict: true, // For the standalone admin API, require valid CORS headers
123123
origin: ALLOWED_ORIGINS, // Only allow requests from our origins, to avoid XSRF
124-
maxAge: 86400 // Cache CORS responses for as long as possible
124+
maxAge: 86400, // Cache CORS responses for as long as possible
125+
allowPrivateNetworkAccess: true // Allow access from non-local domains in Chrome 102+
125126
},
126127
webSocketKeepAlive: 20000, // Send a keep-alive ping to Mockttp clients every minute
127128
ruleParameters // Rule parameter dictionary

0 commit comments

Comments
 (0)