thttpclient_ssl_remotenetwork.nim 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #
  2. #
  3. # Nim - SSL integration tests
  4. # (c) Copyright 2017 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Test with:
  10. ## nim r --putenv:NIM_TESTAMENT_REMOTE_NETWORKING:1 -d:ssl -p:. --threads:on tests/untestable/thttpclient_ssl_remotenetwork.nim
  11. ##
  12. ## See https://github.com/FedericoCeratto/ssl-comparison/blob/master/README.md
  13. ## for a comparison with other clients.
  14. from stdtest/testutils import enableRemoteNetworking
  15. when enableRemoteNetworking and (defined(nimTestsEnableFlaky) or not defined(windows) and not defined(openbsd)):
  16. # Not supported on Windows due to old openssl version
  17. import
  18. httpclient,
  19. net,
  20. strutils,
  21. threadpool,
  22. unittest
  23. type
  24. # bad and dubious tests should not pass SSL validation
  25. # "_broken" mark the test as skipped. Some tests have different
  26. # behavior depending on OS and SSL version!
  27. # TODO: chase and fix the broken tests
  28. Category = enum
  29. good, bad, dubious, good_broken, bad_broken, dubious_broken
  30. CertTest = tuple[url:string, category:Category, desc: string]
  31. const certificate_tests: array[0..54, CertTest] = [
  32. ("https://wrong.host.badssl.com/", bad, "wrong.host"),
  33. ("https://captive-portal.badssl.com/", bad, "captive-portal"),
  34. ("https://expired.badssl.com/", bad, "expired"),
  35. ("https://google.com/", good, "good"),
  36. ("https://self-signed.badssl.com/", bad, "self-signed"),
  37. ("https://untrusted-root.badssl.com/", bad, "untrusted-root"),
  38. ("https://revoked.badssl.com/", bad_broken, "revoked"),
  39. ("https://pinning-test.badssl.com/", bad_broken, "pinning-test"),
  40. ("https://no-common-name.badssl.com/", bad, "no-common-name"),
  41. ("https://no-subject.badssl.com/", bad, "no-subject"),
  42. ("https://sha1-intermediate.badssl.com/", bad, "sha1-intermediate"),
  43. ("https://sha256.badssl.com/", good, "sha256"),
  44. ("https://sha384.badssl.com/", bad, "sha384"),
  45. ("https://sha512.badssl.com/", bad, "sha512"),
  46. ("https://1000-sans.badssl.com/", bad, "1000-sans"),
  47. ("https://10000-sans.badssl.com/", good_broken, "10000-sans"),
  48. ("https://ecc256.badssl.com/", good_broken, "ecc256"),
  49. ("https://ecc384.badssl.com/", good_broken, "ecc384"),
  50. ("https://rsa2048.badssl.com/", good, "rsa2048"),
  51. ("https://rsa8192.badssl.com/", dubious_broken, "rsa8192"),
  52. ("http://http.badssl.com/", good, "regular http"),
  53. ("https://http.badssl.com/", bad_broken, "http on https URL"), # FIXME
  54. ("https://cbc.badssl.com/", dubious, "cbc"),
  55. ("https://rc4-md5.badssl.com/", bad, "rc4-md5"),
  56. ("https://rc4.badssl.com/", bad, "rc4"),
  57. ("https://3des.badssl.com/", bad, "3des"),
  58. ("https://null.badssl.com/", bad, "null"),
  59. ("https://mozilla-old.badssl.com/", bad_broken, "mozilla-old"),
  60. ("https://mozilla-intermediate.badssl.com/", dubious_broken, "mozilla-intermediate"),
  61. ("https://mozilla-modern.badssl.com/", good, "mozilla-modern"),
  62. ("https://dh480.badssl.com/", bad, "dh480"),
  63. ("https://dh512.badssl.com/", bad, "dh512"),
  64. ("https://dh1024.badssl.com/", dubious_broken, "dh1024"),
  65. ("https://dh2048.badssl.com/", good, "dh2048"),
  66. ("https://dh-small-subgroup.badssl.com/", bad_broken, "dh-small-subgroup"),
  67. ("https://dh-composite.badssl.com/", bad_broken, "dh-composite"),
  68. ("https://static-rsa.badssl.com/", dubious, "static-rsa"),
  69. ("https://tls-v1-0.badssl.com:1010/", dubious, "tls-v1-0"),
  70. ("https://tls-v1-1.badssl.com:1011/", dubious, "tls-v1-1"),
  71. ("https://invalid-expected-sct.badssl.com/", bad, "invalid-expected-sct"),
  72. ("https://hsts.badssl.com/", good, "hsts"),
  73. ("https://upgrade.badssl.com/", good, "upgrade"),
  74. ("https://preloaded-hsts.badssl.com/", good, "preloaded-hsts"),
  75. ("https://subdomain.preloaded-hsts.badssl.com/", bad, "subdomain.preloaded-hsts"),
  76. ("https://https-everywhere.badssl.com/", good, "https-everywhere"),
  77. ("https://long-extended-subdomain-name-containing-many-letters-and-dashes.badssl.com/", good,
  78. "long-extended-subdomain-name-containing-many-letters-and-dashes"),
  79. ("https://longextendedsubdomainnamewithoutdashesinordertotestwordwrapping.badssl.com/", good,
  80. "longextendedsubdomainnamewithoutdashesinordertotestwordwrapping"),
  81. ("https://superfish.badssl.com/", bad, "(Lenovo) Superfish"),
  82. ("https://edellroot.badssl.com/", bad, "(Dell) eDellRoot"),
  83. ("https://dsdtestprovider.badssl.com/", bad, "(Dell) DSD Test Provider"),
  84. ("https://preact-cli.badssl.com/", bad, "preact-cli"),
  85. ("https://webpack-dev-server.badssl.com/", bad, "webpack-dev-server"),
  86. ("https://mitm-software.badssl.com/", bad, "mitm-software"),
  87. ("https://sha1-2016.badssl.com/", dubious, "sha1-2016"),
  88. ("https://sha1-2017.badssl.com/", bad, "sha1-2017"),
  89. ]
  90. template evaluate(exception_msg: string, category: Category, desc: string) =
  91. # Evaluate test outcome. Tests flagged as `_broken` are evaluated and skipped
  92. let raised = (exception_msg.len > 0)
  93. let should_not_raise = category in {good, dubious_broken, bad_broken}
  94. if should_not_raise xor raised:
  95. # we are seeing a known behavior
  96. if category in {good_broken, dubious_broken, bad_broken}:
  97. skip()
  98. if raised:
  99. # check exception_msg == "No SSL certificate found." or
  100. doAssert exception_msg == "No SSL certificate found." or
  101. exception_msg == "SSL Certificate check failed." or
  102. exception_msg.contains("certificate verify failed") or
  103. exception_msg.contains("key too small") or
  104. exception_msg.contains("alert handshake failure") or
  105. exception_msg.contains("bad dh p length") or
  106. # TODO: This one should only triggers for 10000-sans
  107. exception_msg.contains("excessive message size"), exception_msg
  108. else:
  109. # this is unexpected
  110. var fatal = true
  111. var msg = ""
  112. if raised:
  113. msg = " $# ($#) raised: $#" % [desc, $category, exception_msg]
  114. if "500 Internal Server Error" in exception_msg:
  115. # refs https://github.com/nim-lang/Nim/issues/16338#issuecomment-804300278
  116. # we got: `good (good) raised: 500 Internal Server Error`
  117. fatal = false
  118. msg.add " (http 500 => assuming this is not our problem)"
  119. else:
  120. msg = " $# ($#) did not raise" % [desc, $category]
  121. if category in {good, dubious, bad} and fatal:
  122. echo "D20210322T121353: error: " & msg
  123. fail()
  124. else:
  125. echo "D20210322T121353: warning: " & msg
  126. suite "SSL certificate check - httpclient":
  127. for i, ct in certificate_tests:
  128. test ct.desc:
  129. var ctx = newContext(verifyMode=CVerifyPeer)
  130. var client = newHttpClient(sslContext=ctx)
  131. let exception_msg =
  132. try:
  133. let a = $client.getContent(ct.url)
  134. ""
  135. except:
  136. getCurrentExceptionMsg()
  137. evaluate(exception_msg, ct.category, ct.desc)
  138. # threaded tests
  139. type
  140. TTOutcome = ref object
  141. desc, exception_msg: string
  142. category: Category
  143. proc run_t_test(ct: CertTest): TTOutcome {.thread.} =
  144. ## Run test in a {.thread.} - return by ref
  145. result = TTOutcome(desc:ct.desc, exception_msg:"", category: ct.category)
  146. try:
  147. var ctx = newContext(verifyMode=CVerifyPeer)
  148. var client = newHttpClient(sslContext=ctx)
  149. let a = $client.getContent(ct.url)
  150. except:
  151. result.exception_msg = getCurrentExceptionMsg()
  152. suite "SSL certificate check - httpclient - threaded":
  153. when defined(nimTestsEnableFlaky) or not defined(linux): # xxx pending bug #16338
  154. # Spawn threads before the "test" blocks
  155. var outcomes = newSeq[FlowVar[TTOutcome]](certificate_tests.len)
  156. for i, ct in certificate_tests:
  157. let t = spawn run_t_test(ct)
  158. outcomes[i] = t
  159. # create "test" blocks and handle thread outputs
  160. for t in outcomes:
  161. let outcome = ^t # wait for a thread to terminate
  162. test outcome.desc:
  163. evaluate(outcome.exception_msg, outcome.category, outcome.desc)
  164. else:
  165. echo "skipped test"
  166. # net tests
  167. type NetSocketTest = tuple[hostname: string, port: Port, category:Category, desc: string]
  168. const net_tests:array[0..3, NetSocketTest] = [
  169. ("imap.gmail.com", 993.Port, good, "IMAP"),
  170. ("wrong.host.badssl.com", 443.Port, bad, "wrong.host"),
  171. ("captive-portal.badssl.com", 443.Port, bad, "captive-portal"),
  172. ("expired.badssl.com", 443.Port, bad, "expired"),
  173. ]
  174. # TODO: ("null.badssl.com", 443.Port, bad_broken, "null"),
  175. suite "SSL certificate check - sockets":
  176. for ct in net_tests:
  177. test ct.desc:
  178. var sock = newSocket()
  179. var ctx = newContext()
  180. ctx.wrapSocket(sock)
  181. let exception_msg =
  182. try:
  183. sock.connect(ct.hostname, ct.port)
  184. ""
  185. except:
  186. getCurrentExceptionMsg()
  187. evaluate(exception_msg, ct.category, ct.desc)