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(router): handle non-trailing slashes correctly #1478

Closed

Conversation

hekike
Copy link
Member

@hekike hekike commented Sep 9, 2017

Issues

Closes:

#1464

Non-trailing slash leads to incorrect routing match (and parameter parsing).

Changes

Fix router regexp matching with non-trailing slash(es)

@hekike hekike force-pushed the fix/router-non-trailing-double-slash branch 2 times, most recently from 5c517b2 to 54f8430 Compare September 9, 2017 13:05
@hekike hekike force-pushed the fix/router-non-trailing-double-slash branch 2 times, most recently from 765769e to 0c3862d Compare September 10, 2017 06:40
t.ok(trailing.path.test('/trailing'));

var noTrailing = server.router.routes.GET[1];
t.ok(noTrailing.path.test('/no-trailing'));
t.ok(noTrailing.path.test('//no-trailing//'));
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I have enough context to understand why this change is necessary. Why did we change an existing test as opposed to adding a separate test for a single leading /?

Copy link
Member Author

Choose a reason for hiding this comment

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

The original test case is not related to the code what it tests. This test should focus to double no-trailing slashes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sry, I mean it should focus to slashes at the end of the path :P

@DonutEspresso
Copy link
Member

@hekike thanks for taking this on! I think you're right, the behavior should be to not remove double slashes by default - that should be taken care of by the dedupeSlashes plugin. This is going to be a breaking change, perhaps we can slide this in before we cut 6.x @retrohacker?

@hekike
Copy link
Member Author

hekike commented Sep 13, 2017

Apologize I didn't explain very well the context of this pr.
To be on the same page, the current PR:

  • Allows only one slash at the middle to prevent route pram parsing issues
  • Allows zero or more slashes at the end

I believe what @DonutEspresso proposes is a one step further and wouldn't allow, more than one slash at the end neither.

The new behavior after this proposal would be:

  • Allows only one slash at the middle to prevent route pram parsing issues
  • Allows zero or one slash at the end

@retrohacker
Copy link
Member

Thanks for tackling this @hekike!

I'm 👍 on leaving the path exactly as is, and allowing the user to specify the dedupe behavior through a plugin. My main motivation is that we should do as little "magic" as possible, and changing paths before they get to the user seems like magic. I think our plugin logic should behave like merge_slashes in nginx. If users need a different behavior, they can specify that as their own plugin.

@retrohacker
Copy link
Member

So, tl;dr, I propose:

  1. We remove all url scrubbing logic from the router
  2. We update our dedupe plugin to match NGinx's behavior
  3. We document how you can specify your own url transformer through server.pre.

@retrohacker
Copy link
Member

As for holding v6, I'd suggest we go ahead and ship. We've been in a state of perpetually staged breaking changes before (as @yunong pointed out yesterday) and it wasn't a great place to be.

Instead, I'd suggest we create a new branch called next that we merge BREAKING CHANGEs into until we think we have enough for a new major release. Then we can cut from there. This gives us two advantages:

  1. We don't have to block bug/feature fixes because BREAKING is on the current master (like we are doing now)
  2. Users who need the new BREAKING changes can opt into tracking the next branch by using restify/node-restify#next in their package.json.

Peter Marton added 2 commits September 28, 2017 08:58
BREAKING CHANGE: use server.pre(restify.plugins.pre.dedupeSlashes()) to deal with double slashes
@hekike hekike force-pushed the fix/router-non-trailing-double-slash branch from 0c3862d to d83ef0f Compare September 28, 2017 07:31
@hekike
Copy link
Member Author

hekike commented Sep 28, 2017

@retrohacker I removed the double slash merging magic from the router, see my latest commit.
The restify.plugins.pre.dedupeSlashes() plugin is already documented.

I also refactored the dedupeSlashes test to cover cases with more than two slashes.
If I understand correctly restify's dedupleSlashes plugin already worked like nginx's merge_slashes option, just the test didn't point it out earlier:

Enables or disables compression of two or more adjacent slashes in a URI into a single slash. - merge_slashes

Copy link
Member

@retrohacker retrohacker left a comment

Choose a reason for hiding this comment

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

This LGTM 👍

lib/router.js Outdated
@@ -103,7 +103,7 @@ function compileURL(options) {
return (false);
}

pattern += '\\/+';
pattern += '\\/{1}';
Copy link
Member

Choose a reason for hiding this comment

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

I think this quantifier is unnecessary

@@ -134,7 +134,7 @@ function compileURL(options) {
}

if (!options.strict) {
pattern += '[\\/]*';
pattern += '[\\/]?';
Copy link
Member

Choose a reason for hiding this comment

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

I think this list is unnecessary

@retrohacker
Copy link
Member

This shouldn't merge into master unless we want to cut a new release since it is going to be a breaking change. Can we open this PR against a new branch next?

@hekike
Copy link
Member Author

hekike commented Oct 9, 2017

@retrohacker I've no permission to create a next branch in this repo, could you create one? I cannot open a PR to a non-existent branch.

@hekike
Copy link
Member Author

hekike commented Oct 9, 2017

Closed by the favor of #1512
cc @retrohacker

@hekike hekike closed this Oct 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants