From 9325140b90f8ac625ae5b26075748c22f6f06158 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Mon, 8 May 2023 06:56:26 +0300 Subject: [PATCH] API key improvements Fixed: Special characters in API key New: Add heathcheck for API Key --- .../Calendar/iCal/CalendarLinkModalContent.js | 2 +- frontend/src/Components/SignalRConnector.js | 4 +-- .../Checks/ApiKeyValidationCheck.cs | 34 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 src/NzbDrone.Core/HealthCheck/Checks/ApiKeyValidationCheck.cs diff --git a/frontend/src/Calendar/iCal/CalendarLinkModalContent.js b/frontend/src/Calendar/iCal/CalendarLinkModalContent.js index 2df0caf6d..5abcb249d 100644 --- a/frontend/src/Calendar/iCal/CalendarLinkModalContent.js +++ b/frontend/src/Calendar/iCal/CalendarLinkModalContent.js @@ -40,7 +40,7 @@ function getUrls(state) { icalUrl += `tags=${tags.toString()}&`; } - icalUrl += `apikey=${window.Sonarr.apiKey}`; + icalUrl += `apikey=${encodeURIComponent(window.Sonarr.apiKey)}`; const iCalHttpUrl = `${window.location.protocol}//${icalUrl}`; const iCalWebCalUrl = `webcal://${icalUrl}`; diff --git a/frontend/src/Components/SignalRConnector.js b/frontend/src/Components/SignalRConnector.js index 98b1396f1..5f8b1f6e0 100644 --- a/frontend/src/Components/SignalRConnector.js +++ b/frontend/src/Components/SignalRConnector.js @@ -61,7 +61,7 @@ function Logger(minimumLogLevel) { } Logger.prototype.cleanse = function(message) { - const apikey = new RegExp(`access_token=${window.Sonarr.apiKey}`, 'g'); + const apikey = new RegExp(`access_token=${encodeURIComponent(window.Sonarr.apiKey)}`, 'g'); return message.replace(apikey, 'access_token=(removed)'); }; @@ -105,7 +105,7 @@ class SignalRConnector extends Component { this.connection = new signalR.HubConnectionBuilder() .configureLogging(new Logger(signalR.LogLevel.Information)) - .withUrl(`${url}?access_token=${window.Sonarr.apiKey}`) + .withUrl(`${url}?access_token=${encodeURIComponent(window.Sonarr.apiKey)}`) .withAutomaticReconnect({ nextRetryDelayInMilliseconds: (retryContext) => { if (retryContext.elapsedMilliseconds > 180000) { diff --git a/src/NzbDrone.Core/HealthCheck/Checks/ApiKeyValidationCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/ApiKeyValidationCheck.cs new file mode 100644 index 000000000..00b5b7562 --- /dev/null +++ b/src/NzbDrone.Core/HealthCheck/Checks/ApiKeyValidationCheck.cs @@ -0,0 +1,34 @@ +using System.Text.RegularExpressions; +using NLog; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Configuration.Events; +using NzbDrone.Core.Lifecycle; + +namespace NzbDrone.Core.HealthCheck.Checks +{ + [CheckOn(typeof(ApplicationStartedEvent))] + [CheckOn(typeof(ConfigSavedEvent))] + public class ApiKeyValidationCheck : HealthCheckBase + { + private readonly IConfigFileProvider _configFileProvider; + private readonly Logger _logger; + + public ApiKeyValidationCheck(IConfigFileProvider configFileProvider, Logger logger) + { + _configFileProvider = configFileProvider; + _logger = logger; + } + + public override HealthCheck Check() + { + if (_configFileProvider.ApiKey.Length < 20) + { + _logger.Warn("Please update your API key to be at least 20 characters long. You can do this via settings or the config file"); + + return new HealthCheck(GetType(), HealthCheckResult.Warning, "Please update your API key to be at least 20 characters long. You can do this via settings or the config file", "#invalid-api-key"); + } + + return new HealthCheck(GetType()); + } + } +}