Hardening Toastr Notifications: A Guide to Safe Implementation
S4E.io
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.

- To learn more about the XSS vulnerability affecting all versions of Toastr, visit this security report.
control security posture