-
Notifications
You must be signed in to change notification settings - Fork 118
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
feat: support redirection with query params #799
base: main
Are you sure you want to change the base?
Conversation
router.query.redirect && | ||
router.query.redirect.toString().startsWith("/") | ||
) { | ||
await router.push(router.query.redirect.toString()); |
Check warning
Code scanning / CodeQL
Client-side URL redirect Medium
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 16 days ago
To fix the problem, we need to ensure that the redirection URL is validated against a list of authorized paths. This can be achieved by maintaining a list of allowed paths and checking if the user-provided redirect path is in this list before performing the redirection. This approach ensures that only safe and pre-approved paths are used for redirection.
- Define a list of authorized redirect paths.
- Check if the user-provided redirect path is in the list of authorized paths.
- Perform the redirection only if the path is authorized.
-
Copy modified lines R64-R65 -
Copy modified lines R68-R70
@@ -63,9 +63,9 @@ | ||
}; | ||
const AUTHORIZED_REDIRECT_PATHS = ["/home", "/dashboard", "/profile"]; // Add authorized paths here | ||
|
||
const authRedirection = async (isAuthenticated: boolean) => { | ||
if (isAuthenticated) { | ||
if ( | ||
router.query.redirect && | ||
router.query.redirect.toString().startsWith("/") | ||
) { | ||
await router.push(router.query.redirect.toString()); | ||
const redirectPath = router.query.redirect?.toString() || ""; | ||
if (AUTHORIZED_REDIRECT_PATHS.includes(redirectPath)) { | ||
await router.push(redirectPath); | ||
} else if (hasPublicPath) { |
router.query.redirect && | ||
router.query.redirect.toString().startsWith("/") | ||
) { | ||
await router.push(router.query.redirect.toString()); |
Check failure
Code scanning / CodeQL
Client-side cross-site scripting High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 16 days ago
To fix the problem, we need to ensure that the redirect
parameter is properly sanitized before using it. One way to do this is to use a library like DOMPurify
to sanitize the input. This will help prevent any malicious scripts from being executed.
- Import the
DOMPurify
library. - Use
DOMPurify.sanitize
to sanitize theredirect
parameter before using it in therouter.push
method. - Ensure that the sanitized value is used in place of the original
router.query.redirect.toString()
.
-
Copy modified line R10 -
Copy modified lines R71-R72
@@ -9,2 +9,3 @@ | ||
import getConfig from "next/config"; | ||
import DOMPurify from "dompurify"; | ||
import { useRouter } from "next/router"; | ||
@@ -69,3 +70,4 @@ | ||
) { | ||
await router.push(router.query.redirect.toString()); | ||
const sanitizedRedirect = DOMPurify.sanitize(router.query.redirect.toString()); | ||
await router.push(sanitizedRedirect); | ||
} else if (hasPublicPath) { |
-
Copy modified lines R47-R48
@@ -46,3 +46,4 @@ | ||
"react-query": "^3.39.3", | ||
"socket.io-client": "^4.7.5" | ||
"socket.io-client": "^4.7.5", | ||
"dompurify": "^3.2.4" | ||
}, |
Package | Version | Security advisories |
dompurify (npm) | 3.2.4 | None |
Motivation
This PR ensures that query parameters are retained when a user logs out. After logging back in, the user is redirected to their previous pathname with the original query parameters preserved
Fixes #798
Type of change:
Please delete options that are not relevant.
Checklist: