headers.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/core/lib/surface/server.h"
  19. #include "test/core/bad_client/bad_client.h"
  20. #define PFX_STR \
  21. "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
  22. "\x00\x00\x00\x04\x00\x00\x00\x00\x00"
  23. static void verifier(grpc_server* server, grpc_completion_queue* cq,
  24. void* /*registered_method*/) {
  25. while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
  26. GPR_ASSERT(grpc_completion_queue_next(
  27. cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
  28. .type == GRPC_QUEUE_TIMEOUT);
  29. }
  30. }
  31. int main(int argc, char** argv) {
  32. grpc::testing::TestEnvironment env(argc, argv);
  33. grpc_init();
  34. /* partial http2 header prefixes */
  35. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00",
  36. GRPC_BAD_CLIENT_DISCONNECT);
  37. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00",
  38. GRPC_BAD_CLIENT_DISCONNECT);
  39. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00",
  40. GRPC_BAD_CLIENT_DISCONNECT);
  41. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01",
  42. GRPC_BAD_CLIENT_DISCONNECT);
  43. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01\x00",
  44. GRPC_BAD_CLIENT_DISCONNECT);
  45. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01\x04",
  46. GRPC_BAD_CLIENT_DISCONNECT);
  47. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01\x05",
  48. GRPC_BAD_CLIENT_DISCONNECT);
  49. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  50. PFX_STR "\x00\x00\x00\x01\x04\x00",
  51. GRPC_BAD_CLIENT_DISCONNECT);
  52. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  53. PFX_STR "\x00\x00\x00\x01\x04\x00\x00",
  54. GRPC_BAD_CLIENT_DISCONNECT);
  55. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  56. PFX_STR "\x00\x00\x00\x01\x04\x00\x00\x00",
  57. GRPC_BAD_CLIENT_DISCONNECT);
  58. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  59. PFX_STR "\x00\x00\x00\x01\x04\x00\x00\x00\x00",
  60. GRPC_BAD_CLIENT_DISCONNECT);
  61. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  62. PFX_STR "\x00\x00\x00\x01\x04\x00\x00\x00\x01",
  63. GRPC_BAD_CLIENT_DISCONNECT);
  64. /* test adding prioritization data */
  65. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  66. PFX_STR
  67. "\x00\x00\x01\x01\x24\x00\x00\x00\x01"
  68. "\x00",
  69. 0);
  70. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  71. PFX_STR
  72. "\x00\x00\x02\x01\x24\x00\x00\x00\x01"
  73. "\x00\x00",
  74. 0);
  75. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  76. PFX_STR
  77. "\x00\x00\x03\x01\x24\x00\x00\x00\x01"
  78. "\x00\x00\x00",
  79. 0);
  80. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  81. PFX_STR
  82. "\x00\x00\x04\x01\x24\x00\x00\x00\x01"
  83. "\x00\x00\x00\x00",
  84. 0);
  85. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  86. PFX_STR
  87. "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
  88. "",
  89. GRPC_BAD_CLIENT_DISCONNECT);
  90. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  91. PFX_STR
  92. "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
  93. "\x00",
  94. GRPC_BAD_CLIENT_DISCONNECT);
  95. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  96. PFX_STR
  97. "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
  98. "\x00\x00",
  99. GRPC_BAD_CLIENT_DISCONNECT);
  100. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  101. PFX_STR
  102. "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
  103. "\x00\x00\x00",
  104. GRPC_BAD_CLIENT_DISCONNECT);
  105. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  106. PFX_STR
  107. "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
  108. "\x00\x00\x00\x00",
  109. GRPC_BAD_CLIENT_DISCONNECT);
  110. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  111. PFX_STR
  112. "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
  113. "\x00\x00\x00\x00\x00",
  114. GRPC_BAD_CLIENT_DISCONNECT);
  115. /* test looking up an invalid index */
  116. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  117. PFX_STR
  118. "\x00\x00\x01\x01\x04\x00\x00\x00\x01"
  119. "\xfe",
  120. 0);
  121. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  122. PFX_STR
  123. "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
  124. "\x7f\x7f\x01"
  125. "a",
  126. 0);
  127. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  128. PFX_STR
  129. "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
  130. "\x0f\x7f\x01"
  131. "a",
  132. 0);
  133. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  134. PFX_STR
  135. "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
  136. "\x1f\x7f\x01"
  137. "a",
  138. 0);
  139. /* test nvr, not indexed in static table */
  140. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  141. PFX_STR
  142. "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
  143. "\x01\x01"
  144. "a",
  145. GRPC_BAD_CLIENT_DISCONNECT);
  146. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  147. PFX_STR
  148. "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
  149. "\x11\x01"
  150. "a",
  151. GRPC_BAD_CLIENT_DISCONNECT);
  152. /* illegal op code */
  153. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  154. PFX_STR
  155. "\x00\x00\x01\x01\x04\x00\x00\x00\x01"
  156. "\x80",
  157. 0);
  158. /* parse some long indices */
  159. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  160. PFX_STR
  161. "\x00\x00\x02\x01\x04\x00\x00\x00\x01"
  162. "\xff\x00",
  163. 0);
  164. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  165. PFX_STR
  166. "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
  167. "\xff\x80\x00",
  168. 0);
  169. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  170. PFX_STR
  171. "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
  172. "\xff\x80\x80\x00",
  173. 0);
  174. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  175. PFX_STR
  176. "\x00\x00\x05\x01\x04\x00\x00\x00\x01"
  177. "\xff\x80\x80\x80\x00",
  178. 0);
  179. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  180. PFX_STR
  181. "\x00\x00\x06\x01\x04\x00\x00\x00\x01"
  182. "\xff\x80\x80\x80\x80\x00",
  183. 0);
  184. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  185. PFX_STR
  186. "\x00\x00\x07\x01\x04\x00\x00\x00\x01"
  187. "\xff\x80\x80\x80\x80\x80\x00",
  188. 0);
  189. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  190. PFX_STR
  191. "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
  192. "\xff",
  193. GRPC_BAD_CLIENT_DISCONNECT);
  194. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  195. PFX_STR
  196. "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
  197. "\xff\x80",
  198. GRPC_BAD_CLIENT_DISCONNECT);
  199. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  200. PFX_STR
  201. "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
  202. "\xff\x80\x80",
  203. GRPC_BAD_CLIENT_DISCONNECT);
  204. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  205. PFX_STR
  206. "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
  207. "\xff\x80\x80\x80",
  208. GRPC_BAD_CLIENT_DISCONNECT);
  209. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  210. PFX_STR
  211. "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
  212. "\xff\x80\x80\x80\x80",
  213. GRPC_BAD_CLIENT_DISCONNECT);
  214. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  215. PFX_STR
  216. "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
  217. "\xff\x80\x80\x80\x80\x80",
  218. GRPC_BAD_CLIENT_DISCONNECT);
  219. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  220. PFX_STR
  221. "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
  222. "\xff\x80\x80\x80\x80\x80\x80",
  223. GRPC_BAD_CLIENT_DISCONNECT);
  224. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  225. PFX_STR
  226. "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
  227. "\xff\x80\x80\x80\x80\x80\x80\x00",
  228. 0);
  229. /* overflow on byte 4 */
  230. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  231. PFX_STR
  232. "\x00\x00\x06\x01\x04\x00\x00\x00\x01"
  233. "\xff\x80\x80\x80\x80\x7f",
  234. GRPC_BAD_CLIENT_DISCONNECT);
  235. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  236. PFX_STR
  237. "\x00\x00\x06\x01\x04\x00\x00\x00\x01"
  238. "\xff\xff\xff\xff\xff\x0f",
  239. GRPC_BAD_CLIENT_DISCONNECT);
  240. /* overflow after byte 4 */
  241. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  242. PFX_STR
  243. "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
  244. "\xff\x80\x80\x80\x80\x80\x80\x02",
  245. 0);
  246. /* end of headers mid-opcode */
  247. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  248. PFX_STR
  249. "\x00\x00\x01\x01\x04\x00\x00\x00\x01"
  250. "\x01",
  251. GRPC_BAD_CLIENT_DISCONNECT);
  252. /* dynamic table size update: set to default */
  253. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  254. PFX_STR
  255. "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
  256. "\x3f\xe1\x1f",
  257. GRPC_BAD_CLIENT_DISCONNECT);
  258. /* dynamic table size update: set too large */
  259. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  260. PFX_STR
  261. "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
  262. "\x3f\xf1\x1f",
  263. 0);
  264. /* dynamic table size update: set twice */
  265. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  266. PFX_STR
  267. "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
  268. "\x20\x3f\xe1\x1f",
  269. GRPC_BAD_CLIENT_DISCONNECT);
  270. /* dynamic table size update: set thrice */
  271. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  272. PFX_STR
  273. "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
  274. "\x20\x20\x20",
  275. 0);
  276. /* non-ending header followed by continuation frame */
  277. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  278. PFX_STR
  279. "\x00\x00\x00\x01\x00\x00\x00\x00\x01"
  280. "\x00\x00\x00\x09\x04\x00\x00\x00\x01",
  281. GRPC_BAD_CLIENT_DISCONNECT);
  282. /* non-ending header followed by non-continuation frame */
  283. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  284. PFX_STR
  285. "\x00\x00\x00\x01\x00\x00\x00\x00\x01"
  286. "\x00\x00\x00\x00\x04\x00\x00\x00\x01",
  287. 0);
  288. /* non-ending header followed by a continuation frame for a different stream
  289. */
  290. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  291. PFX_STR
  292. "\x00\x00\x00\x01\x04\x00\x00\x00\x01"
  293. "\x00\x00\x00\x01\x00\x00\x00\x00\x03"
  294. "\x00\x00\x00\x09\x04\x00\x00\x00\x01",
  295. 0);
  296. /* opening with a continuation frame */
  297. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  298. PFX_STR "\x00\x00\x00\x09\x04\x00\x00\x00\x01", 0);
  299. /* three header frames */
  300. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  301. PFX_STR
  302. "\x00\x00\x00\x01\x04\x00\x00\x00\x01"
  303. "\x00\x00\x00\x01\x04\x00\x00\x00\x01"
  304. "\x00\x00\x00\x01\x04\x00\x00\x00\x01",
  305. GRPC_BAD_CLIENT_DISCONNECT);
  306. /* an invalid header found with fuzzing */
  307. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  308. PFX_STR "\x00\x00\x00\x01\x39\x67\xed\x1d\x64",
  309. GRPC_BAD_CLIENT_DISCONNECT);
  310. /* a badly encoded timeout value */
  311. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  312. PFX_STR
  313. "\x00\x00\x19\x01\x04\x00\x00\x00\x01"
  314. "\x10\x0cgrpc-timeout\x0a"
  315. "15 seconds",
  316. GRPC_BAD_CLIENT_DISCONNECT);
  317. /* a badly encoded timeout value: twice (catches caching) */
  318. GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
  319. PFX_STR
  320. "\x00\x00\x19\x01\x04\x00\x00\x00\x01"
  321. "\x10\x0cgrpc-timeout\x0a"
  322. "15 seconds"
  323. "\x00\x00\x19\x01\x04\x00\x00\x00\x03"
  324. "\x10\x0cgrpc-timeout\x0a"
  325. "15 seconds",
  326. GRPC_BAD_CLIENT_DISCONNECT);
  327. grpc_shutdown();
  328. return 0;
  329. }