From fbe61cc36c94501f53fbb260bfd5c2d6e1c86e52 Mon Sep 17 00:00:00 2001 From: John Stebbins Date: Tue, 12 Feb 2019 07:22:45 -0700 Subject: [PATCH] LinGui: sanitize preset export filename Removes illegal characters that may be in the preset name from the suggested filename and trims leading and trailing white space. (cherry picked from commit f7804e7ff4cd81986e09509e74e06e735c68408e) --- gtk/src/ghbcompat.h | 6 ++++++ gtk/src/presets.c | 13 ++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/gtk/src/ghbcompat.h b/gtk/src/ghbcompat.h index b4cf9deea..382125132 100644 --- a/gtk/src/ghbcompat.h +++ b/gtk/src/ghbcompat.h @@ -28,6 +28,12 @@ #include #include +#if defined(_WIN32) +#define GHB_UNSAFE_FILENAME_CHARS "/:<>\"\\|?*" +#else +#define GHB_UNSAFE_FILENAME_CHARS "/" +#endif + static inline void ghb_widget_get_preferred_width( GtkWidget *widget, gint *min_width, gint * natural_width) { diff --git a/gtk/src/presets.c b/gtk/src/presets.c index 671c9d2f9..db7da0c6b 100644 --- a/gtk/src/presets.c +++ b/gtk/src/presets.c @@ -2050,17 +2050,18 @@ preset_export_action_cb(GSimpleAction *action, GVariant *param, signal_user_data_t *ud) { hb_preset_index_t *path; - const gchar *name; GtkWindow *hb_window; GtkWidget *dialog; GtkResponseType response; const gchar *exportDir; gchar *filename; GhbValue *dict; + char *preset_name; path = get_selected_path(ud); if (path == NULL || path->depth <= 0) { + const gchar *name; char * new_name; free(path); @@ -2080,7 +2081,7 @@ preset_export_action_cb(GSimpleAction *action, GVariant *param, { return; } - name = ghb_dict_get_string(dict, "PresetName"); + preset_name = g_strdup(ghb_dict_get_string(dict, "PresetName")); hb_window = GTK_WINDOW(GHB_WIDGET(ud->builder, "hb_window")); dialog = gtk_file_chooser_dialog_new(_("Export Preset"), hb_window, @@ -2094,10 +2095,16 @@ preset_export_action_cb(GSimpleAction *action, GVariant *param, { exportDir = "."; } - filename = g_strdup_printf("%s.json", name); + + // Clean up preset name for use as a filename. Removing leading + // and trailing whitespace and filename illegal characters. + g_strstrip(preset_name); + g_strdelimit(preset_name, GHB_UNSAFE_FILENAME_CHARS, '_'); + filename = g_strdup_printf("%s.json", preset_name); gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), exportDir); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename); g_free(filename); + g_free(preset_name); response = gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_hide(dialog); -- 2.40.0