This product is not supported for your selected Datadog site. ().

Metadata

ID: typescript-browser-security/inner-outer-html

Language: TypeScript

Severity: Warning

Category: Security

CWE: 79

Description

Properties like innerHTML and outerHTML should not be modified directly unless such modifications are clearly reviewed. Modifying innerHTML or outerHTML using user inputs that has not been validated can lead to XSS injection.

Learn More

Non-Compliant Code Examples

function display(text) {
    const mealPlanDiv = document.getElementById("meal-plan");
    const defaultMessage = document.getElementById("default-message");
    defaultMessage.style.display = 'none';
    something.innerHTML = `
        
        ${something}
        
        <div style="background:white; padding:15px; border-radius:10px;">
        </div>
    `;


    somethingElse.innerHTML = `
        <div style="background:white; padding:15px; border-radius:10px;">
            ${DOMPurify.sanitize(marked(text))}
        </div>
    `;

    somethingElseElse.innerHTML = `
        
        ${something}
        
        <div style="background:white; padding:15px; border-radius:10px;">
            ${DOMPurify.sanitize(marked(text))}
        </div>
    `;
}
function nonCompliant(argument) {
  const content = '<div>' + argument + '</div>';
  document.write(content);
}
function nonCompliant(myArgument) {
  document.body.outerHTML = myArgument;

  document.getElementById('example').innerHTML = '<div>' + hello + '</div>'
}
if (typeof(SERVER_DOMAIN) === 'undefined') {
   window.location.replace("/unconfigured.html");
}

const RECEIVE_URL = SERVER_DOMAIN + "/challenge_scoreboard.html" + "?origin=" + get_domain();

var window_ref = null;

document.getElementById("username").focus();

function store_username() {
   var username;
   var username_obj;

   username_obj = document.getElementById("username");
   username = username_obj.value

   var welcome;
   welcome = document.getElementById("welcome");
   welcome.innerHTML = "Welcome " + html_encode (username);

   var set_username;
   set_username = document.getElementById("set_username");
   set_username.style.display="none";

   var game;
   game = document.getElementById("game");
   game.style.display="inline";

   start_game();
   // have to do time out so the window can open
   setTimeout (function () {send_username(username);}, 1000);

   return false;
}

Compliant Code Examples

foo = document.getElementById("bar");

foo.innerHTML = "<br />";
function safeUpdate(content: string) {
    const element = document.getElementById("content");
    // Safe: using DOMPurify to sanitize content
    element.innerHTML = DOMPurify.sanitize(content);
}

function safeTextUpdate(text: string) {
    const element = document.getElementById("content");
    // Safe: using textContent instead of innerHTML
    element.textContent = text;
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security