Skip to content

Commit bb14185

Browse files
Store assembly table as XML to eliminate BinaryFormatter security issues.
1 parent 4dae04f commit bb14185

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

ClearScript/Util/AssemblyTable.NetFramework.cs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
using System;
55
using System.Collections.Concurrent;
6+
using System.Collections.Generic;
67
using System.IO;
8+
using System.Linq;
79
using System.Reflection;
8-
using System.Runtime.Serialization.Formatters.Binary;
10+
using System.Xml.Serialization;
911
using Microsoft.Win32;
1012

1113
namespace Microsoft.ClearScript.Util
@@ -71,8 +73,8 @@ private static bool ReadAssemblyTable(string rootPath)
7173
{
7274
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
7375
{
74-
var formatter = new BinaryFormatter();
75-
table = (ConcurrentDictionary<string, string>)formatter.Deserialize(stream);
76+
var serializer = new XmlSerializer(typeof(AssemblyTableData));
77+
table = ((AssemblyTableData)serializer.Deserialize(stream)).CreateTable();
7678
}
7779
}
7880
});
@@ -120,8 +122,8 @@ private static bool WriteAssemblyTable(string rootPath)
120122
var filePath = GetFilePath(rootPath);
121123
using (var stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
122124
{
123-
var formatter = new BinaryFormatter();
124-
formatter.Serialize(stream, table);
125+
var serializer = new XmlSerializer(typeof(AssemblyTableData));
126+
serializer.Serialize(stream, new AssemblyTableData(table));
125127
}
126128
});
127129
}
@@ -131,7 +133,7 @@ private static string GetFilePath(string rootPath)
131133
var dirPath = Path.Combine(rootPath, GetRuntimeVersionDirectoryName());
132134
Directory.CreateDirectory(dirPath);
133135

134-
return Path.Combine(dirPath, "AssemblyTable.bin");
136+
return Path.Combine(dirPath, "AssemblyTable.xml");
135137
}
136138

137139
private static string GetRuntimeVersionDirectoryName()
@@ -142,4 +144,43 @@ private static string GetRuntimeVersionDirectoryName()
142144

143145
#endregion
144146
}
147+
148+
/// <exclude/>
149+
[XmlRoot("AssemblyTable")]
150+
public sealed class AssemblyTableData
151+
{
152+
/// <exclude/>
153+
[XmlArray] public List<Entry> Entries;
154+
155+
/// <exclude/>
156+
public AssemblyTableData()
157+
{
158+
}
159+
160+
/// <exclude/>
161+
public AssemblyTableData(ConcurrentDictionary<string, string> table)
162+
{
163+
Entries = table.Select(pair => new Entry { Name = pair.Key, FullName = pair.Value }).ToList();
164+
}
165+
166+
/// <exclude/>
167+
public ConcurrentDictionary<string, string> CreateTable()
168+
{
169+
return (Entries is null) ? null : new ConcurrentDictionary<string, string>(Entries.Select(entry => new KeyValuePair<string, string>(entry.Name, entry.FullName)));
170+
}
171+
172+
#region Nested type: Entry
173+
174+
/// <exclude/>
175+
public sealed class Entry
176+
{
177+
/// <exclude/>
178+
[XmlAttribute] public string Name { get; set; }
179+
180+
/// <exclude/>
181+
[XmlAttribute] public string FullName { get; set; }
182+
}
183+
184+
#endregion
185+
}
145186
}

0 commit comments

Comments
 (0)