5
5
import type { SafeString as GlimmerSafeString } from '@glimmer/runtime' ;
6
6
7
7
/**
8
- A wrapper around a string that has been marked as safe ( "trusted") . **When
8
+ A wrapper around a string that has been marked as "trusted". **When
9
9
rendered in HTML, Ember will not perform any escaping.**
10
10
11
11
Note:
12
12
13
13
1. This does not *make* the string safe; it means that some code in your
14
- application has *marked* it as safe using the `htmlSafe ()` function.
14
+ application has *marked* it as trusted using the `trustHTML ()` function.
15
15
16
- 2. The only public API for getting a `SafeString ` is calling `htmlSafe ()`. It
16
+ 2. The only public API for getting a `TrutsedHTML ` is calling `trustHTML ()`. It
17
17
is *not* user-constructible.
18
18
19
19
If a string contains user inputs or other untrusted data, you must sanitize
20
- the string before using the `htmlSafe ` method. Otherwise your code is
20
+ the string before using the `trustHTML ` method. Otherwise your code is
21
21
vulnerable to [Cross-Site Scripting][xss]. There are many open source
22
22
sanitization libraries to choose from, both for front end and server-side
23
23
sanitization.
24
24
25
25
[xss]: https://owasp.org/www-community/attacks/DOM_Based_XSS
26
26
27
27
```javascript
28
- import { htmlSafe } from '@ember/template';
28
+ import { trustHTML } from '@ember/template';
29
29
30
30
let someTrustedOrSanitizedString = "<div>Hello!</div>"
31
31
32
- htmlSafe (someTrustedorSanitizedString);
32
+ trustHTML (someTrustedorSanitizedString);
33
33
```
34
34
35
35
@for @ember /template
36
- @class SafeString
37
- @since 4.12 .0
36
+ @class TrustedHTML
37
+ @since 6.7 .0
38
38
@public
39
39
*/
40
- export class SafeString implements GlimmerSafeString {
40
+ export class TrustedHTML implements GlimmerSafeString {
41
41
private readonly __string : string ;
42
42
43
43
constructor ( string : string ) {
@@ -67,6 +67,42 @@ export class SafeString implements GlimmerSafeString {
67
67
}
68
68
}
69
69
70
+ /**
71
+ A wrapper around a string that has been marked as safe ("trusted"). **When
72
+ rendered in HTML, Ember will not perform any escaping.**
73
+
74
+ Note:
75
+
76
+ 1. This does not *make* the string safe; it means that some code in your
77
+ application has *marked* it as safe using the `htmlSafe()` function.
78
+
79
+ 2. The only public API for getting a `SafeString` is calling `htmlSafe()`. It
80
+ is *not* user-constructible.
81
+
82
+ If a string contains user inputs or other untrusted data, you must sanitize
83
+ the string before using the `htmlSafe` method. Otherwise your code is
84
+ vulnerable to [Cross-Site Scripting][xss]. There are many open source
85
+ sanitization libraries to choose from, both for front end and server-side
86
+ sanitization.
87
+
88
+ [xss]: https://owasp.org/www-community/attacks/DOM_Based_XSS
89
+
90
+ ```javascript
91
+ import { htmlSafe } from '@ember/template';
92
+
93
+ let someTrustedOrSanitizedString = "<div>Hello!</div>"
94
+
95
+ htmlSafe(someTrustedorSanitizedString);
96
+ ```
97
+
98
+ @for @ember /template
99
+ @class SafeString
100
+ @since 4.12.0
101
+ @public
102
+ */
103
+ export const SafeString = TrustedHTML ;
104
+ export type SafeString = TrustedHTML ;
105
+
70
106
/**
71
107
Use this method to indicate that a string should be rendered as HTML
72
108
when the string is used in a template. To say this another way,
@@ -96,13 +132,46 @@ export class SafeString implements GlimmerSafeString {
96
132
@return {SafeString } A string that will not be HTML escaped by Handlebars.
97
133
@public
98
134
*/
99
- export function htmlSafe ( str : string ) : SafeString {
135
+ export const htmlSafe = trustHTML ;
136
+
137
+ /**
138
+ Use this method to indicate that a string should be rendered as HTML
139
+ without escaping when the string is used in a template. To say this another way,
140
+ strings marked with `trustHTML` will not be HTML escaped.
141
+
142
+ A word of warning - The `trustHTML` method does not make the string safe;
143
+ it only tells the framework to treat the string as if it is safe to render
144
+ as HTML - that we trust its contents to be safe. If a string contains user inputs or other untrusted
145
+ data, you must sanitize the string before using the `trustHTML` method.
146
+ Otherwise your code is vulnerable to
147
+ [Cross-Site Scripting](https://owasp.org/www-community/attacks/DOM_Based_XSS).
148
+ There are many open source sanitization libraries to choose from,
149
+ both for front end and server-side sanitization.
150
+
151
+ ```glimmer-js
152
+ import { trustHTML } from '@ember/template';
153
+
154
+ const someTrustedOrSanitizedString = "<div>Hello!</div>"
155
+
156
+ <template>
157
+ {{trustHTML someTrustedOrSanitizedString}}
158
+ </template>
159
+ ```
160
+
161
+ @method trustHTML
162
+ @for @ember /template
163
+ @param str {String} The string to treat as trusted.
164
+ @static
165
+ @return {TrustedHTML } A string that will not be HTML escaped by Handlebars.
166
+ @public
167
+ */
168
+ export function trustHTML ( str : string ) : TrustedHTML {
100
169
if ( str === null || str === undefined ) {
101
170
str = '' ;
102
171
} else if ( typeof str !== 'string' ) {
103
172
str = String ( str ) ;
104
173
}
105
- return new SafeString ( str ) ;
174
+ return new TrustedHTML ( str ) ;
106
175
}
107
176
108
177
/**
@@ -124,12 +193,33 @@ export function htmlSafe(str: string): SafeString {
124
193
@return {Boolean } `true` if the string was decorated with `htmlSafe`, `false` otherwise.
125
194
@public
126
195
*/
127
- export function isHTMLSafe ( str : unknown ) : str is SafeString {
196
+ export const isHTMLSafe = isTrustedHTML ;
197
+
198
+ /**
199
+ Detects if a string was decorated using `trustHTML`.
200
+
201
+ ```javascript
202
+ import { trustHTML, isTrustedHTML } from '@ember/template';
203
+
204
+ let plainString = 'plain string';
205
+ let safeString = trustHTML('<div>someValue</div>');
206
+
207
+ isTrustedHTML(plainString); // false
208
+ isTrustedHTML(safeString); // true
209
+ ```
210
+
211
+ @method isTrustedHTML
212
+ @for @ember /template
213
+ @static
214
+ @return {Boolean } `true` if the string was decorated with `htmlSafe`, `false` otherwise.
215
+ @public
216
+ */
217
+ export function isTrustedHTML ( str : unknown ) : str is TrustedHTML {
128
218
return (
129
219
// SAFETY: cast `as SafeString` only present to make this check "legal"; we
130
220
// can further improve this by changing the behavior to do an `in` check
131
221
// instead, but that's worth landing as a separate change for bisecting if
132
222
// it happens to have an impact on e.g. perf.
133
- str !== null && typeof str === 'object' && typeof ( str as SafeString ) . toHTML === 'function'
223
+ str !== null && typeof str === 'object' && typeof ( str as TrustedHTML ) . toHTML === 'function'
134
224
) ;
135
225
}
0 commit comments