Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
Is it possible to escape a single quote to inject javascript in html element?

Comparing 2 html elements

const htmlElement = `<p myAttr=${attackerValue}>Appended element!</p>`;

It is easy to inject javascript here, I can use an attackerValue of </p><script>alert("injected JS") </script>

However, if the there are single quotes wrapping attackerValue in the htmlElement, is it still possible to inject javascript?

const htmlElement = `<p myAttr='${attackerValue}'>Appended element!</p>`;

Codesandbox: https://codesandbox.io/s/young-hill-4nww5k?file=/src/index.js

Edit: Found my answer, I can simply escape the string single quote by using an input such as '</p><script>alert("injected JS") </script>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For front-end code it does not matter if it is possible. <script/> tags inserted via innerHTML are not executed:

<html>
    Example:
    <script>
        const injection = '<script>alert("hello")<\/script>';
        document.body.innerHTML += ` this is a test ${injection}`;

        // You will never see the "hello" alert because
        // browser ignores your script tag.
    </script>
</html>

The browser ignores all script tags inserted into the DOM for exactly this reason - to prevent an injection attack.

It is possible to inject scripts via document.write():

<html>
    Example: 
    <script>
        const injection = '<script>alert("hello")<\/script>';
        document.write(` this is a test ${injection}`);

        // You will see the "hello" alert!
    </script>
</html>

The solution to that is simple: don't use document.write().

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda