From c2e3d0e225e38550089a907e872b56769106da0f Mon Sep 17 00:00:00 2001 From: kaso17 Date: Mon, 16 Jan 2017 18:08:02 +0100 Subject: [PATCH] Add imdbid validation --- src/Jackett/Controllers/TorznabController.cs | 43 +++++++++++++++++++- src/Jackett/Utils/ParseUtil.cs | 10 +++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/Jackett/Controllers/TorznabController.cs b/src/Jackett/Controllers/TorznabController.cs index db8807397..0e982a9b7 100644 --- a/src/Jackett/Controllers/TorznabController.cs +++ b/src/Jackett/Controllers/TorznabController.cs @@ -1,6 +1,7 @@ using AutoMapper; using Jackett.Models; using Jackett.Services; +using Jackett.Utils; using NLog; using System; using System.Collections.Generic; @@ -12,7 +13,8 @@ using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Http; - +using System.Xml.Linq; + namespace Jackett.Controllers { [AllowAnonymous] @@ -32,6 +34,24 @@ namespace Jackett.Controllers cacheService = c; } + public HttpResponseMessage GetErrorXML(int code, string description) + { + var xdoc = new XDocument( + new XDeclaration("1.0", "UTF-8", null), + new XElement("error", + new XAttribute("code", code.ToString()), + new XAttribute("description", description) + ) + ); + + var xml = xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString(); + + return new HttpResponseMessage() + { + Content = new StringContent(xml, Encoding.UTF8, "application/xml") + }; + } + [HttpGet] public async Task Call(string indexerID) { @@ -62,6 +82,27 @@ namespace Jackett.Controllers { logger.Warn(string.Format("Rejected a request to {0} which is unconfigured.", indexer.DisplayName)); return Request.CreateResponse(HttpStatusCode.Forbidden, "This indexer is not configured."); + } + + if (torznabQuery.ImdbID != null) + { + if (torznabQuery.QueryType != "movie") + { + logger.Warn(string.Format("A non movie request with an imdbid was made from {0}.", Request.GetOwinContext().Request.RemoteIpAddress)); + return GetErrorXML(201, "Incorrect parameter: only movie-search supports the imdbid parameter"); + } + + if (torznabQuery.SearchTerm != null) + { + logger.Warn(string.Format("A movie-search request from {0} was made contining q and imdbid.", Request.GetOwinContext().Request.RemoteIpAddress)); + return GetErrorXML(201, "Incorrect parameter: please specify either imdbid or q"); + } + + if (ParseUtil.GetImdbID(torznabQuery.ImdbID) == null) + { + logger.Warn(string.Format("A movie-search request from {0} was made with an invalid imdbid.", Request.GetOwinContext().Request.RemoteIpAddress)); + return GetErrorXML(201, "Incorrect parameter: invalid imdbid format"); + } } var releases = await indexer.PerformQuery(torznabQuery); diff --git a/src/Jackett/Utils/ParseUtil.cs b/src/Jackett/Utils/ParseUtil.cs index b152103d8..9472149cf 100644 --- a/src/Jackett/Utils/ParseUtil.cs +++ b/src/Jackett/Utils/ParseUtil.cs @@ -9,6 +9,7 @@ namespace Jackett.Utils new Regex( @"(?