PortBlocking.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <AK/BinarySearch.h>
  8. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  9. #include <LibWeb/Fetch/Infrastructure/PortBlocking.h>
  10. #include <LibWeb/Fetch/Infrastructure/URL.h>
  11. namespace Web::Fetch::Infrastructure {
  12. // https://fetch.spec.whatwg.org/#block-bad-port
  13. RequestOrResponseBlocking block_bad_port(Request const& request)
  14. {
  15. // 1. Let url be request’s current URL.
  16. auto const& url = request.current_url();
  17. // 2. If url’s scheme is an HTTP(S) scheme and url’s port is a bad port, then return blocked.
  18. if (is_http_or_https_scheme(url.scheme()) && url.port().has_value() && is_bad_port(*url.port()))
  19. return RequestOrResponseBlocking::Blocked;
  20. // 3. Return allowed.
  21. return RequestOrResponseBlocking::Allowed;
  22. }
  23. // https://fetch.spec.whatwg.org/#bad-port
  24. bool is_bad_port(u16 port)
  25. {
  26. // A port is a bad port if it is listed in the first column of the following table.
  27. static constexpr auto bad_ports = Array {
  28. 1, // tcpmux
  29. 7, // echo
  30. 9, // discard
  31. 11, // systat
  32. 13, // daytime
  33. 15, // netstat
  34. 17, // qotd
  35. 19, // chargen
  36. 20, // ftp-data
  37. 21, // ftp
  38. 22, // ssh
  39. 23, // telnet
  40. 25, // smtp
  41. 37, // time
  42. 42, // name
  43. 43, // nicname
  44. 53, // domain
  45. 69, // tftp
  46. 77, // —
  47. 79, // finger
  48. 87, // —
  49. 95, // supdup
  50. 101, // hostname
  51. 102, // iso-tsap
  52. 103, // gppitnp
  53. 104, // acr-nema
  54. 109, // pop2
  55. 110, // pop3
  56. 111, // sunrpc
  57. 113, // auth
  58. 115, // sftp
  59. 117, // uucp-path
  60. 119, // nntp
  61. 123, // ntp
  62. 135, // epmap
  63. 137, // netbios-ns
  64. 139, // netbios-ssn
  65. 143, // imap
  66. 161, // snmp
  67. 179, // bgp
  68. 389, // ldap
  69. 427, // svrloc
  70. 465, // submissions
  71. 512, // exec
  72. 513, // login
  73. 514, // shell
  74. 515, // printer
  75. 526, // tempo
  76. 530, // courier
  77. 531, // chat
  78. 532, // netnews
  79. 540, // uucp
  80. 548, // afp
  81. 554, // rtsp
  82. 556, // remotefs
  83. 563, // nntps
  84. 587, // submission
  85. 601, // syslog-conn
  86. 636, // ldaps
  87. 989, // ftps-data
  88. 990, // ftps
  89. 993, // imaps
  90. 995, // pop3s
  91. 1719, // h323gatestat
  92. 1720, // h323hostcall
  93. 1723, // pptp
  94. 2049, // nfs
  95. 3659, // apple-sasl
  96. 4045, // npp
  97. 5060, // sip
  98. 5061, // sips
  99. 6000, // x11
  100. 6566, // sane-port
  101. 6665, // ircu
  102. 6666, // ircu
  103. 6667, // ircu
  104. 6668, // ircu
  105. 6669, // ircu
  106. 6697, // ircs-u
  107. 10080, // amanda
  108. };
  109. return binary_search(bad_ports.span(), port);
  110. }
  111. }