diff --git a/PluralKit.Bot/CommandSystem/Context/ContextAvatarExt.cs b/PluralKit.Bot/CommandSystem/Context/ContextAvatarExt.cs index aad38c23..3d36529a 100644 --- a/PluralKit.Bot/CommandSystem/Context/ContextAvatarExt.cs +++ b/PluralKit.Bot/CommandSystem/Context/ContextAvatarExt.cs @@ -23,11 +23,8 @@ public static class ContextAvatarExt if (arg.StartsWith("<") && arg.EndsWith(">")) arg = arg.Substring(1, arg.Length - 2); - if (!Uri.TryCreate(arg, UriKind.Absolute, out var uri)) - throw Errors.InvalidUrl(arg); - - if (uri.Scheme != "http" && uri.Scheme != "https") - throw Errors.InvalidUrl(arg); + if (!Core.MiscUtils.TryMatchUri(arg, out var uri)) + throw Errors.InvalidUrl; // ToString URL-decodes, which breaks URLs to spaces; AbsoluteUri doesn't return new ParsedImage { Url = uri.AbsoluteUri, Source = AvatarSource.Url }; diff --git a/PluralKit.Bot/Commands/ImportExport.cs b/PluralKit.Bot/Commands/ImportExport.cs index 36722357..2d4197a7 100644 --- a/PluralKit.Bot/Commands/ImportExport.cs +++ b/PluralKit.Bot/Commands/ImportExport.cs @@ -36,6 +36,9 @@ public class ImportExport var url = ctx.RemainderOrNull() ?? ctx.Message.Attachments.FirstOrDefault()?.Url; if (url == null) throw Errors.NoImportFilePassed; + if (!Core.MiscUtils.TryMatchUri(url, out var _)) + throw Errors.InvalidUrl; + await ctx.BusyIndicator(async () => { JObject data; diff --git a/PluralKit.Bot/Errors.cs b/PluralKit.Bot/Errors.cs index ee46cb49..04f3d33e 100644 --- a/PluralKit.Bot/Errors.cs +++ b/PluralKit.Bot/Errors.cs @@ -115,7 +115,7 @@ public static class Errors public static PKError AvatarDimensionsTooLarge(int width, int height) => new( $"Image too large ({width}x{height} > {Limits.AvatarDimensionLimit}x{Limits.AvatarDimensionLimit}), try resizing the image."); - public static PKError InvalidUrl(string url) => new("The given URL is invalid."); + public static PKError InvalidUrl => new("The given URL is invalid."); public static PKError UrlTooLong(string url) => new($"The given URL is too long ({url.Length}/{Limits.MaxUriLength} characters)."); diff --git a/PluralKit.Bot/Utils/AvatarUtils.cs b/PluralKit.Bot/Utils/AvatarUtils.cs index 4d1e2ace..9e4647c2 100644 --- a/PluralKit.Bot/Utils/AvatarUtils.cs +++ b/PluralKit.Bot/Utils/AvatarUtils.cs @@ -21,7 +21,7 @@ public static class AvatarUtils }; if (!PluralKit.Core.MiscUtils.TryMatchUri(url, out var uri)) - throw Errors.InvalidUrl(url); + throw Errors.InvalidUrl; url = TryRewriteCdnUrl(url);