function sanitizeCollectionInput(collection, allowedMethods = [], overrides = {}) {
if (!Array.isArray(collection)) {
return null; // Or throw an error, depending on desired behavior
}
const sanitizedCollection = [...collection]; // Create a copy to avoid modifying the original
for (let i = 0; i < sanitizedCollection.length; i++) {
const item = sanitizedCollection[i];
if (typeof item === 'object' && item !== null) {
// Handle objects (dictionaries/maps)
for (const key in item) {
if (item.hasOwnProperty(key)) {
const value = item[key];
if (typeof value === 'string') {
//Sanitize string values
const sanitizedValue = value.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&"); //Basic HTML entity encoding
item[key] = sanitizedValue;
}
//Allow specific methods
if (!allowedMethods.includes(key) && !overrides[key]) {
delete item[key]; //Remove unknown/disallowed properties
}
}
}
}
}
return sanitizedCollection;
}
export default sanitizeCollectionInput;
Add your comment