What is Cross Site Scripting? How To Prevent XSS Attack In 2021?


XSS is the most well-known security weakness in programming today. This ought not to be the situation as XSS is anything but difficult to track down and simple to fix. 

  1. What is XSS
  2. Working
  3. Types
  4. Uses
  5. Prevention
  6. Examples

1. What is XSS

first, let’s see what is cross site scripting? Cross-site Scripting (XSS) is a customer side code infusion assault. The aggressor expects to execute malevolent content in an internet browser of the victim by adding noxious code for a real page or web application. It permits an assailant to evade a similar origin policy, which is intended to isolate various sites from one another. Cross-site scripting weaknesses typically permit an aggressor to take on the appearance of a victim client, to do any activities that the client can perform, and to get to any of the client’s information.

2. Working

Cross-website scripting works by controlling a weak website so it returns malevolent JavaScript to users. At the point when the malignant code executes inside a victim’s browser, the assailant can completely bargain their connection with the application. in the event that the web application is defenseless against XSS assaults, the client provided input executes as code. For instance, in the solicitation underneath, the content shows a message box with the content “xss.” 


There are numerous approaches to trigger an XSS assault. For instance, the execution could be set off consequently when the page loads or when a client floats over explicit components of the page (e.g., hyperlinks).

3. Types

  • Stored XSS 

takes place where the noxious content comes from the site’s database. Happens when the pernicious payload is put away in a database. It renders to different clients when information is requested—if there is no yield encoding or sanitization

  • Reflected XSS 

What is reflected cross-site scripting? It Happens when a web application sends assailant-provider strings to a victim’s browser so the browser executes part of the string as code. The payload echoes back accordingly since it doesn’t have any server-side yield encoding.

  • DOM Based XSS

DOM Based XSS is a type of XSS where the whole corrupted data stream from source to sink happens in the browser, i.e., the origin of the data is in the DOM, the sink is likewise in the DOM, and the information stream never leaves the browser. For instance, the source (where vindictive data is perused) could be the URL of the page (e.g., document.location.href), or it very well may be a component of the HTML, and the sink is a sensitive technique consider that causes the execution of the pernicious information (e.g., document. write).”

4. Uses

An assailant who misuses a cross-site scripting weakness is ordinarily ready to: 

  • Imitate or take on the appearance of the victim client. 
  • Complete any activity that the client can perform. 
  • Peruse any information that the client can get to. 
  • Catch the client’s login accreditations. 
  • Perform virtual disfigurement of the site. 
  • Infuse Trojan functionality into the site.

5. Prevention

What is the most effective defense against cross-site scripting attacks?

  • Escaping 

The principal technique you can and should use to keep XSS weaknesses from showing up in your applications is by getting away from user input. Getting away from data implies taking the information an application has gotten and guaranteeing it’s safe prior to delivering it to the end client. By getting away from client input, key characters in the data received by a page will be kept from being deciphered in any pernicious manner. Fundamentally, you’re controlling the information your page gets such that will prohibit the characters – particularly < and > characters – from being delivered, which in any case could harm the application as well as clients.

  • Validating Input

Validating input is the way toward guaranteeing an application is delivering the right information and keeping malignant information from doing damage to the site, database, and clients. While whitelisting and input validation are all the more ordinarily connected with SQL infusion, they can likewise be utilized as an extra strategy for prevention for XSS. While boycotting, or forbidding certain, foreordained characters in client input, denies just known bad characters, whitelisting just permits known good characters and is a superior technique for forestalling XSS assaults just as others.

  • Sanitizing

 A third method to forestall cross-site scripting assaults is to disinfect client input. sanitizing data is solid protection, yet ought not to be utilized alone to fight XSS assaults. It’s absolutely conceivable you’ll discover the need to utilize every one of the three strategies for counteraction in pursuing a safer application. sanitizing client’s input is particularly useful on destinations that permit HTML markup, to guarantee information received can do no damage to clients and your database by scouring the information clean of possibly unsafe markup, changing unsuitable client contribution to an adequate arrangement.

6. Examples

Following are the examples of XSS Attacks

  • Reflective XSS 

There are numerous manners by which an assailant can allure a victim into starting a reflective XSS demand. For instance, the aggressor could send the victim a deceptive email with a link containing noxious JavaScript. In the event that the victim taps on the link, the HTTP demand is started from the casualty’s program and shipped off the weak web application. The noxious JavaScript is then reflected in the victim’s program, where it is executed with regard to the victim client’s meeting.

  • Persistent XSS

Consider a web application that allows users to enter a username that is displayed on each user’s profile page. The application stores each username in a local database. A malicious user notices that the web application fails to sanitize the username field and inputs malicious JavaScript code as part of their username. When other users view the attacker’s profile page, the malicious code automatically executes in the context of their session.


To be genuinely cautious against XSS and other common, weakening weaknesses, similar to the remainder of the OWASP Top 10, it’s imperative to utilize a blend of code audit, robotized static testing during advancement, and dynamic testing once the application is live, what’s more, obviously, to utilizing secure coding rehearses that will help forestall weaknesses like cross-site scripting in any case.

So, have you made up your mind to make a career in Cyber Security? Visit our Master Certificate in Cyber Security (Red Team) for further help. It is the first program in offensive technologies in India and allows learners to practice in a real-time simulated ecosystem, that will give them an edge in this competitive world.

Also Read

Related Articles

Please wait while your application is being created.
Request Callback