Potential fix for code scanning alert no. 83: Client-side URL redirect#1133
Potential fix for code scanning alert no. 83: Client-side URL redirect#1133farabi-deriv wants to merge 1 commit intomasterfrom
Conversation
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.OpenSSF Scorecard
Scanned Manifest Files |
|
Check warning
Code scanning / CodeQL
Client-side URL redirect Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
To fix the issue, we need to ensure that redirect_url is always validated against a list of trusted URLs or domains before being used in window.location.replace. This involves:
- Maintaining a strict list of allowed URLs or domains.
- Validating
redirect_urlagainst this list, even when it is modified based on language settings or other parameters. - Rejecting or defaulting to a safe URL if
redirect_urldoes not pass validation.
The changes will be made in src/javascript/app/pages/callback/callback.jsx to ensure that redirect_url is validated before redirection.
| @@ -85,10 +85,13 @@ | ||
| // redirect back | ||
| let set_default = true; | ||
| const trusted_urls = [ | ||
| urlFor('user/metatrader'), | ||
| Client.defaultRedirectUrl(), | ||
| urlFor('home'), | ||
| ]; | ||
| const isTrustedUrl = (url) => { | ||
| const trusted_urls = [ | ||
| urlFor('user/metatrader'), | ||
| Client.defaultRedirectUrl(), | ||
| urlFor('home'), | ||
| ]; | ||
| return trusted_urls.includes(url); | ||
| }; | ||
|
|
||
| if (redirect_url && trusted_urls.includes(redirect_url)) { | ||
| let set_default = true; | ||
| if (redirect_url && isTrustedUrl(redirect_url)) { | ||
| set_default = false; | ||
| @@ -96,5 +99,5 @@ | ||
|
|
||
| if (set_default) { | ||
| const lang_cookie = Cookies.get('language') || getLanguage(); | ||
| const language = getLanguage(); | ||
| if (set_default) { | ||
| const lang_cookie = Cookies.get('language') || getLanguage(); | ||
| const language = getLanguage(); | ||
| redirect_url = | ||
| @@ -110,2 +113,7 @@ | ||
| } | ||
|
|
||
| if (!isTrustedUrl(redirect_url)) { | ||
| redirect_url = Client.defaultRedirectUrl(); | ||
| } | ||
|
|
||
| getElementById('loading_link').setAttribute('href', redirect_url); |
Check warning
Code scanning / CodeQL
Client-side URL redirect Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
To fix the issue, we need to ensure that the redirect_url is validated against a strict list of trusted URLs and does not rely on user-controlled input. This can be achieved by maintaining a whitelist of authorized redirect paths and validating redirect_url against this whitelist. If the URL is not in the whitelist, the code should default to a safe URL.
Steps to implement the fix:
- Replace the
trusted_urlsarray with a whitelist of authorized paths (e.g.,/user/metatrader,/home). - Extract the path from
redirect_urland validate it against the whitelist. - If the path is not in the whitelist, default to a safe URL.
- Ensure that the validation logic is robust and accounts for edge cases.
| @@ -86,9 +86,6 @@ | ||
| let set_default = true; | ||
| const trusted_urls = [ | ||
| urlFor('user/metatrader'), | ||
| Client.defaultRedirectUrl(), | ||
| urlFor('home'), | ||
| ]; | ||
| const trusted_paths = ['/user/metatrader', '/home']; | ||
| const url_path = new URL(redirect_url, window.location.origin).pathname; | ||
|
|
||
| if (redirect_url && trusted_urls.includes(redirect_url)) { | ||
| if (redirect_url && trusted_paths.includes(url_path)) { | ||
| set_default = false; | ||
| @@ -99,5 +96,5 @@ | ||
| const language = getLanguage(); | ||
| redirect_url = | ||
| Client.isAccountOfType('financial') || Client.isOptionsBlocked() | ||
| ? urlFor('user/metatrader') | ||
| redirect_url = | ||
| Client.isAccountOfType('financial') || Client.isOptionsBlocked() | ||
| ? urlFor('user/metatrader') | ||
| : Client.defaultRedirectUrl(); |
Potential fix for https://github.com/deriv-com/smarttrader/security/code-scanning/83
To fix the issue, we need to ensure that the
redirect_urlis validated against a whitelist of trusted domains or paths before redirection. This can be achieved by maintaining a list of authorized URLs on the server or in the client code and checking theredirect_urlagainst this list. If theredirect_urlis not in the whitelist, the code should default to a safe URL.Steps to fix:
redirect_urlagainst this whitelist before using it inwindow.location.replace.redirect_urlis not in the whitelist, set it to a default safe URL.Suggested fixes powered by Copilot Autofix. Review carefully before merging.