PageRenderTime 38ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DotNetOpenId.Test/UntrustedWebRequestTests.cs

https://github.com/tt/dotnetopenid
C# | 125 lines | 96 code | 8 blank | 21 comment | 0 complexity | a688943b18feae56d8d348bcbd370d2c MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text.RegularExpressions;
  5. using DotNetOpenId.Test.Mocks;
  6. using NUnit.Framework;
  7. namespace DotNetOpenId.Test {
  8. [TestFixture]
  9. public class UntrustedWebRequestTests {
  10. TimeSpan timeoutDefault;
  11. [SetUp]
  12. public void SetUp() {
  13. UntrustedWebRequest.WhitelistHosts.Clear();
  14. UntrustedWebRequest.WhitelistHostsRegex.Clear();
  15. UntrustedWebRequest.BlacklistHosts.Clear();
  16. UntrustedWebRequest.BlacklistHostsRegex.Clear();
  17. timeoutDefault = UntrustedWebRequest.Timeout;
  18. }
  19. [TearDown]
  20. public void TearDown() {
  21. UntrustedWebRequest.Timeout = timeoutDefault; // in case a test changed it
  22. }
  23. [Test]
  24. public void DisallowUnsafeHosts() {
  25. string[] unsafeHosts = new[] {
  26. // IPv4 loopback representations
  27. "http://127.0.0.1",
  28. "http://127.100.0.1",
  29. "http://127.0.0.100",
  30. "http://2130706433", // 127.0.0.1 in decimal format
  31. "http://0x7f000001", // 127.0.0.1 in hex format
  32. // IPv6 loopback representation
  33. "http://[::1]",
  34. // disallowed schemes
  35. "ftp://ftp.microsoft.com",
  36. "xri://boo",
  37. };
  38. foreach (string unsafeHost in unsafeHosts) {
  39. try {
  40. UntrustedWebRequest.Request(new Uri(unsafeHost));
  41. Assert.Fail("ArgumentException expected but none thrown.");
  42. } catch (ArgumentException) {
  43. // expected exception caught.
  44. }
  45. }
  46. }
  47. [Test]
  48. public void Whitelist() {
  49. UntrustedWebRequest.WhitelistHosts.Add("localhost");
  50. // if this works, then we'll be waiting around for localhost to not respond
  51. // for a while unless we take the timeout to zero.
  52. UntrustedWebRequest.Timeout = TimeSpan.Zero; // will be reset in TearDown method
  53. try {
  54. UntrustedWebRequest.Request(new Uri("http://localhost:1234"));
  55. // We're verifying that an ArgumentException is not thrown
  56. // since we requested localhost to be allowed.
  57. } catch (WebException) {
  58. // It's ok, we're not expecting the request to succeed.
  59. }
  60. }
  61. [Test]
  62. public void WhitelistRegex() {
  63. UntrustedWebRequest.WhitelistHostsRegex.Add(new Regex(@"^127\.\d+\.\d+\.\d+$"));
  64. // if this works, then we'll be waiting around for localhost to not respond
  65. // for a while unless we take the timeout to zero.
  66. UntrustedWebRequest.Timeout = TimeSpan.Zero; // will be reset in TearDown method
  67. try {
  68. UntrustedWebRequest.Request(new Uri("http://127.0.0.1:1234"));
  69. // We're verifying that an ArgumentException is not thrown
  70. // since we requested localhost to be allowed.
  71. } catch (WebException) {
  72. // It's ok, we're not expecting the request to succeed.
  73. }
  74. }
  75. [Test, ExpectedException(typeof(ArgumentException))]
  76. public void Blacklist() {
  77. UntrustedWebRequest.BlacklistHosts.Add("www.microsoft.com");
  78. UntrustedWebRequest.Request(new Uri("http://WWW.MICROSOFT.COM"));
  79. }
  80. [Test, ExpectedException(typeof(ArgumentException))]
  81. public void BlacklistRegex() {
  82. UntrustedWebRequest.BlacklistHostsRegex.Add(new Regex(@"\Wmicrosoft.com$"));
  83. UntrustedWebRequest.Request(new Uri("http://WWW.MICROSOFT.COM"));
  84. }
  85. /// <summary>
  86. /// Tests an implicit redirect where the HTTP server changes the responding URI without even
  87. /// redirecting the client.
  88. /// </summary>
  89. [Test]
  90. public void Redirects() {
  91. UntrustedWebRequest.WhitelistHosts.Add("localhost");
  92. UntrustedWebResponse resp = new UntrustedWebResponse(
  93. new Uri("http://localhost/req"), new Uri("http://localhost/resp"),
  94. new WebHeaderCollection(), HttpStatusCode.OK, "text/html", null, new MemoryStream());
  95. MockHttpRequest.RegisterMockResponse(resp);
  96. Assert.AreSame(resp, UntrustedWebRequest.Request(new Uri("http://localhost/req")));
  97. }
  98. /// <summary>
  99. /// Tests that HTTP Location headers that only use a relative path get interpreted correctly.
  100. /// </summary>
  101. [Test]
  102. public void RelativeRedirect() {
  103. UntrustedWebRequest.WhitelistHosts.Add("localhost");
  104. UntrustedWebResponse resp1 = new UntrustedWebResponse(
  105. new Uri("http://localhost/dir/file1"), new Uri("http://localhost/dir/file1"),
  106. new WebHeaderCollection {
  107. { HttpResponseHeader.Location, "file2" },
  108. }, HttpStatusCode.Redirect, "text/html", null, new MemoryStream());
  109. MockHttpRequest.RegisterMockResponse(resp1);
  110. UntrustedWebResponse resp2 = new UntrustedWebResponse(
  111. new Uri("http://localhost/dir/file2"), new Uri("http://localhost/dir/file2"),
  112. new WebHeaderCollection(), HttpStatusCode.OK, "text/html", null, new MemoryStream());
  113. MockHttpRequest.RegisterMockResponse(resp2);
  114. Assert.AreSame(resp2, UntrustedWebRequest.Request(new Uri("http://localhost/dir/file1")));
  115. }
  116. }
  117. }