fix(plugins): check websocket callbacks before calling (#6314)

This commit is contained in:
nerix
2025-07-05 12:27:08 +02:00
committed by GitHub
parent 70bdeb79c3
commit c10172fee8
3 changed files with 30 additions and 4 deletions
+1 -1
View File
@@ -7,7 +7,7 @@
- Minor: Add an option for the reduced opacity of message history. (#6121)
- Minor: Make paused chat indicator more visible, and fix its zoom behavior. (#6123)
- Minor: Added interactive REPL for plugins. (#6120)
- Minor: Added WebSocket API for plugins. (#6076, #6186)
- Minor: Added WebSocket API for plugins. (#6076, #6186, #6314)
- Minor: Allow for themes to set transparent values for window background on Linux. (#6137)
- Minor: Popup overlay now only draws an outline when being interacted with. (#6140)
- Minor: Made filters searchable in the Settings dialog search bar. (#5890)
+6 -3
View File
@@ -144,7 +144,10 @@ void WebSocketListenerProxy::onClose(std::unique_ptr<WebSocketListener> self)
auto cb = std::move(strong->onClose);
strong->onText.reset();
strong->onBinary.reset();
loggedVoidCall(cb, u"WebSocket.on_close", strong->plugin);
if (cb)
{
loggedVoidCall(cb, u"WebSocket.on_close", strong->plugin);
}
}
});
}
@@ -154,7 +157,7 @@ void WebSocketListenerProxy::onTextMessage(QByteArray data)
auto target = this->target;
runInGuiThread([target, data{std::move(data)}] {
auto strong = target.lock();
if (strong)
if (strong && strong->onText)
{
loggedVoidCall(strong->onText, u"WebSocket.on_text", strong->plugin,
data);
@@ -167,7 +170,7 @@ void WebSocketListenerProxy::onBinaryMessage(QByteArray data)
auto target = this->target;
runInGuiThread([target, data{std::move(data)}] {
auto strong = target.lock();
if (strong)
if (strong && strong->onBinary)
{
loggedVoidCall(strong->onBinary, u"WebSocket.on_binary",
strong->plugin, data);
+23
View File
@@ -816,4 +816,27 @@ TEST_F(PluginTest, testWebSocketApi)
ASSERT_TRUE(ok);
}
TEST_F(PluginTest, testWebSocketUnsetFns)
{
configure({PluginPermission{{{"type", "Network"}}}});
RequestWaiter waiter;
lua->set("done", [&] {
waiter.requestDone();
});
lua->script(R"lua(
local ws = c2.WebSocket.new("wss://127.0.0.1:9050/echo")
ws.on_close = function()
done()
end
ws:send_text("message1")
ws:send_text("message2")
ws:send_binary("message3")
ws:send_binary("/CLOSE")
)lua");
waiter.waitForRequest();
}
#endif