Skip to content

fix(urlMatcherFactory): Decode slashes in string routes #2071

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,9 @@ function $UrlMatcherFactory() {
$$UMFP = this;

var isCaseInsensitive = false, isStrictMode = true, defaultSquashPolicy = false;
var slashReplacement = "%2F";

function valToString(val) { return val != null ? val.toString().replace(/\//g, "%2F") : val; }
function valToString(val) { return val != null ? val.toString().replace(/\//g, slashReplacement) : val; }
function valFromString(val) { return val != null ? val.toString().replace(/%2F/g, "/") : val; }

var $types = {}, enqueue = true, typeQueue = [], injector, defaultTypes = {
Expand All @@ -578,7 +579,7 @@ function $UrlMatcherFactory() {
decode: valFromString,
// TODO: in 1.0, make string .is() return false if value is undefined/null by default.
// In 0.2.x, string params are optional by default for backwards compat
is: function(val) { return val == null || !isDefined(val) || typeof val === "string"; },
is: function(val) { return (val == null || !isDefined(val) || typeof val === "string") && (val.toString().indexOf(slashReplacement) === -1); },
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This added condition fails when val is null. I tested it in my project and came out with the following improvement:

is: function(val) { return val == null || !isDefined(val) || (isString(val) && val.indexOf(slashReplacement) === -1); },

What do you think @chliebel?

pattern: /[^/]*/
},
int: {
Expand Down
1 change: 1 addition & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe("UrlMatcher", function () {
var matcher = new UrlMatcher('/:foo');
expect(matcher.format({ foo: "/" })).toBe('/%252F');
expect(matcher.format({ foo: "//" })).toBe('/%252F%252F');
expect(matcher.exec('/hello%2Fworld')).toEqual({ foo: 'hello/world' });
});

describe("snake-case parameters", function() {
Expand Down