Bläddra i källkod

[frontend] Fix deXSS of falsy values

Johan Ahlen 4 år sedan
förälder
incheckning
1c69549dca

+ 12 - 0
desktop/core/src/desktop/js/utils/hueUtils.test.ts

@@ -44,6 +44,18 @@ describe('hue.utils.js', () => {
     expect(hueUtils.deXSS(undefined)).toEqual('');
   });
 
+  it('should return false when the value is false', () => {
+    expect(hueUtils.deXSS(false)).toEqual('false');
+  });
+
+  it('should return 0 when the value is 0', () => {
+    expect(hueUtils.deXSS(0)).toEqual('0');
+  });
+
+  it('should return null when the value is null', () => {
+    expect(hueUtils.deXSS(null)).toEqual('null');
+  });
+
   it('should remove JS code from a string', () => {
     expect(hueUtils.deXSS('hello <script>alert(123)</script>world')).toEqual('hello world');
   });

+ 2 - 1
desktop/core/src/desktop/js/utils/hueUtils.ts

@@ -303,7 +303,8 @@ export const logError = (error: unknown): void => {
 export const equalIgnoreCase = (a?: string, b?: string): boolean =>
   !!a && !!b && a.toLowerCase() === b.toLowerCase();
 
-export const deXSS = (str?: string): string => (str && sanitizeHtml(str)) || '';
+export const deXSS = (str?: string | number | null): string =>
+  (typeof str !== 'undefined' && sanitizeHtml(str as string)) || '';
 
 export const getStyleFromCSSClass = (
   cssClass: string