[ui] Simple url sanitization for get-env and document.cookie (#21711)

Simple url sanitization for get-env and document.cookie
This commit is contained in:
Phil Renaud 2024-09-12 12:27:22 -04:00 committed by GitHub
parent a3ac555a5e
commit 0cc0fa7188
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 3 deletions

3
.changelog/21711.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:security
Implement HTML sanitization for user-generated content to prevent XSS attacks in the UI.
```

View File

@ -4,6 +4,19 @@
*/
import { runInDebug } from '@ember/debug';
import { htmlSafe } from '@ember/template';
function sanitizeString(str) {
return htmlSafe(
String(str)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
);
}
// 'environment' getter
// there are currently 3 levels of environment variables:
// 1. Those that can be set by the user by setting localStorage values
@ -58,9 +71,16 @@ export default function (config = {}, win = window, doc = document) {
} else {
str = cookies(doc.cookie).join(';');
const tab = win.open('', '_blank');
tab.document.write(
`<body><pre>${location.href}#${str}</pre><br /><a href="javascript:Scenario('${str}')">Scenario</a></body>`
);
if (tab) {
const safeLocationHref = sanitizeString(location.href);
const safeStr = sanitizeString(str);
tab.document.write(`
<body>
<pre>${safeLocationHref}#${safeStr}</pre><br />
<a href="#" onclick="window.opener.Scenario('${safeStr}');window.close();return false;">Scenario</a>
</body>
`);
}
}
};