You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.4 KiB

  1. commit dc2ee27c7a1908ca3157a10ad131f13644bcaea3
  2. Author: Christopher Faulet <cfaulet@haproxy.com>
  3. Date: Fri Jul 26 16:17:01 2019 +0200
  4. BUG/MEDIUM: hlua: Check the calling direction in lua functions of the HTTP class
  5. It is invalid to manipulate responses from http-request rules or to manipulate
  6. requests from http-response rules. When http-request rules are evaluated, the
  7. connection to server is not yet established, so there is no response at all. And
  8. when http-response rules are evaluated, the request has already been sent to the
  9. server.
  10. Now, the calling direction is checked. So functions "txn.http:req_*" can now
  11. only be called from http-request rules and the functions "txn.http:res_*" can
  12. only be called from http-response rules.
  13. This issue was reported on Github (#190).
  14. This patch must be backported to all versions since the 1.6.
  15. (cherry picked from commit 84a6d5bc217a418db8efc4e76a0a32860db2c608)
  16. Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
  17. diff --git a/src/hlua.c b/src/hlua.c
  18. index f9d1d699..21351cd6 100644
  19. --- a/src/hlua.c
  20. +++ b/src/hlua.c
  21. @@ -5346,6 +5346,9 @@ __LJMP static int hlua_http_req_get_headers(lua_State *L)
  22. MAY_LJMP(check_args(L, 1, "req_get_headers"));
  23. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  24. + if (htxn->dir != SMP_OPT_DIR_REQ)
  25. + WILL_LJMP(lua_error(L));
  26. +
  27. return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
  28. }
  29. @@ -5356,6 +5359,9 @@ __LJMP static int hlua_http_res_get_headers(lua_State *L)
  30. MAY_LJMP(check_args(L, 1, "res_get_headers"));
  31. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  32. + if (htxn->dir != SMP_OPT_DIR_RES)
  33. + WILL_LJMP(lua_error(L));
  34. +
  35. return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
  36. }
  37. @@ -5393,6 +5399,9 @@ __LJMP static int hlua_http_req_rep_hdr(lua_State *L)
  38. MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
  39. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  40. + if (htxn->dir != SMP_OPT_DIR_REQ)
  41. + WILL_LJMP(lua_error(L));
  42. +
  43. return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
  44. }
  45. @@ -5403,6 +5412,9 @@ __LJMP static int hlua_http_res_rep_hdr(lua_State *L)
  46. MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
  47. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  48. + if (htxn->dir != SMP_OPT_DIR_RES)
  49. + WILL_LJMP(lua_error(L));
  50. +
  51. return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
  52. }
  53. @@ -5413,6 +5425,9 @@ __LJMP static int hlua_http_req_rep_val(lua_State *L)
  54. MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
  55. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  56. + if (htxn->dir != SMP_OPT_DIR_REQ)
  57. + WILL_LJMP(lua_error(L));
  58. +
  59. return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
  60. }
  61. @@ -5423,6 +5438,9 @@ __LJMP static int hlua_http_res_rep_val(lua_State *L)
  62. MAY_LJMP(check_args(L, 4, "res_rep_val"));
  63. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  64. + if (htxn->dir != SMP_OPT_DIR_RES)
  65. + WILL_LJMP(lua_error(L));
  66. +
  67. return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
  68. }
  69. @@ -5462,6 +5480,9 @@ __LJMP static int hlua_http_req_del_hdr(lua_State *L)
  70. MAY_LJMP(check_args(L, 2, "req_del_hdr"));
  71. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  72. + if (htxn->dir != SMP_OPT_DIR_REQ)
  73. + WILL_LJMP(lua_error(L));
  74. +
  75. return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
  76. }
  77. @@ -5469,9 +5490,12 @@ __LJMP static int hlua_http_res_del_hdr(lua_State *L)
  78. {
  79. struct hlua_txn *htxn;
  80. - MAY_LJMP(check_args(L, 2, "req_del_hdr"));
  81. + MAY_LJMP(check_args(L, 2, "res_del_hdr"));
  82. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  83. + if (htxn->dir != SMP_OPT_DIR_RES)
  84. + WILL_LJMP(lua_error(L));
  85. +
  86. return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
  87. }
  88. @@ -5523,6 +5547,9 @@ __LJMP static int hlua_http_req_add_hdr(lua_State *L)
  89. MAY_LJMP(check_args(L, 3, "req_add_hdr"));
  90. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  91. + if (htxn->dir != SMP_OPT_DIR_REQ)
  92. + WILL_LJMP(lua_error(L));
  93. +
  94. return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
  95. }
  96. @@ -5533,6 +5560,9 @@ __LJMP static int hlua_http_res_add_hdr(lua_State *L)
  97. MAY_LJMP(check_args(L, 3, "res_add_hdr"));
  98. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  99. + if (htxn->dir != SMP_OPT_DIR_RES)
  100. + WILL_LJMP(lua_error(L));
  101. +
  102. return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
  103. }
  104. @@ -5543,6 +5573,9 @@ static int hlua_http_req_set_hdr(lua_State *L)
  105. MAY_LJMP(check_args(L, 3, "req_set_hdr"));
  106. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  107. + if (htxn->dir != SMP_OPT_DIR_REQ)
  108. + WILL_LJMP(lua_error(L));
  109. +
  110. hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
  111. return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
  112. }
  113. @@ -5554,6 +5587,9 @@ static int hlua_http_res_set_hdr(lua_State *L)
  114. MAY_LJMP(check_args(L, 3, "res_set_hdr"));
  115. htxn = MAY_LJMP(hlua_checkhttp(L, 1));
  116. + if (htxn->dir != SMP_OPT_DIR_RES)
  117. + WILL_LJMP(lua_error(L));
  118. +
  119. hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
  120. return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
  121. }
  122. @@ -5565,6 +5601,9 @@ static int hlua_http_req_set_meth(lua_State *L)
  123. size_t name_len;
  124. const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
  125. + if (htxn->dir != SMP_OPT_DIR_REQ)
  126. + WILL_LJMP(lua_error(L));
  127. +
  128. lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
  129. return 1;
  130. }
  131. @@ -5576,6 +5615,9 @@ static int hlua_http_req_set_path(lua_State *L)
  132. size_t name_len;
  133. const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
  134. + if (htxn->dir != SMP_OPT_DIR_REQ)
  135. + WILL_LJMP(lua_error(L));
  136. +
  137. lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
  138. return 1;
  139. }
  140. @@ -5587,6 +5629,9 @@ static int hlua_http_req_set_query(lua_State *L)
  141. size_t name_len;
  142. const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
  143. + if (htxn->dir != SMP_OPT_DIR_REQ)
  144. + WILL_LJMP(lua_error(L));
  145. +
  146. /* Check length. */
  147. if (name_len > trash.size - 1) {
  148. lua_pushboolean(L, 0);
  149. @@ -5611,6 +5656,9 @@ static int hlua_http_req_set_uri(lua_State *L)
  150. size_t name_len;
  151. const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
  152. + if (htxn->dir != SMP_OPT_DIR_REQ)
  153. + WILL_LJMP(lua_error(L));
  154. +
  155. lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
  156. return 1;
  157. }
  158. @@ -5622,6 +5670,9 @@ static int hlua_http_res_set_status(lua_State *L)
  159. unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
  160. const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
  161. + if (htxn->dir != SMP_OPT_DIR_RES)
  162. + WILL_LJMP(lua_error(L));
  163. +
  164. http_set_status(code, reason, htxn->s);
  165. return 0;
  166. }