diff options
author | Richard Hughes <richard@hughsie.com> | 2016-08-17 20:25:12 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2016-08-17 20:30:25 +0100 |
commit | 2ec31bac7bd77e7b39c502276f4acec21ed9d576 (patch) | |
tree | 9a0b21c53e41e6dbe755e40c280ee9c7444ddf41 | |
parent | e35d8320711a2e9f0de5f042d95527823a814191 (diff) | |
download | appstream-glib-2ec31bac7bd77e7b39c502276f4acec21ed9d576.tar.gz |
Sanity check YAML files before parsing
It seems as_node_yaml_process_layer() just spins if you pass it data with an
invalid header. If we can't trust libyaml, try to check the first line and
return an error if it doesn't match what we expect.
Works around https://bugs.launchpad.net/ubuntu/+source/fwupd/+bug/1591868
although the real bugs lie both in libyaml for spinning forever on invalid
input, *and* whatever project wrote that invalid DEP-11 file.
-rw-r--r-- | libappstream-glib/as-self-test.c | 18 | ||||
-rw-r--r-- | libappstream-glib/as-yaml.c | 15 |
2 files changed, 33 insertions, 0 deletions
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index e824de4..8c33406 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -4377,6 +4377,22 @@ as_test_store_metadata_index_func (void) } static void +as_test_yaml_broken_func (void) +{ +#if AS_BUILD_DEP11 + g_autoptr(AsYaml) node = NULL; + g_autoptr(GError) error = NULL; + node = as_yaml_from_data ("s---\n" + "File: DEP-11\n", + -1, &error); + g_assert_error (error, AS_NODE_ERROR, AS_NODE_ERROR_INVALID_MARKUP); + g_assert (node == NULL); +#else + g_test_skip ("Compiled without YAML (DEP-11) support"); +#endif +} + +static void as_test_yaml_func (void) { #if AS_BUILD_DEP11 @@ -4410,6 +4426,7 @@ as_test_yaml_func (void) /* simple list */ node = as_yaml_from_data ( + "---\n" "Mimetypes:\n" " - text/html\n" " - text/xml\n" @@ -5263,6 +5280,7 @@ main (int argc, char **argv) g_test_add_func ("/AppStream/monitor{file}", as_test_monitor_file_func); } g_test_add_func ("/AppStream/yaml", as_test_yaml_func); + g_test_add_func ("/AppStream/yaml{broken}", as_test_yaml_broken_func); g_test_add_func ("/AppStream/store", as_test_store_func); g_test_add_func ("/AppStream/store{unique}", as_test_store_unique_func); g_test_add_func ("/AppStream/store{merge}", as_test_store_merge_func); diff --git a/libappstream-glib/as-yaml.c b/libappstream-glib/as-yaml.c index b1b486d..e0c9c3f 100644 --- a/libappstream-glib/as-yaml.c +++ b/libappstream-glib/as-yaml.c @@ -272,6 +272,21 @@ as_yaml_from_data (const gchar *data, gssize data_len, GError **error) AsNode *node = NULL; #if AS_BUILD_DEP11 yaml_parser_t parser; + g_autofree gchar *prefix = NULL; + + /* sanity check */ + prefix = g_strndup (data, 64); + g_strdelimit (prefix, "\n", '\0'); + if (!g_str_has_prefix (prefix, "---") && + !g_str_has_prefix (prefix, "#") && + !g_str_has_prefix (prefix, "File: ")) { + g_set_error (error, + AS_NODE_ERROR, + AS_NODE_ERROR_INVALID_MARKUP, + "YAML prefix invalid: %s expected '---' or '#'", + prefix); + return NULL; + } /* parse */ yaml_parser_initialize (&parser); |