Skip to content

Commit 4f8c680

Browse files
urajcopybara-github
authored andcommitted
Allow assigning TT to dom sinks with patched lib.dom.d.ts, per request of #26.
PiperOrigin-RevId: 458421580 Change-Id: Ie5afc247632d77e4229cd401c95951700ba26871
1 parent 0f97f2e commit 4f8c680

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

common/third_party/tsetse/util/is_trusted_type.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {TrustedTypesConfig} from './trusted_types_configuration';
1313
export function isExpressionOfAllowedTrustedType(
1414
tc: ts.TypeChecker, expr: ts.Expression,
1515
allowedType: TrustedTypesConfig): boolean {
16-
if (isIntersectionOfTrustedType(tc, expr, allowedType)) return true;
16+
if (isTrustedType(tc, expr, allowedType)) return true;
1717
if (isTrustedTypeCastToUnknownToString(tc, expr, allowedType)) return true;
1818
if (isTrustedTypeUnionWithStringCastToString(tc, expr, allowedType)) return true;
1919
if (isTrustedTypeUnwrapperFunction(tc, expr, allowedType)) return true;
@@ -26,13 +26,15 @@ export function isExpressionOfAllowedTrustedType(
2626
*/
2727
function isAllowedSymbol(
2828
tc: ts.TypeChecker, symbol: ts.Symbol|undefined,
29-
allowedType: TrustedTypesConfig) {
29+
allowedType: TrustedTypesConfig,
30+
allowAmbientTrustedTypesDeclaration: boolean) {
3031
debugLog(() => `isAllowedSymbol called with symbol ${symbol?.getName()}`);
3132
if (!symbol) return false;
3233

3334
const fqn = tc.getFullyQualifiedName(symbol);
3435
debugLog(() => `fully qualified name is ${fqn}`);
35-
if (allowedType.allowAmbientTrustedTypesDeclaration &&
36+
if (allowAmbientTrustedTypesDeclaration &&
37+
allowedType.allowAmbientTrustedTypesDeclaration &&
3638
fqn === allowedType.typeName) {
3739
return true;
3840
}
@@ -73,7 +75,7 @@ function isTrustedTypeCastToUnknownToString(
7375
const castSource = innerExpr.expression;
7476
debugLog(() => `looking at cast source ${castSource.getText()}`);
7577
return isAllowedSymbol(
76-
tc, tc.getTypeAtLocation(castSource).getSymbol(), allowedType);
78+
tc, tc.getTypeAtLocation(castSource).getSymbol(), allowedType, false);
7779
}
7880

7981
/**
@@ -94,7 +96,7 @@ function isTrustedTypeUnionWithStringCastToString(
9496
// do not care how many types are in the union. As long as one of them is
9597
// the configured Trusted type we are happy.
9698
innerExprType.types.some(
97-
type => isAllowedSymbol(tc, type.getSymbol(), allowedType));
99+
type => isAllowedSymbol(tc, type.getSymbol(), allowedType, false));
98100
}
99101

100102
/**
@@ -113,18 +115,22 @@ function isTrustedTypeUnwrapperFunction(
113115

114116
return expr.arguments.length > 0 &&
115117
isAllowedSymbol(
116-
tc, tc.getTypeAtLocation(expr.arguments[0]).getSymbol(), allowedType);
118+
tc, tc.getTypeAtLocation(expr.arguments[0]).getSymbol(),
119+
allowedType, false);
117120
}
118121

119122
/**
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.
122125
*/
123-
function isIntersectionOfTrustedType(
126+
function isTrustedType(
124127
tc: ts.TypeChecker, expr: ts.Expression, allowedType: TrustedTypesConfig) {
125128
const type = tc.getTypeAtLocation(expr);
126129

130+
if (isAllowedSymbol(tc, type.getSymbol(), allowedType, true)) return true;
131+
127132
if (!type.isIntersection()) return false;
128133

129-
return type.types.some(t => isAllowedSymbol(tc, t.getSymbol(), allowedType));
134+
return type.types.some(
135+
t => isAllowedSymbol(tc, t.getSymbol(), allowedType, true));
130136
}

0 commit comments

Comments
 (0)