Skip to content

Commit

Permalink
icon-validator: Hard code all icon requirements in validator
Browse files Browse the repository at this point in the history
Since we are the only user of the icon-validator we don't need to split
the hard coded limits for icon validation between xdp-utils and the
icon-validator itself.
This moves icon size limit to the icon-validator and drops the second
format check from xdp-validate_serialized_icon.
  • Loading branch information
jsparber committed Aug 26, 2024
1 parent 3aba008 commit aec44ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 53 deletions.
64 changes: 22 additions & 42 deletions src/validate-icon.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
#endif

#define ICON_VALIDATOR_GROUP "Icon Validator"
#define MAX_ICON_SIZE 512
#define MAX_SVG_ICON_SIZE 4096

static int
validate_icon (const char *arg_width,
const char *arg_height,
const char *filename)
validate_icon (const char *filename)
{
GdkPixbufFormat *format;
int max_width, max_height;
int max_size;
int width, height;
g_autofree char *name = NULL;
const char *allowed_formats[] = { "png", "jpeg", "svg", NULL };
Expand All @@ -57,38 +57,26 @@ validate_icon (const char *arg_width,
return 1;
}

if (width != height)
{
g_printerr ("Expected a square icon but got: %dx%d\n", width, height);
return 1;
}

name = gdk_pixbuf_format_get_name (format);
if (!g_strv_contains (allowed_formats, name))
{
g_printerr ("Format %s not accepted\n", name);
return 1;
}

if (!g_str_equal (name, "svg"))
{
max_width = g_ascii_strtoll (arg_width, NULL, 10);
if (max_width < 16 || max_width > 4096)
{
g_printerr ("Bad width limit: %s\n", arg_width);
return 1;
}

max_height = g_ascii_strtoll (arg_height, NULL, 10);
if (max_height < 16 || max_height > 4096)
{
g_printerr ("Bad height limit: %s\n", arg_height);
return 1;
}
}
else
{
/* Sanity check for vector files */
max_height = max_width = 4096;
}
/* Sanity check for vector files */
max_size = g_str_equal (name, "svg") ? MAX_SVG_ICON_SIZE : MAX_ICON_SIZE;

if (width > max_width || height > max_height)
/* The icon is a square so we only need to check one side */
if (width > max_size)
{
g_printerr ("Image too large (%dx%d). Max. size %dx%d\n", width, height, max_width, max_height);
g_printerr ("Image too large (%dx%d). Max. size %dx%d\n", width, height, max_size, max_size);
return 1;
}

Expand All @@ -99,12 +87,6 @@ validate_icon (const char *arg_width,
return 1;
}

if (width != height)
{
g_printerr ("Expected a square icon but got: %dx%d\n", width, height);
return 1;
}

/* Print the format and size for consumption by (at least) the dynamic
* launcher portal. xdg-desktop-portal has a copy of this file. Use a
* GKeyFile so the output can be easily extended in the future in a backwards
Expand Down Expand Up @@ -165,9 +147,7 @@ flatpak_get_bwrap (void)
}

static int
rerun_in_sandbox (const char *arg_width,
const char *arg_height,
const char *filename)
rerun_in_sandbox (const char *filename)
{
const char * const usrmerged_dirs[] = { "bin", "lib32", "lib64", "lib", "sbin" };
int i;
Expand Down Expand Up @@ -234,7 +214,7 @@ rerun_in_sandbox (const char *arg_width,
if (g_getenv ("G_MESSAGES_PREFIXED"))
add_args (args, "--setenv", "G_MESSAGES_PREFIXED", g_getenv ("G_MESSAGES_PREFIXED"), NULL);

add_args (args, validate_icon, arg_width, arg_height, filename, NULL);
add_args (args, validate_icon, filename, NULL);
g_ptr_array_add (args, NULL);

execvpe (flatpak_get_bwrap (), (char **) args->pdata, NULL);
Expand All @@ -257,24 +237,24 @@ main (int argc, char *argv[])
g_autoptr(GOptionContext) context = NULL;
g_autoptr(GError) error = NULL;

context = g_option_context_new ("WIDTH HEIGHT PATH");
context = g_option_context_new ("PATH");
g_option_context_add_main_entries (context, entries, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_printerr ("Error: %s\n", error->message);
return 1;
}

if (argc != 4)
if (argc != 2)
{
g_printerr ("Usage: %s [OPTION…] WIDTH HEIGHT PATH\n", argv[0]);
g_printerr ("Usage: %s [OPTION…] PATH\n", argv[0]);
return 1;
}

#ifdef HELPER
if (opt_sandbox)
return rerun_in_sandbox (argv[1], argv[2], argv[3]);
return rerun_in_sandbox (argv[1]);
else
#endif
return validate_icon (argv[1], argv[2], argv[3]);
return validate_icon (argv[1]);
}
16 changes: 5 additions & 11 deletions src/xdp-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,11 +591,8 @@ xdp_validate_serialized_icon (GVariant *v,
g_autofree char *stderrlog = NULL;
g_autoptr(GError) error = NULL;
const char *icon_validator = LIBEXECDIR "/xdg-desktop-portal-validate-icon";
const char *args[6];
/* same allowed formats as Flatpak */
const char *allowed_icon_formats[] = { "png", "jpeg", "svg", NULL };
const char *args[4];
int size;
const char *MAX_ICON_SIZE = "512";
gconstpointer bytes_data;
gsize bytes_len;
g_autoptr(GKeyFile) key_file = NULL;
Expand Down Expand Up @@ -657,10 +654,8 @@ xdp_validate_serialized_icon (GVariant *v,

args[0] = icon_validator;
args[1] = "--sandbox";
args[2] = MAX_ICON_SIZE;
args[3] = MAX_ICON_SIZE;
args[4] = name;
args[5] = NULL;
args[2] = name;
args[3] = NULL;

if (!g_spawn_sync (NULL, (char **)args, NULL, 0, NULL, NULL, &stdoutlog, &stderrlog, &status, &error))
{
Expand All @@ -682,10 +677,9 @@ xdp_validate_serialized_icon (GVariant *v,
g_warning ("Icon validation: %s", error->message);
return FALSE;
}
if (!(format = g_key_file_get_string (key_file, ICON_VALIDATOR_GROUP, "format", &error)) ||
!g_strv_contains (allowed_icon_formats, format))
if (!(format = g_key_file_get_string (key_file, ICON_VALIDATOR_GROUP, "format", &error)))
{
g_warning ("Icon validation: %s", error ? error->message : "not allowed format");
g_warning ("Icon validation: %s", error->message);
return FALSE;
}
if (!(size = g_key_file_get_integer (key_file, ICON_VALIDATOR_GROUP, "width", &error)))
Expand Down

0 comments on commit aec44ca

Please sign in to comment.