From 0415a65083fa64b89b9b2048e1b8d02e1411c2f3 Mon Sep 17 00:00:00 2001 From: alex-huff Date: Sun, 25 May 2025 11:43:40 -0500 Subject: [PATCH 1/2] gtk: improve app id validation 'g_application_id_is_valid' doesn't allow empty elements or elements that start with digits. This commit updates 'isValidAppId' to be more consistant with 'g_application_id_is_valid' avoiding the app id defaulting to 'GTK Application' for app ids like '0foo.bar' or 'foo..bar'. --- src/apprt/gtk/App.zig | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index da828b973..9e5037d5c 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -1690,30 +1690,33 @@ fn initActions(self: *App) void { } fn isValidAppId(app_id: [:0]const u8) bool { - if (app_id.len > 255 or app_id.len == 0) return false; - if (app_id[0] == '.') return false; - if (app_id[app_id.len - 1] == '.') return false; + if (app_id.len > 255) return false; - var hasDot = false; + var hasSep = false; + var lastWasSep = true; for (app_id) |char| { switch (char) { - 'a'...'z', 'A'...'Z', '0'...'9', '_', '-' => {}, - '.' => hasDot = true, + 'a'...'z', 'A'...'Z', '_', '-' => {}, + '0'...'9', '.' => if (lastWasSep) return false, else => return false, } + lastWasSep = char == '.'; + hasSep = hasSep or lastWasSep; } - if (!hasDot) return false; - - return true; + return hasSep and !lastWasSep; } test "isValidAppId" { try testing.expect(isValidAppId("foo.bar")); try testing.expect(isValidAppId("foo.bar.baz")); + try testing.expect(isValidAppId("f00.bar")); + try testing.expect(isValidAppId("foo-bar._baz")); try testing.expect(!isValidAppId("foo")); try testing.expect(!isValidAppId("foo.bar?")); try testing.expect(!isValidAppId("foo.")); try testing.expect(!isValidAppId(".foo")); try testing.expect(!isValidAppId("")); try testing.expect(!isValidAppId("foo" ** 86)); + try testing.expect(!isValidAppId("foo..bar")); + try testing.expect(!isValidAppId("0foo.bar")); } From 113c196078bc21c0dfd6d15d04203a255839a091 Mon Sep 17 00:00:00 2001 From: alex-huff Date: Sun, 25 May 2025 13:20:29 -0500 Subject: [PATCH 2/2] gtk: use 'gio.Application.idIsValid' instead of 'isValidAppId' --- src/apprt/gtk/App.zig | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-) diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index 9e5037d5c..55c0be5e0 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -288,7 +288,7 @@ pub fn init(core_app: *CoreApp, opts: Options) !App { // can develop Ghostty in Ghostty. const app_id: [:0]const u8 = app_id: { if (config.class) |class| { - if (isValidAppId(class)) { + if (gio.Application.idIsValid(class) != 0) { break :app_id class; } else { log.warn("invalid 'class' in config, ignoring", .{}); @@ -1688,35 +1688,3 @@ fn initActions(self: *App) void { action_map.addAction(action.as(gio.Action)); } } - -fn isValidAppId(app_id: [:0]const u8) bool { - if (app_id.len > 255) return false; - - var hasSep = false; - var lastWasSep = true; - for (app_id) |char| { - switch (char) { - 'a'...'z', 'A'...'Z', '_', '-' => {}, - '0'...'9', '.' => if (lastWasSep) return false, - else => return false, - } - lastWasSep = char == '.'; - hasSep = hasSep or lastWasSep; - } - return hasSep and !lastWasSep; -} - -test "isValidAppId" { - try testing.expect(isValidAppId("foo.bar")); - try testing.expect(isValidAppId("foo.bar.baz")); - try testing.expect(isValidAppId("f00.bar")); - try testing.expect(isValidAppId("foo-bar._baz")); - try testing.expect(!isValidAppId("foo")); - try testing.expect(!isValidAppId("foo.bar?")); - try testing.expect(!isValidAppId("foo.")); - try testing.expect(!isValidAppId(".foo")); - try testing.expect(!isValidAppId("")); - try testing.expect(!isValidAppId("foo" ** 86)); - try testing.expect(!isValidAppId("foo..bar")); - try testing.expect(!isValidAppId("0foo.bar")); -}