feat(webhooks): verify that url is accessible before saving it

This commit is contained in:
spiral 2021-11-25 16:45:00 -05:00
parent 0a244eb9b5
commit ece17f7470
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
2 changed files with 15 additions and 2 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -126,6 +127,15 @@ namespace PluralKit.Bot
if (_webhookRegex.IsMatch(newUrl)) if (_webhookRegex.IsMatch(newUrl))
throw new PKError("PluralKit does not currently support setting a Discord webhook URL as your system's webhook URL."); 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(); var newToken = StringUtils.GenerateToken();
await _repo.UpdateSystem(ctx.System.Id, new() await _repo.UpdateSystem(ctx.System.Id, new()

View File

@ -21,7 +21,7 @@ namespace PluralKit.Core
_provider = provider; _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)) if (!await DispatchExt.ValidateUri(webhookUrl))
{ {
@ -35,6 +35,9 @@ namespace PluralKit.Core
} }
catch (HttpRequestException e) catch (HttpRequestException e)
{ {
if (isVerify)
throw;
else
_logger.Error("Could not dispatch webhook request!", e); _logger.Error("Could not dispatch webhook request!", e);
} }
} }