Fix crash on malformed blurhash (#2331)
authorAjay Bura <32841439+ajbura@users.noreply.github.com>
Wed, 21 May 2025 11:58:13 +0000 (17:28 +0530)
committerGitHub <noreply@github.com>
Wed, 21 May 2025 11:58:13 +0000 (17:28 +0530)
src/app/components/message/content/ImageContent.tsx
src/app/components/message/content/VideoContent.tsx
src/app/utils/blurHash.ts

index 69c7ade8e51a6f00c20d070e74b8d1266f3c3966..cc0c0c914e951ae226df6b2da2dd2d41e9cf127b 100644 (file)
@@ -30,6 +30,7 @@ import { stopPropagation } from '../../../utils/keyboard';
 import { decryptFile, downloadEncryptedMedia, mxcUrlToHttp } from '../../../utils/matrix';
 import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
 import { ModalWide } from '../../../styles/Modal.css';
+import { validBlurHash } from '../../../utils/blurHash';
 
 type RenderViewerProps = {
   src: string;
@@ -77,7 +78,7 @@ export const ImageContent = as<'div', ImageContentProps>(
   ) => {
     const mx = useMatrixClient();
     const useAuthentication = useMediaAuthentication();
-    const blurHash = info?.[MATRIX_BLUR_HASH_PROPERTY_NAME];
+    const blurHash = validBlurHash(info?.[MATRIX_BLUR_HASH_PROPERTY_NAME]);
 
     const [load, setLoad] = useState(false);
     const [error, setError] = useState(false);
index f6ddbb5a5beb531cce4e34be5435ea17bd581062..0505f204e9013fea55acced42eb07f9cd827d2ef 100644 (file)
@@ -31,6 +31,7 @@ import {
   mxcUrlToHttp,
 } from '../../../utils/matrix';
 import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
+import { validBlurHash } from '../../../utils/blurHash';
 
 type RenderVideoProps = {
   title: string;
@@ -68,7 +69,7 @@ export const VideoContent = as<'div', VideoContentProps>(
   ) => {
     const mx = useMatrixClient();
     const useAuthentication = useMediaAuthentication();
-    const blurHash = info.thumbnail_info?.[MATRIX_BLUR_HASH_PROPERTY_NAME];
+    const blurHash = validBlurHash(info.thumbnail_info?.[MATRIX_BLUR_HASH_PROPERTY_NAME]);
 
     const [load, setLoad] = useState(false);
     const [error, setError] = useState(false);
index 3fe1ade0e7f8ffc635a9877f3e5724637dae782d..566f6d189872c6a332a535b8b4660f35d1e2db56 100644 (file)
@@ -1,4 +1,4 @@
-import { encode } from 'blurhash';
+import { encode, isBlurhashValid } from 'blurhash';
 
 export const encodeBlurHash = (
   img: HTMLImageElement | HTMLVideoElement,
@@ -17,3 +17,13 @@ export const encodeBlurHash = (
   const data = context.getImageData(0, 0, canvas.width, canvas.height);
   return encode(data.data, data.width, data.height, 4, 4);
 };
+
+export const validBlurHash = (hash?: string): string | undefined => {
+  if (typeof hash === 'string') {
+    const validity = isBlurhashValid(hash);
+
+    return validity.result ? hash : undefined;
+  }
+
+  return undefined;
+};