Skip to content

Commit aa7de77

Browse files
misticevilebottnawi
authored andcommitted
fix: ignore proxy when bypass return false (#1696)
1 parent 34a4a26 commit aa7de77

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/Server.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,22 @@ class Server {
381381
}
382382
}
383383

384-
const bypass = typeof proxyConfig.bypass === 'function';
385-
386-
const bypassUrl =
387-
(bypass && proxyConfig.bypass(req, res, proxyConfig)) || false;
388-
389-
if (bypassUrl) {
384+
// - Check if we have a bypass function defined
385+
// - In case the bypass function is defined we'll retrieve the
386+
// bypassUrl from it otherwise byPassUrl would be null
387+
const isByPassFuncDefined =
388+
typeof proxyConfig.bypass === 'function';
389+
const bypassUrl = isByPassFuncDefined
390+
? proxyConfig.bypass(req, res, proxyConfig)
391+
: null;
392+
393+
if (typeof bypassUrl === 'boolean') {
394+
// skip the proxy
395+
req.url = null;
396+
next();
397+
} else if (typeof bypassUrl === 'string') {
398+
// byPass to that url
390399
req.url = bypassUrl;
391-
392400
next();
393401
} else if (proxyMiddleware) {
394402
return proxyMiddleware(req, res, next);

test/Proxy.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ const proxyOptionPathsAsProperties = {
2323
if (/\.html$/.test(req.path)) {
2424
return '/index.html';
2525
}
26+
27+
return null;
28+
},
29+
},
30+
'/proxyfalse': {
31+
bypass(req) {
32+
if (/\/proxyfalse$/.test(req.path)) {
33+
return false;
34+
}
2635
},
2736
},
2837
};
@@ -116,6 +125,10 @@ describe('Proxy', () => {
116125
it('should pass through a proxy when a bypass function returns null', (done) => {
117126
req.get('/foo.js').expect(200, /Hey/, done);
118127
});
128+
129+
it('should not pass through a proxy when a bypass function returns false', (done) => {
130+
req.get('/proxyfalse').expect(404, done);
131+
});
119132
});
120133
});
121134

0 commit comments

Comments
 (0)