[gnome-shell] mpris: Validate received data against the expected types from the spec



commit 975280fc50651bd67c3551840cf3026b2d75a33b
Author: Philip Chimento <philip endlessm com>
Date:   Mon Oct 28 17:06:40 2019 -0700

    mpris: Validate received data against the expected types from the spec
    
    In the wild we have buggy clients (notably Chromium 77 and earlier) that
    send metadata with the wrong types. Previously, this would throw an
    exception and prevent the MPRIS information from showing up in the
    message list.
    
    This changes the code to check if any incoming metadata is of the type
    it is expected to be, and logs a warning if not, then continues on with
    a default value.
    
    https://gitlab.gnome.org/GNOME/gnome-shell/issues/1362

 js/ui/mpris.js | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)
---
diff --git a/js/ui/mpris.js b/js/ui/mpris.js
index c46f9f01c5..240e99e5f2 100644
--- a/js/ui/mpris.js
+++ b/js/ui/mpris.js
@@ -95,6 +95,7 @@ var MprisPlayer = class MprisPlayer {
         this._trackArtists = [];
         this._trackTitle = '';
         this._trackCoverUrl = '';
+        this._busName = busName;
     }
 
     get status() {
@@ -177,9 +178,36 @@ var MprisPlayer = class MprisPlayer {
         for (let prop in this._playerProxy.Metadata)
             metadata[prop] = this._playerProxy.Metadata[prop].deep_unpack();
 
-        this._trackArtists = metadata['xesam:artist'] || [_("Unknown artist")];
-        this._trackTitle = metadata['xesam:title'] || _("Unknown title");
-        this._trackCoverUrl = metadata['mpris:artUrl'] || '';
+        // Validate according to the spec; some clients send buggy metadata:
+        // https://www.freedesktop.org/wiki/Specifications/mpris-spec/metadata
+        this._trackArtists = metadata['xesam:artist'];
+        if (!Array.isArray(this._trackArtists) ||
+            !this._trackArtists.every(artist => typeof artist === 'string')) {
+            if (typeof this._trackArtists !== 'undefined')
+                log(`Received faulty track artist metadata from ${
+                    this._busName}; expected an array of strings, got ${
+                    this._trackArtists} (${typeof this._trackArtists})`);
+            this._trackArtists =  [_("Unknown artist")];
+        }
+
+        this._trackTitle = metadata['xesam:title'];
+        if (typeof this._trackTitle !== 'string') {
+            if (typeof this._trackTitle !== 'undefined')
+                log(`Received faulty track title metadata from ${
+                    this._busName}; expected a string, got ${
+                    this._trackTitle} (${typeof this._trackTitle})`);
+            this._trackTitle = _("Unknown title");
+        }
+
+        this._trackCoverUrl = metadata['mpris:artUrl'];
+        if (typeof this._trackCoverUrl !== 'string') {
+            if (typeof this._trackCoverUrl !== 'undefined')
+                log(`Received faulty track cover art metadata from ${
+                    this._busName}; expected a string, got ${
+                    this._trackCoverUrl} (${typeof this._trackCoverUrl})`);
+            this._trackCoverUrl = '';
+        }
+
         this.emit('changed');
 
         let visible = this._playerProxy.CanPlay;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]