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
22 changes: 15 additions & 7 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,22 @@ class Server {
}
}

const bypass = typeof proxyConfig.bypass === 'function';

const bypassUrl =
(bypass && proxyConfig.bypass(req, res, proxyConfig)) || false;

if (bypassUrl) {
// - Check if we have a bypass function defined
// - In case the bypass function is defined we'll retrieve the
// bypassUrl from it otherwise byPassUrl would be null
const isByPassFuncDefined =
typeof proxyConfig.bypass === 'function';
const bypassUrl = isByPassFuncDefined
mistic marked this conversation as resolved.
Show resolved Hide resolved
? proxyConfig.bypass(req, res, proxyConfig)
: null;

if (typeof bypassUrl === 'boolean') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not aligning to the documentation

In the function you get access to the request, response and proxy options. It must return either false or a path that will be served instead of continuing to proxy the request.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR welcome to webpack docs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// skip the proxy
req.url = null;
next();
} else if (typeof bypassUrl === 'string') {
// byPass to that url
req.url = bypassUrl;

next();
} else if (proxyMiddleware) {
return proxyMiddleware(req, res, next);
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