FlashCrossDomainPolicy.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // FlashCrossDomainPolicy.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. namespace System.Net.Policy {
  35. partial class FlashCrossDomainPolicy : BaseDomainPolicy {
  36. private string site_control;
  37. public FlashCrossDomainPolicy ()
  38. {
  39. AllowedAccesses = new List<AllowAccessFrom> ();
  40. AllowedHttpRequestHeaders = new List<AllowHttpRequestHeadersFrom> ();
  41. }
  42. public List<AllowAccessFrom> AllowedAccesses { get; private set; }
  43. public List<AllowHttpRequestHeadersFrom> AllowedHttpRequestHeaders { get; private set; }
  44. public string SiteControl {
  45. get { return String.IsNullOrEmpty (site_control) ? "all" : site_control; }
  46. set { site_control = value; }
  47. }
  48. public override bool IsAllowed (WebRequest request)
  49. {
  50. return IsAllowed (request.RequestUri, request.Headers.AllKeys);
  51. }
  52. public bool IsAllowed (Uri uri, string [] headerKeys)
  53. {
  54. switch (SiteControl) {
  55. case "all":
  56. case "master-only":
  57. case "by-ftp-filename":
  58. break;
  59. default:
  60. // others, e.g. 'none', are not supported/accepted
  61. return false;
  62. }
  63. if (AllowedAccesses.Count > 0 &&
  64. !AllowedAccesses.Any (a => a.IsAllowed (uri, headerKeys)))
  65. return false;
  66. if (AllowedHttpRequestHeaders.Count > 0 &&
  67. AllowedHttpRequestHeaders.Any (h => h.IsRejected (uri, headerKeys)))
  68. return false;
  69. return true;
  70. }
  71. public class AllowAccessFrom {
  72. public AllowAccessFrom ()
  73. {
  74. Secure = true; // true by default
  75. }
  76. public string Domain { get; set; }
  77. public bool AllowAnyPort { get; set; }
  78. public int [] ToPorts { get; set; }
  79. public bool Secure { get; set; }
  80. public bool IsAllowed (Uri uri, string [] headerKeys)
  81. {
  82. // "A Flash policy file must allow access to all domains to be used by the Silverlight runtime."
  83. // http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx
  84. if (Domain != "*")
  85. return false;
  86. if (!AllowAnyPort && ToPorts != null && Array.IndexOf (ToPorts, uri.Port) < 0)
  87. return false;
  88. // if Secure is false then it allows applications from HTTP to download data from HTTPS servers
  89. if (!Secure)
  90. return true;
  91. // if Secure is true then data on HTTPS servers can only be accessed by application on HTTPS servers
  92. if (uri.Scheme == Uri.UriSchemeHttps)
  93. return (ApplicationUri.Scheme == Uri.UriSchemeHttps);
  94. // otherwise FILE/HTTP applications can access HTTP uris
  95. return true;
  96. }
  97. }
  98. public class AllowHttpRequestHeadersFrom {
  99. public AllowHttpRequestHeadersFrom ()
  100. {
  101. Headers = new Headers ();
  102. }
  103. public string Domain { get; set; }
  104. public bool AllowAllHeaders { get; set; }
  105. public Headers Headers { get; private set; }
  106. public bool Secure { get; set; }
  107. public bool IsRejected (Uri uri, string [] headerKeys)
  108. {
  109. // "A Flash policy file must allow access to all domains to be used by the Silverlight runtime."
  110. // http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx
  111. if (Domain != "*")
  112. return false;
  113. if (Headers.IsAllowed (headerKeys))
  114. return false;
  115. // if Secure is false then it allows applications from HTTP to download data from HTTPS servers
  116. if (!Secure)
  117. return true;
  118. // if Secure is true then only application on HTTPS servers can access data on HTTPS servers
  119. if (ApplicationUri.Scheme == Uri.UriSchemeHttps)
  120. return (uri.Scheme == Uri.UriSchemeHttps);
  121. // otherwise FILE/HTTP applications can access HTTP uris
  122. return true;
  123. }
  124. }
  125. }
  126. }
  127. #endif