From ece17f7470bcefe9a1c2162ab1a2a6a2857b46d3 Mon Sep 17 00:00:00 2001 From: spiral Date: Thu, 25 Nov 2021 16:45:00 -0500 Subject: [PATCH] feat(webhooks): verify that url is accessible before saving it --- PluralKit.Bot/Commands/Api.cs | 10 ++++++++++ PluralKit.Core/Dispatch/DispatchService.cs | 7 +++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/PluralKit.Bot/Commands/Api.cs b/PluralKit.Bot/Commands/Api.cs index ad2de2ff..0b594d1c 100644 --- a/PluralKit.Bot/Commands/Api.cs +++ b/PluralKit.Bot/Commands/Api.cs @@ -1,3 +1,4 @@ +using System; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -126,6 +127,15 @@ namespace PluralKit.Bot if (_webhookRegex.IsMatch(newUrl)) throw new PKError("PluralKit does not currently support setting a Discord webhook URL as your system's webhook URL."); + try + { + await _dispatch.DoPostRequest(ctx.System.Id, newUrl, null, true); + } + catch (Exception e) + { + throw new PKError($"Could not verify that the new URL is working: {e.Message}"); + } + var newToken = StringUtils.GenerateToken(); await _repo.UpdateSystem(ctx.System.Id, new() diff --git a/PluralKit.Core/Dispatch/DispatchService.cs b/PluralKit.Core/Dispatch/DispatchService.cs index 367f2052..c911101b 100644 --- a/PluralKit.Core/Dispatch/DispatchService.cs +++ b/PluralKit.Core/Dispatch/DispatchService.cs @@ -21,7 +21,7 @@ namespace PluralKit.Core _provider = provider; } - private async Task DoPostRequest(SystemId system, string webhookUrl, HttpContent content) + public async Task DoPostRequest(SystemId system, string webhookUrl, HttpContent content, bool isVerify = false) { if (!await DispatchExt.ValidateUri(webhookUrl)) { @@ -35,7 +35,10 @@ namespace PluralKit.Core } catch (HttpRequestException e) { - _logger.Error("Could not dispatch webhook request!", e); + if (isVerify) + throw; + else + _logger.Error("Could not dispatch webhook request!", e); } }