Skip to content

Commit 335aeeb

Browse files
jsmylnyckySmylnycky, Jason M
and
Smylnycky, Jason M
authored
Skip sending the proxyReq event when the expect header is present (#1447)
* Skip sending the proxyReq event when the expect header is present * Adjust padding to match advisory Co-authored-by: Smylnycky, Jason M <jason.smylnycky@cengage.com>
1 parent dba3966 commit 335aeeb

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/http-proxy/passes/web-incoming.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ module.exports = {
129129

130130
// Enable developers to modify the proxyReq before headers are sent
131131
proxyReq.on('socket', function(socket) {
132-
if(server) { server.emit('proxyReq', proxyReq, req, res, options); }
132+
if(server && !proxyReq.getHeader('expect')) {
133+
server.emit('proxyReq', proxyReq, req, res, options);
134+
}
133135
});
134136

135137
// allow outgoing socket to timeout so that we could

test/lib-http-proxy-passes-web-incoming-test.js

+44
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,50 @@ describe('#createProxyServer.web() using own http server', function () {
126126
http.request('http://127.0.0.1:8081', function() {}).end();
127127
});
128128

129+
it('should skip proxyReq event when handling a request with header "expect: 100-continue" [https://www.npmjs.com/advisories/1486]', function (done) {
130+
var proxy = httpProxy.createProxyServer({
131+
target: 'http://127.0.0.1:8080',
132+
});
133+
134+
proxy.on('proxyReq', function(proxyReq, req, res, options) {
135+
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
136+
});
137+
138+
function requestHandler(req, res) {
139+
proxy.web(req, res);
140+
}
141+
142+
var proxyServer = http.createServer(requestHandler);
143+
144+
var source = http.createServer(function(req, res) {
145+
source.close();
146+
proxyServer.close();
147+
expect(req.headers['x-special-proxy-header']).to.not.eql('foobar');
148+
done();
149+
});
150+
151+
proxyServer.listen('8081');
152+
source.listen('8080');
153+
154+
const postData = ''.padStart(1025, 'x');
155+
156+
const postOptions = {
157+
hostname: '127.0.0.1',
158+
port: 8081,
159+
path: '/',
160+
method: 'POST',
161+
headers: {
162+
'Content-Type': 'application/x-www-form-urlencoded',
163+
'Content-Length': Buffer.byteLength(postData),
164+
'expect': '100-continue'
165+
}
166+
};
167+
168+
const req = http.request(postOptions, function() {});
169+
req.write(postData);
170+
req.end();
171+
});
172+
129173
it('should proxy the request and handle error via callback', function(done) {
130174
var proxy = httpProxy.createProxyServer({
131175
target: 'http://127.0.0.1:8080'

0 commit comments

Comments
 (0)