1. /**
  2. * Sanitizes user input for development purposes.
  3. * This function focuses on basic sanitization and is NOT suitable for production environments.
  4. * It's designed to help identify potential issues during development.
  5. *
  6. * @param {string} input The user input to sanitize.
  7. * @returns {string} The sanitized input.
  8. */
  9. function sanitizeInput(input) {
  10. // 1. Remove HTML tags (basic). More robust libraries exist for production.
  11. input = input.replace(/<[^>]*>/g, "");
  12. // 2. Escape special characters. Important to prevent XSS.
  13. input = input.replace(/&/g, "&amp;");
  14. input = input.replace(/</g, "&lt;");
  15. input = input.replace(/>/g, "&gt;");
  16. input = input.replace(/"/g, "&quot;");
  17. input = input.replace(/'/g, "&#039;");
  18. // 3. Limit length to prevent buffer overflows. Adjust as needed.
  19. if (input.length > 255) {
  20. input = input.substring(0, 255);
  21. }
  22. // 4. Remove control characters (e.g., newline, tab). Helps prevent unexpected behavior.
  23. input = input.replace(/\x00/g, ""); //Null character
  24. input = input.replace(/\x09/g, ""); //Tab
  25. input = input.replace(/\x0A/g, ""); //Newline
  26. input = input.replace(/\x0D/g, ""); //Carriage return
  27. // 5. Basic character filtering - remove potentially harmful characters.
  28. input = input.replace(/[`~]/g, ""); //Remove brackets and tildes
  29. input = input.replace(/\\/g, ""); // Remove backslashes
  30. // Return the sanitized input.
  31. return input;
  32. }

Add your comment