1. function sanitizeInput(input, allowedMethods = [], allowedParams = {}) {
  2. // Basic sanitization: remove potentially harmful characters.
  3. const sanitized = input.replace(/[&<>"']/g, '');
  4. // Allow specific methods if provided.
  5. if (allowedMethods && allowedMethods.length > 0) {
  6. const validMethods = allowedMethods.map(method => method.toLowerCase());
  7. if (!validMethods.includes(input.toLowerCase())) {
  8. return null; // Reject if method is not allowed.
  9. }
  10. }
  11. // Allow specific parameters if provided.
  12. if (allowedParams) {
  13. for (const param in allowedParams) {
  14. if (input.toLowerCase().indexOf(param.toLowerCase()) === -1) {
  15. return null; // Reject if required parameter is missing
  16. }
  17. }
  18. }
  19. // Additional validation/sanitization (example - length)
  20. if (sanitized.length > 255) {
  21. return null; //Reject if too long
  22. }
  23. return sanitized;
  24. }

Add your comment