Skip to content

Security Cipher

  • Home
  • Blog
  • About Us
  • Resources
    • Security Terminologies
    • Vulnerability Explain
    • Secure Code Explain
    • Linux Commands
    • AWS Cloud Security Checklist
  • My Resume
Contact Us

Secure Code Explain

  • DOM Cross-Site-Scripting (XSS)
  • Stored Cross-Site-Scripting (XSS)
  • Reflected Cross-Site-Scripting (XSS)
  • SQL Injection
  • XXE Injection
  • Remote File Inclusion (RFI)
  • Local File Inclusion
  • Clickjacking
  • Remote Code Execution (RCE)
  • Insecure direct object references (IDOR)
  • Secure Cookie not set
  • Log Injection
  • Cross-Site Request Forgery (CSRF)
  • HttpOnly Flag not set

Vulnerability Explain

  • SQL Injection
  • Cross-Site-Scripting (XSS)
  • Home
  • Secure Code Explain
  • Stored Cross-Site-Scripting (XSS)

Stored Cross-Site-Scripting (XSS)

Table of Contents
  • 🥺 Vulnerable Code
  • 😎 Secure Code

Here is an example of vulnerable code for a profile update page that is susceptible to stored cross-site scripting (XSS) attacks:

🥺 Vulnerable Code #

public class ProfileServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
String bio = request.getParameter("bio");
String website = request.getParameter("website");

User user = new User();
user.setName(name);
user.setBio(bio);
user.setWebsite(website);
// Save the user profile
saveUserProfile(user); 

// Redirect to the profile page 
response.sendRedirect("/profile");

}
}

This code allows a user to update their profile by submitting a form with their name, bio, and website. However, it does not properly sanitize the user input, making it vulnerable to XSS attacks.

An attacker could exploit this vulnerability by submitting a form with malicious JavaScript code in the “name“, “bio“, or “website” field. When the form is submitted, the malicious code would be stored in the database and displayed on the user’s profile page, potentially allowing the attacker to execute arbitrary JavaScript in the context of the user’s browser.

😎 Secure Code #

To secure this code against XSS attacks, we can sanitize the user input by escaping special characters in the user input. Here is an example of how the code could be modified to do this:

public class ProfileServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {

String name = request.getParameter("name");
String bio = request.getParameter("bio");
String website = request.getParameter("website");

// Sanitize user input
name = sanitizeInput(name);
bio = sanitizeInput(bio);
website = sanitizeInput(website);

User user = new User();
user.setName(name);
user.setBio(bio);
user.setWebsite(website);

// Save the user profile
saveUserProfile(user);

// Redirect to the profile page
response.sendRedirect("/profile");
}
private String sanitizeInput(String input) {
return input.replaceAll("<", "<").replaceAll(">", ">");
}
}

This code sanitizes the user input by replacing the “<” and “>” characters with their HTML escape codes, which prevents them from being interpreted as HTML tags. This effectively neutralizes any malicious code submitted by the user and makes the code more secure against XSS attacks.

Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
DOM Cross-Site-Scripting (XSS)Reflected Cross-Site-Scripting (XSS)

Leave a Reply Cancel reply

Table of Contents
  • 🥺 Vulnerable Code
  • 😎 Secure Code
logo name

Learn penetration Testing …

Instagram Twitter Youtube Telegram Linkedin

Useful Links

Blogs
About
Contact

Recent Post

  • Threat Modeling : Everything You Need to Know for Web Application Security
  • How to Conduct a Successful Penetration Test: A Step-by-Step Guide
  • OSV-Scanner: Protecting Your Open-Source Dependencies

Subscribe Now

Don’t miss our future updates! Get Subscribed Today!

Subscription Form

By entering your email, you agree to our terms & Conditions and Privacy policy.

©2023. Security Cipher. All Rights Reserved.

Privacy Policies
Terms & Conditions