From 3ef94c00444f2b72a6f68e0fd9cff1b3f783c555 Mon Sep 17 00:00:00 2001
From: Sasha Sorokin <dafri.nochiterov8@gmail.com>
Date: Fri, 10 Jul 2020 03:32:36 +0700
Subject: [PATCH] Improve safety of Blurhash component (#14278)

There was a missed empty hash check. As well as rendering is now wrapped
in try/catch block, so app won't crash if any Blurhash component fails
to render its contents as it's not that critical.
---
 app/javascript/mastodon/components/blurhash.js | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/app/javascript/mastodon/components/blurhash.js b/app/javascript/mastodon/components/blurhash.js
index 172f8c2f5..2af5cfc56 100644
--- a/app/javascript/mastodon/components/blurhash.js
+++ b/app/javascript/mastodon/components/blurhash.js
@@ -6,7 +6,7 @@ import PropTypes from 'prop-types';
 
 /**
  * @typedef BlurhashPropsBase
- * @property {string} hash Hash to render
+ * @property {string?} hash Hash to render
  * @property {number} width
  * Width of the blurred region in pixels. Defaults to 32
  * @property {number} [height]
@@ -37,13 +37,17 @@ function Blurhash({
     const { current: canvas } = canvasRef;
     canvas.width = canvas.width; // resets canvas
 
-    if (dummy) return;
+    if (dummy || !hash) return;
 
-    const pixels = decode(hash, width, height);
-    const ctx = canvas.getContext('2d');
-    const imageData = new ImageData(pixels, width, height);
+    try {
+      const pixels = decode(hash, width, height);
+      const ctx = canvas.getContext('2d');
+      const imageData = new ImageData(pixels, width, height);
 
-    ctx.putImageData(imageData, 0, 0);
+      ctx.putImageData(imageData, 0, 0);
+    } catch (err) {
+      console.error('Blurhash decoding failure', { err, hash });
+    }
   }, [dummy, hash, width, height]);
 
   return (