Skip to content

Commit

Permalink
Backport of [ui] Simple url sanitization for get-env and document.coo…
Browse files Browse the repository at this point in the history
…kie into release/1.19.x (#21721)

backport of commit 9fb851d

Co-authored-by: Phil Renaud <[email protected]>
  • Loading branch information
hc-github-team-consul-core and philrenaud committed Sep 13, 2024
1 parent c0c9b5b commit 39c00d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/21711.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:security
Implement HTML sanitization for user-generated content to prevent XSS attacks in the UI.
```
26 changes: 23 additions & 3 deletions ui/packages/consul-ui/app/utils/get-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
*/

import { runInDebug } from '@ember/debug';
import { htmlSafe } from '@ember/template';

function sanitizeString(str) {
return htmlSafe(
String(str)
.replace(/&/g, '&amp;')
.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
Expand Down Expand Up @@ -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>
`);
}
}
};

Expand Down

0 comments on commit 39c00d3

Please sign in to comment.