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