NetworkParseTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using Jellyfin.Networking.Manager;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Common.Net;
  8. using MediaBrowser.Model.Net;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.Logging.Abstractions;
  11. using Moq;
  12. using Xunit;
  13. using IConfigurationManager = MediaBrowser.Common.Configuration.IConfigurationManager;
  14. namespace Jellyfin.Networking.Tests
  15. {
  16. public class NetworkParseTests
  17. {
  18. internal static IConfigurationManager GetMockConfig(NetworkConfiguration conf)
  19. {
  20. var configManager = new Mock<IConfigurationManager>
  21. {
  22. CallBase = true
  23. };
  24. configManager.Setup(x => x.GetConfiguration(It.IsAny<string>())).Returns(conf);
  25. return configManager.Object;
  26. }
  27. /// <summary>
  28. /// Checks the ability to ignore virtual interfaces.
  29. /// </summary>
  30. /// <param name="interfaces">Mock network setup, in the format (IP address, interface index, interface name) | .... </param>
  31. /// <param name="lan">LAN addresses.</param>
  32. /// <param name="value">Bind addresses that are excluded.</param>
  33. [Theory]
  34. // All valid
  35. [InlineData("192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.0/24", "[192.168.1.208/24,200.200.200.200/24]")]
  36. // eth16 only
  37. [InlineData("192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.208/24]")]
  38. // eth16 only without mask
  39. [InlineData("192.168.1.208,-16,eth16|200.200.200.200,11,eth11", "192.168.1.0/24", "[192.168.1.208/32]")]
  40. // All interfaces excluded. (including loopbacks)
  41. [InlineData("192.168.1.208/24,-16,vEthernet1|192.168.2.208/24,-16,vEthernet212|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[]")]
  42. // vEthernet1 and vEthernet212 should be excluded.
  43. [InlineData("192.168.1.200/24,-20,vEthernet1|192.168.2.208/24,-16,vEthernet212|200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.200/24", "[200.200.200.200/24]")]
  44. // Overlapping interface,
  45. [InlineData("192.168.1.110/24,-20,br0|192.168.1.10/24,-16,br0|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.110/24,192.168.1.10/24]")]
  46. public void IgnoreVirtualInterfaces(string interfaces, string lan, string value)
  47. {
  48. var conf = new NetworkConfiguration()
  49. {
  50. EnableIPv6 = true,
  51. EnableIPv4 = true,
  52. LocalNetworkSubnets = lan?.Split(';') ?? throw new ArgumentNullException(nameof(lan))
  53. };
  54. NetworkManager.MockNetworkSettings = interfaces;
  55. var startupConf = new Mock<IConfiguration>();
  56. using var nm = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
  57. NetworkManager.MockNetworkSettings = string.Empty;
  58. Assert.Equal(value, "[" + string.Join(",", nm.GetInternalBindAddresses().Select(x => x.Address + "/" + x.Subnet.PrefixLength)) + "]");
  59. }
  60. /// <summary>
  61. /// Checks valid IP address formats.
  62. /// </summary>
  63. /// <param name="address">IP Address.</param>
  64. [Theory]
  65. [InlineData("127.0.0.1")]
  66. [InlineData("127.0.0.1/8")]
  67. [InlineData("192.168.1.2")]
  68. [InlineData("192.168.1.2/24")]
  69. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
  70. [InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]")]
  71. [InlineData("fe80::7add:12ff:febb:c67b%16")]
  72. [InlineData("[fe80::7add:12ff:febb:c67b%16]:123")]
  73. [InlineData("fe80::7add:12ff:febb:c67b%16:123")]
  74. [InlineData("[fe80::7add:12ff:febb:c67b%16]")]
  75. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517/56")]
  76. public static void TryParseValidIPStringsTrue(string address)
  77. {
  78. Assert.True(NetworkUtils.TryParseToSubnet(address, out _));
  79. Assert.True(NetworkUtils.TryParseToSubnet('!' + address, out _, true));
  80. }
  81. /// <summary>
  82. /// Checks invalid IP address formats.
  83. /// </summary>
  84. /// <param name="address">IP Address.</param>
  85. [Theory]
  86. [InlineData("127.0.0.1#")]
  87. [InlineData("localhost!")]
  88. [InlineData("256.128.0.0.0.1")]
  89. [InlineData("fd23:184f:2029:0:3139:7386:67d7:d517:1231")]
  90. [InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517:1231]")]
  91. public static void TryParseInvalidIPStringsFalse(string address)
  92. => Assert.False(NetworkUtils.TryParseToSubnet(address, out _));
  93. /// <summary>
  94. /// Checks if IPv4 address is within a defined subnet.
  95. /// </summary>
  96. /// <param name="netMask">Network mask.</param>
  97. /// <param name="ipAddress">IP Address.</param>
  98. [Theory]
  99. [InlineData("192.168.5.85/24", "192.168.5.1")]
  100. [InlineData("192.168.5.85/24", "192.168.5.254")]
  101. [InlineData("10.128.240.50/30", "10.128.240.48")]
  102. [InlineData("10.128.240.50/30", "10.128.240.49")]
  103. [InlineData("10.128.240.50/30", "10.128.240.50")]
  104. [InlineData("10.128.240.50/30", "10.128.240.51")]
  105. [InlineData("127.0.0.1/8", "127.0.0.1")]
  106. public void IPv4SubnetMaskMatchesValidIPAddress(string netMask, string ipAddress)
  107. {
  108. var ipa = IPAddress.Parse(ipAddress);
  109. Assert.True(NetworkUtils.TryParseToSubnet(netMask, out var subnet) && subnet.Contains(IPAddress.Parse(ipAddress)));
  110. }
  111. /// <summary>
  112. /// Checks if IPv4 address is not within a defined subnet.
  113. /// </summary>
  114. /// <param name="netMask">Network mask.</param>
  115. /// <param name="ipAddress">IP Address.</param>
  116. [Theory]
  117. [InlineData("192.168.5.85/24", "192.168.4.254")]
  118. [InlineData("192.168.5.85/24", "191.168.5.254")]
  119. [InlineData("10.128.240.50/30", "10.128.240.47")]
  120. [InlineData("10.128.240.50/30", "10.128.240.52")]
  121. [InlineData("10.128.240.50/30", "10.128.239.50")]
  122. [InlineData("10.128.240.50/30", "10.127.240.51")]
  123. public void IPv4SubnetMaskDoesNotMatchInvalidIPAddress(string netMask, string ipAddress)
  124. {
  125. var ipa = IPAddress.Parse(ipAddress);
  126. Assert.False(NetworkUtils.TryParseToSubnet(netMask, out var subnet) && subnet.Contains(IPAddress.Parse(ipAddress)));
  127. }
  128. /// <summary>
  129. /// Checks if IPv6 address is within a defined subnet.
  130. /// </summary>
  131. /// <param name="netMask">Network mask.</param>
  132. /// <param name="ipAddress">IP Address.</param>
  133. [Theory]
  134. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
  135. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFFF")]
  136. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:0001:0000:0000:0000")]
  137. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0012:FFFF:FFFF:FFFF:FFF0")]
  138. [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0000")]
  139. public void IPv6SubnetMaskMatchesValidIPAddress(string netMask, string ipAddress)
  140. {
  141. Assert.True(NetworkUtils.TryParseToSubnet(netMask, out var subnet) && subnet.Contains(IPAddress.Parse(ipAddress)));
  142. }
  143. [Theory]
  144. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFFF")]
  145. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0000:0000:0000:0000")]
  146. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0013:0001:0000:0000:0000")]
  147. [InlineData("2001:db8:abcd:0012::0/64", "2001:0DB8:ABCD:0011:FFFF:FFFF:FFFF:FFF0")]
  148. [InlineData("2001:db8:abcd:0012::0/128", "2001:0DB8:ABCD:0012:0000:0000:0000:0001")]
  149. public void IPv6SubnetMaskDoesNotMatchInvalidIPAddress(string netMask, string ipAddress)
  150. {
  151. Assert.False(NetworkUtils.TryParseToSubnet(netMask, out var subnet) && subnet.Contains(IPAddress.Parse(ipAddress)));
  152. }
  153. [Theory]
  154. // Testing bind interfaces.
  155. // On my system eth16 is internal, eth11 external (Windows defines the indexes).
  156. //
  157. // This test is to replicate how DLNA requests work throughout the system.
  158. // User on internal network, we're bound internal and external - so result is internal.
  159. [InlineData("192.168.1.1", "eth16,eth11", false, "eth16")]
  160. // User on external network, we're bound internal and external - so result is external.
  161. [InlineData("8.8.8.8", "eth16,eth11", false, "eth11")]
  162. // User on internal network, we're bound internal only - so result is internal.
  163. [InlineData("10.10.10.10", "eth16", false, "eth16")]
  164. // User on internal network, no binding specified - so result is the 1st internal.
  165. [InlineData("192.168.1.1", "", false, "eth16")]
  166. // User on external network, internal binding only - so result is the 1st internal.
  167. [InlineData("jellyfin.org", "eth16", false, "eth16")]
  168. // User on external network, no binding - so result is the 1st external.
  169. [InlineData("jellyfin.org", "", false, "eth11")]
  170. // Dns failure - should skip the test.
  171. // https://en.wikipedia.org/wiki/.test
  172. [InlineData("invalid.domain.test", "", false, "eth11")]
  173. // User assumed to be internal, no binding - so result is the 1st internal.
  174. [InlineData("", "", false, "eth16")]
  175. public void TestBindInterfaces(string source, string bindAddresses, bool ipv6enabled, string result)
  176. {
  177. ArgumentNullException.ThrowIfNull(source);
  178. ArgumentNullException.ThrowIfNull(bindAddresses);
  179. ArgumentNullException.ThrowIfNull(result);
  180. var conf = new NetworkConfiguration()
  181. {
  182. LocalNetworkAddresses = bindAddresses.Split(','),
  183. EnableIPv6 = ipv6enabled,
  184. EnableIPv4 = true
  185. };
  186. NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11";
  187. var startupConf = new Mock<IConfiguration>();
  188. using var nm = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
  189. NetworkManager.MockNetworkSettings = string.Empty;
  190. // Check to see if DNS resolution is working. If not, skip test.
  191. if (!NetworkUtils.TryParseHost(source, out var host))
  192. {
  193. return;
  194. }
  195. if (nm.TryParseInterface(result, out var resultObj))
  196. {
  197. result = resultObj[0].Address.ToString();
  198. var intf = nm.GetBindAddress(source, out _);
  199. Assert.Equal(intf, result);
  200. }
  201. }
  202. [Theory]
  203. // Testing bind interfaces. These are set for my system so won't work elsewhere.
  204. // On my system eth16 is internal, eth11 external (Windows defines the indexes).
  205. //
  206. // This test is to replicate how subnet bound ServerPublisherUri work throughout the system.
  207. // User on internal network, we're bound internal and external - so result is internal override.
  208. [InlineData("192.168.1.1", "192.168.1.0/24", "eth16,eth11", false, "192.168.1.0/24=internal.jellyfin", "internal.jellyfin")]
  209. // User on external network, we're bound internal and external - so result is override.
  210. [InlineData("8.8.8.8", "192.168.1.0/24", "eth16,eth11", false, "all=http://helloworld.com", "http://helloworld.com")]
  211. // User on internal network, we're bound internal only, but the address isn't in the LAN - so return the override.
  212. [InlineData("10.10.10.10", "192.168.1.0/24", "eth16", false, "external=http://internalButNotDefinedAsLan.com", "http://internalButNotDefinedAsLan.com")]
  213. // User on internal network, no binding specified - so result is the 1st internal.
  214. [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "external=http://helloworld.com", "eth16")]
  215. // User on external network, internal binding only - so assumption is a proxy forward, return external override.
  216. [InlineData("jellyfin.org", "192.168.1.0/24", "eth16", false, "external=http://helloworld.com", "http://helloworld.com")]
  217. // User on external network, no binding - so result is the 1st external which is overridden.
  218. [InlineData("jellyfin.org", "192.168.1.0/24", "", false, "external=http://helloworld.com", "http://helloworld.com")]
  219. // User assumed to be internal, no binding - so result is the 1st matching interface.
  220. [InlineData("", "192.168.1.0/24", "", false, "all=http://helloworld.com", "eth16")]
  221. // User is internal, no binding - so result is the 1st internal interface, which is then overridden.
  222. [InlineData("192.168.1.1", "192.168.1.0/24", "", false, "eth16=http://helloworld.com", "http://helloworld.com")]
  223. public void TestBindInterfaceOverrides(string source, string lan, string bindAddresses, bool ipv6enabled, string publishedServers, string result)
  224. {
  225. ArgumentNullException.ThrowIfNull(lan);
  226. ArgumentNullException.ThrowIfNull(bindAddresses);
  227. var conf = new NetworkConfiguration()
  228. {
  229. LocalNetworkSubnets = lan.Split(','),
  230. LocalNetworkAddresses = bindAddresses.Split(','),
  231. EnableIPv6 = ipv6enabled,
  232. EnableIPv4 = true,
  233. PublishedServerUriBySubnet = new string[] { publishedServers }
  234. };
  235. NetworkManager.MockNetworkSettings = "192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11";
  236. var startupConf = new Mock<IConfiguration>();
  237. using var nm = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
  238. NetworkManager.MockNetworkSettings = string.Empty;
  239. if (nm.TryParseInterface(result, out IReadOnlyList<IPData>? resultObj) && resultObj is not null)
  240. {
  241. // Parse out IPAddresses so we can do a string comparison (ignore subnet masks).
  242. result = resultObj[0].Address.ToString();
  243. }
  244. var intf = nm.GetBindAddress(source, out int? _);
  245. Assert.Equal(result, intf);
  246. }
  247. [Theory]
  248. [InlineData("185.10.10.10,200.200.200.200", "79.2.3.4", RemoteAccessPolicyResult.RejectDueToNotAllowlistedRemoteIP)]
  249. [InlineData("185.10.10.10", "185.10.10.10", RemoteAccessPolicyResult.Allow)]
  250. [InlineData("", "100.100.100.100", RemoteAccessPolicyResult.Allow)]
  251. public void HasRemoteAccess_GivenWhitelist_AllowsOnlyIPsInWhitelist(string addresses, string remoteIP, RemoteAccessPolicyResult expectedResult)
  252. {
  253. // Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
  254. // If left blank, all remote addresses will be allowed.
  255. var conf = new NetworkConfiguration()
  256. {
  257. EnableIPv4 = true,
  258. RemoteIPFilter = addresses.Split(','),
  259. IsRemoteIPFilterBlacklist = false
  260. };
  261. var startupConf = new Mock<IConfiguration>();
  262. using var nm = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
  263. Assert.Equal(expectedResult, nm.ShouldAllowServerAccess(IPAddress.Parse(remoteIP)));
  264. }
  265. [Theory]
  266. [InlineData("185.10.10.10,200.200.200.200", "79.2.3.4", RemoteAccessPolicyResult.RejectDueToRemoteAccessDisabled)]
  267. [InlineData("185.10.10.10", "127.0.0.1", RemoteAccessPolicyResult.Allow)]
  268. [InlineData("", "100.100.100.100", RemoteAccessPolicyResult.RejectDueToRemoteAccessDisabled)]
  269. public void HasRemoteAccess_GivenRemoteAccessDisabled_IgnoresAllowlist(string addresses, string remoteIP, RemoteAccessPolicyResult expectedResult)
  270. {
  271. // Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
  272. // If left blank, all remote addresses will be allowed.
  273. var conf = new NetworkConfiguration()
  274. {
  275. EnableIPv4 = true,
  276. EnableRemoteAccess = false,
  277. RemoteIPFilter = addresses.Split(','),
  278. IsRemoteIPFilterBlacklist = false
  279. };
  280. var startupConf = new Mock<IConfiguration>();
  281. using var nm = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
  282. Assert.Equal(expectedResult, nm.ShouldAllowServerAccess(IPAddress.Parse(remoteIP)));
  283. }
  284. [Theory]
  285. [InlineData("185.10.10.10", "79.2.3.4", RemoteAccessPolicyResult.Allow)]
  286. [InlineData("185.10.10.10", "185.10.10.10", RemoteAccessPolicyResult.RejectDueToIPBlocklist)]
  287. [InlineData("", "100.100.100.100", RemoteAccessPolicyResult.Allow)]
  288. public void HasRemoteAccess_GivenBlacklist_BlacklistTheIPs(string addresses, string remoteIP, RemoteAccessPolicyResult expectedResult)
  289. {
  290. // Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
  291. // If left blank, all remote addresses will be allowed.
  292. var conf = new NetworkConfiguration()
  293. {
  294. EnableIPv4 = true,
  295. RemoteIPFilter = addresses.Split(','),
  296. IsRemoteIPFilterBlacklist = true
  297. };
  298. var startupConf = new Mock<IConfiguration>();
  299. using var nm = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
  300. Assert.Equal(expectedResult, nm.ShouldAllowServerAccess(IPAddress.Parse(remoteIP)));
  301. }
  302. [Theory]
  303. [InlineData("192.168.1.209/24,-16,eth16", "192.168.1.0/24", "", "192.168.1.209")] // Only 1 address so use it.
  304. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "", "192.168.1.208")] // LAN address is specified by default.
  305. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "10.0.0.1", "10.0.0.1")] // return bind address
  306. public void GetBindInterface_NoSourceGiven_Success(string interfaces, string lan, string bind, string result)
  307. {
  308. var conf = new NetworkConfiguration
  309. {
  310. EnableIPv4 = true,
  311. LocalNetworkSubnets = lan.Split(','),
  312. LocalNetworkAddresses = bind.Split(',')
  313. };
  314. NetworkManager.MockNetworkSettings = interfaces;
  315. var startupConf = new Mock<IConfiguration>();
  316. using var nm = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
  317. var interfaceToUse = nm.GetBindAddress(string.Empty, out _);
  318. Assert.Equal(result, interfaceToUse);
  319. }
  320. [Theory]
  321. [InlineData("192.168.1.209/24,-16,eth16", "192.168.1.0/24", "", "192.168.1.210", "192.168.1.209")] // Source on LAN
  322. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "", "192.168.1.209", "192.168.1.208")] // Source on LAN
  323. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "", "8.8.8.8", "10.0.0.1")] // Source external.
  324. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "10.0.0.1", "192.168.1.209", "10.0.0.1")] // LAN not bound, so return external.
  325. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "192.168.1.208,10.0.0.1", "8.8.8.8", "10.0.0.1")] // return external bind address
  326. [InlineData("192.168.1.208/24,-16,eth16|10.0.0.1/24,10,eth7", "192.168.1.0/24", "192.168.1.208,10.0.0.1", "192.168.1.210", "192.168.1.208")] // return LAN bind address
  327. public void GetBindInterface_ValidSourceGiven_Success(string interfaces, string lan, string bind, string source, string result)
  328. {
  329. var conf = new NetworkConfiguration
  330. {
  331. EnableIPv4 = true,
  332. LocalNetworkSubnets = lan.Split(','),
  333. LocalNetworkAddresses = bind.Split(',')
  334. };
  335. NetworkManager.MockNetworkSettings = interfaces;
  336. var startupConf = new Mock<IConfiguration>();
  337. using var nm = new NetworkManager(NetworkParseTests.GetMockConfig(conf), startupConf.Object, new NullLogger<NetworkManager>());
  338. var interfaceToUse = nm.GetBindAddress(source, out _);
  339. Assert.Equal(result, interfaceToUse);
  340. }
  341. }
  342. }