]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: httpclient/lua: be stricter with httpclient parameters
authorWilliam Lallemand <wlallemand@haproxy.org>
Thu, 3 Mar 2022 14:33:12 +0000 (15:33 +0100)
committerWilliam Lallemand <wlallemand@haproxy.org>
Wed, 30 Mar 2022 10:18:16 +0000 (12:18 +0200)
Checks the argument passed to the httpclient send functions so we don't
add a mispelled argument.

src/hlua.c

index b8dd0c7598e59ce92b9de2671ec5d6d6c8ad6206..cf58e9288f116174631917c00edb47c734172181 100644 (file)
@@ -7231,7 +7231,6 @@ __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth)
        struct hlua *hlua;
        const char *url_str = NULL;
        const char *body_str = NULL;
-       int timeout;
        size_t buf_len;
        int ret;
 
@@ -7246,38 +7245,38 @@ __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth)
 
        hlua_hc = hlua_checkhttpclient(L, 1);
 
-       ret = lua_getfield(L, -1, "dst");
-       if (ret == LUA_TSTRING) {
-               if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0)
-                       WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument"));
-       }
-       lua_pop(L, 1);
+       lua_pushnil(L);  /* first key */
+       while (lua_next(L, 2)) {
+               if (strcmp(lua_tostring(L, -2), "dst") == 0) {
+                       if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0)
+                               WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument"));
 
-       ret = lua_getfield(L, -1, "url");
-       if (ret == LUA_TSTRING) {
-               url_str = lua_tostring(L, -1);
-       }
-       lua_pop(L, 1);
+               } else if (strcmp(lua_tostring(L, -2), "url") == 0) {
+                       if (lua_type(L, -1) != LUA_TSTRING)
+                               WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string"));
+                       url_str = lua_tostring(L, -1);
 
-       ret = lua_getfield(L, -1, "timeout");
-       if (ret == LUA_TNUMBER) {
-               timeout = lua_tointeger(L, -1);
-               httpclient_set_timeout(hlua_hc->hc, timeout);
-       }
-       lua_pop(L, 1);
+               } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) {
+                       if (lua_type(L, -1) != LUA_TNUMBER)
+                               WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number"));
+                       httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1));
 
-       ret = lua_getfield(L, -1, "headers");
-       if (ret == LUA_TTABLE) {
-               hdrs = hlua_httpclient_table_to_hdrs(L);
-       }
-       lua_pop(L, 1);
+               } else if (strcmp(lua_tostring(L, -2), "headers") == 0) {
+                       if (lua_type(L, -1) != LUA_TTABLE)
+                               WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table"));
+                       hdrs = hlua_httpclient_table_to_hdrs(L);
 
-       ret = lua_getfield(L, -1, "body");
-       if (ret == LUA_TSTRING) {
-               body_str = lua_tolstring(L, -1, &buf_len);
-       }
+               } else if (strcmp(lua_tostring(L, -2), "body") == 0) {
+                       if (lua_type(L, -1) != LUA_TSTRING)
+                               WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string"));
+                       body_str = lua_tolstring(L, -1, &buf_len);
 
-       lua_pop(L, 1);
+               } else {
+                       WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2)));
+               }
+               /* removes 'value'; keeps 'key' for next iteration */
+               lua_pop(L, 1);
+       }
 
        if (!url_str) {
                WILL_LJMP(luaL_error(L, "'get' need a 'url' argument"));