What is Cross-site scripting (XSS)?

A vulnerability in which a web application outputs user input into HTML as is, so that a script contained in the input runs in visitors' browsers as a page of that site. Escaping characters with special meaning on output is the fundamental countermeasure.

Japanese name: クロスサイトスクリプティング(XSS)

Cross-site scripting (XSS) appears in 4 practice questions across 2 topics, including Web Technology Basics, Information Security Basics.

Field: Information Security Basics

Try it: questions about Cross-site scripting (XSS)

Which of the following correctly describes a difference between the browser's localStorage and cookies?

  1. AlocalStorage data is not attached to requests automatically; the JavaScript of pages of the same origin reads and writes it
  2. BlocalStorage data, like cookies, is put into a header and sent to the server automatically with every request to the same site
  3. ClocalStorage can also be given the HttpOnly attribute, so even if a session ID is stored there, the page's scripts cannot read it
Show answer and explanation

Answer: A. localStorage data is not attached to requests automatically; the JavaScript of pages of the same origin reads and writes it

localStorage is a storage area in the browser that keeps string keys and values per origin; it has no way to set an expiry and generally remains until deleted. It is not sent to the server automatically, so to send it, JavaScript puts it into a request. Cookies are small, generally up to about 4 KB each, and are sent automatically with every request, whereas larger data can be kept in localStorage. However, the page's JavaScript can read it at any time, so if there is an XSS flaw its contents can be stolen. This risk must be considered when storing values used for authentication.

A bulletin-board web application had a vulnerability in which scripts contained in text posted by users were executed in other visitors' browsers. Which combination correctly gives the name of this vulnerability and its fundamental countermeasure?

  1. ASQL injection. Lengthen the password used to connect to the database
  2. BCross-site scripting (XSS). Escape characters with special meaning, such as < and >, when outputting posted text as HTML
  3. CCross-site scripting (XSS). Encrypt communication between the server and the browser with HTTPS
Show answer and explanation

Answer: B. Cross-site scripting (XSS). Escape characters with special meaning, such as < and >, when outputting posted text as HTML

Cross-site scripting (XSS) is a vulnerability in which a web application outputs user input into HTML as is, so a script contained in the input runs in visitors' browsers as part of the page. Because the script runs as a page of that site, it may steal the visitor's session information (cookies) or display fake input fields. The fundamental countermeasure is to escape characters with special meaning, such as <, >, & and quotation marks, according to where they are output (for example, converting < to &lt;) so they are displayed as plain characters. Supplementary measures include giving cookies the HttpOnly attribute so scripts cannot read them, and restricting which scripts may run with a Content Security Policy. HTTPS protects the communication channel and cannot prevent a script embedded in a legitimate page from running.

Cross-site scripting (XSS) in the explanations

Questions that mention Cross-site scripting (XSS) (4)

  1. Which of the following correctly describes the cookie set by the header above?
  2. Which of the following correctly describes a difference between the browser's localStorage and cookies?
  3. A bulletin-board web application had a vulnerability in which scripts contained in text posted by users were executed in other visitors' browsers. Which combination correctly gives the name of this vulnerability and its fundamental countermeasure?
  4. A web application has a vulnerability in which, if a logged-in user opens a trap page prepared by an attacker, the user's browser sends requests to the legitimate site that the user did not intend, such as transferring money or changing settings. Which is an appropriate countermeasure?

Books on Information Security Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

Related terms

Cookie (HTTP cookie) Digital signature Hash function Message authentication code Multi-factor authentication Phishing Principle of least privilege Public-key cryptography Ransomware Salt

Back to the glossary