function sanitizeInput(input, allowedMethods = [], allowedParams = {}) {
// Basic sanitization: remove potentially harmful characters.
const sanitized = input.replace(/[&<>"']/g, '');
// Allow specific methods if provided.
if (allowedMethods && allowedMethods.length > 0) {
const validMethods = allowedMethods.map(method => method.toLowerCase());
if (!validMethods.includes(input.toLowerCase())) {
return null; // Reject if method is not allowed.
}
}
// Allow specific parameters if provided.
if (allowedParams) {
for (const param in allowedParams) {
if (input.toLowerCase().indexOf(param.toLowerCase()) === -1) {
return null; // Reject if required parameter is missing
}
}
}
// Additional validation/sanitization (example - length)
if (sanitized.length > 255) {
return null; //Reject if too long
}
return sanitized;
}
Add your comment