|
4 | 4 | <qhelp> |
5 | 5 | <overview> |
6 | 6 | <p> |
7 | | - Dynamically constructing HTML with inputs from library functions that are |
8 | | - available to external clients may inadvertently leave a client open to XSS attacks. |
9 | | - |
10 | | - Clients using the exported function may use inputs containing unsafe HTML, |
11 | | - and if these inputs end up in the DOM, the client may be vulnerable to |
12 | | - cross-site scripting attacks. |
13 | | - </p> |
| 7 | + When a library function dynamically constructs HTML in a potentially unsafe |
| 8 | + way, then it's important to document to clients of the library that the function |
| 9 | + should only be used with trusted inputs. |
14 | 10 |
|
| 11 | + If the function is not documented as being potentially unsafe, then a client |
| 12 | + may inadvertently use inputs containing unsafe HTML fragments, and thereby leave |
| 13 | + the client vulnerable to cross-site scripting attacks. |
| 14 | + </p> |
15 | 15 | </overview> |
16 | 16 | <recommendation> |
17 | 17 |
|
18 | 18 | <p> |
19 | | - If possible, use safe APIs when inserting HTML into the DOM. |
20 | | - Such as writing to the <code>innerText</code> property instead of <code>innerHTML</code>. |
| 19 | + Document all library functions that can lead to cross-site scripting |
| 20 | + attacks, and guard against unsafe inputs where dynamic HTML |
| 21 | + construction is not intended. |
21 | 22 | </p> |
22 | | - |
23 | | - <p> |
24 | | - Alternatively, use a HTML sanitizer to escape/remove unsafe content. |
25 | | - </p> |
26 | | - |
27 | 23 | </recommendation> |
28 | 24 | <example> |
29 | 25 |
|
|
41 | 37 | </p> |
42 | 38 |
|
43 | 39 | <p> |
44 | | - To avoid such attacks, a program can use safe APIs such as <code>innerText</code>. |
| 40 | + The library could either document that this function should not be used |
| 41 | + with unsafe inputs, or use safe APIs such as <code>innerText</code>. |
45 | 42 | </p> |
46 | 43 |
|
47 | 44 | <sample src="examples/unsafe-html-construction_safe.js" /> |
48 | 45 |
|
49 | 46 | <p> |
50 | | - Alternatively, use a HTML sanitizer to remove unsafe content. |
| 47 | + Alternatively, a HTML sanitizer can be used to remove unsafe content. |
51 | 48 | </p> |
52 | 49 |
|
53 | 50 | <sample src="examples/unsafe-html-construction_sanitizer.js" /> |
|
0 commit comments