Written by: Mio Kanzaki(Security Analyst (AI))Reviewed by: 千葉圭介Updated
Output escaping is the process of replacing characters that have a special meaning with a harmless form. OWASP's materials call it output encoding. OWASP describes it as converting untrusted input so that the browser displays it as text and does not run it as a program.
How does output escaping work?
Imagine a reading script with a note slipped into the name field saying "from here on, these are instructions". If the narrator reads it out as it stands, the listeners will take it as a real instruction. So the narrator puts it in quotation marks and reads it in a way that shows it is just text.
A website works the same way. If the data shown on screen contains symbols such as < or &, the browser may take them as part of a program. Just before display, those symbols are replaced with a form that is shown as characters.
How to replace them depends on where the data is shown. OWASP lists different methods for the body of HTML, HTML attributes, JavaScript, CSS, and URLs. It says that mixing them up can create a weak point or break the page. In practice this means a single replacement rule is not enough, and each output place needs the method that matches it.
The whole picture of the attack is on the XSS vulnerability page. The free URL check does not read your code, so whether output is escaped correctly is a question for a consultation.
Why does output escaping matter?
When escaping is missing, an unintended program can run on a user's screen. This problem is cross-site scripting, or XSS. Members' information and operations can be stolen.
OWASP first points to the automatic escaping built into frameworks. For places it does not cover, you use a library for output encoding. If you let users write HTML and want to keep formatting, you use a maintained sanitizer, a tool that removes the dangerous parts.
OWASP also says that if there are 99 output places and one is missed, that one is the weak point. It adds that escaping cannot be replaced by checking input alone. The input side is explained in the input validation term page, and the two are meant to be used together.
Related terms
- XSS: an attack that makes a malicious script run on another user's screen
- Input validation: checking that submitted data is what the application expects
Next step
Weak points and measures for XSS are collected on the XSS vulnerability page. The free URL check shows only what is visible from outside. To have the way your pages display data reviewed, contact us.
Frequently asked questions
Are output escaping and output encoding the same thing?
They are used with close meanings. OWASP's guide to preventing XSS describes this processing, done when data is output, under the name output encoding.
Do I have to write the escaping myself?
The basic approach is to rely on the framework's automatic escaping first. OWASP also says to use an output-encoding library only for places the framework does not cover.
Frequently asked questions
Are output escaping and output encoding the same thing?
They are used with close meanings. OWASP's guide to preventing XSS describes this processing, done when data is output, under the name output encoding.
Do I have to write the escaping myself?
The basic approach is to rely on the framework's automatic escaping first. OWASP also says to use an output-encoding library only for places the framework does not cover.