From c8c2c339521791aa94bd05ed369cd18a54fcf505 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 28 Apr 2021 15:58:46 +0200 Subject: [PATCH] plugin: Prevent plugins from registering native notification topics They may already have subscribers, and they may crash if presented with a malformed notification. --- lightningd/notification.c | 9 +++++++-- lightningd/notification.h | 4 ++++ lightningd/plugin.c | 8 ++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lightningd/notification.c b/lightningd/notification.c index bcd4491d0..63d2bec60 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -18,10 +18,15 @@ static struct notification *find_notification_by_topic(const char* topic) return NULL; } -bool notifications_have_topic(const struct plugins *plugins, const char *topic) +bool notifications_topic_is_native(const char *topic) { struct notification *noti = find_notification_by_topic(topic); - if (noti) + return noti != NULL; +} + +bool notifications_have_topic(const struct plugins *plugins, const char *topic) +{ + if (notifications_topic_is_native(topic)) return true; /* Some plugin at some point announced it'd be emitting diff --git a/lightningd/notification.h b/lightningd/notification.h index 6736db912..452926a37 100644 --- a/lightningd/notification.h +++ b/lightningd/notification.h @@ -27,6 +27,10 @@ struct wally_psbt; bool notifications_have_topic(const struct plugins *plugins, const char *topic); +/* Is the provided notification topic native, i.e., provided by + * lightningd itself? */ +bool notifications_topic_is_native(const char *topic); + struct notification { const char *topic; /* the serialization interface */ diff --git a/lightningd/plugin.c b/lightningd/plugin.c index c139a38dd..907e0cf84 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -1327,6 +1327,14 @@ static const char *plugin_notifications_add(const char *buffer, i); name = json_strdup(plugin->plugins, buffer, method); + + if (notifications_topic_is_native(name)) + return tal_fmt(plugin, + "plugin attempted to register a native " + "notification topic \"%s\", these may " + "however only be sent by lightningd", + name); + tal_arr_expand(&plugin->plugins->notification_topics, name); }