[rygel] server: Validate passed dates



commit 950b0b1fda8c227a86c9a1985da02f94687ec836
Author: Jens Georg <mail jensge org>
Date:   Sat Jun 28 14:32:02 2014 +0200

    server: Validate passed dates
    
    Signed-off-by: Jens Georg <mail jensge org>

 src/librygel-server/rygel-item-updater.vala |   64 +++++++++++++++++++++++++++
 1 files changed, 64 insertions(+), 0 deletions(-)
---
diff --git a/src/librygel-server/rygel-item-updater.vala b/src/librygel-server/rygel-item-updater.vala
index d8506dc..95ea52e 100644
--- a/src/librygel-server/rygel-item-updater.vala
+++ b/src/librygel-server/rygel-item-updater.vala
@@ -29,6 +29,8 @@ using Gee;
  * UpdateObject action implementation.
  */
 internal class Rygel.ItemUpdater: GLib.Object, Rygel.StateMachine {
+    public const string QN_UPNP_DATE = "dc:date";
+
     private static Regex escape_regex;
 
     private string object_id;
@@ -117,6 +119,60 @@ internal class Rygel.ItemUpdater: GLib.Object, Rygel.StateMachine {
         return tag_values;
     }
 
+    // Check if the date is not in the recommended format
+    private static void check_date_tag (string [] current_tag,
+                                        string [] new_tag) throws Error {
+        int date_index = -1;
+
+        // Find the current tag index and
+        // check the validity of the new tag in the same index
+        foreach (unowned string cur_str in current_tag) {
+            date_index++;
+            if (cur_str.index_of (QN_UPNP_DATE) != -1) {
+                var date_val = new_tag[date_index].split ("</")[0]
+                                                  .split (">")[1]._strip ();
+                ItemUpdater.check_date (date_val);
+
+                break;
+            }
+        }
+
+        date_index = -1;
+        // If the current tag does not then search new tag for dc:date
+        foreach (unowned string new_str in new_tag) {
+            date_index++;
+            if (new_str.index_of (QN_UPNP_DATE) != -1) {
+                var date_val = new_tag[date_index].split ("</")[0]
+                                                  .split (">")[1]._strip ();
+                ItemUpdater.check_date (date_val);
+
+                break;
+            }
+        }
+    }
+
+    // Same logic used in object-creator class
+    private static void check_date (string date_value) throws Error {
+        int year = 0, month = 0, day = 0;
+        if (date_value.scanf ("%4d-%02d-%02d",
+                                  out year,
+                                  out month,
+                                  out day) != 3) {
+            throw new ContentDirectoryError.INVALID_NEW_TAG_VALUE
+                                    (_("Invalid date format: %s"),
+                                     date_value);
+        }
+
+        var date = GLib.Date ();
+        date.set_dmy ((DateDay) day, (DateMonth) month, (DateYear) year);
+
+        if (!date.valid ()) {
+            throw new ContentDirectoryError.INVALID_NEW_TAG_VALUE
+                                    (_("Invalid date: %s"),
+                                     date_value);
+        }
+    }
+
     private static LinkedList<string> csv_split (string? tag_values) {
         var list = new LinkedList<string> ();
 
@@ -178,6 +234,14 @@ internal class Rygel.ItemUpdater: GLib.Object, Rygel.StateMachine {
         var media_object = yield this.fetch_object ();
         var current_list = csv_split (this.current_tag_value);
         var new_list = csv_split (this.new_tag_value);
+
+        // If the size is not equal it will be handled downstream and
+        // different error will be thrown
+        if (current_list.size == new_list.size) {
+            ItemUpdater.check_date_tag (current_list.to_array (),
+                                        new_list.to_array ());
+        }
+
         var result = yield media_object.apply_fragments
                                         (current_list,
                                          new_list,


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