Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix proxy bypass return false #1696

Merged
merged 8 commits into from
Mar 19, 2019
17 changes: 14 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,23 @@ class Server {
}

const bypass = typeof proxyConfig.bypass === 'function';
mistic marked this conversation as resolved.
Show resolved Hide resolved

const bypassRawValue = bypass
? proxyConfig.bypass(req, res, proxyConfig)
: null;
const bypassUrl =
(bypass && proxyConfig.bypass(req, res, proxyConfig)) || false;
typeof bypassRawValue === 'string'
? bypassRawValue
: typeof bypassRawValue === 'boolean' &&
bypassRawValue === false;
mistic marked this conversation as resolved.
Show resolved Hide resolved

if (bypassUrl) {
mistic marked this conversation as resolved.
Show resolved Hide resolved
req.url = bypassUrl;
if (typeof bypassUrl === 'boolean') {
req.url = null;
}

if (typeof bypassUrl === 'string') {
req.url = bypassUrl;
}

next();
} else if (proxyMiddleware) {
Expand Down
13 changes: 13 additions & 0 deletions test/Proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const proxyOptionPathsAsProperties = {
if (/\.html$/.test(req.path)) {
return '/index.html';
}

return null;
},
},
'/proxyfalse': {
bypass(req) {
if (/\/proxyfalse$/.test(req.path)) {
return false;
}
},
},
};
Expand Down Expand Up @@ -116,6 +125,10 @@ describe('Proxy', () => {
it('should pass through a proxy when a bypass function returns null', (done) => {
req.get('/foo.js').expect(200, /Hey/, done);
});

it('should not pass through a proxy when a bypass function returns false', (done) => {
req.get('/proxyfalse').expect(404, done);
});
});
});

Expand Down