Skip to content

Commit

Permalink
Merge pull request #79 from tarantool/51-html-escaping
Browse files Browse the repository at this point in the history
Check ptr to string before use it
  • Loading branch information
Satbek authored Nov 3, 2020
2 parents 8909e39 + 16c761a commit e7e00ea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
39 changes: 34 additions & 5 deletions http/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,37 @@ tpl_term(int type, const char *str, size_t len, void *data)
}
}

/**
* This function exists because lua_tostring does not use
* __tostring metamethod, and this metamethod has to be used
* if we want to print Lua userdata correctly.
*/
static const char *
luaT_tolstring(lua_State *L, int idx, size_t *len)
{
if (!luaL_callmeta(L, idx, "__tostring")) {
switch (lua_type(L, idx)) {
case LUA_TNUMBER:
case LUA_TSTRING:
lua_pushvalue(L, idx);
break;
case LUA_TBOOLEAN: {
int val = lua_toboolean(L, idx);
lua_pushstring(L, val ? "true" : "false");
break;
}
case LUA_TNIL:
lua_pushliteral(L, "nil");
break;
default:
lua_pushfstring(L, "%s: %p", luaL_typename(L, idx),
lua_topointer(L, idx));
}
}

return lua_tolstring(L, -1, len);
}

static int
lbox_httpd_escape_html(struct lua_State *L)
{
Expand All @@ -109,11 +140,8 @@ lbox_httpd_escape_html(struct lua_State *L)
}

for (i = 1; i <= top; i++) {
if (lua_isnil(L, i)) {
luaL_addstring(&b, "nil");
continue;
}
const char *s = lua_tostring(L, i);
const char *s = luaT_tolstring(L, i, NULL);
int str_idx = lua_gettop(L);
for (; *s; s++) {
switch(*s) {
case '&':
Expand All @@ -136,6 +164,7 @@ lbox_httpd_escape_html(struct lua_State *L)
break;
}
}
lua_remove(L, str_idx);
}

luaL_pushresult(&b);
Expand Down
5 changes: 5 additions & 0 deletions test/unit/lib_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ g.test_template = function()
local rendered = http_lib.template(template, { t = tt })
t.assert(#rendered > 10000, "rendered size")
t.assert_equals(rendered:sub(#rendered - 7, #rendered - 1), "</html>", "rendered eof")

-- gh-51 Incorrect arguments escaping leads to segfault
template = [[<%= {{continue}} %>"]]
local result = http_lib.template(template, {continue = '/'})
t.assert(result:find('\"') ~= nil)
end

g.test_parse_request = function()
Expand Down

0 comments on commit e7e00ea

Please sign in to comment.