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

Commit 187226e

Browse files
bjarklerjelbourn
authored andcommitted
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. (cherry picked from commit 4e354a6)
1 parent e9e4647 commit 187226e

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
@@ -933,8 +933,7 @@ angular
933933

934934
var MD_PANEL_Z_INDEX = 80;
935935
var MD_PANEL_HIDDEN = '_md-panel-hidden';
936-
var FOCUS_TRAP_TEMPLATE = angular.element(
937-
'<div class="_md-panel-focus-trap" tabindex="0"></div>');
936+
var FOCUS_TRAP_TEMPLATE;
938937

939938
var _presets = {};
940939

@@ -2232,6 +2231,12 @@ MdPanelRef.prototype._configureTrapFocus = function() {
22322231
var element = this.panelEl;
22332232
// Set up elements before and after the panel to capture focus and
22342233
// redirect back into the panel.
2234+
if (!FOCUS_TRAP_TEMPLATE) {
2235+
var template = document.createElement('div');
2236+
template.className = '_md-panel-focus-trap';
2237+
template.tabIndex = 0;
2238+
FOCUS_TRAP_TEMPLATE = angular.element(template);
2239+
}
22352240
this._topFocusTrap = FOCUS_TRAP_TEMPLATE.clone()[0];
22362241
this._bottomFocusTrap = FOCUS_TRAP_TEMPLATE.clone()[0];
22372242

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',
@@ -1017,6 +1016,13 @@ function OptionDirective($mdButtonInkRipple, $mdUtil, $mdTheming) {
10171016

10181017
if (selectCtrl.isMultiple) {
10191018
element.addClass('md-checkbox-enabled');
1019+
if (!CHECKBOX_SELECTION_INDICATOR) {
1020+
var indicator = document.createElement('div');
1021+
indicator.className = 'md-container';
1022+
indicator.appendChild(document.createElement('div'));
1023+
indicator.firstChild.className = 'md-icon';
1024+
CHECKBOX_SELECTION_INDICATOR = angular.element(indicator);
1025+
}
10201026
element.prepend(CHECKBOX_SELECTION_INDICATOR.clone());
10211027
}
10221028

0 commit comments

Comments
 (0)