1. function sanitizeCollectionInput(collection, allowedMethods = [], overrides = {}) {
  2. if (!Array.isArray(collection)) {
  3. return null; // Or throw an error, depending on desired behavior
  4. }
  5. const sanitizedCollection = [...collection]; // Create a copy to avoid modifying the original
  6. for (let i = 0; i < sanitizedCollection.length; i++) {
  7. const item = sanitizedCollection[i];
  8. if (typeof item === 'object' && item !== null) {
  9. // Handle objects (dictionaries/maps)
  10. for (const key in item) {
  11. if (item.hasOwnProperty(key)) {
  12. const value = item[key];
  13. if (typeof value === 'string') {
  14. //Sanitize string values
  15. const sanitizedValue = value.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/&/g, "&amp;"); //Basic HTML entity encoding
  16. item[key] = sanitizedValue;
  17. }
  18. //Allow specific methods
  19. if (!allowedMethods.includes(key) && !overrides[key]) {
  20. delete item[key]; //Remove unknown/disallowed properties
  21. }
  22. }
  23. }
  24. }
  25. }
  26. return sanitizedCollection;
  27. }
  28. export default sanitizeCollectionInput;

Add your comment