Skip to content

Commit cbfcd05

Browse files
committed
refactor: move trusted types logic into CDK (angular#32387)
Moves the logic around trusted types into the CDK so it can be reused. (cherry picked from commit 1d42431)
1 parent 6e83e63 commit cbfcd05

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

goldens/cdk/private/index.api.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ export class _CdkPrivateStyleLoader {
1616
static ɵprov: i0.ɵɵInjectableDeclaration<_CdkPrivateStyleLoader>;
1717
}
1818

19+
// @public (undocumented)
20+
export interface TrustedHTML {
21+
// (undocumented)
22+
__brand__: 'TrustedHTML';
23+
}
24+
25+
// @public
26+
export function trustedHTMLFromString(html: string): TrustedHTML;
27+
1928
// @public
2029
export class _VisuallyHiddenLoader {
2130
// (undocumented)

src/cdk/private/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88

99
export * from './style-loader';
1010
export * from './visually-hidden/visually-hidden';
11+
export {TrustedHTML, trustedHTMLFromString} from './trusted-types';

src/material/icon/trusted-types.ts renamed to src/cdk/private/trusted-types.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
/**
10-
* @fileoverview
11-
* A module to facilitate use of a Trusted Types policy internally within
12-
* Angular Material. It lazily constructs the Trusted Types policy, providing
13-
* helper utilities for promoting strings to Trusted Types. When Trusted Types
14-
* are not available, strings are used as a fallback.
15-
* @security All use of this module is security-sensitive and should go through
16-
* security review.
17-
*/
9+
// A module to facilitate use of a Trusted Types policy internally within
10+
// Angular Material. It lazily constructs the Trusted Types policy, providing
11+
// helper utilities for promoting strings to Trusted Types. When Trusted Types
12+
// are not available, strings are used as a fallback.
13+
// All use of this module is security-sensitive and should go through security review.
1814

19-
export declare interface TrustedHTML {
15+
export interface TrustedHTML {
2016
__brand__: 'TrustedHTML';
2117
}
2218

23-
export declare interface TrustedTypePolicyFactory {
19+
interface TrustedTypePolicyFactory {
2420
createPolicy(
2521
policyName: string,
2622
policyOptions: {
@@ -29,7 +25,7 @@ export declare interface TrustedTypePolicyFactory {
2925
): TrustedTypePolicy;
3026
}
3127

32-
export declare interface TrustedTypePolicy {
28+
interface TrustedTypePolicy {
3329
createHTML(input: string): TrustedHTML;
3430
}
3531

@@ -61,7 +57,8 @@ function getPolicy(): TrustedTypePolicy | null {
6157
/**
6258
* Unsafely promote a string to a TrustedHTML, falling back to strings when
6359
* Trusted Types are not available.
64-
* @security This is a security-sensitive function; any use of this function
60+
*
61+
* Important!!! This is a security-sensitive function; any use of this function
6562
* must go through security review. In particular, it must be assured that the
6663
* provided string will never cause an XSS vulnerability if used in a context
6764
* that will be interpreted as HTML by a browser, e.g. when assigning to

src/material/icon/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ ng_project(
6868
"icon-registry.ts",
6969
"index.ts",
7070
"public-api.ts",
71-
"trusted-types.ts",
7271
],
7372
assets = [
7473
":css",
@@ -79,6 +78,7 @@ ng_project(
7978
"//:node_modules/@angular/platform-browser",
8079
"//:node_modules/rxjs",
8180
"//src:dev_mode_types",
81+
"//src/cdk/private",
8282
"//src/material/core",
8383
],
8484
)

src/material/icon/icon-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import {TrustedHTML, trustedHTMLFromString} from '@angular/cdk/private';
910
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
1011
import {
1112
ErrorHandler,
@@ -19,7 +20,6 @@ import {
1920
import {DomSanitizer, SafeHtml, SafeResourceUrl} from '@angular/platform-browser';
2021
import {forkJoin, Observable, of as observableOf, throwError as observableThrow} from 'rxjs';
2122
import {catchError, finalize, map, share, tap} from 'rxjs/operators';
22-
import {TrustedHTML, trustedHTMLFromString} from './trusted-types';
2323

2424
/**
2525
* Returns an exception to be thrown in the case when attempting to

0 commit comments

Comments
 (0)