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 URL parsing w/ trailing slash & querystring #1539

Merged
merged 3 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions lib/core/httpRouter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const
RoutePart = require('./routePart'),
{ Request, errors: { KuzzleError } } = require('kuzzle-common-objects');

const
LeadingSlashRegex = /\/+$/,
CharsetRegex = /charset=([\w-]+)/i;
const CharsetRegex = /charset=([\w-]+)/i;

/**
* Attach handler to routes and dispatch a HTTP
Expand Down Expand Up @@ -148,8 +146,6 @@ class Router {
return this.routeUnhandledHttpMethod(message, cb);
}

message.url = message.url.replace(LeadingSlashRegex, '');

let routeHandler;

try {
Expand Down Expand Up @@ -245,10 +241,10 @@ class Router {
* @param {RoutePart} target
*/
function attach(url, handler, target) {
const cleanedUrl = url.replace(LeadingSlashRegex, '');
const sanitized = url[url.length - 1] === '/' ? url.slice(0, -1) : url;

if (!attachParts(cleanedUrl.split('/'), handler, target)) {
errorsManager.throw('duplicate_url', cleanedUrl);
if (!attachParts(sanitized.split('/'), handler, target)) {
errorsManager.throw('duplicate_url', sanitized);
}
}

Expand Down
18 changes: 13 additions & 5 deletions lib/core/httpRouter/routePart.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
'use strict';

const
URL = require('url'),
querystring = require('querystring'),
RouteHandler = require('./routeHandler'),
URL = require('url'),
{ has } = require('../../util/safeObject');

/**
Expand Down Expand Up @@ -73,10 +73,18 @@ class RoutePart {
* @return {RouteHandler} registered function handler
*/
getHandler(message) {
const
parsed = URL.parse(message.url, true),
pathname = parsed.pathname || '', // pathname is set to null if empty
routeHandler = new RouteHandler(pathname, parsed.query, message);
// Do not use WHATWG API yet, stick with the legacy (and deprecated) URL
// There are two issues:
// - Heavy performance impact: https://github.com/nodejs/node/issues/30334
// - Double slash bug: https://github.com/nodejs/node/issues/30776
const parsed = URL.parse(message.url, true);
let pathname = parsed.pathname || ''; // pathname is set to null if empty

if (pathname[pathname.length - 1] === '/') {
pathname = pathname.slice(0, -1);
}

const routeHandler = new RouteHandler(pathname, parsed.query, message);

return getHandlerPart(this, pathname.split('/'), routeHandler);
}
Expand Down
47 changes: 47 additions & 0 deletions test/core/httpRouter/httpRouter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ describe('core/httpRouter', () => {

it('should raise an internal error when trying to add a duplicate', () => {
router.post('/foo/bar', handler);

should(function () { router.post('/foo/bar', handler); })
.throw(InternalError, { id: 'network.http.duplicate_url' });
should(function () { router.post('/foo/bar/', handler); })
.throw(InternalError, { id: 'network.http.duplicate_url' });
});
});

Expand Down Expand Up @@ -166,6 +169,50 @@ describe('core/httpRouter', () => {
});
});

it('should properly handle querystrings (w/o url trailing slash)', done => {
router.post('/foo/bar', handler);

rq.url = '/foo/bar?foo=bar';
rq.method = 'POST';

router.route(rq, () => {
try {
should(handler).be.calledOnce();

const payload = handler.firstCall.args[0];
should(payload).be.instanceOf(Request);
should(payload.input.args.foo).eql('bar');

done();
}
catch (e) {
done(e);
}
});
});

it('should properly handle querystrings (w/ url trailing slash)', done => {
router.post('/foo/bar', handler);

rq.url = '/foo/bar/?foo=bar';
rq.method = 'POST';

router.route(rq, () => {
try {
should(handler).be.calledOnce();

const payload = handler.firstCall.args[0];
should(payload).be.instanceOf(Request);
should(payload.input.args.foo).eql('bar');

done();
}
catch (e) {
done(e);
}
});
});

it('should amend the request object if a body is found in the content', done => {
router.post('/foo/bar', handler);

Expand Down