Skip to content

Commit

Permalink
Proxy protocol v1 support: test proxy protocol header too large
Browse files Browse the repository at this point in the history
  • Loading branch information
rmruano committed Sep 27, 2021
1 parent d45a95c commit d613937
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ var smpp = require('smpp');
var server = smpp.createServer({
debug: true
}, function(session) {
session.on('error', function (err) {
// Something ocurred, not listening for this event will terminate the program
});
session.on('bind_transceiver', function(pdu) {
// we pause the session to prevent further incoming pdu events,
// untill we authorize the session with some async operation.
Expand All @@ -81,9 +84,12 @@ var server = smpp.createServer({
});
});
});

server.listen(2775);
```

It's very important to listen for session errors, not listening for error events will terminate the program.

### Debug
To enable a simple debug of ingoing/outgoing messages pass `debug: true` as server/client option. Debug is disabled by default.

Expand Down
11 changes: 7 additions & 4 deletions lib/smpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function Session(options) {
clearTimeout(connectTimeout);
self.debug("socket.error", e.message, e);
if (self.socket) self.socket.destroy();
if(self._interval) {
if (self._interval) {
clearInterval(self._interval);
self._interval = 0;
}
Expand Down Expand Up @@ -157,6 +157,7 @@ Session.prototype._extractPDUs = function() {
}
this.debug("pdu.command.in", pdu.command, pdu);
} catch (e) {
this.debug("pdu.command.error", e.message, e);
this.emit('error', e);
return;
}
Expand Down Expand Up @@ -188,9 +189,11 @@ Session.prototype._handleProxyProtocolV1 = function() {

if (char === "\n" && prevChar === "\r") break;

if (proxyProtocol.length > 102) {
this.debug("proxy_protocol.error", "Proxy protocol header cannot exceed 102 bytes", proxyProtocol);
break;
if (proxyProtocol.length > 108) {
this.debug("proxy_protocol.error", "Proxy protocol header cannot exceed 108 bytes", proxyProtocol);
var e = new Error('Proxy protocol header cannot exceed 108 bytes');
e.code = "PROXY_PROTOCOL_HEADER_TOO_LARGE";
throw e;
}

prevChar = char;
Expand Down
21 changes: 19 additions & 2 deletions test/proxy_protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var assert = require('assert'),
Buffer = require("safer-buffer").Buffer;

describe('ProxyProtocol', function() {
var server, port, debugBuffer = [];
var server, port, debugBuffer = [], lastServerError;

// Write fake proxy protocol headers into the socket to test proxy protocol support
var proxyProtocol = {
Expand Down Expand Up @@ -31,21 +31,27 @@ describe('ProxyProtocol', function() {
enable_proxy_protocol_detection: true,
}, function (session) {
debugBuffer = [];
lastServerError = null;
session.on('pdu', function(pdu) {
session.send(pdu.response()); // Always reply
});
// We'll use the debug event to track what happened inside the server
session.on('debug', function(type, msg, payload) {
debugBuffer.push({type: type, msg: msg, payload: payload});
});
// Errors
session.on('error', function (err) {
lastServerError = err;
session.close();
});
});
server.listen(0, done);
port = server.address().port;
});

after(function (done) {
server.sessions.forEach(function (session) {
session.close();
session.destroy();
});
server.close(done);
});
Expand Down Expand Up @@ -115,4 +121,15 @@ describe('ProxyProtocol', function() {
});
});

it('should fail with a header larger than 108 bytes', function (done) {
var session = smpp.connect({port: port}, function () {
proxyProtocol.sendFakeHeader(session.socket, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer a neque at ex gravida feugiat et a erat. Donec mattis vulputate metus, bibendum dapibus felis egestas vitae. Cras suscipit sodales felis sed tincidunt. Fusce mattis rutrum purus pulvinar porta. Ut feugiat sed turpis nec consectetur. Etiam imperdiet a libero in vulputate. Mauris feugiat arcu ex, et sollicitudin orci laoreet non. Phasellus consequat erat sit amet felis ullamcorper consequat.");
session.enquire_link(function (pdu) {});
session.on("close", function() {
assert.equal(lastServerError.code, "PROXY_PROTOCOL_HEADER_TOO_LARGE");
done();
});
});
});

});

0 comments on commit d613937

Please sign in to comment.