thttpclient_ssl_remotenetwork.nim 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. # badssl certs sometimes expire, set to false when that happens
  32. # badssl now disabled indefinitely
  33. when false:
  34. const certificate_tests: array[0..54, CertTest] = [
  35. ("https://wrong.host.badssl.com/", bad, "wrong.host"),
  36. ("https://captive-portal.badssl.com/", bad, "captive-portal"),
  37. ("https://expired.badssl.com/", bad, "expired"),
  38. ("https://google.com/", good, "good"),
  39. ("https://self-signed.badssl.com/", bad, "self-signed"),
  40. ("https://untrusted-root.badssl.com/", bad, "untrusted-root"),
  41. ("https://revoked.badssl.com/", bad_broken, "revoked"),
  42. ("https://pinning-test.badssl.com/", bad_broken, "pinning-test"),
  43. ("https://no-common-name.badssl.com/", bad, "no-common-name"),
  44. ("https://no-subject.badssl.com/", bad, "no-subject"),
  45. ("https://sha1-intermediate.badssl.com/", bad, "sha1-intermediate"),
  46. ("https://sha256.badssl.com/", good, "sha256"),
  47. ("https://sha384.badssl.com/", bad, "sha384"),
  48. ("https://sha512.badssl.com/", bad, "sha512"),
  49. ("https://1000-sans.badssl.com/", bad, "1000-sans"),
  50. ("https://10000-sans.badssl.com/", good_broken, "10000-sans"),
  51. ("https://ecc256.badssl.com/", good_broken, "ecc256"),
  52. ("https://ecc384.badssl.com/", good_broken, "ecc384"),
  53. ("https://rsa2048.badssl.com/", good, "rsa2048"),
  54. ("https://rsa8192.badssl.com/", dubious_broken, "rsa8192"),
  55. ("http://http.badssl.com/", good, "regular http"),
  56. ("https://http.badssl.com/", bad_broken, "http on https URL"), # FIXME
  57. ("https://cbc.badssl.com/", dubious, "cbc"),
  58. ("https://rc4-md5.badssl.com/", bad, "rc4-md5"),
  59. ("https://rc4.badssl.com/", bad, "rc4"),
  60. ("https://3des.badssl.com/", bad, "3des"),
  61. ("https://null.badssl.com/", bad, "null"),
  62. ("https://mozilla-old.badssl.com/", bad_broken, "mozilla-old"),
  63. ("https://mozilla-intermediate.badssl.com/", dubious_broken, "mozilla-intermediate"),
  64. ("https://mozilla-modern.badssl.com/", good, "mozilla-modern"),
  65. ("https://dh480.badssl.com/", bad, "dh480"),
  66. ("https://dh512.badssl.com/", bad, "dh512"),
  67. ("https://dh1024.badssl.com/", dubious_broken, "dh1024"),
  68. ("https://dh2048.badssl.com/", good, "dh2048"),
  69. ("https://dh-small-subgroup.badssl.com/", bad_broken, "dh-small-subgroup"),
  70. ("https://dh-composite.badssl.com/", bad_broken, "dh-composite"),
  71. ("https://static-rsa.badssl.com/", dubious, "static-rsa"),
  72. ("https://tls-v1-0.badssl.com:1010/", dubious, "tls-v1-0"),
  73. ("https://tls-v1-1.badssl.com:1011/", dubious, "tls-v1-1"),
  74. ("https://invalid-expected-sct.badssl.com/", bad, "invalid-expected-sct"),
  75. ("https://hsts.badssl.com/", good, "hsts"),
  76. ("https://upgrade.badssl.com/", good, "upgrade"),
  77. ("https://preloaded-hsts.badssl.com/", good, "preloaded-hsts"),
  78. ("https://subdomain.preloaded-hsts.badssl.com/", bad, "subdomain.preloaded-hsts"),
  79. ("https://https-everywhere.badssl.com/", good, "https-everywhere"),
  80. ("https://long-extended-subdomain-name-containing-many-letters-and-dashes.badssl.com/", good,
  81. "long-extended-subdomain-name-containing-many-letters-and-dashes"),
  82. ("https://longextendedsubdomainnamewithoutdashesinordertotestwordwrapping.badssl.com/", good,
  83. "longextendedsubdomainnamewithoutdashesinordertotestwordwrapping"),
  84. ("https://superfish.badssl.com/", bad, "(Lenovo) Superfish"),
  85. ("https://edellroot.badssl.com/", bad, "(Dell) eDellRoot"),
  86. ("https://dsdtestprovider.badssl.com/", bad, "(Dell) DSD Test Provider"),
  87. ("https://preact-cli.badssl.com/", bad, "preact-cli"),
  88. ("https://webpack-dev-server.badssl.com/", bad, "webpack-dev-server"),
  89. ("https://mitm-software.badssl.com/", bad, "mitm-software"),
  90. ("https://sha1-2016.badssl.com/", dubious, "sha1-2016"),
  91. ("https://sha1-2017.badssl.com/", bad, "sha1-2017"),
  92. ]
  93. else:
  94. const certificate_tests: array[0..0, CertTest] = [
  95. ("https://google.com/", good, "good")
  96. ]
  97. template evaluate(exception_msg: string, category: Category, desc: string) =
  98. # Evaluate test outcome. Tests flagged as `_broken` are evaluated and skipped
  99. let raised = (exception_msg.len > 0)
  100. let should_not_raise = category in {good, dubious_broken, bad_broken}
  101. if should_not_raise xor raised:
  102. # we are seeing a known behavior
  103. if category in {good_broken, dubious_broken, bad_broken}:
  104. skip()
  105. if raised:
  106. # check exception_msg == "No SSL certificate found." or
  107. doAssert exception_msg == "No SSL certificate found." or
  108. exception_msg == "SSL Certificate check failed." or
  109. exception_msg.contains("certificate verify failed") or
  110. exception_msg.contains("key too small") or
  111. exception_msg.contains("alert handshake failure") or
  112. exception_msg.contains("bad dh p length") or
  113. # TODO: This one should only triggers for 10000-sans
  114. exception_msg.contains("excessive message size"), exception_msg
  115. else:
  116. # this is unexpected
  117. var fatal = true
  118. var msg = ""
  119. if raised:
  120. msg = " $# ($#) raised: $#" % [desc, $category, exception_msg]
  121. if "500 Internal Server Error" in exception_msg:
  122. # refs https://github.com/nim-lang/Nim/issues/16338#issuecomment-804300278
  123. # we got: `good (good) raised: 500 Internal Server Error`
  124. fatal = false
  125. msg.add " (http 500 => assuming this is not our problem)"
  126. else:
  127. msg = " $# ($#) did not raise" % [desc, $category]
  128. if category in {good, dubious, bad} and fatal:
  129. echo "D20210322T121353: error: " & msg
  130. fail()
  131. else:
  132. echo "D20210322T121353: warning: " & msg
  133. suite "SSL certificate check - httpclient":
  134. for i, ct in certificate_tests:
  135. test ct.desc:
  136. var ctx = newContext(verifyMode=CVerifyPeer)
  137. var client = newHttpClient(sslContext=ctx)
  138. let exception_msg =
  139. try:
  140. let a = $client.getContent(ct.url)
  141. ""
  142. except:
  143. getCurrentExceptionMsg()
  144. evaluate(exception_msg, ct.category, ct.desc)
  145. # threaded tests
  146. type
  147. TTOutcome = ref object
  148. desc, exception_msg: string
  149. category: Category
  150. proc run_t_test(ct: CertTest): TTOutcome {.thread.} =
  151. ## Run test in a {.thread.} - return by ref
  152. result = TTOutcome(desc:ct.desc, exception_msg:"", category: ct.category)
  153. try:
  154. var ctx = newContext(verifyMode=CVerifyPeer)
  155. var client = newHttpClient(sslContext=ctx)
  156. let a = $client.getContent(ct.url)
  157. except:
  158. result.exception_msg = getCurrentExceptionMsg()
  159. suite "SSL certificate check - httpclient - threaded":
  160. when defined(nimTestsEnableFlaky) or not defined(linux): # xxx pending bug #16338
  161. # Spawn threads before the "test" blocks
  162. var outcomes = newSeq[FlowVar[TTOutcome]](certificate_tests.len)
  163. for i, ct in certificate_tests:
  164. let t = spawn run_t_test(ct)
  165. outcomes[i] = t
  166. # create "test" blocks and handle thread outputs
  167. for t in outcomes:
  168. let outcome = ^t # wait for a thread to terminate
  169. test outcome.desc:
  170. evaluate(outcome.exception_msg, outcome.category, outcome.desc)
  171. else:
  172. echo "skipped test"
  173. # net tests
  174. type NetSocketTest = tuple[hostname: string, port: Port, category:Category, desc: string]
  175. # badssl certs sometimes expire, set to false when that happens
  176. when false:
  177. const net_tests:array[0..3, NetSocketTest] = [
  178. ("imap.gmail.com", 993.Port, good, "IMAP"),
  179. ("wrong.host.badssl.com", 443.Port, bad, "wrong.host"),
  180. ("captive-portal.badssl.com", 443.Port, bad, "captive-portal"),
  181. ("expired.badssl.com", 443.Port, bad, "expired"),
  182. ]
  183. else:
  184. const net_tests: array[0..0, NetSocketTest] = [
  185. ("imap.gmail.com", 993.Port, good, "IMAP")
  186. ]
  187. # TODO: ("null.badssl.com", 443.Port, bad_broken, "null"),
  188. suite "SSL certificate check - sockets":
  189. for ct in net_tests:
  190. test ct.desc:
  191. var sock = newSocket()
  192. var ctx = newContext()
  193. ctx.wrapSocket(sock)
  194. let exception_msg =
  195. try:
  196. sock.connect(ct.hostname, ct.port)
  197. ""
  198. except:
  199. getCurrentExceptionMsg()
  200. evaluate(exception_msg, ct.category, ct.desc)