Skip to content

Commit 08a1a3a

Browse files
committed
Add normalize whitelist of characters
1 parent a1ae346 commit 08a1a3a

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"size-limit": [
3535
{
3636
"path": "dist/index.js",
37-
"limit": "1.7 kB"
37+
"limit": "1.75 kB"
3838
}
3939
],
4040
"jest": {

src/index.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,6 +2832,14 @@ describe("path-to-regexp", function() {
28322832
]);
28332833
});
28342834

2835+
it("should not normalize whitelisted characters", function() {
2836+
const input = "/test/route%2F%25";
2837+
2838+
expect(pathToRegexp.normalizePathname(input)).toEqual(
2839+
"/test/route%2F%25"
2840+
);
2841+
});
2842+
28352843
it("should fix repeated slashes", function() {
28362844
const input = encodeURI("/test///route");
28372845

src/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,20 @@ export interface ParseOptions {
1919
* slash and normalizes unicode characters to "NFC". When using this method,
2020
* `decode` should be an identity function so you don't decode strings twice.
2121
*/
22-
export function normalizePathname(pathname: string) {
23-
return decodeURIComponent(pathname)
22+
export function normalizePathname(
23+
pathname: string,
24+
whitelist: string | string[] = "%/-."
25+
) {
26+
return pathname
2427
.replace(/\/+/g, "/")
28+
.replace(
29+
/(?:%[ef][0-9a-f](?:%[0-9a-f]{2}){2}|%[cd][0-9a-f]%[0-9a-f]{2}|%[0-9a-f]{2})/gi,
30+
function(m) {
31+
const char = decodeURIComponent(m);
32+
if (whitelist.indexOf(char) > -1) return m;
33+
return char;
34+
}
35+
)
2536
.normalize();
2637
}
2738

0 commit comments

Comments
 (0)