OutboundUrlFilter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using System.Net;
  31. using System.Reflection;
  32. using log4net;
  33. using LukeSkywalker.IPNetwork;
  34. using Nini.Config;
  35. using IPNetwork = LukeSkywalker.IPNetwork.IPNetwork;
  36. namespace OpenSim.Framework
  37. {
  38. public class OutboundUrlFilter
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. public string Name { get; private set; }
  42. private List<IPNetwork> m_blacklistNetworks;
  43. private List<IPEndPoint> m_blacklistEndPoints;
  44. private List<IPNetwork> m_blacklistExceptionNetworks;
  45. private List<IPEndPoint> m_blacklistExceptionEndPoints;
  46. public OutboundUrlFilter(
  47. string name,
  48. List<IPNetwork> blacklistNetworks, List<IPEndPoint> blacklistEndPoints,
  49. List<IPNetwork> blacklistExceptionNetworks, List<IPEndPoint> blacklistExceptionEndPoints)
  50. {
  51. Name = name;
  52. m_blacklistNetworks = blacklistNetworks;
  53. m_blacklistEndPoints = blacklistEndPoints;
  54. m_blacklistExceptionNetworks = blacklistExceptionNetworks;
  55. m_blacklistExceptionEndPoints = blacklistExceptionEndPoints;
  56. }
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="OpenSim.Framework.OutboundUrlFilter"/> class.
  59. /// </summary>
  60. /// <param name="name">Name of the filter for logging purposes.</param>
  61. /// <param name="config">Filter configuration</param>
  62. public OutboundUrlFilter(string name, IConfigSource config)
  63. {
  64. Name = name;
  65. string configBlacklist
  66. = "0.0.0.0/8|10.0.0.0/8|100.64.0.0/10|127.0.0.0/8|169.254.0.0/16|172.16.0.0/12|192.0.0.0/24|192.0.2.0/24|192.88.99.0/24|192.168.0.0/16|198.18.0.0/15|198.51.100.0/24|203.0.113.0/24|224.0.0.0/4|240.0.0.0/4|255.255.255.255/32";
  67. string configBlacklistExceptions = "";
  68. IConfig networkConfig = config.Configs["Network"];
  69. if (networkConfig != null)
  70. {
  71. configBlacklist = networkConfig.GetString("OutboundDisallowForUserScripts", configBlacklist);
  72. configBlacklistExceptions
  73. = networkConfig.GetString("OutboundDisallowForUserScriptsExcept", configBlacklistExceptions);
  74. }
  75. m_log.DebugFormat(
  76. "[OUTBOUND URL FILTER]: OutboundDisallowForUserScripts for {0} is [{1}]", Name, configBlacklist);
  77. m_log.DebugFormat(
  78. "[OUTBOUND URL FILTER]: OutboundDisallowForUserScriptsExcept for {0} is [{1}]", Name, configBlacklistExceptions);
  79. OutboundUrlFilter.ParseConfigList(
  80. configBlacklist, Name, out m_blacklistNetworks, out m_blacklistEndPoints);
  81. OutboundUrlFilter.ParseConfigList(
  82. configBlacklistExceptions, Name, out m_blacklistExceptionNetworks, out m_blacklistExceptionEndPoints);
  83. }
  84. private static void ParseConfigList(
  85. string fullConfigEntry, string filterName, out List<IPNetwork> networks, out List<IPEndPoint> endPoints)
  86. {
  87. // Parse blacklist
  88. string[] configBlacklistEntries
  89. = fullConfigEntry.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  90. configBlacklistEntries = configBlacklistEntries.Select(e => e.Trim()).ToArray();
  91. networks = new List<IPNetwork>();
  92. endPoints = new List<IPEndPoint>();
  93. foreach (string configEntry in configBlacklistEntries)
  94. {
  95. if (configEntry.Contains("/"))
  96. {
  97. IPNetwork network;
  98. if (!IPNetwork.TryParse(configEntry, out network))
  99. {
  100. m_log.ErrorFormat(
  101. "[OUTBOUND URL FILTER]: Entry [{0}] is invalid network for {1}", configEntry, filterName);
  102. continue;
  103. }
  104. networks.Add(network);
  105. }
  106. else
  107. {
  108. Uri configEntryUri;
  109. if (!Uri.TryCreate("http://" + configEntry, UriKind.Absolute, out configEntryUri))
  110. {
  111. m_log.ErrorFormat(
  112. "[OUTBOUND URL FILTER]: EndPoint entry [{0}] is invalid endpoint for {1}",
  113. configEntry, filterName);
  114. continue;
  115. }
  116. IPAddress[] addresses = Dns.GetHostAddresses(configEntryUri.Host);
  117. foreach (IPAddress addr in addresses)
  118. {
  119. if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  120. {
  121. // m_log.DebugFormat("[OUTBOUND URL FILTER]: Found address [{0}] in config", addr);
  122. IPEndPoint configEntryEp = new IPEndPoint(addr, configEntryUri.Port);
  123. endPoints.Add(configEntryEp);
  124. // m_log.DebugFormat("[OUTBOUND URL FILTER]: Added blacklist exception [{0}]", configEntryEp);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Determines if an url is in a list of networks and endpoints.
  132. /// </summary>
  133. /// <returns></returns>
  134. /// <param name="url">IP address</param>
  135. /// <param name="port"></param>
  136. /// <param name="networks">Networks.</param>
  137. /// <param name="endPoints">End points.</param>
  138. /// <param name="filterName">Filter name.</param>
  139. private static bool IsInNetwork(
  140. IPAddress addr, int port, List<IPNetwork> networks, List<IPEndPoint> endPoints, string filterName)
  141. {
  142. foreach (IPNetwork ipn in networks)
  143. {
  144. // m_log.DebugFormat(
  145. // "[OUTBOUND URL FILTER]: Checking [{0}] against network [{1}]", addr, ipn);
  146. if (IPNetwork.Contains(ipn, addr))
  147. {
  148. // m_log.DebugFormat(
  149. // "[OUTBOUND URL FILTER]: Found [{0}] in network [{1}]", addr, ipn);
  150. return true;
  151. }
  152. }
  153. // m_log.DebugFormat("[OUTBOUND URL FILTER]: Found address [{0}]", addr);
  154. foreach (IPEndPoint ep in endPoints)
  155. {
  156. // m_log.DebugFormat(
  157. // "[OUTBOUND URL FILTER]: Checking [{0}:{1}] against endpoint [{2}]",
  158. // addr, port, ep);
  159. if (addr.Equals(ep.Address) && port == ep.Port)
  160. {
  161. // m_log.DebugFormat(
  162. // "[OUTBOUND URL FILTER]: Found [{0}:{1}] in endpoint [{2}]", addr, port, ep);
  163. return true;
  164. }
  165. }
  166. // m_log.DebugFormat("[OUTBOUND URL FILTER]: Did not find [{0}:{1}] in list", addr, port);
  167. return false;
  168. }
  169. /// <summary>
  170. /// Checks whether the given url is allowed by the filter.
  171. /// </summary>
  172. /// <returns></returns>
  173. public bool CheckAllowed(Uri url)
  174. {
  175. bool allowed = true;
  176. // Check that we are permitted to make calls to this endpoint.
  177. bool foundIpv4Address = false;
  178. IPAddress[] addresses = null;
  179. try
  180. {
  181. addresses = Dns.GetHostAddresses(url.Host);
  182. }
  183. catch
  184. {
  185. // If there is a DNS error, we can't stop the script!
  186. return true;
  187. }
  188. foreach (IPAddress addr in addresses)
  189. {
  190. if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  191. {
  192. // m_log.DebugFormat("[OUTBOUND URL FILTER]: Found address [{0}]", addr);
  193. foundIpv4Address = true;
  194. // Check blacklist
  195. if (OutboundUrlFilter.IsInNetwork(addr, url.Port, m_blacklistNetworks, m_blacklistEndPoints, Name))
  196. {
  197. // m_log.DebugFormat("[OUTBOUND URL FILTER]: Found [{0}] in blacklist for {1}", url, Name);
  198. // Check blacklist exceptions
  199. allowed
  200. = OutboundUrlFilter.IsInNetwork(
  201. addr, url.Port, m_blacklistExceptionNetworks, m_blacklistExceptionEndPoints, Name);
  202. // if (allowed)
  203. // m_log.DebugFormat("[OUTBOUND URL FILTER]: Found [{0}] in whitelist for {1}", url, Name);
  204. }
  205. }
  206. // Found at least one address in a blacklist and not a blacklist exception
  207. if (!allowed)
  208. return false;
  209. // else
  210. // m_log.DebugFormat("[OUTBOUND URL FILTER]: URL [{0}] not in blacklist for {1}", url, Name);
  211. }
  212. // We do not know how to handle IPv6 securely yet.
  213. if (!foundIpv4Address)
  214. return false;
  215. // m_log.DebugFormat("[OUTBOUND URL FILTER]: Allowing request [{0}]", url);
  216. return allowed;
  217. }
  218. }
  219. }