Propagate nil in Lua APIs more often

This commit is contained in:
Justine Tunney 2022-08-05 17:34:13 -07:00
parent c9d7838213
commit 638c56e3a5
5 changed files with 78 additions and 43 deletions

View file

@ -47,9 +47,13 @@ assert(hex(0x1940efe9d47ae889) == "0x1940efe9d47ae889")
assert(oct(0x1940efe9d47ae889) == "0145007376472436564211")
assert(bin(0x1940efe9d47ae889) == "0b0001100101000000111011111110100111010100011110101110100010001001")
assert(EscapeHtml(nil) == nil)
assert(EscapeHtml("?hello&there<>") == "?hello&amp;there&lt;&gt;")
assert(EscapeParam(nil) == nil)
assert(EscapeParam("?hello&there<>") == "%3Fhello%26there%3C%3E")
assert(DecodeLatin1(nil) == nil)
assert(DecodeLatin1("hello\xff\xc0") == "helloÿÀ")
assert(EncodeLatin1("helloÿÀ") == "hello\xff\xc0")
@ -65,6 +69,7 @@ assert(url.fragment == "frag")
assert(Decimate("\xff\xff\x00\x00\xff\xff\x00\x00\xff\xff\x00\x00") == "\xff\x00\xff\x00\xff\x00")
assert(Underlong(nil) == nil)
assert(Underlong("hello") == "hello")
assert(Underlong("hello\xc0\x80") == "hello\x00")
@ -91,6 +96,7 @@ assert(not IsLoopbackIp(0x08080808))
assert(not IsLoopbackIp(0x0a000000))
assert(IsLoopbackIp(0x7f000001))
assert(IndentLines(nil) == nil)
assert(IndentLines("hi\nthere") == " hi\n there")
assert(IndentLines("hi\nthere\n") == " hi\n there\n")
assert(IndentLines("hi\nthere\n", 2) == " hi\n there\n")

View file

@ -15,6 +15,8 @@
unix.pledge('stdio')
assert(Md5(nil) == nil)
-- https://datatracker.ietf.org/doc/html/rfc1321#appendix-A.5
assert(Md5("hello") == "\x5d\x41\x40\x2a\xbc\x4b\x2a\x76\xb9\x71\x9d\x91\x10\x17\xc5\x92")
assert(Md5("") == "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e")

View file

