]> git.proxmox.com Git - pve-cluster.git/commitdiff
pmxcfs: get config property: escape double quote and backslash
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 13 Jun 2019 07:01:59 +0000 (09:01 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 13 Jun 2019 07:22:46 +0000 (09:22 +0200)
This are the most relevant from as the bite JSON really and can in
theory be in our configuration property values.

While technically a literal \t, \b, \f, \r (but not \n) can be an
issue too, this values normally really do not get written into the
config by our stack, if it has been manually added, but that's off
limits. If we really need it we can add it always in the future

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
data/src/status.c

index 5957e67b03ace7cf95200118c9b354730e622cee..b197e615028a4a681411bf7ac7a6c0fdc7c93439 100644 (file)
@@ -836,6 +836,21 @@ next:
        return NULL; // not found
 }
 
+static void
+_g_str_append_kv_jsonescaped(GString *str, const char *k, const char *v)
+{
+       g_string_append_printf(str, "\"%s\": \"", k);
+
+       for (; *v; v++) {
+               if (*v == '\\' || *v == '"') {
+                       g_string_append_c(str, '\\');
+               }
+               g_string_append_c(str, *v);
+       }
+
+       g_string_append_c(str, '"');
+}
+
 int
 cfs_create_guest_conf_property_msg(GString *str, memdb_t *memdb, const char *prop, uint32_t vmid)
 {
@@ -870,7 +885,9 @@ cfs_create_guest_conf_property_msg(GString *str, memdb_t *memdb, const char *pro
                char *val = _get_property_value(tmp, prop, prop_len);
                if (val == NULL) goto ret;
 
-               g_string_append_printf(str, "\"%u\": { \"%s\": \"%s\"\n }", vmid, prop, val);
+               g_string_append_printf(str, "\"%u\":{", vmid);
+               _g_str_append_kv_jsonescaped(str, prop, val);
+               g_string_append_c(str, '}');
 
        } else {
                GHashTableIter iter;
@@ -894,7 +911,9 @@ cfs_create_guest_conf_property_msg(GString *str, memdb_t *memdb, const char *pro
                        if (!first) g_string_append_printf(str, ",\n");
                        else first = 0;
 
-                       g_string_append_printf(str, "\"%u\": {\"%s\": \"%s\"}", vminfo->vmid, prop, val);
+                       g_string_append_printf(str, "\"%u\":{", vminfo->vmid);
+                       _g_str_append_kv_jsonescaped(str, prop, val);
+                       g_string_append_c(str, '}');
                }
        }
 ret: