diff --git a/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs b/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs
index b5567b08..9fcd3618 100644
--- a/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs
+++ b/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs
@@ -1223,7 +1223,14 @@ namespace OpenSim.Region.ScriptEngine.Yengine
return false;
try
{
- Convert.FromBase64String(snapshotN.InnerText);
+ byte[] migration = Convert.FromBase64String(snapshotN.InnerText);
+ // The first byte is YEngine's explicit migration version.
+ // Reject definitely incompatible OAR data before replacing
+ // an existing .state file. Full object-graph validation
+ // still requires the matching compiled script on staging.
+ if (migration.Length < 3 ||
+ migration[0] != XMRInstance.migrationVersion)
+ return false;
}
catch (FormatException)
{
diff --git a/doc/NexVerse/BINARYFORMATTER_RUNTIME_CLEANUP.md b/doc/NexVerse/BINARYFORMATTER_RUNTIME_CLEANUP.md
index ebbf055c..484c6074 100644
--- a/doc/NexVerse/BINARYFORMATTER_RUNTIME_CLEANUP.md
+++ b/doc/NexVerse/BINARYFORMATTER_RUNTIME_CLEANUP.md
@@ -106,3 +106,10 @@ Legacy XEngine imports retain their distinct Variables-based format.
Script timers are not changed until the YEngine binary snapshot was
decoded successfully. No automatic historic graph execution is enabled.
+
+Import preflight also rejects YEngine snapshot headers whose migration version
+does not match `XMRInstance.migrationVersion`. This protects existing state
+files from obviously incompatible payloads; it **does not** prove that the
+remaining object graph is valid or compatible. Only a controlled staged
+restore with its compiled script can establish that. No unsafe object
+deserialization is used in preflight.
diff --git a/tools/ci/YEngineBinaryFormatterRegression/Program.cs b/tools/ci/YEngineBinaryFormatterRegression/Program.cs
index 777a94c1..dcdd318b 100644
--- a/tools/ci/YEngineBinaryFormatterRegression/Program.cs
+++ b/tools/ci/YEngineBinaryFormatterRegression/Program.cs
@@ -237,10 +237,17 @@ try
Check(File.ReadAllText(importedPath) == beforeImport,
"oversized OAR XML left saved state unchanged");
- Check(engine.SetXMLState(stateId, importPrefix + "AQID" + importSuffix),
- "valid bounded YEngine OAR state imported");
- Check(File.ReadAllText(importedPath).Contains("AQID"),
- "valid OAR state atomically installed");
+ Check(!engine.SetXMLState(stateId, importPrefix + "AQID" + importSuffix),
+ "unsupported YEngine migration version rejected before import");
+ Check(File.ReadAllText(importedPath) == beforeImport,
+ "unknown-version OAR state did not overwrite existing state");
+ string matchingHeader = Convert.ToBase64String(
+ new byte[] { XMRInstance.migrationVersion, 16, 0 });
+ Check(engine.SetXMLState(stateId, importPrefix + matchingHeader + importSuffix),
+ "bounded YEngine OAR header imported without full script decoding");
+ Check(File.ReadAllText(importedPath).Contains(
+ "" + matchingHeader + ""),
+ "matching-header OAR state atomically installed");
Check(Directory.GetFiles(Path.GetDirectoryName(importedPath)!).Length == 1,
"OAR importer left no temporary files");
diff --git a/tools/ci/verify_yengine_binaryformatter_safety.py b/tools/ci/verify_yengine_binaryformatter_safety.py
index 07dc31f6..0d6aeafa 100644
--- a/tools/ci/verify_yengine_binaryformatter_safety.py
+++ b/tools/ci/verify_yengine_binaryformatter_safety.py
@@ -34,6 +34,7 @@ assert "doc = XMRInstance.ParsePersistedStateXml(xml)" in engine
assert "XMRInstance.WriteStateFileAtomically(statePath, scriptStateN.OuterXml)" in engine
assert "using (FileStream ss = File.Create(statePath))" not in engine
assert "Convert.FromBase64String(snapshotN.InnerText)" in engine
+assert "migration[0] != XMRInstance.migrationVersion" in engine
assert "restoredScriptTime.HasValue" in ctor
assert "DtdProcessing = DtdProcessing.Prohibit" in capture
assert "File.Move(temporary, statePath, overwrite: true)" in capture