throw 400 in case of malformed paths
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alex Kocharin · committed by Jonathan Ong · parent 34c83d7 · Browse files · 3 files·+52 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+52 added−39 removed
- tests:+43 added−17 removed
- other:+9 added−22 removed
Provenance
- Commit
7b0dca0- Tree
9fd7bc4- Author
Alex Kocharin <alex@kocharin.ru>- Committer
Jonathan Ong <jonathanrichardong@gmail.com>- Parent
34c83d7
3 files changed+52 added−39 removed
modified:lib/router/route.js+9 added-3 removed
55 unmodified lines56575859606061626162636465666768697071726 unmodified lines55 unmodified lines
for (var i = 1, len = m.length; i < len; ++i) { var key = keys[i - 1];
try { var val = 'string' == typeof m[i] ? utils.decode(m[i]) : m[i]; var val = 'string' == typeof m[i] ? decodeURIComponent(m[i]) : m[i]; } catch(e) { var err = new Error("Failed to decode param '" + m[i] + "'"); err.status = 400; throw err; }
if (key) { params[key.name] = val; } else {6 unmodified linesmodified:lib/utils.js-19 removed
310 unmodified lines311 .replace(/([\/.])/g, '\\$1')312 .replace(/\*/g, '(.*)');313 return new RegExp('^' + path + '$', sensitive ? '' : 'i');314 }315-316-317-/**318- * Decodes a URI component. Returns319- * the original string if the component320- * is malformed.321- *322- * @param {String} str323- * @return {String}324- * @api private325- */326-327-exports.decode = function(str) {328- try {329- return decodeURIComponent(str);330- } catch (e) {331- return str;332- }333-}modified:test/app.router.js+43 added-17 removed
25 unmodified lines26 })27 });28 })29 30+ describe('decode querystring', function(){30- it('should decode params', function(done){31- var app = express();31+ it('should decode correct params', function(done){32+ var app = express();33 33- app.get('/:name', function(req, res, next){34- res.send(req.params.name);35- });34+ app.get('/:name', function(req, res, next){35+ res.send(req.params.name);36+ });37 37- request(app)38- .get('/foo%2Fbar')39- .expect('foo/bar', done);40- })38+ request(app)39+ .get('/foo%2Fbar')40+ .expect('foo/bar', done);41+ })42 42- it('should accept params in malformed paths', function(done) {43- var app = express();43+ it('should not accept params in malformed paths', function(done) {44+ var app = express();45 45- app.get('/:name', function(req, res, next){46- res.send(req.params.name);47- });46+ app.get('/:name', function(req, res, next){47+ res.send(req.params.name);48+ });49 49- request(app)50- .get('/%foobar')51- .expect('%foobar', done);50+ request(app)51+ .get('/%foobar')52+ .expect(400, done);53+ })54+55+ it('should not decode spaces', function(done) {56+ var app = express();57+58+ app.get('/:name', function(req, res, next){59+ res.send(req.params.name);60+ });61+62+ request(app)63+ .get('/foo+bar')64+ .expect('foo+bar', done);65+ })66+67+ it('should work with unicode', function(done) {68+ var app = express();69+70+ app.get('/:name', function(req, res, next){71+ res.send(req.params.name);72+ });73+74+ request(app)75+ .get('/%ce%b1')76+ .expect('\u03b1', done);77+ })78 })79 80 it('should be .use()able', function(done){81 var app = express();543 unmodified lines