Skip to content

Commit

Permalink
Allow hyphens in the last segment of a component-ID
Browse files Browse the repository at this point in the history
Resolves: #547
  • Loading branch information
ximion committed Nov 2, 2023
1 parent b9f2314 commit 6211f54
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/as-validator-issue-tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ AsValidatorIssueTag as_validator_issue_tag_list[] = {
N_("The component ID starts with punctuation. This is not allowed.")
},

{ "cid-contains-hyphen",
AS_ISSUE_SEVERITY_INFO,
N_("The component ID contains a hyphen/minus. Using a hyphen is strongly discouraged, to improve interoperability with other tools such as D-Bus. "
"A good option is to replace any hyphens with underscores (`_`).")
{ "cid-rdns-contains-hyphen",
AS_ISSUE_SEVERITY_WARNING,
N_("The component ID contains a hyphen/minus in its domain part. Using a hyphen is strongly discouraged to improve interoperability with other tools such as D-Bus. "
"A good option is to replace any hyphens with underscores (`_`). Hypens are only allowed in the last segment of a component ID.")
},

{ "cid-has-number-prefix",
Expand Down
34 changes: 25 additions & 9 deletions src/as-validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ static void
as_validator_validate_component_id (AsValidator *validator, xmlNode *idnode, AsComponent *cpt)
{
g_auto(GStrv) cid_parts = NULL;
guint cid_parts_n;
gboolean hyphen_found = FALSE;
g_autofree gchar *cid = as_xml_get_node_value_raw (idnode);
g_return_if_fail (cid != NULL);
Expand All @@ -1122,7 +1123,8 @@ as_validator_validate_component_id (AsValidator *validator, xmlNode *idnode, AsC
return;

cid_parts = g_strsplit (cid, ".", -1);
if (g_strv_length (cid_parts) < 3) {
cid_parts_n = g_strv_length (cid_parts);
if (cid_parts_n < 3) {
if (as_component_get_kind (cpt) == AS_COMPONENT_KIND_DESKTOP_APP) {
/* since the ID and .desktop-file-id are tied together, we can't make this an error for desktop apps */
as_validator_add_issue (validator, idnode, "cid-desktopapp-is-not-rdns", cid);
Expand Down Expand Up @@ -1159,23 +1161,37 @@ as_validator_validate_component_id (AsValidator *validator, xmlNode *idnode, AsC
"%s: '%s'", cid, c);
}

if (!hyphen_found && cid[i] == '-') {
hyphen_found = TRUE;
/* a hyphen in the ID is bad news, because we can't use the ID on DBus and it also clashes with other naming schemes */
as_validator_add_issue (validator, idnode, "cid-contains-hyphen", cid);
}

if (g_ascii_isalpha (cid[i]) && g_ascii_isupper (cid[i]))
as_validator_add_issue (validator, idnode, "cid-contains-uppercase-letter", cid);
}

/* check if any segment starts with a number */
for (guint i = 0; cid_parts[i] != NULL; i++) {
gboolean is_last_segment = cid_parts[i + 1] == NULL;

if (g_ascii_isdigit (cid_parts[i][0])) {
as_validator_add_issue (validator, idnode,
"cid-has-number-prefix",
"%s: %s → _%s", cid, cid_parts[i], cid_parts[i]);
break;
"%s: %s → _%s",
cid,
cid_parts[i],
cid_parts[i]);
}

/* check for hyphens in anything but the last segment of an ID, and only if we have
* a rDNS-style ID */
if (is_last_segment || hyphen_found || cid_parts_n < 3)
continue;
for (guint j = 0; cid_parts[i][j] != '\0'; j++) {
if (cid_parts[i][j] == '-') {
hyphen_found = TRUE;
/* a hyphen in the ID is bad news, because we can't use the ID on DBus and it also clashes with other naming schemes */
as_validator_add_issue (validator,
idnode,
"cid-rdns-contains-hyphen",
cid);
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/samples/validate_many-errors-desktopapp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
to contain a lot of issues for the validator to catch -->

<component type="desktop-application">
<id>7-bad-ID</id>
<id>invalid.7-bad-ID.app</id>
<metadata_license>GPL-2.0+</metadata_license>
<project_license>weird</project_license>

Expand Down
20 changes: 12 additions & 8 deletions tests/test-validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,25 @@ test_validator_manyerrors_desktopapp (void)
"", -1,
AS_ISSUE_SEVERITY_ERROR,
},
{ "cid-contains-hyphen",
"7-bad-ID", 7,
AS_ISSUE_SEVERITY_INFO,
{ "cid-domain-not-lowercase",
"invalid.7-bad-ID.app", 7,
AS_ISSUE_SEVERITY_ERROR,
},
{ "cid-rdns-contains-hyphen",
"invalid.7-bad-ID.app", 7,
AS_ISSUE_SEVERITY_WARNING,
},
{ "cid-contains-uppercase-letter",
"7-bad-ID", 7,
"invalid.7-bad-ID.app", 7,
AS_ISSUE_SEVERITY_PEDANTIC,
},
{ "cid-has-number-prefix",
"7-bad-ID: 7-bad-ID → _7-bad-ID", 7,
"invalid.7-bad-ID.app: 7-bad-ID → _7-bad-ID", 7,
AS_ISSUE_SEVERITY_INFO,
},
{ "cid-desktopapp-is-not-rdns",
"7-bad-ID", 7,
AS_ISSUE_SEVERITY_WARNING,
{ "cid-maybe-not-rdns",
"invalid.7-bad-ID.app", 7,
AS_ISSUE_SEVERITY_INFO,
},
{ "metadata-license-invalid",
"GPL-2.0+", 8,
Expand Down

0 comments on commit 6211f54

Please sign in to comment.