Hardening Toastr Notifications: A Guide to Safe Implementation

SecurityForEveryone

S4E.io

17/Dec/24

Toastr is widely used for creating sleek and responsive notifications in web applications. However, its default configuration can unintentionally open the door to security vulnerabilities, particularly Cross-Site Scripting (XSS) attacks. These vulnerabilities arise when user-supplied data—such as notification messages or headers—is not properly sanitized. Left unchecked, this can allow attackers to inject malicious scripts, potentially leading to unauthorized actions or data breaches.

In this guide, we’ll explore the best practices to harden your Toastr implementation, ensuring a secure and robust notification system for your application.

1- Update to the Latest Version

Ensure you are always using the latest version of Toastr. Updates often include important security patches that fix known vulnerabilities.

2- Enable HTML Escaping

To prevent malicious code execution, enable the escapeHtml property in Toastr. This ensures that HTML characters are automatically escaped, neutralizing potential XSS payloads.

toastr.options.escapeHtml = true;

3- Sanitize User Input on the Server-Side

For an added layer of security, sanitize user inputs server-side to ensure no harmful content reaches the front end. Example of a simple sanitization function:


function sanitizeInput(input) {
    const div = document.createElement('div');
    div.textContent = input;
    return div.innerHTML;
}

const userInput = "<script>alert('XSS!')</script>";
const safeInput = sanitizeInput(userInput);
toastr.success(safeInput); // Secure notification

4- Restrict Debug Features in Production

Detailed debug information can expose your application to unnecessary risks. Always disable debug modes and ensure error reporting is minimized in production environments.

toastr.options.debug = false;  

5- Secure Configurations by Default

To prevent Toastr from becoming a potential attack vector:

  • Avoid using untrusted or raw user input in title or message fields.
  • Configure a default timeout for notifications to limit their visibility.
  • Ensure sensitive application data is never displayed in notifications.

What is Toastr ? 

Toastr is a JavaScript library and is used to display notification messages in web applications. It allows you to show various messages such as information, success, error or warning to the user in an elegant and simple way. The main advantage of Toastr is that it is a very lightweight and easy-to-use library.

Features of Toastr:

  • Simple Operation: You can easily add notifications with a single line of JavaScript code.
  • Various Message Types: Supports informational, success, error and warning notifications. Visually distinguishes the type of messages with colors and icons.
  • Customisability: Many features of notifications such as display time, placement, closing behavior can be customized.
  • Browser Support: It works in most modern browsers and is mobile-friendly.

Vulnerabilities in Toastr

  • Toastr is a library that has not been updated for an extended period. Given this, it is advisable to consider alternative libraries. The library contains an XSS vulnerability that affects nearly all versions.
Summary of toastr package vulnerabilities highlighting a high-severity Cross-Site Scripting (XSS) risk affecting all versions, with detailed breakdowns of package versions and their respective vulnerability statuses. Crucial for understanding security risks in JavaScript libraries.
  • To learn more about the XSS vulnerability affecting all versions of Toastr, visit this security report.
cyber security services for everyone one. Free security tools, continuous vulnerability scanning and many more.
Try it yourself,
control security posture