@@ -13,14 +13,16 @@ import {trustedHTMLFromString} from '../util/security/trusted_types';
1313import { getInertBodyHelper , InertBodyHelper } from './inert_body' ;
1414import { _sanitizeUrl } from './url_sanitizer' ;
1515
16- function tagSet ( tags : string ) : { [ k : string ] : boolean } {
17- const res : { [ k : string ] : boolean } = { } ;
16+ type BooleanRecord = Record < string , boolean > ;
17+
18+ function tagSet ( tags : string ) : BooleanRecord {
19+ const res : BooleanRecord = { } ;
1820 for ( const t of tags . split ( ',' ) ) res [ t ] = true ;
1921 return res ;
2022}
2123
22- function merge ( ...sets : { [ k : string ] : boolean } [ ] ) : { [ k : string ] : boolean } {
23- const res : { [ k : string ] : boolean } = { } ;
24+ function merge ( ...sets : BooleanRecord [ ] ) : BooleanRecord {
25+ const res : BooleanRecord = { } ;
2426 for ( const s of sets ) {
2527 for ( const v in s ) {
2628 if ( s . hasOwnProperty ( v ) ) res [ v ] = true ;
@@ -66,15 +68,15 @@ const INLINE_ELEMENTS = merge(
6668 ) ,
6769) ;
6870
69- export const VALID_ELEMENTS : { [ k : string ] : boolean } = merge (
71+ export const VALID_ELEMENTS : BooleanRecord = merge (
7072 VOID_ELEMENTS ,
7173 BLOCK_ELEMENTS ,
7274 INLINE_ELEMENTS ,
7375 OPTIONAL_END_TAG_ELEMENTS ,
7476) ;
7577
7678// Attributes that have href and hence need to be sanitized
77- export const URI_ATTRS : { [ k : string ] : boolean } = tagSet (
79+ const URI_ATTRS : BooleanRecord = tagSet (
7880 'background,cite,href,itemtype,longdesc,poster,src,xlink:href' ,
7981) ;
8082
@@ -105,7 +107,7 @@ const ARIA_ATTRS = tagSet(
105107// can be sanitized, but they increase security surface area without a legitimate use case, so they
106108// are left out here.
107109
108- export const VALID_ATTRS : { [ k : string ] : boolean } = merge ( URI_ATTRS , HTML_ATTRS , ARIA_ATTRS ) ;
110+ export const VALID_ATTRS : BooleanRecord = merge ( URI_ATTRS , HTML_ATTRS , ARIA_ATTRS ) ;
109111
110112// Elements whose content should not be traversed/preserved, if the elements themselves are invalid.
111113//
@@ -114,6 +116,16 @@ export const VALID_ATTRS: {[k: string]: boolean} = merge(URI_ATTRS, HTML_ATTRS,
114116// don't want to preserve the content, if the elements themselves are going to be removed.
115117const SKIP_TRAVERSING_CONTENT_IF_INVALID_ELEMENTS = tagSet ( 'script,style,template' ) ;
116118
119+ /**
120+ * Attributes that are potential attach vectors and may need to be sanitized.
121+ */
122+ export const SENSITIVE_ATTRS : BooleanRecord = merge (
123+ URI_ATTRS ,
124+ // Note: we don't include these attributes in `URI_ATTRS`, because `URI_ATTRS` also
125+ // determines whether an attribute should be dropped when sanitizing an HTML string.
126+ tagSet ( 'action,formaction,data,codebase' ) ,
127+ ) ;
128+
117129/**
118130 * SanitizingHtmlSerializer serializes a DOM fragment, stripping out any unsafe elements and unsafe
119131 * attributes.
0 commit comments