@ -22,6 +22,7 @@
unix.pledge('stdio')
-- SHA-1
assert(Sha1(nil) == nil)
assert(Sha1("abc") == "\xa9\x99\x3e\x36\x47\x06\x81\x6a\xba\x3e\x25\x71\x78\x50\xc2\x6c\x9c\xd0\xd8\x9d")
assert(
Sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") ==
@ -57,6 +58,7 @@ assert(
)
-- SHA-224
assert(Sha224(nil) == nil)
assert(
Sha224("abc") ==
"\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2" ..
@ -106,6 +108,7 @@ assert(
)
-- SHA-256
assert(Sha256(nil) == nil)
assert(
Sha256("abc") ==
"\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23" ..
@ -150,6 +153,7 @@ assert(
)
-- SHA-384
assert(Sha384(nil) == nil)
assert(
Sha384("abc") ==
"\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63" ..
@ -203,6 +207,7 @@ assert(
)
-- SHA-512
assert(Sha512(nil) == nil)
assert(
Sha512("abc") ==
"\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2" ..

View file

@ -31,9 +31,11 @@ end
-- this intercepts all requests if it's defined
function OnHttpRequest()
Log(kLogInfo, "client is running %s and reports %s" % {
finger.GetSynFingerOs(finger.FingerSyn(syn)),
VisualizeControlCodes(GetHeader('User-Agent'))})
if GetHeader('User-Agent') then
Log(kLogInfo, "client is running %s and reports %s" % {
finger.GetSynFingerOs(finger.FingerSyn(syn)),
VisualizeControlCodes(GetHeader('User-Agent'))})
end
if HasParam('magic') then
Write('<p>\r\n')
Write('OnHttpRequest() has intercepted your request<br>\r\n')

View file

@ -267,16 +267,20 @@ int LuaParseParams(lua_State *L) {
size_t size;
const char *data;
struct UrlParams h;
data = luaL_checklstring(L, 1, &size);
bzero(&h, sizeof(h));
if ((m = ParseParams(data, size, &h))) {
LuaPushUrlParams(L, &h);
free(h.p);
free(m);
return 1;
if (!lua_isnoneornil(L, 1)) {
data = luaL_checklstring(L, 1, &size);
bzero(&h, sizeof(h));
if ((m = ParseParams(data, size, &h))) {
LuaPushUrlParams(L, &h);
free(h.p);
free(m);
return 1;
} else {
luaL_error(L, "out of memory");
unreachable;
}
} else {
luaL_error(L, "out of memory");
unreachable;
return lua_gettop(L);
}
}
@ -285,17 +289,21 @@ int LuaParseHost(lua_State *L) {
size_t n;
struct Url h;
const char *p;
bzero(&h, sizeof(h));
p = luaL_checklstring(L, 1, &n);
if ((m = ParseHost(p, n, &h))) {
lua_newtable(L);
LuaPushUrlView(L, &h.host);
LuaPushUrlView(L, &h.port);
free(m);
return 1;
if (!lua_isnoneornil(L, 1)) {
bzero(&h, sizeof(h));
p = luaL_checklstring(L, 1, &n);
if ((m = ParseHost(p, n, &h))) {
lua_newtable(L);
LuaPushUrlView(L, &h.host);
LuaPushUrlView(L, &h.port);
free(m);
return 1;
} else {
luaL_error(L, "out of memory");
unreachable;
}
} else {
luaL_error(L, "out of memory");
unreachable;
return lua_gettop(L);
}
}
@ -347,16 +355,20 @@ int LuaCrc32c(lua_State *L) {
int LuaIndentLines(lua_State *L) {
void *p;
size_t n, j;
p = luaL_checklstring(L, 1, &n);
j = luaL_optinteger(L, 2, 1);
if (!(0 <= j && j <= 65535)) {
luaL_argerror(L, 2, "not in range 0..65535");
unreachable;
if (!lua_isnoneornil(L, 1)) {
p = luaL_checklstring(L, 1, &n);
j = luaL_optinteger(L, 2, 1);
if (!(0 <= j && j <= 65535)) {
luaL_argerror(L, 2, "not in range 0..65535");
unreachable;
}
p = IndentLines(p, n, &n, j);
lua_pushlstring(L, p, n);
free(p);
return 1;
} else {
return lua_gettop(L);
}
p = IndentLines(p, n, &n, j);
lua_pushlstring(L, p, n);
free(p);
return 1;
}
int LuaGetMonospaceWidth(lua_State *L) {
@ -624,15 +636,19 @@ static dontinline int LuaCoderImpl(lua_State *L,
char *C(const char *, size_t, size_t *)) {
void *p;
size_t n;
p = luaL_checklstring(L, 1, &n);
if ((p = C(p, n, &n))) {
lua_pushlstring(L, p, n);
free(p);
if (!lua_isnoneornil(L, 1)) {
p = luaL_checklstring(L, 1, &n);
if ((p = C(p, n, &n))) {
lua_pushlstring(L, p, n);
free(p);
} else {
luaL_error(L, "out of memory");
unreachable;
}
return 1;
} else {
luaL_error(L, "out of memory");
unreachable;
return lua_gettop(L);
}
return 1;
}
static dontinline int LuaCoder(lua_State *L,
@ -715,11 +731,15 @@ static dontinline int LuaHasherImpl(lua_State *L, size_t k,
void *p;
size_t n;
uint8_t d[64];
p = luaL_checklstring(L, 1, &n);
H(p, n, d);
lua_pushlstring(L, (void *)d, k);
mbedtls_platform_zeroize(d, sizeof(d));
return 1;
if (!lua_isnoneornil(L, 1)) {
p = luaL_checklstring(L, 1, &n);
H(p, n, d);
lua_pushlstring(L, (void *)d, k);
mbedtls_platform_zeroize(d, sizeof(d));
return 1;
} else {
return lua_gettop(L);
}
}
static dontinline int LuaHasher(lua_State *L, size_t k,