Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
fix(core): Improved null check for sanitizer.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheller committed Feb 17, 2022
1 parent c87af35 commit b14bbac
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions libs/barista-components/core/src/util/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import { SecurityContext } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { isObject } from './type-util';

/** Sanitizes a nested object or string from malicious html code */
export const sanitize = <T extends {} | string>(
Expand All @@ -25,16 +26,18 @@ export const sanitize = <T extends {} | string>(
return sanitizer.sanitize(SecurityContext.HTML, option) as T;
}

Object.keys(option).forEach((key) => {
if (typeof option[key] === 'string') {
option[key] = sanitizer.sanitize(SecurityContext.HTML, option[key]);
} else if (Array.isArray(option[key])) {
option[key].forEach((item, i) => {
option[key][i] = sanitize(item, sanitizer);
});
} else if (typeof option[key] === 'object') {
option[key] = sanitize(option[key], sanitizer);
}
});
if (option && isObject(option)) {
Object.keys(option).forEach((key) => {
if (typeof option[key] === 'string') {
option[key] = sanitizer.sanitize(SecurityContext.HTML, option[key]);
} else if (Array.isArray(option[key])) {
option[key].forEach((item, i) => {
option[key][i] = sanitize(item, sanitizer);
});
} else if (typeof option[key] === 'object') {
option[key] = sanitize(option[key], sanitizer);
}
});
}
return option;
};

0 comments on commit b14bbac

Please sign in to comment.