Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 4e354a6

Browse files
authored
fix(all): fix Trusted Types violations during initialization (#12128)
Currently, when Angular Material is loaded in an application that has Trusted Types enforced, two violations occur. Both violations are caused when a plain string is passed to angular.element since there is no guarantee that the string was not derived from user input, which could cause XSS. It should be noted that, in this case, neither call to angular.element represents a security vulnerability, but this blocks Trusted Types adoption in applications that load Angular Material. To fix the violations, refactor the calls to angular.element to use safe DOM operations instead. This change does not alter any functionality and is fully backwards compatible.
1 parent 8add881 commit 4e354a6

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/components/panel/panel.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,7 @@ angular
869869

870870
var MD_PANEL_Z_INDEX = 80;
871871
var MD_PANEL_HIDDEN = '_md-panel-hidden';
872-
var FOCUS_TRAP_TEMPLATE = angular.element(
873-
'<div class="_md-panel-focus-trap" tabindex="0"></div>');
872+
var FOCUS_TRAP_TEMPLATE;
874873

875874
var _presets = {};
876875

@@ -2107,6 +2106,12 @@ MdPanelRef.prototype._configureTrapFocus = function() {
21072106
var element = this.panelEl;
21082107
// Set up elements before and after the panel to capture focus and
21092108
// redirect back into the panel.
2109+
if (!FOCUS_TRAP_TEMPLATE) {
2110+
var template = document.createElement('div');
2111+
template.className = '_md-panel-focus-trap';
2112+
template.tabIndex = 0;
2113+
FOCUS_TRAP_TEMPLATE = angular.element(template);
2114+
}
21102115
this._topFocusTrap = FOCUS_TRAP_TEMPLATE.clone()[0];
21112116
this._bottomFocusTrap = FOCUS_TRAP_TEMPLATE.clone()[0];
21122117

src/components/select/select.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
var SELECT_EDGE_MARGIN = 8;
1414
var selectNextId = 0;
15-
var CHECKBOX_SELECTION_INDICATOR =
16-
angular.element('<div class="md-container"><div class="md-icon"></div></div>');
15+
var CHECKBOX_SELECTION_INDICATOR;
1716

1817
angular.module('material.components.select', [
1918
'material.core',
@@ -1270,6 +1269,13 @@ function OptionDirective($mdButtonInkRipple, $mdUtil, $mdTheming) {
12701269

12711270
if (selectMenuCtrl.isMultiple) {
12721271
element.addClass('md-checkbox-enabled');
1272+
if (!CHECKBOX_SELECTION_INDICATOR) {
1273+
var indicator = document.createElement('div');
1274+
indicator.className = 'md-container';
1275+
indicator.appendChild(document.createElement('div'));
1276+
indicator.firstChild.className = 'md-icon';
1277+
CHECKBOX_SELECTION_INDICATOR = angular.element(indicator);
1278+
}
12731279
element.prepend(CHECKBOX_SELECTION_INDICATOR.clone());
12741280
}
12751281

0 commit comments

Comments
 (0)