Skip to content

Commit 9d7cc97

Browse files
authored
Update AJAX_Security_Cheat_Sheet.md (#1903)
1 parent 1442f69 commit 9d7cc97

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

cheatsheets/AJAX_Security_Cheat_Sheet.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ Manipulating the Document Object Model (DOM) is common in web applications, espe
1414

1515
The `innerHTML` property sets or gets the HTML content of an element, including tags, which the browser parses and renders as part of the DOM. For example, setting `innerHTML = "<p>Hello</p>"` creates a paragraph element.
1616

17-
##### Why does `innerHTML` requires extreme cautions?
17+
##### Why does `innerHTML` require extreme caution?
1818

1919
Using `innerHTML` with untrusted data (e.g., from API responses in AJAX) can allow malicious JavaScript to execute in the user’s browser, leading to XSS vulnerabilities. Potential risks include:
2020

2121
- Stealing user session cookies.
2222
- Defacing the website.
2323
- Redirecting users to malicious sites.
2424
- Performing unauthorized actions (e.g., API calls on behalf of the user).
25+
- Keylogging user inputs.
2526

2627
###### Vulnerable Example
2728

@@ -80,6 +81,8 @@ document.getElementById('content').innerText = userInput;
8081
- **Use `textContent`**: Use textContent in monolithic applications to safely insert plain text content returned from APIs.
8182
- **Use `innerText`**: Only when CSS visibility or rendered text formatting (e.g. ignoring text in `display: none` elements) is required.
8283

84+
> Note: `textContent` is slightly faster and more predictable; use it unless you need to respect rendered text formatting (`innerText`).
85+
8386
##### Note
8487

8588
- While `textContent` and `innerText` are safe for inserting plain text into the DOM, they do not protect against XSS in other contexts such as HTML attributes, JavaScript event handlers, or URLs. Always validate and sanitize untrusted input.
@@ -89,11 +92,13 @@ document.getElementById('content').innerText = userInput;
8992

9093
`eval()` function is dangerous, never use it. Needing to use eval() usually indicates a problem in your design.
9194

95+
> Note: Using `eval()` or `new Function()` opens doors to remote code execution and XSS. Avoid it entirely.
96+
9297
#### Encode Data Before Use in an Output Context
9398

9499
When using data to build HTML, script, CSS, XML, JSON, etc., make sure you take into account how that data must be presented in a literal sense to keep its logical meaning.
95100

96-
Data should be properly encoded before used in this manner to prevent injection style issues, and to make sure the logical meaning is preserved.
101+
Data should be properly encoded before being used in this manner to prevent injection style issues, and to make sure the logical meaning is preserved.
97102

98103
[Check out the OWASP Java Encoder Project.](https://owasp.org/www-project-java-encoder/)
99104

0 commit comments

Comments
 (0)