Improve messaging when NZB contains invalid XML

(cherry picked from commit 728df146ada115a367bf1ce808482a4625e6098d)
This commit is contained in:
Mark McDowall 2025-04-28 15:58:25 -07:00 committed by Bogdan
parent 9f229bb684
commit 47c32c9963

View file

@ -1,3 +1,4 @@
using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Xml; using System.Xml;
@ -15,39 +16,53 @@ namespace NzbDrone.Core.Download
{ {
public void Validate(string filename, byte[] fileContent) public void Validate(string filename, byte[] fileContent)
{ {
var reader = new StreamReader(new MemoryStream(fileContent)); try
using (var xmlTextReader = XmlReader.Create(reader, new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, IgnoreComments = true }))
{ {
var xDoc = XDocument.Load(xmlTextReader); var reader = new StreamReader(new MemoryStream(fileContent));
var nzb = xDoc.Root;
if (nzb == null) using (var xmlTextReader = XmlReader.Create(reader,
new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, IgnoreComments = true }))
{ {
throw new InvalidNzbException("Invalid NZB: No Root element [{0}]", filename); var xDoc = XDocument.Load(xmlTextReader);
} var nzb = xDoc.Root;
// nZEDb has an bug in their error reporting code spitting out invalid http status codes if (nzb == null)
if (nzb.Name.LocalName.Equals("error") && {
nzb.TryGetAttributeValue("code", out var code) && throw new InvalidNzbException("Invalid NZB: No Root element [{0}]", filename);
nzb.TryGetAttributeValue("description", out var description)) }
{
throw new InvalidNzbException("Invalid NZB: Contains indexer error: {0} - {1}", code, description);
}
if (!nzb.Name.LocalName.Equals("nzb")) // nZEDb has an bug in their error reporting code spitting out invalid http status codes
{ if (nzb.Name.LocalName.Equals("error") &&
throw new InvalidNzbException("Invalid NZB: Unexpected root element. Expected 'nzb' found '{0}' [{1}]", nzb.Name.LocalName, filename); nzb.TryGetAttributeValue("code", out var code) &&
} nzb.TryGetAttributeValue("description", out var description))
{
throw new InvalidNzbException("Invalid NZB: Contains indexer error: {0} - {1}", code, description);
}
var ns = nzb.Name.Namespace; if (!nzb.Name.LocalName.Equals("nzb"))
var files = nzb.Elements(ns + "file").ToList(); {
throw new InvalidNzbException(
"Invalid NZB: Unexpected root element. Expected 'nzb' found '{0}' [{1}]", nzb.Name.LocalName, filename);
}
if (files.Empty()) var ns = nzb.Name.Namespace;
{ var files = nzb.Elements(ns + "file").ToList();
throw new InvalidNzbException("Invalid NZB: No files [{0}]", filename);
if (files.Empty())
{
throw new InvalidNzbException("Invalid NZB: No files [{0}]", filename);
}
} }
} }
catch (InvalidNzbException)
{
// Throw the original exception
throw;
}
catch (Exception ex)
{
throw new InvalidNzbException("Invalid NZB: Unable to parse [{0}]", ex, filename);
}
} }
} }
} }