Xml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. namespace Wayne.Lib
  7. {
  8. /// <summary>
  9. /// A util class to create <see cref="XmlDocument"/> and <see cref="XmlTextReader"/> classes resistant to XXE attacks.>
  10. /// </summary>
  11. /// <seealso>
  12. /// <cref>https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet</cref>
  13. /// </seealso>
  14. public static class Xml
  15. {
  16. #region XmlDocument Constructors
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="XmlDocument"/> class.
  19. /// </summary>
  20. public static XmlDocument Document()
  21. {
  22. return FixDocument(new XmlDocument());
  23. }
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="XmlDocument"/> class with the specified <see cref="XmlNameTable"/>.
  26. /// </summary>
  27. public static XmlDocument Document(XmlNameTable nt)
  28. {
  29. return FixDocument(new XmlDocument(nt));
  30. }
  31. #endregion
  32. #region XmlReader Constructors
  33. /// <summary>
  34. /// Replace <see cref="XmlReader"/> Create method. Creates a new <see cref="XmlReader"/> instance using the specified <see cref="Stream"/> with default settings.
  35. /// </summary>
  36. public static XmlReader Reader(Stream input)
  37. {
  38. return XmlReader.Create(input, FixSettings());
  39. }
  40. /// <summary>
  41. /// Replace <see cref="XmlReader"/> Create method. Creates a new <see cref="XmlReader"/> instance using the specified <see cref="Stream"/> and <see cref="XmlReaderSettings"/>.
  42. /// </summary>
  43. public static XmlReader Reader(Stream input, XmlReaderSettings settings)
  44. {
  45. return XmlReader.Create(input, FixSettings(settings));
  46. }
  47. /// <summary>
  48. /// Replace <see cref="XmlReader"/> Create method. Creates a new <see cref="XmlReader"/> instance by using the specified <see cref="System.IO.TextReader"/>.
  49. /// </summary>
  50. public static XmlReader Reader(TextReader input)
  51. {
  52. return XmlReader.Create(input, FixSettings());
  53. }
  54. /// <summary>
  55. /// Replace <see cref="XmlReader"/> Create method. Creates a new <see cref="XmlReader"/> instance by using the specified <see cref="System.IO.TextReader"/> and <see cref="XmlReaderSettings"/>.
  56. /// </summary>
  57. public static XmlReader Reader(TextReader input, XmlReaderSettings settings)
  58. {
  59. return XmlReader.Create(input, FixSettings(settings));
  60. }
  61. #endregion
  62. #region GetEncoding
  63. /// <summary>
  64. /// Gets the encoded <see cref="string"/> of the undeling xml data. Uses <see cref="XmlTextReader"/> to read the data.
  65. /// </summary>
  66. public static string GetEncodedString(byte[] data, int offset, int length)
  67. {
  68. string ret = null;
  69. System.Text.Encoding encoding = GetEncoding(data, offset, length);
  70. if (encoding != null && encoding.CodePage != 0)
  71. {
  72. ret = encoding.GetString(data, offset, length);
  73. }
  74. else
  75. {
  76. ret = System.Text.Encoding.UTF8.GetString(data, offset, length);
  77. }
  78. return ret;
  79. }
  80. #endregion
  81. #region Serialization
  82. /// <summary>
  83. /// Serializes a class instance to <see cref="String"/>.
  84. /// </summary>
  85. public static string Serialize<T>(T instance) where T : class
  86. {
  87. var xmlWriterSettings = new XmlWriterSettings
  88. {
  89. Encoding = new UTF8Encoding(false)
  90. };
  91. using (var stream = new MemoryStream())
  92. using (var writer = XmlWriter.Create(stream, xmlWriterSettings))
  93. {
  94. var serializer = new XmlSerializer(typeof(T));
  95. serializer.Serialize(writer, instance);
  96. return System.Text.Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length);
  97. }
  98. }
  99. /// <summary>
  100. /// Deserialize a xml <see cref="String"/>.
  101. /// </summary>
  102. public static T Deserialize<T>(string xml) where T : class
  103. {
  104. using (var reader = new StringReader(xml))
  105. {
  106. var serializer = new XmlSerializer(typeof(T));
  107. return serializer.Deserialize(reader) as T;
  108. }
  109. }
  110. /// <summary>
  111. /// Deserializes XmlDocument object to Serializable object of type T.
  112. /// </summary>
  113. /// <typeparam name="T">Serializable object type as output type.</typeparam>
  114. /// <param name="document">XmlDocument object to be deserialized.</param>
  115. /// <returns>Deserialized serializable object of type T.</returns>
  116. public static T Deserialize<T>(XmlDocument document) where T : class
  117. {
  118. using (var reader = new XmlNodeReader(document))
  119. {
  120. var serializer = new XmlSerializer(typeof(T));
  121. return serializer.Deserialize(reader) as T;
  122. }
  123. }
  124. #endregion
  125. #region Implementation
  126. private static System.Text.Encoding GetEncoding(byte[] data, int offset, int length)
  127. {
  128. System.Text.Encoding encoding = null;
  129. using (var stream = new MemoryStream(data, offset, length))
  130. {
  131. using (var reader = FixTextReader(new XmlTextReader(stream)))
  132. {
  133. reader.Read();
  134. encoding = reader.Encoding;
  135. }
  136. }
  137. return encoding;
  138. }
  139. private static XmlDocument FixDocument(XmlDocument document)
  140. {
  141. document.XmlResolver = null; // Setting XmlResolver to NULL disables DTDs - Its NOT null by default.
  142. return document;
  143. }
  144. private static XmlTextReader FixTextReader(XmlTextReader reader)
  145. {
  146. #if WindowsCE || OldFramework //Dtd processing is not supprted in .Net Compact Framework
  147. #else
  148. reader.DtdProcessing = DtdProcessing.Prohibit; // Needed because the default is Parse!!
  149. #endif
  150. return reader;
  151. }
  152. private static XmlReaderSettings FixSettings()
  153. {
  154. return FixSettings(null);
  155. }
  156. private static XmlReaderSettings FixSettings(XmlReaderSettings settings)
  157. {
  158. if (settings == null)
  159. {
  160. settings = new XmlReaderSettings();
  161. }
  162. #if WindowsCE || OldFramework //Dtd processing is not supprted in .Net Compact Framework
  163. #else
  164. settings.DtdProcessing = DtdProcessing.Prohibit; // Needed because the default is Parse!!
  165. #endif
  166. return settings;
  167. }
  168. #endregion
  169. }
  170. }