diff --git a/lib/api/middlewares/error-middleware.js b/lib/api/middlewares/error-middleware.js index 313ebea8..ffc64b2b 100644 --- a/lib/api/middlewares/error-middleware.js +++ b/lib/api/middlewares/error-middleware.js @@ -38,7 +38,7 @@ module.exports = function errorMiddleware (/* options */) { res.json(errorResponseBody); } - next(); + return next(); }; }; diff --git a/lib/api/middlewares/pubsub-metrics.js b/lib/api/middlewares/pubsub-metrics.js index 1a672362..35707f4a 100644 --- a/lib/api/middlewares/pubsub-metrics.js +++ b/lib/api/middlewares/pubsub-metrics.js @@ -1,6 +1,7 @@ 'use strict'; const EVENT_VERSION = '1'; +const MAX_LENGTH = 100; function pubSubMetrics (pubSubMetricsBackend) { if (!pubSubMetricsBackend.isEnabled()) { @@ -19,9 +20,9 @@ function pubSubMetrics (pubSubMetricsBackend) { } function getEventData (req, res) { - const event = req.get('Carto-Event'); - const eventSource = req.get('Carto-Event-Source'); - const eventGroupId = req.get('Carto-Event-Group-Id'); + const event = normalizedField(req.get('Carto-Event')); + const eventSource = normalizedField(req.get('Carto-Event-Source')); + const eventGroupId = normalizedField(req.get('Carto-Event-Group-Id')); if (!event || !eventSource) { return [undefined, undefined]; @@ -43,4 +44,8 @@ function getEventData (req, res) { return { event, attributes }; } +function normalizedField (field) { + return field.toString().substr(0, MAX_LENGTH); +} + module.exports = pubSubMetrics; diff --git a/test/integration/pubsub-metrics-test.js b/test/integration/pubsub-metrics-test.js index 99dd58f7..fb0855ab 100644 --- a/test/integration/pubsub-metrics-test.js +++ b/test/integration/pubsub-metrics-test.js @@ -12,6 +12,15 @@ const metricsHeaders = { 'Carto-Event-Group-Id': '1' }; +const tooLongField = 'If you are sending a text this long in a header you kind of deserve the worst, honestly. I mean ' + + 'this is not a header, it is almost a novel, and you do not see any Novel cookie here, right?'; + +const badHeaders = { + 'Carto-Event': tooLongField, + 'Carto-Event-Source': 'test', + 'Carto-Event-Group-Id': 1 +}; + const mapConfig = { version: '1.7.0', layers: [ @@ -106,6 +115,25 @@ describe('pubsub metrics middleware', function () { }); }); + it('should normalized headers type and length', function (done) { + global.environment.pubSubMetrics.enabled = true; + const eventAttributes = buildEventAttributes(200); + const maxLength = 100; + const eventName = tooLongField.substr(0, maxLength); + + testClient = new TestClient(mapConfig, 1234, badHeaders); + + testClient.getLayergroup((err, body) => { + if (err) { + return done(err); + } + + assert.strictEqual(typeof body.metadata, 'object'); + assert(fakeTopic.publish.calledOnceWith(Buffer.from(eventName), eventAttributes)); + return done(); + }); + }); + it('should send event for map requests', function (done) { global.environment.pubSubMetrics.enabled = true; const eventAttributes = buildEventAttributes(200);