Repository navigation
Update WPT - #4358
Update WPT#4358
Conversation
3bc0446 to
60712b3
Compare
193af49 to
5627bb3
Compare
490deb5 to
24cdbde
Compare
24cdbde to
7f71421
Compare
| await test_driver.set_permission({ name: 'local-network-access' }, permission_value); | ||
|
|
||
| const child = document.createElement('iframe'); | ||
| child.src = new URL(window.location).searchParams.get('url'); |
Check warning
Code scanning / CodeQL
Client-side URL redirect
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we should validate the value of the url parameter before using it as the src of the iframe. The best way is to maintain a whitelist of allowed URLs or restrict the URLs to same-origin or a set of trusted origins. For demonstration, we can restrict the iframe source to only allow URLs that are same-origin with the current page, or match a list of allowed paths. This can be done by parsing the URL and checking its origin or path before assigning it to the iframe. The changes should be made in the script block, specifically at line 32 where the iframe's src is set.
| @@ -29,7 +29,22 @@ | ||
| await test_driver.set_permission({ name: 'local-network-access' }, permission_value); | ||
|
|
||
| const child = document.createElement('iframe'); | ||
| child.src = new URL(window.location).searchParams.get('url'); | ||
| const urlParam = new URL(window.location).searchParams.get('url'); | ||
| let safeUrl = ''; | ||
| if (urlParam) { | ||
| try { | ||
| const candidateUrl = new URL(urlParam, window.location.origin); | ||
| // Only allow same-origin URLs | ||
| if (candidateUrl.origin === window.location.origin) { | ||
| safeUrl = candidateUrl.href; | ||
| } | ||
| } catch (e) { | ||
| // Invalid URL, do not set src | ||
| } | ||
| } | ||
| if (safeUrl) { | ||
| child.src = safeUrl; | ||
| } | ||
| document.body.appendChild(child); | ||
| }); | ||
| </script> |
| await test_driver.set_permission({ name: 'local-network-access' }, permission_value); | ||
|
|
||
| const child = document.createElement('iframe'); | ||
| child.src = new URL(window.location).searchParams.get('url'); |
Check failure
Code scanning / CodeQL
Client-side cross-site scripting
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix this DOM-based XSS vulnerability, we must ensure that the value assigned to the iframe.src attribute is safe. The best way is to validate that the url parameter is a well-formed, safe URL, and only allow certain schemes (e.g., http:, https:) or restrict it to same-origin URLs. We should reject or ignore any value that does not meet these criteria. This can be done by parsing the URL, checking its protocol, and possibly its origin. If the value is not valid or safe, we should avoid setting the src or set it to a safe default.
The changes should be made in the region around line 32 in test/fixtures/wpt/fetch/local-network-access/resources/iframer.html. No new libraries are needed; we can use the built-in URL API for validation.
| @@ -29,7 +29,24 @@ | ||
| await test_driver.set_permission({ name: 'local-network-access' }, permission_value); | ||
|
|
||
| const child = document.createElement('iframe'); | ||
| child.src = new URL(window.location).searchParams.get('url'); | ||
| const urlParam = new URL(window.location).searchParams.get('url'); | ||
| let safeUrl = ''; | ||
| try { | ||
| if (urlParam) { | ||
| const parsedUrl = new URL(urlParam, window.location.origin); | ||
| if (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') { | ||
| safeUrl = parsedUrl.href; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| // Invalid URL, leave safeUrl as empty string | ||
| } | ||
| if (safeUrl) { | ||
| child.src = safeUrl; | ||
| } else { | ||
| // Optionally, set a safe default or do not set src at all | ||
| child.src = 'about:blank'; | ||
| } | ||
| document.body.appendChild(child); | ||
| }); | ||
| </script> |
| <script> | ||
| const window_url = new URL(window.location.href); | ||
| targetUrl = window_url.searchParams.get('url'); | ||
| window.location.href = targetUrl; |
Check warning
Code scanning / CodeQL
Client-side URL redirect
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we should avoid redirecting to arbitrary URLs provided by the user. Instead, we should maintain a whitelist of allowed URLs or restrict redirects to same-origin URLs only. The best way to do this in the context of this snippet is to check that the target URL is either a relative path or has the same origin as the current page. If the check fails, we should avoid redirecting or redirect to a safe default location. The changes should be made in the <script> block, specifically lines 7-8, and may require a helper function to validate the URL.
| @@ -4,6 +4,20 @@ | ||
| <body></body> | ||
| <script> | ||
| const window_url = new URL(window.location.href); | ||
| targetUrl = window_url.searchParams.get('url'); | ||
| window.location.href = targetUrl; | ||
| const targetUrl = window_url.searchParams.get('url'); | ||
| // Only allow same-origin or relative URLs for redirect | ||
| if (targetUrl) { | ||
| try { | ||
| const parsedUrl = new URL(targetUrl, window.location.origin); | ||
| if (parsedUrl.origin === window.location.origin) { | ||
| window.location.href = parsedUrl.href; | ||
| } else { | ||
| // Unsafe redirect, go to homepage or show error | ||
| window.location.href = '/'; | ||
| } | ||
| } catch (e) { | ||
| // Malformed URL, go to homepage or show error | ||
| window.location.href = '/'; | ||
| } | ||
| } | ||
| </script> |
| <script> | ||
| const window_url = new URL(window.location.href); | ||
| targetUrl = window_url.searchParams.get('url'); | ||
| window.location.href = targetUrl; |
Check failure
Code scanning / CodeQL
Client-side cross-site scripting
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix this vulnerability, the code should validate and sanitize the url parameter before using it as a navigation target. The safest approach is to only allow navigation to trusted URLs. A common pattern is to only allow relative URLs (i.e., URLs that do not specify a protocol or domain), or to check that the URL is on the same origin. Alternatively, a whitelist of allowed domains can be used. In this case, we will restrict navigation to same-origin URLs by checking that the resolved target URL has the same origin as the current page. If the check fails, we can either do nothing or navigate to a safe default.
Required changes:
- Parse the
targetUrlas a URL, resolving it relative to the current location. - Check that the resulting URL's
originmatcheswindow.location.origin. - Only assign to
window.location.hrefif the check passes. - Optionally, handle the case where the check fails (e.g., show an error or navigate to a safe default).
No new imports are needed, as the standard URL API is available in browsers.
| @@ -4,6 +4,19 @@ | ||
| <body></body> | ||
| <script> | ||
| const window_url = new URL(window.location.href); | ||
| targetUrl = window_url.searchParams.get('url'); | ||
| window.location.href = targetUrl; | ||
| const targetUrl = window_url.searchParams.get('url'); | ||
| if (targetUrl) { | ||
| try { | ||
| const resolvedUrl = new URL(targetUrl, window.location.href); | ||
| if (resolvedUrl.origin === window.location.origin) { | ||
| window.location.href = resolvedUrl.href; | ||
| } else { | ||
| // Optionally, handle invalid navigation attempt | ||
| // alert('Navigation to external origins is not allowed.'); | ||
| } | ||
| } catch (e) { | ||
| // Optionally, handle invalid URL | ||
| // alert('Invalid URL.'); | ||
| } | ||
| } | ||
| </script> |
Co-authored-by: Uzlopak <5059100+Uzlopak@users.noreply.github.com>
Automated update of the WPT