/**
* Sanitizes user input for development purposes.
* This function focuses on basic sanitization and is NOT suitable for production environments.
* It's designed to help identify potential issues during development.
*
* @param {string} input The user input to sanitize.
* @returns {string} The sanitized input.
*/
function sanitizeInput(input) {
// 1. Remove HTML tags (basic). More robust libraries exist for production.
input = input.replace(/<[^>]*>/g, "");
// 2. Escape special characters. Important to prevent XSS.
input = input.replace(/&/g, "&");
input = input.replace(/</g, "<");
input = input.replace(/>/g, ">");
input = input.replace(/"/g, """);
input = input.replace(/'/g, "'");
// 3. Limit length to prevent buffer overflows. Adjust as needed.
if (input.length > 255) {
input = input.substring(0, 255);
}
// 4. Remove control characters (e.g., newline, tab). Helps prevent unexpected behavior.
input = input.replace(/\x00/g, ""); //Null character
input = input.replace(/\x09/g, ""); //Tab
input = input.replace(/\x0A/g, ""); //Newline
input = input.replace(/\x0D/g, ""); //Carriage return
// 5. Basic character filtering - remove potentially harmful characters.
input = input.replace(/[`~]/g, ""); //Remove brackets and tildes
input = input.replace(/\\/g, ""); // Remove backslashes
// Return the sanitized input.
return input;
}
Add your comment