Skip to content

Commit

Permalink
Replace whitelist with decodeURI
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Nov 12, 2019
1 parent f9bf1e0 commit 611513c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
17 changes: 7 additions & 10 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@ npm install path-to-regexp --save
## Usage

```javascript
const {
pathToRegexp,
match,
parse,
compile,
normalizePathname
} = require("path-to-regexp");
const { pathToRegexp, match, parse, compile } = require("path-to-regexp");

// pathToRegexp(path, keys?, options?)
// match(path)
// parse(path)
// compile(path)
// normalizePathname(path)
```

- **path** A string, array of strings, or a regular expression.
Expand Down Expand Up @@ -171,16 +164,20 @@ match("/invalid"); //=> false

### Normalize Pathname

The `normalizePathname` function will return a normalized string for matching with `pathToRegexp`.
The `normalizePathname` function will return a normalized string for matching with `pathToRegexp`:

```js
const re = pathToRegexp("/caf\u00E9");
const input = encodeURI("/cafe\u0301");
const input = encodeURI("/caf\u00E9");

re.test(input); //=> false
re.test(normalizePathname(input)); //=> true
```

**Note:** It may be preferable to implement something in your own library that normalizes the pathname for matching. E.g. [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically URI encodes paths for you, which would result in a consistent match.

**Tip:** Consider using [`String.prototype.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) to resolve unicode variants of the same string.

### Parse

The `parse` function will return a list of strings and keys from a path string:
Expand Down
8 changes: 3 additions & 5 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2832,12 +2832,10 @@ describe("path-to-regexp", function() {
).toEqual(["/caf\u00E9"]);
});

it("should not normalize whitelisted characters", function() {
const input = "/test/route%2F%25";
it("should not normalize encoded slash", function() {
const input = "/test/route%2F";

expect(pathToRegexp.normalizePathname(input)).toEqual(
"/test/route%2F%25"
);
expect(pathToRegexp.normalizePathname(input)).toEqual("/test/route%2F");
});

it("should fix repeated slashes", function() {
Expand Down
16 changes: 2 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,8 @@ export interface ParseOptions {
* slash and normalizes unicode characters to "NFC". When using this method,
* `decode` should be an identity function so you don't decode strings twice.
*/
export function normalizePathname(
pathname: string,
whitelist: string | string[] = "%/-."
) {
return pathname
.replace(/\/+/g, "/")
.replace(
/(?:%[ef][0-9a-f](?:%[0-9a-f]{2}){2}|%[cd][0-9a-f]%[0-9a-f]{2}|%[0-9a-f]{2})/gi,
m => {
const char = decodeURIComponent(m);
if (whitelist.indexOf(char) > -1) return m;
return char;
}
);
export function normalizePathname(pathname: string) {
return decodeURI(pathname).replace(/\/+/g, "/");
}

/**
Expand Down

0 comments on commit 611513c

Please sign in to comment.