Merge pull request #376 from opengenesislink/fix/iar-control-xml-utf8-compat-20261104

fix(iar): correct UTF-8 archive.xml and safely accept legacy OpenSim XML
This commit is contained in:
OpenGenesis (MPL 2.0) authored and GitHub committed 2026-11-04 11:28:42 +01:00
commit 7a745a90b4
5 files changed
+101 -23

No files matched your search

@@ -160,15 +160,37 @@ namespace NexVerse.RegionModules.Archives
private static void ValidateControlFile(byte[] payload)
{
using MemoryStream stream = new(payload, writable: false);
using XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings
XmlReaderSettings settings = new()
{
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null,
MaxCharactersInDocument = MaxControlBytes,
MaxCharactersFromEntities = 0
});
XDocument document = XDocument.Load(reader);
};
XDocument document;
// Legacy OpenSim generated this exact UTF-16 declaration through
// XmlTextWriter(StringWriter), but TarArchiveWriter persisted UTF-8
// bytes without a UTF-16 BOM. Preserve compatibility with those
// already-produced control files, without disabling XML security.
// The special case requires a byte-exact legacy declaration and
// strictly valid UTF-8; other files use the normal XML byte parser.
const string legacyDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
byte[] legacyPrefix = Encoding.ASCII.GetBytes(legacyDeclaration);
bool legacyUtf8 = payload.AsSpan().StartsWith(legacyPrefix);
if (legacyUtf8)
{
using StringReader decoded = new(new UTF8Encoding(false, true).GetString(payload));
using XmlReader reader = XmlReader.Create(decoded, settings);
document = XDocument.Load(reader);
}
else
{
using MemoryStream stream = new(payload, writable: false);
using XmlReader reader = XmlReader.Create(stream, settings);
document = XDocument.Load(reader);
}
XElement archive = document.Element("archive");
if (archive == null ||
!int.TryParse((string)archive.Attribute("major_version"), out int major) ||
@@ -623,25 +623,24 @@ namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver
m_log.InfoFormat("[OGL-IAR]: Erzeuge IAR-Archivformat Version {0}.{1}.", majorVersion, minorVersion);
StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
xtw.WriteStartElement("archive");
xtw.WriteAttributeString("major_version", majorVersion.ToString());
xtw.WriteAttributeString("minor_version", minorVersion.ToString());
// TarArchiveWriter.WriteFile(string) encodes this string as UTF-8.
// A StringWriter-backed XmlTextWriter would instead declare
// encoding="utf-16", making the UTF-8 archive.xml fail byte-stream
// XML validation (no UTF-16 BOM). Serialize to a UTF-8 stream.
using MemoryStream controlXml = new();
using (XmlTextWriter xtw = new(controlXml, new System.Text.UTF8Encoding(false)))
{
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
xtw.WriteStartElement("archive");
xtw.WriteAttributeString("major_version", majorVersion.ToString());
xtw.WriteAttributeString("minor_version", minorVersion.ToString());
xtw.WriteElementString("assets_included", SaveAssets.ToString());
xtw.WriteEndElement();
xtw.Flush();
}
xtw.WriteElementString("assets_included", SaveAssets.ToString());
xtw.WriteEndElement();
xtw.Flush();
xtw.Close();
String s = sw.ToString();
sw.Close();
return s;
return System.Text.Encoding.UTF8.GetString(controlXml.ToArray());
}
}
}
+18
View File
@@ -133,6 +133,24 @@ Das URI-Präfix der Stadt-NexVerse-G1-Installation lautet
keine Schreibberechtigung für MKCOL und PUT. TLS-Zertifikatsprüfung
darf auch bei der Fehlersuche nicht abgeschaltet werden.
### IAR-Kontrolldatei: UTF-8/UTF-16-Kompatibilität
Der native OpenSimulator-IAR-Schreiber erzeugte bisher `archive.xml`
über einen `XmlTextWriter(StringWriter)`, der `encoding="utf-16"`
deklarierte. Der TAR-Schreiber speicherte die zurückgegebene Zeichenkette
jedoch als UTF-8 ohne BOM. Die strengere IAR-Vorabprüfung verweigerte
deshalb das fertige Archiv mit `There is no Unicode byte order mark.
Cannot switch to Unicode.`, bevor der WebDAV-Upload erfolgen konnte.
Neue IAR-Kontrolldateien werden jetzt als tatsächliche UTF-8-XML-Daten
mit passender Deklaration erzeugt. Bereits exportierte, historisch
inkonsistent deklarierte OpenSim-IARs werden nur bei **exakt bekanntem
Legacy-Prolog** und **strikt gültigem UTF-8** akzeptiert. DTD, XXE,
Pfad-Traversal, TAR-Prüfsummen, Größen- und Entpackungsgrenzen bleiben
unverändert geschützt. Ein Export ist erst nach erfolgreicher
Integritätsprüfung **und** bestätigt abgeschlossenem WebDAV-Upload
als gesichert anzusehen.
### Große Inventare: NexBus-Laufzeit und fehlende Asset-Referenzen
Der NexBus-HTTP-Endpunkt darf nicht durch eine komplette IAR-Inventararchivierung
+29 -1
View File
@@ -74,6 +74,34 @@ internal static class Program
var inventory = new Entry("inventory/Test/item.xml", Encoding.UTF8.GetBytes("<InventoryItem/>"));
var asset = new Entry("assets/01234567-89ab-cdef-0123-456789abcdef_texture.jp2", new byte[] { 1, 2, 3 });
Evaluate(true, "valid IAR", new[] { xml, inventory, asset });
// Legacy OpenSim wrote UTF-8 bytes under an XML declaration that
// incorrectly said UTF-16 (StringWriter + TarArchiveWriter).
string legacyXml = "<?xml version=\"1.0\" encoding=\"utf-16\"?><archive major_version=\"0\" minor_version=\"3\"><assets_included>True</assets_included></archive>";
var legacyControl = new Entry("archive.xml", Encoding.UTF8.GetBytes(legacyXml));
Evaluate(true, "legacy UTF-8 data with UTF-16 declaration", new[] { legacyControl, inventory, asset });
string correctedXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><archive major_version=\"0\" minor_version=\"3\"/>";
Evaluate(true, "correct UTF-8 XML declaration",
new[] { new Entry("archive.xml", Encoding.UTF8.GetBytes(correctedXml)), inventory, asset });
// Genuine UTF-16 with BOM still uses the normal byte-stream parser.
byte[] utf16Body = Encoding.Unicode.GetBytes(legacyXml);
byte[] utf16WithBom = new byte[utf16Body.Length + 2];
utf16WithBom[0] = 0xff; utf16WithBom[1] = 0xfe;
Buffer.BlockCopy(utf16Body, 0, utf16WithBom, 2, utf16Body.Length);
Evaluate(true, "genuine UTF-16 with BOM",
new[] { new Entry("archive.xml", utf16WithBom), inventory });
string legacyDtd = "<?xml version=\"1.0\" encoding=\"utf-16\"?><!DOCTYPE archive [<!ENTITY payload SYSTEM \"file:///etc/passwd\">]><archive major_version=\"0\" minor_version=\"3\"/>";
Evaluate(false, "legacy declaration must not permit DTD",
new[] { new Entry("archive.xml", Encoding.UTF8.GetBytes(legacyDtd)), inventory });
// Strict UTF-8 decoder must reject illegal bytes; no replacement chars.
byte[] malformedUtf8 = Encoding.UTF8.GetBytes(legacyXml);
malformedUtf8[malformedUtf8.Length - 12] = 0xff;
Evaluate(false, "legacy declaration rejects malformed UTF-8",
new[] { new Entry("archive.xml", malformedUtf8), inventory });
Evaluate(false, "traversal", new[] { xml, new Entry("inventory/../evil.xml", new byte[] { 1 }) });
Evaluate(false, "absolute path", new[] { xml, new Entry("/inventory/evil.xml", new byte[] { 1 }) });
Evaluate(false, "duplicate inventory path", new[] { xml, inventory, inventory });
@@ -101,7 +129,7 @@ internal static class Program
"10 GiB compressed archive permitted");
Assert(!OglIarInspector.IsAllowedCompressedSize(tenGiB + 1),
"compressed IAR beyond 10 GiB rejected");
Console.WriteLine("OGL IAR bounded preflight: PASS (11 fixtures + size boundaries)");
Console.WriteLine("OGL IAR bounded preflight: PASS (16 fixtures + size boundaries)");
return 0;
}
}
+11
View File
@@ -45,6 +45,17 @@ assert "MaxExpandedBytes = 40L * 1024 * 1024 * 1024" in policy
assert "if (!IsAllowedCompressedSize(info.Length))" in policy
assert "MaxFileBytes = 64L * 1024 * 1024" in policy
assert "MaxEntries = 100000" in policy
# The IAR control XML must advertise the UTF-8 encoding actually stored by
# TarArchiveWriter.WriteFile(string), while legacy archives remain inspectable
# without permitting arbitrary encoding spoofing or XML external entities.
assert 'new(controlXml, new System.Text.UTF8Encoding(false))' in src["writer"]
assert 'Encoding.UTF8.GetString(controlXml.ToArray())' in src["writer"]
assert 'XmlTextWriter xtw = new XmlTextWriter(sw)' not in src["writer"]
assert 'const string legacyDeclaration = ' in policy
assert 'payload.AsSpan().StartsWith(legacyPrefix)' in policy
assert 'new UTF8Encoding(false, true).GetString(payload)' in policy
assert 'DtdProcessing = DtdProcessing.Prohibit' in policy
assert 'XmlResolver = null' in policy
# Terminal handoff must not disappear after the last asset-progress log.
assert 'GC.Collect();\n PerformAssetsRequestCallback(m_timeout);' not in src["assets"]
assert "Archivabschluss wird gestartet" in src["assets"]