@@ -13,7 +13,7 @@ import {TrustedTypesConfig} from './trusted_types_configuration';
13
13
export function isExpressionOfAllowedTrustedType (
14
14
tc : ts . TypeChecker , expr : ts . Expression ,
15
15
allowedType : TrustedTypesConfig ) : boolean {
16
- if ( isIntersectionOfTrustedType ( tc , expr , allowedType ) ) return true ;
16
+ if ( isTrustedType ( tc , expr , allowedType ) ) return true ;
17
17
if ( isTrustedTypeCastToUnknownToString ( tc , expr , allowedType ) ) return true ;
18
18
if ( isTrustedTypeUnionWithStringCastToString ( tc , expr , allowedType ) ) return true ;
19
19
if ( isTrustedTypeUnwrapperFunction ( tc , expr , allowedType ) ) return true ;
@@ -26,13 +26,15 @@ export function isExpressionOfAllowedTrustedType(
26
26
*/
27
27
function isAllowedSymbol (
28
28
tc : ts . TypeChecker , symbol : ts . Symbol | undefined ,
29
- allowedType : TrustedTypesConfig ) {
29
+ allowedType : TrustedTypesConfig ,
30
+ allowAmbientTrustedTypesDeclaration : boolean ) {
30
31
debugLog ( ( ) => `isAllowedSymbol called with symbol ${ symbol ?. getName ( ) } ` ) ;
31
32
if ( ! symbol ) return false ;
32
33
33
34
const fqn = tc . getFullyQualifiedName ( symbol ) ;
34
35
debugLog ( ( ) => `fully qualified name is ${ fqn } ` ) ;
35
- if ( allowedType . allowAmbientTrustedTypesDeclaration &&
36
+ if ( allowAmbientTrustedTypesDeclaration &&
37
+ allowedType . allowAmbientTrustedTypesDeclaration &&
36
38
fqn === allowedType . typeName ) {
37
39
return true ;
38
40
}
@@ -73,7 +75,7 @@ function isTrustedTypeCastToUnknownToString(
73
75
const castSource = innerExpr . expression ;
74
76
debugLog ( ( ) => `looking at cast source ${ castSource . getText ( ) } ` ) ;
75
77
return isAllowedSymbol (
76
- tc , tc . getTypeAtLocation ( castSource ) . getSymbol ( ) , allowedType ) ;
78
+ tc , tc . getTypeAtLocation ( castSource ) . getSymbol ( ) , allowedType , false ) ;
77
79
}
78
80
79
81
/**
@@ -94,7 +96,7 @@ function isTrustedTypeUnionWithStringCastToString(
94
96
// do not care how many types are in the union. As long as one of them is
95
97
// the configured Trusted type we are happy.
96
98
innerExprType . types . some (
97
- type => isAllowedSymbol ( tc , type . getSymbol ( ) , allowedType ) ) ;
99
+ type => isAllowedSymbol ( tc , type . getSymbol ( ) , allowedType , false ) ) ;
98
100
}
99
101
100
102
/**
@@ -113,18 +115,22 @@ function isTrustedTypeUnwrapperFunction(
113
115
114
116
return expr . arguments . length > 0 &&
115
117
isAllowedSymbol (
116
- tc , tc . getTypeAtLocation ( expr . arguments [ 0 ] ) . getSymbol ( ) , allowedType ) ;
118
+ tc , tc . getTypeAtLocation ( expr . arguments [ 0 ] ) . getSymbol ( ) ,
119
+ allowedType , false ) ;
117
120
}
118
121
119
122
/**
120
- * Returns true if the expression is a value of a type that is the
121
- * intersection of Trusted Types and other types.
123
+ * Returns true if the expression is a value of Trusted Types, or a type that is
124
+ * the intersection of Trusted Types and other types.
122
125
*/
123
- function isIntersectionOfTrustedType (
126
+ function isTrustedType (
124
127
tc : ts . TypeChecker , expr : ts . Expression , allowedType : TrustedTypesConfig ) {
125
128
const type = tc . getTypeAtLocation ( expr ) ;
126
129
130
+ if ( isAllowedSymbol ( tc , type . getSymbol ( ) , allowedType , true ) ) return true ;
131
+
127
132
if ( ! type . isIntersection ( ) ) return false ;
128
133
129
- return type . types . some ( t => isAllowedSymbol ( tc , t . getSymbol ( ) , allowedType ) ) ;
134
+ return type . types . some (
135
+ t => isAllowedSymbol ( tc , t . getSymbol ( ) , allowedType , true ) ) ;
130
136
}
0 commit comments