Skip to content

Cross-site scripting (XSS)

In XSS, external content becomes executable code in the browser. The key issue is how an application inserts input into its pages.

Also known asXSS
IT security

Cross-site scripting, or XSS, is an attack in which external code runs in the browser in the context of an affected website. The application inserts untrusted data into a page in a way that makes the browser treat it as executable content. The code can, for example, alter the displayed page or make requests with the signed-in person's permissions.

A comment in a project portal

A fictional project portal stores comments in the noteText field and later shows them to other team members. The field is intended for ordinary text. If its content is inserted as HTML without appropriate checks, specially crafted input can inject executable content. Storing a comment in your own database does not make it trustworthy for later output. Values from connected systems must also be handled appropriately for where they are rendered.

If the comment should be text only, the application uses a suitable text output mechanism or its framework's safe default rendering. If formatted HTML is deliberately allowed, it needs a maintained sanitisation solution with defined permitted content.

The output context determines the protection

MDN and OWASP distinguish handling for text, HTML, attributes and other output contexts. Encoding suitable for ordinary HTML text is not universal protection for JavaScript or URLs. Input validation alone does not replace protection at output.

A Content Security Policy can additionally restrict which code runs. It replaces neither safe output locations nor necessary HTML sanitisation. The portal scenario illustrates that distinction; it does not describe a security test that was performed.

Sources