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 d5a1fac commit da92e3c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
6 changes: 3 additions & 3 deletions docs/xml/metainfo-component.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@
<important>
<title>Escaping characters in the component ID</title>
<para>
To ensures the greatest possible compatibility of an AppStream ID, it is recommended to replace any hyphens in the ID with underscores, and prefix every leading digit of
a section with an underscore as well. Since the underscore is not a valid character in domain names, the uniqueness of the ID is kept.
For example, the ID <code>org.7-zip.7zip</code> could become <code>org._7_zip._7zip</code>.
To ensures the greatest possible compatibility of an AppStream ID, it is recommended to replace any hyphens in the ID in all but the last segment of it with underscores,
and prefix every leading digit of a segment with an underscore as well. Since the underscore is not a valid character in domain names, the uniqueness of the ID is kept.
For example, the ID <code>org.7-zip.7-zip</code> could become <code>org._7_zip._7-zip</code>.
</para>
</important>

Expand Down
8 changes: 4 additions & 4 deletions src/as-validator-issue-tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,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
29 changes: 21 additions & 8 deletions src/as-validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,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 @@ -1142,7 +1143,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,
Expand Down Expand Up @@ -1183,12 +1185,6 @@ as_validator_validate_component_id (AsValidator *validator, xmlNode *idnode, AsC
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,
Expand All @@ -1198,6 +1194,8 @@ as_validator_validate_component_id (AsValidator *validator, xmlNode *idnode, AsC

/* 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,
Expand All @@ -1206,7 +1204,22 @@ as_validator_validate_component_id (AsValidator *validator, xmlNode *idnode, AsC
cid,
cid_parts[i],
cid_parts[i]);
break;
}

/* 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
16 changes: 10 additions & 6 deletions tests/test-validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,24 @@ 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",
"cid-contains-uppercase-letter", "invalid.7-bad-ID.app",
7, AS_ISSUE_SEVERITY_PEDANTIC,
},
{
"cid-has-number-prefix", "7-bad-ID: 7-bad-ID → _7-bad-ID",
"cid-has-number-prefix", "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+",
Expand Down

0 comments on commit da92e3c

Please sign in to comment.