Skip to content

Truly independent web browser

LibWebView: Keep notification text and icons within what a daemon takes

sideshowbarkercc89b59e59db
diff --git a/Libraries/LibWebView/NotificationAttribution.cpp b/Libraries/LibWebView/NotificationAttribution.cpp
index 7aa7b658338..ccc2955c326 100644
--- a/Libraries/LibWebView/NotificationAttribution.cpp
+++ b/Libraries/LibWebView/NotificationAttribution.cpp
@@ -4,10 +4,25 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/CharacterTypes.h>
+#include <AK/StringBuilder.h>
+#include <AK/Utf16View.h>
#include <LibWebView/NotificationAttribution.h>
namespace WebView {
+String platform_text(Utf16String const& text)
+{
+ StringBuilder builder;
+ for (auto code_point : text.utf16_view()) {
+ if (is_unicode_surrogate(code_point))
+ builder.append_code_point(0xFFFD);
+ else
+ builder.append_code_point(code_point);
+ }
+ return MUST(builder.to_string());
+}
+
String attribution_for_origin(URL::Origin const& origin)
{
if (origin.is_opaque())
diff --git a/Libraries/LibWebView/NotificationAttribution.h b/Libraries/LibWebView/NotificationAttribution.h
index 4f05d6716d5..f887f8e3d77 100644
--- a/Libraries/LibWebView/NotificationAttribution.h
+++ b/Libraries/LibWebView/NotificationAttribution.h
@@ -7,6 +7,7 @@
#pragma once
#include <AK/String.h>
+#include <AK/Utf16String.h>
#include <LibURL/Origin.h>
#include <LibWebView/Export.h>
@@ -16,4 +17,8 @@ namespace WebView {
// so that "example.com" reads as the site. An opaque origin reads as "this page".
WEBVIEW_API String attribution_for_origin(URL::Origin const&);
+// A page's string as a platform takes it: valid UTF-8, with every lone surrogate replaced. A platform handed a lone
+// surrogate's bytes may refuse the string, or worse.
+WEBVIEW_API String platform_text(Utf16String const&);
+
}
diff --git a/Libraries/LibWebView/NotificationFreedesktopArguments.cpp b/Libraries/LibWebView/NotificationFreedesktopArguments.cpp
index 5a2f2694fb2..fb77885c089 100644
--- a/Libraries/LibWebView/NotificationFreedesktopArguments.cpp
+++ b/Libraries/LibWebView/NotificationFreedesktopArguments.cpp
@@ -5,6 +5,7 @@
*/
#include <AK/StringBuilder.h>
+#include <LibWebView/NotificationAttribution.h>
#include <LibWebView/NotificationFreedesktopArguments.h>
namespace WebView {
@@ -14,6 +15,8 @@ static constexpr i32 expire_timeout_never = 0;
static constexpr i32 expire_timeout_without_notification_center_ms = 25000;
static constexpr u8 urgency_normal = 1;
static constexpr u8 urgency_critical = 2;
+// The longest side an icon may have before it's left out rather than put on the bus.
+static constexpr int icon_maximum_side = 128;
static String escape_markup(String const& text)
{
@@ -69,16 +72,23 @@ FreedesktopNotifyArguments build_notify_arguments(PlatformNotification const& no
arguments.app_name = "Ladybird"_string;
arguments.replaces_id = replaces_daemon_id;
arguments.app_icon = "org.ladybird.Ladybird"_string;
- arguments.summary = notification.data.title.to_utf8();
+ auto title = platform_text(notification.data.title);
+ auto body = platform_text(notification.data.body);
+ // The origin always shows: in the body where the daemon shows one, else ahead of the title.
if (capabilities.body) {
+ arguments.summary = title;
auto attribution = notification.attribution;
- auto body = notification.data.body.to_utf8();
if (capabilities.body_markup) {
attribution = escape_markup(attribution);
body = escape_markup(body);
}
- arguments.body = MUST(String::formatted("{}\n\n{}", attribution, body));
+ if (body.is_empty())
+ arguments.body = attribution;
+ else
+ arguments.body = MUST(String::formatted("{}\n\n{}", attribution, body));
+ } else {
+ arguments.summary = MUST(String::formatted("{} \u2014 {}", notification.attribution, title));
}
// "default" makes the whole notification clickable where the daemon shows actions as buttons.
@@ -87,10 +97,12 @@ FreedesktopNotifyArguments build_notify_arguments(PlatformNotification const& no
bool sticky = notification.data.require_interaction;
arguments.hints.append({ "urgency"_string, sticky ? urgency_critical : urgency_normal });
- if (notification.data.silent == true)
+ // A replacement alerts again only when the page asked to renotify.
+ bool quiet_replacement = notification.replaces.has_value() && !notification.data.renotify;
+ if (notification.data.silent == true || quiet_replacement)
arguments.hints.append({ "suppress-sound"_string, true });
arguments.hints.append({ "desktop-entry"_string, "org.ladybird.Ladybird"_string });
- if (notification.icon)
+ if (notification.icon && notification.icon->width() <= icon_maximum_side && notification.icon->height() <= icon_maximum_side)
arguments.hints.append({ "image-data"_string, image_data_for(*notification.icon) });
if (sticky)
diff --git a/Tests/LibWebView/TestNotificationFreedesktopArguments.cpp b/Tests/LibWebView/TestNotificationFreedesktopArguments.cpp
index 4541fb366c4..70e7e1568d3 100644
--- a/Tests/LibWebView/TestNotificationFreedesktopArguments.cpp
+++ b/Tests/LibWebView/TestNotificationFreedesktopArguments.cpp
@@ -54,12 +54,51 @@ TEST_CASE(markup_is_escaped_only_when_the_daemon_renders_it)
EXPECT_EQ(markup.body, "example.com\n\na &lt; b &amp; c"_string);
}
-TEST_CASE(daemon_without_body_support_gets_no_body)
+TEST_CASE(daemon_without_body_support_gets_the_attribution_in_the_summary)
{
- auto arguments = build_notify_arguments(notification("t"_utf16, "b"_utf16), {}, 0);
+ auto arguments = build_notify_arguments(notification("New mail"_utf16, "b"_utf16), {}, 0);
+ EXPECT_EQ(arguments.summary, "example.com \xe2\x80\x94"
+ " New mail"_string);
EXPECT_EQ(arguments.body, ""_string);
}
+TEST_CASE(empty_body_leaves_just_the_attribution)
+{
+ auto arguments = build_notify_arguments(notification("t"_utf16, ""_utf16), { .body = true }, 0);
+ EXPECT_EQ(arguments.body, "example.com"_string);
+}
+
+TEST_CASE(lone_surrogates_become_replacement_characters)
+{
+ auto arguments = build_notify_arguments(notification(Utf16String::from_utf16({ u"a\xD800"
+ u"b",
+ 3 }),
+ Utf16String::from_utf16({ u"\xDC00", 1 })),
+ { .body = true }, 0);
+ EXPECT_EQ(arguments.summary, "a\xef\xbf\xbd"
+ "b"_string);
+ EXPECT_EQ(arguments.body, "example.com\n\n\xef\xbf\xbd"_string);
+}
+
+TEST_CASE(replacement_without_renotify_is_silent)
+{
+ auto quiet = notification("t"_utf16, "b"_utf16);
+ quiet.replaces = 3;
+ EXPECT(hint(build_notify_arguments(quiet, {}, 0), "suppress-sound"sv).has_value());
+
+ auto loud = notification("t"_utf16, "b"_utf16);
+ loud.replaces = 3;
+ loud.data.renotify = true;
+ EXPECT(!hint(build_notify_arguments(loud, {}, 0), "suppress-sound"sv).has_value());
+}
+
+TEST_CASE(oversized_icon_is_left_out)
+{
+ auto huge = notification("t"_utf16, "b"_utf16);
+ huge.icon = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 129, 10 }));
+ EXPECT(!hint(build_notify_arguments(huge, {}, 0), "image-data"sv).has_value());
+}
+
TEST_CASE(expire_timeout_follows_persistence_and_require_interaction)
{
EXPECT_EQ(build_notify_arguments(notification("t"_utf16, "b"_utf16), {}, 0).expire_timeout, 25000);