diff options
author | Richard Hughes <richard@hughsie.com> | 2017-06-27 15:42:22 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2017-06-27 15:42:22 +0100 |
commit | 5aacd135b45a3256200c4381ffff88a13c118ff1 (patch) | |
tree | 2c3b00dc7e0f18eac7e096ce3db795a01ae1671f | |
parent | e11abd151fd6c7931f96b42a43f9f2a979a51da5 (diff) | |
download | appstream-glib-5aacd135b45a3256200c4381ffff88a13c118ff1.tar.gz |
Validate the <id> format according to the spec
-rw-r--r-- | data/tests/broken.appdata.xml | 2 | ||||
-rw-r--r-- | libappstream-glib/as-app-validate.c | 35 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 6 |
3 files changed, 40 insertions, 3 deletions
diff --git a/data/tests/broken.appdata.xml b/data/tests/broken.appdata.xml index bc8f1b5..126d419 100644 --- a/data/tests/broken.appdata.xml +++ b/data/tests/broken.appdata.xml @@ -1,6 +1,6 @@ <!-- Author: 2013 Richard Hughes <richard@hughsie.com> --> <application> - <id type="unknown">gnome-power-statistics</id> + <id type="unknown">gnome-power-{statistics}</id> <metadata_license>GPLv3+</metadata_license> <metadata_license>BSD</metadata_license> <project_license>CC1</project_license> diff --git a/libappstream-glib/as-app-validate.c b/libappstream-glib/as-app-validate.c index a055752..8cdf1df 100644 --- a/libappstream-glib/as-app-validate.c +++ b/libappstream-glib/as-app-validate.c @@ -1080,6 +1080,40 @@ as_app_validate_helper_free (AsAppValidateHelper *helper) G_DEFINE_AUTOPTR_CLEANUP_FUNC(AsAppValidateHelper, as_app_validate_helper_free); +static gboolean +as_app_validate_check_id_char (const gchar c) +{ + const gchar valid[] = { '-', '_', '.', '\0' }; + for (guint i = 0; valid[i] != '\0'; i++) { + if (valid[i] == c) + return TRUE; + } + return g_ascii_isalnum (c); +} + +static void +as_app_validate_check_id (AsAppValidateHelper *helper, const gchar *id) +{ + /* check valid */ + if (id == NULL) { + ai_app_validate_add (helper, + AS_PROBLEM_KIND_MARKUP_INVALID, + "<id> is not set"); + return; + } + + /* check contains permitted chars */ + for (guint i = 0; id[i] != '\0'; i++) { + if (!as_app_validate_check_id_char (id[i])) { + ai_app_validate_add (helper, + AS_PROBLEM_KIND_MARKUP_INVALID, + "<id> has invalid character [%c]", + id[i]); + break; + } + } +} + /** * as_app_validate: * @app: a #AsApp instance. @@ -1232,6 +1266,7 @@ as_app_validate (AsApp *app, AsAppValidateFlags flags, GError **error) AS_PROBLEM_KIND_MARKUP_INVALID, "<id> does not have correct extension for kind"); } + as_app_validate_check_id (helper, id); /* metadata_license */ license = as_app_get_metadata_license (app); diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 3df66f6..b3f043b 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -2190,13 +2190,15 @@ as_test_app_validate_file_bad_func (void) "<release> timestamp is in the future"); as_test_app_validate_check (probs, AS_PROBLEM_KIND_TAG_MISSING, "<content_rating> required for game"); - g_assert_cmpint (probs->len, ==, 35); + as_test_app_validate_check (probs, AS_PROBLEM_KIND_MARKUP_INVALID, + "<id> has invalid character"); + g_assert_cmpint (probs->len, ==, 36); /* again, harder */ probs2 = as_app_validate (app, AS_APP_VALIDATE_FLAG_STRICT, &error); as_test_app_validate_check (probs2, AS_PROBLEM_KIND_TAG_INVALID, "XML data contains unknown tag"); - g_assert_cmpint (probs2->len, ==, 41); + g_assert_cmpint (probs2->len, ==, 42); } static void |