FlashCrossDomainPolicyParser.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // FlashCrossDomainPolicyParser.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Moonlight List ([email protected])
  7. //
  8. // Copyright (C) 2009-2010 Novell, Inc. http://www.novell.com
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if MOBILE
  30. using System;
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using System.Linq;
  34. using System.Xml;
  35. /*
  36. Specification: http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html
  37. # This grammar is based on the xsd from Adobe, but the schema is wrong.
  38. # It should have used interleave (all). Some crossdomain.xml are invalidated.
  39. # (For example, try mono-xmltool --validate-xsd http://www.adobe.com/xml/schemas/PolicyFile.xsd http://twitter.com/crossdomain.xml)
  40. default namespace = ""
  41. grammar {
  42. start = cross-domain-policy
  43. cross-domain-policy = element cross-domain-policy {
  44. element site-control {
  45. attribute permitted-cross-domain-policies {
  46. "all" | "by-contract-type" | "by-ftp-filename" | "master-only" | "none"
  47. }
  48. }?,
  49. element allow-access-from {
  50. attribute domain { text },
  51. attribute to-ports { text }?,
  52. attribute secure { xs:boolean }?
  53. }*,
  54. element allow-http-request-headers-from {
  55. attribute domain { text },
  56. attribute headers { text },
  57. attribute secure { xs:boolean }?
  58. }*,
  59. element allow-access-from-identity {
  60. element signatory {
  61. element certificate {
  62. attribute fingerprint { text },
  63. attribute fingerprint-algorithm { text }
  64. }
  65. }
  66. }*
  67. }
  68. }
  69. */
  70. namespace System.Net.Policy {
  71. partial class FlashCrossDomainPolicy {
  72. static bool ReadBooleanAttribute (string attribute)
  73. {
  74. switch (attribute) {
  75. case null:
  76. case "true":
  77. return true;
  78. case "false":
  79. return false;
  80. default:
  81. throw new XmlException ();
  82. }
  83. }
  84. // only "domain" and "secure" attributes are allowed - anything else is considered invalid
  85. static AllowAccessFrom CreateAllowAccessFrom (XmlReader reader)
  86. {
  87. int n = reader.AttributeCount;
  88. string domain = reader.GetAttribute ("domain");
  89. if (domain != null)
  90. n--;
  91. string secure = reader.GetAttribute ("secure");
  92. if (secure != null)
  93. n--;
  94. if (n != 0)
  95. throw new XmlException ("unknown/unsupported attributes");
  96. return new AllowAccessFrom () { Domain = domain, Secure = ReadBooleanAttribute (secure) };
  97. }
  98. // only "domain", "secure" and "headers" attributes are allowed - anything else is considered invalid
  99. static AllowHttpRequestHeadersFrom CreateAllowHttpRequestHeadersFrom (XmlReader reader)
  100. {
  101. int n = reader.AttributeCount;
  102. string domain = reader.GetAttribute ("domain");
  103. if (domain != null)
  104. n--;
  105. string secure = reader.GetAttribute ("secure");
  106. if (secure != null)
  107. n--;
  108. string headers = reader.GetAttribute ("headers");
  109. if (headers != null)
  110. n--;
  111. if (n != 0)
  112. throw new XmlException ("unknown/unsupported attributes");
  113. var h = new AllowHttpRequestHeadersFrom () { Domain = domain, Secure = ReadBooleanAttribute (secure) };
  114. h.Headers.SetHeaders (headers);
  115. return h;
  116. }
  117. // only "permitted-cross-domain-policies" attribute is allowed - anything else is considered invalid
  118. static string GetSiteControl (XmlReader reader)
  119. {
  120. int n = reader.AttributeCount;
  121. string site = reader.GetAttribute ("permitted-cross-domain-policies");
  122. if (site != null)
  123. n--;
  124. if (n != 0)
  125. throw new XmlException ("unknown/unsupported attributes");
  126. return site;
  127. }
  128. static public ICrossDomainPolicy FromStream (Stream stream)
  129. {
  130. FlashCrossDomainPolicy cdp = new FlashCrossDomainPolicy ();
  131. // Silverlight accepts whitespaces before the XML - which is invalid XML
  132. StreamReader sr = new StreamReader (stream);
  133. while (Char.IsWhiteSpace ((char) sr.Peek ()))
  134. sr.Read ();
  135. XmlReaderSettings policy_settings = new XmlReaderSettings ();
  136. policy_settings.DtdProcessing = DtdProcessing.Ignore;
  137. using (XmlReader reader = XmlReader.Create (sr, policy_settings)) {
  138. reader.MoveToContent ();
  139. if (reader.HasAttributes || reader.IsEmptyElement) {
  140. reader.Skip ();
  141. return null;
  142. }
  143. while (!reader.EOF) {
  144. reader.ReadStartElement ("cross-domain-policy", String.Empty);
  145. for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
  146. if (reader.NodeType != XmlNodeType.Element) {
  147. reader.Skip ();
  148. continue;
  149. }
  150. switch (reader.LocalName) {
  151. case "site-control":
  152. cdp.SiteControl = GetSiteControl (reader);
  153. reader.Skip ();
  154. break;
  155. case "allow-access-from":
  156. var a = CreateAllowAccessFrom (reader);
  157. cdp.AllowedAccesses.Add (a);
  158. reader.Skip ();
  159. break;
  160. case "allow-http-request-headers-from":
  161. var h = CreateAllowHttpRequestHeadersFrom (reader);
  162. cdp.AllowedHttpRequestHeaders.Add (h);
  163. reader.Skip ();
  164. break;
  165. default:
  166. reader.Skip ();
  167. return null;
  168. }
  169. }
  170. reader.ReadEndElement ();
  171. reader.MoveToContent ();
  172. }
  173. }
  174. // if none supplied set a default for headers
  175. if (cdp.AllowedHttpRequestHeaders.Count == 0) {
  176. var h = new AllowHttpRequestHeadersFrom () { Domain = "*", Secure = true };
  177. h.Headers.SetHeaders (null); // defaults
  178. cdp.AllowedHttpRequestHeaders.Add (h);
  179. }
  180. return cdp;
  181. }
  182. }
  183. }
  184. #endif