core: fix cookies removal when using subfolders as path (#14517)

This commit is contained in:
Bogdan 2023-06-29 13:25:40 +03:00 committed by GitHub
parent c301f3c5b9
commit 7713588223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 16 deletions

View File

@ -110,7 +110,6 @@ namespace Jackett.Common.Utils.Clients
protected override async Task<WebResult> Run(WebRequest webRequest)
{
HttpResponseMessage response = null;
var request = new HttpRequestMessage();
request.Headers.ExpectContinue = false;
request.RequestUri = new Uri(webRequest.Url);
@ -176,23 +175,23 @@ namespace Jackett.Common.Utils.Clients
request.Method = HttpMethod.Get;
}
response = await client.SendAsync(request);
using var responseMessage = await client.SendAsync(request);
var result = new WebResult
{
ContentBytes = await response.Content.ReadAsByteArrayAsync()
ContentBytes = await responseMessage.Content.ReadAsByteArrayAsync()
};
foreach (var header in response.Headers)
foreach (var header in responseMessage.Headers)
result.Headers[header.Key.ToLowerInvariant()] = header.Value.ToArray();
foreach (var header in response.Content.Headers)
foreach (var header in responseMessage.Content.Headers)
result.Headers[header.Key.ToLowerInvariant()] = header.Value.ToArray();
// some cloudflare clients are using a refresh header
// Pull it out manually
if (response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable && response.Headers.Contains("Refresh"))
if (responseMessage.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable && responseMessage.Headers.Contains("Refresh"))
{
var refreshHeaders = response.Headers.GetValues("Refresh");
var refreshHeaders = responseMessage.Headers.GetValues("Refresh");
var redirval = "";
var redirtime = 0;
if (refreshHeaders != null)
@ -209,16 +208,16 @@ namespace Jackett.Common.Utils.Clients
// normally we don't want a serviceunavailable (503) to be a redirect, but that's the nature
// of this cloudflare approach..don't want to alter WebResult.IsRedirect because normally
// it shoudln't include service unavailable..only if we have this redirect header.
response.StatusCode = System.Net.HttpStatusCode.Redirect;
responseMessage.StatusCode = System.Net.HttpStatusCode.Redirect;
redirtime = int.Parse(value.Substring(0, end));
System.Threading.Thread.Sleep(redirtime * 1000);
}
}
}
}
if (response.Headers.Location != null)
if (responseMessage.Headers.Location != null)
{
result.RedirectingTo = response.Headers.Location.ToString();
result.RedirectingTo = responseMessage.Headers.Location.ToString();
}
// Mono won't add the baseurl to relative redirects.
// e.g. a "Location: /index.php" header will result in the Uri "file:///index.php"
@ -235,14 +234,14 @@ namespace Jackett.Common.Utils.Clients
logger.Debug("[MONO relative redirect bug] Rewriting relative redirect URL from " + result.RedirectingTo + " to " + newRedirectingTo);
result.RedirectingTo = newRedirectingTo;
}
result.Status = response.StatusCode;
result.Status = responseMessage.StatusCode;
// Compatiblity issue between the cookie format and httpclient
// Pull it out manually ignoring the expiry date then set it manually
// http://stackoverflow.com/questions/14681144/httpclient-not-storing-cookies-in-cookiecontainer
var responseCookies = new List<Tuple<string, string>>();
if (response.Headers.TryGetValues("set-cookie", out var cookieHeaders))
if (responseMessage.Headers.TryGetValues("set-cookie", out var cookieHeaders))
{
foreach (var value in cookieHeaders)
{

View File

@ -49,17 +49,43 @@ namespace Jackett.Common.Utils
{
var table = (Hashtable)cookieJar
.GetType()
.InvokeMember("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField |
BindingFlags.Instance, null, cookieJar, Array.Empty<object>());
foreach (var key in table.Keys)
.InvokeMember("m_domainTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, cookieJar, Array.Empty<object>());
foreach (var tableKey in table.Keys)
{
var domain = (string)key;
var domain = (string)tableKey;
if (domain.StartsWith("."))
{
domain = domain.Substring(1);
}
var list = (SortedList)table[tableKey]
.GetType()
.InvokeMember("m_list", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, table[tableKey], Array.Empty<object>());
foreach (var listKey in list.Keys)
{
foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"http://{domain}{listKey}")))
{
cookie.Expired = true;
}
foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"https://{domain}{listKey}")))
{
cookie.Expired = true;
}
}
foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"http://{domain}")))
{
cookie.Expired = true;
}
foreach (Cookie cookie in cookieJar.GetCookies(new Uri($"https://{domain}")))
{
cookie.Expired = true;
}
}
}

View File

@ -117,5 +117,28 @@ namespace Jackett.Test.Common.Utils
Assert.AreEqual(0, cookiesContainer.GetCookies(domainHttp).Count);
Assert.AreEqual(0, cookiesContainer.GetCookies(domainHttps).Count);
}
[Test]
public void RemoveAllCookiesWithPaths()
{
var cookiesContainer = new CookieContainer();
var domainHttp = new Uri("http://testdomain1.com/path");
var domainHttps = new Uri("https://testdomain2.com/path");
cookiesContainer.Add(domainHttp, new Cookie("cookie1", "value1", "/"));
cookiesContainer.Add(domainHttps, new Cookie("cookie2", "value2", "/"));
cookiesContainer.Add(domainHttp, new Cookie("cookie3", "value3", "/path"));
cookiesContainer.Add(domainHttps, new Cookie("cookie4", "value4", "/path"));
cookiesContainer.Add(domainHttp, new Cookie("cookie5", "value5", "/path", ".testdomain1.com"));
cookiesContainer.Add(domainHttps, new Cookie("cookie6", "value6", "/path", ".testdomain2.com"));
Assert.AreEqual(3, cookiesContainer.GetCookies(domainHttp).Count);
Assert.AreEqual(3, cookiesContainer.GetCookies(domainHttps).Count);
CookieUtil.RemoveAllCookies(cookiesContainer);
Assert.AreEqual(0, cookiesContainer.GetCookies(domainHttp).Count);
Assert.AreEqual(0, cookiesContainer.GetCookies(domainHttps).Count);
}
}
}