Skip to content

Commit

Permalink
app-info: Add a function to validate an app id is a sub-app-id
Browse files Browse the repository at this point in the history
For example, for flatpak, the sub-app-id must be a valid app id in
itself and also have the app-id as a prefix, with a dot as separator.
Host apps don't have any restrictions.

The flatpak_is_valid_name function has been copied from the flatpak
project.
  • Loading branch information
swick authored and GeorgesStavracas committed Oct 4, 2024
1 parent 659e4e3 commit 1082e97
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
128 changes: 128 additions & 0 deletions src/xdp-app-info-flatpak.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,134 @@ struct _XdpAppInfoFlatpak

G_DEFINE_FINAL_TYPE (XdpAppInfoFlatpak, xdp_app_info_flatpak, XDP_TYPE_APP_INFO)

static gboolean
is_valid_initial_name_character (gint c, gboolean allow_dash)
{
return
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c == '_') || (allow_dash && c == '-');
}

static gboolean
is_valid_name_character (gint c, gboolean allow_dash)
{
return
is_valid_initial_name_character (c, allow_dash) ||
(c >= '0' && c <= '9');
}

static const char *
find_last_char (const char *str, gsize len, int c)
{
const char *p = str + len - 1;
while (p >= str)
{
if (*p == c)
return p;
p--;
}
return NULL;
}

/**
* flatpak_is_valid_name:
* @string: The string to check
*
* Checks if @string is a valid application name.
*
* App names are composed of 3 or more elements separated by a period
* ('.') character. All elements must contain at least one character.
*
* Each element must only contain the ASCII characters
* "[A-Z][a-z][0-9]_-". Elements may not begin with a digit.
* Additionally "-" is only allowed in the last element.
*
* App names must not begin with a '.' (period) character.
*
* App names must not exceed 255 characters in length.
*
* The above means that any app name is also a valid DBus well known
* bus name, but not all DBus names are valid app names. The difference are:
* 1) DBus name elements may contain '-' in the non-last element.
* 2) DBus names require only two elements
*
* Returns: %TRUE if valid, %FALSE otherwise.
*/
static gboolean
flatpak_is_valid_name (const char *string)
{
gssize len;
const gchar *s;
const gchar *end;
const gchar *last_dot;
int dot_count;
gboolean last_element;

g_return_val_if_fail (string != NULL, FALSE);

len = strlen (string);
if (G_UNLIKELY (len == 0))
return FALSE;

if (G_UNLIKELY (len > 255))
return FALSE;

end = string + len;

last_dot = find_last_char (string, len, '.');
last_element = FALSE;

s = string;
if (G_UNLIKELY (*s == '.'))
return FALSE;

if (G_UNLIKELY (!is_valid_initial_name_character (*s, last_element)))
return FALSE;

s += 1;
dot_count = 0;
while (s != end)
{
if (*s == '.')
{
if (s == last_dot)
last_element = TRUE;
s += 1;
if (G_UNLIKELY (s == end))
return FALSE;
if (!is_valid_initial_name_character (*s, last_element))
return FALSE;
dot_count++;
}
else if (G_UNLIKELY (!is_valid_name_character (*s, last_element)))
return FALSE;
s += 1;
}

if (G_UNLIKELY (dot_count < 2))
return FALSE;

return TRUE;
}

gboolean
xdp_app_info_flatpak_is_valid_sub_app_id (XdpAppInfo *app_info,
const char *sub_app_id)
{
const char *app_id = xdp_app_info_get_id (app_info);

g_assert (app_id);

if (!g_str_has_prefix (sub_app_id, app_id))
return FALSE;

if (sub_app_id[strlen (app_id)] != '.')
return FALSE;

return flatpak_is_valid_name (sub_app_id);
}

static char *
xdp_app_info_flatpak_remap_path (XdpAppInfo *app_info,
const char *path)
Expand Down
7 changes: 7 additions & 0 deletions src/xdp-app-info-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ struct _XdpAppInfoHost

G_DEFINE_FINAL_TYPE (XdpAppInfoHost, xdp_app_info_host, XDP_TYPE_APP_INFO)

gboolean
xdp_app_info_host_is_valid_sub_app_id (XdpAppInfo *app_info,
const char *sub_app_id)
{
return TRUE;
}

gboolean
xdp_app_info_host_validate_autostart (XdpAppInfo *app_info,
GKeyFile *keyfile,
Expand Down
3 changes: 3 additions & 0 deletions src/xdp-app-info-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ struct _XdpAppInfoClass
{
GObjectClass parent_class;

gboolean (*is_valid_sub_app_id) (XdpAppInfo *app_info,
const char *sub_app_id);

char * (*remap_path) (XdpAppInfo *app_info,
const char *path);

Expand Down
11 changes: 11 additions & 0 deletions src/xdp-app-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ xdp_app_info_get_gappinfo (XdpAppInfo *app_info)
return priv->gappinfo;
}

gboolean
xdp_app_info_is_valid_sub_app_id (XdpAppInfo *app_info,
const char *sub_app_id)
{
if (!XDP_APP_INFO_GET_CLASS (app_info)->is_valid_sub_app_id)
return FALSE;

return XDP_APP_INFO_GET_CLASS (app_info)->is_valid_sub_app_id (app_info,
sub_app_id);
}

gboolean
xdp_app_info_has_network (XdpAppInfo *app_info)
{
Expand Down
3 changes: 3 additions & 0 deletions src/xdp-app-info.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const char * xdp_app_info_get_instance (XdpAppInfo *app_info);

GAppInfo * xdp_app_info_get_gappinfo (XdpAppInfo *app_info);

gboolean xdp_app_info_is_valid_sub_app_id (XdpAppInfo *app_info,
const char *sub_app_id);

gboolean xdp_app_info_has_network (XdpAppInfo *app_info);

gboolean xdp_app_info_get_pidns (XdpAppInfo *app_info,
Expand Down

0 comments on commit 1082e97

Please sign in to comment.