Skip to content

Query parameters cause invalid matches #209

@fidian

Description

@fidian

I'm aware that the ordering of query parameters is outside the scope of this project, as it clearly states in the README. However, this isn't about ordering. If the route ends with a parameter and it is followed by a query string, the parameter gets the ?. Using the Node.js REPL, I can replicate this problem.

> p2r = require('path-to-regexp');
> re = p2r('/whatever/:moo?query=str')
/^\/whatever(?:\/([^\/]+?))?query\=str(?:\/)?$/i
> '/whatever/moo?query=str'.match(re)
[
  '/whatever/moo?query=str',
  'moo?',
  index: 0,
  input: '/whatever/moo?query=str',
  groups: undefined
]

The result of capturing moo? is broken. It appears that the regular expression constructed should look like the following.

/^\/whatever(?:\/([^\/?]+?))?\?query\=str(?:\/)?$/i

I would suggest updating the regular expression so the question mark can not be included in the path portion, which changes (?:\/([^\/]+?))? into (?:\/([^\/?]+?))?. This should be done for safety, since question marks are technically not part of the path as per the RFC, section 3.

Also, it would be nice to clearly update the documentation to indicate one of two positions:

  1. If the path to parse contains query parameters, one must escape the question mark, changing "/path?query=str" into "/path\\?query=str" (two backslashes = send a literal backslash). This isn't really explained in the optional section.

  2. Query strings are not supported at all and they must be removed before trying to parse the path. This appears to be the opinion stated in issues URL parameters behind a variable #47 and query parameters and RegExp's involving path parameter #91, but the documentation currently indicates that the library doesn't manage the ordering of query strings as opposed to query strings being forbidden.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions