Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions .changeset/safe-route-id-decoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@refinedev/core": patch
"@refinedev/react-router": patch
"@refinedev/remix-router": patch
---

Safely handle malformed URI-encoded route params when parsing route ids and query targets.
10 changes: 9 additions & 1 deletion packages/core/src/definitions/helpers/handleUseParams/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
const safeDecodeURIComponent = (value: string) => {
try {
return decodeURIComponent(value);
} catch {
return value;
}
};

export const handleUseParams = (params: any = {}): any => {
if (params?.id) {
return {
...params,
id: decodeURIComponent(params.id),
id: safeDecodeURIComponent(params.id),
};
}
return params;
Expand Down
5 changes: 3 additions & 2 deletions packages/react-router/src/bindings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useParams,
} from "react-router";
import { convertToNumberIfPossible } from "./convert-to-number-if-possible";
import { safeDecodeURIComponent } from "./safe-decode-uri-component";

export const stringifyConfig = {
addQueryPrefix: true,
Expand Down Expand Up @@ -124,7 +125,7 @@ export const routerProvider: RouterProvider = {
const response: ParseResponse = {
...(resource && { resource }),
...(action && { action }),
...(params?.id && { id: decodeURIComponent(params.id) }),
...(params?.id && { id: safeDecodeURIComponent(params.id) }),
// ...(params?.action && { action: params.action }), // lets see if there is a need for this
pathname,
params: {
Expand All @@ -136,7 +137,7 @@ export const routerProvider: RouterProvider = {
combinedParams.pageSize as string,
) as number | undefined,
to: combinedParams.to
? decodeURIComponent(combinedParams.to as string)
? safeDecodeURIComponent(combinedParams.to as string)
: undefined,
},
};
Expand Down
7 changes: 7 additions & 0 deletions packages/react-router/src/safe-decode-uri-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const safeDecodeURIComponent = (value: string) => {
try {
return decodeURIComponent(value);
} catch {
return value;
}
};
7 changes: 4 additions & 3 deletions packages/remix-router/src/bindings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import qs from "qs";
import React, { type ComponentProps, useCallback, useContext } from "react";
import { paramsFromCurrentPath } from "./params-from-current-path";
import { convertToNumberIfPossible } from "./convert-to-number-if-possible";
import { safeDecodeURIComponent } from "./safe-decode-uri-component";

export const stringifyConfig = {
addQueryPrefix: true,
Expand Down Expand Up @@ -116,8 +117,8 @@ export const routerProvider: RouterProvider = {
const response: ParseResponse = {
...(resource && { resource }),
...(action && { action }),
...(inferredId && { id: decodeURIComponent(inferredId) }),
...(params?.id && { id: decodeURIComponent(params.id) }),
...(inferredId && { id: safeDecodeURIComponent(inferredId) }),
...(params?.id && { id: safeDecodeURIComponent(params.id) }),
// ...(params?.action && { action: params.action }), // lets see if there is a need for this
pathname,
params: {
Expand All @@ -129,7 +130,7 @@ export const routerProvider: RouterProvider = {
combinedParams.pageSize as string,
) as number | undefined,
to: combinedParams.to
? decodeURIComponent(combinedParams.to as string)
? safeDecodeURIComponent(combinedParams.to as string)
: undefined,
},
};
Expand Down
7 changes: 7 additions & 0 deletions packages/remix-router/src/safe-decode-uri-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const safeDecodeURIComponent = (value: string) => {
try {
return decodeURIComponent(value);
} catch {
return value;
}
};