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(oct(0x1940efe9d47ae889) == "0145007376472436564211")
assert(bin(0x1940efe9d47ae889) == "0b0001100101000000111011111110100111010100011110101110100010001001") assert(bin(0x1940efe9d47ae889) == "0b0001100101000000111011111110100111010100011110101110100010001001")
assert(EscapeHtml(nil) == nil)
assert(EscapeHtml("?hello&there<>") == "?hello&amp;there&lt;&gt;") assert(EscapeHtml("?hello&there<>") == "?hello&amp;there&lt;&gt;")
assert(EscapeParam(nil) == nil)
assert(EscapeParam("?hello&there<>") == "%3Fhello%26there%3C%3E") assert(EscapeParam("?hello&there<>") == "%3Fhello%26there%3C%3E")
assert(DecodeLatin1(nil) == nil)
assert(DecodeLatin1("hello\xff\xc0") == "helloÿÀ") assert(DecodeLatin1("hello\xff\xc0") == "helloÿÀ")
assert(EncodeLatin1("helloÿÀ") == "hello\xff\xc0") 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(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") == "hello")
assert(Underlong("hello\xc0\x80") == "hello\x00") assert(Underlong("hello\xc0\x80") == "hello\x00")
@ -91,6 +96,7 @@ assert(not IsLoopbackIp(0x08080808))
assert(not IsLoopbackIp(0x0a000000)) assert(not IsLoopbackIp(0x0a000000))
assert(IsLoopbackIp(0x7f000001)) assert(IsLoopbackIp(0x7f000001))
assert(IndentLines(nil) == nil)
assert(IndentLines("hi\nthere") == " hi\n there") assert(IndentLines("hi\nthere") == " hi\n there")
assert(IndentLines("hi\nthere\n") == " hi\n there\n") assert(IndentLines("hi\nthere\n") == " hi\n there\n")
assert(IndentLines("hi\nthere\n", 2) == " hi\n there\n") assert(IndentLines("hi\nthere\n", 2) == " hi\n there\n")

View file

@ -15,6 +15,8 @@
unix.pledge('stdio') unix.pledge('stdio')
assert(Md5(nil) == nil)
-- https://datatracker.ietf.org/doc/html/rfc1321#appendix-A.5 -- 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("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") 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') unix.pledge('stdio')
-- SHA-1 -- 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("abc") == "\xa9\x99\x3e\x36\x47\x06\x81\x6a\xba\x3e\x25\x71\x78\x50\xc2\x6c\x9c\xd0\xd8\x9d")
assert( assert(
Sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") == Sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") ==
@ -57,6 +58,7 @@ assert(
) )
-- SHA-224 -- SHA-224
assert(Sha224(nil) == nil)
assert( assert(
Sha224("abc") == Sha224("abc") ==
"\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2" .. "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2" ..
@ -106,6 +108,7 @@ assert(
) )
-- SHA-256 -- SHA-256
assert(Sha256(nil) == nil)
assert( assert(
Sha256("abc") == Sha256("abc") ==
"\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23" .. "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23" ..
@ -150,6 +153,7 @@ assert(
) )
-- SHA-384 -- SHA-384
assert(Sha384(nil) == nil)
assert( assert(
Sha384("abc") == 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" .. "\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 -- SHA-512
assert(Sha512(nil) == nil)
assert( assert(
Sha512("abc") == 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" .. "\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 -- this intercepts all requests if it's defined
function OnHttpRequest() function OnHttpRequest()
Log(kLogInfo, "client is running %s and reports %s" % { if GetHeader('User-Agent') then
finger.GetSynFingerOs(finger.FingerSyn(syn)), Log(kLogInfo, "client is running %s and reports %s" % {
VisualizeControlCodes(GetHeader('User-Agent'))}) finger.GetSynFingerOs(finger.FingerSyn(syn)),
VisualizeControlCodes(GetHeader('User-Agent'))})
end
if HasParam('magic') then if HasParam('magic') then
Write('<p>\r\n') Write('<p>\r\n')
Write('OnHttpRequest() has intercepted your request<br>\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; size_t size;
const char *data; const char *data;
struct UrlParams h; struct UrlParams h;
data = luaL_checklstring(L, 1, &size); if (!lua_isnoneornil(L, 1)) {
bzero(&h, sizeof(h)); data = luaL_checklstring(L, 1, &size);
if ((m = ParseParams(data, size, &h))) { bzero(&h, sizeof(h));
LuaPushUrlParams(L, &h); if ((m = ParseParams(data, size, &h))) {
free(h.p); LuaPushUrlParams(L, &h);
free(m); free(h.p);
return 1; free(m);
return 1;
} else {
luaL_error(L, "out of memory");
unreachable;
}
} else { } else {
luaL_error(L, "out of memory"); return lua_gettop(L);
unreachable;
} }
} }
@ -285,17 +289,21 @@ int LuaParseHost(lua_State *L) {
size_t n; size_t n;
struct Url h; struct Url h;
const char *p; const char *p;
bzero(&h, sizeof(h)); if (!lua_isnoneornil(L, 1)) {
p = luaL_checklstring(L, 1, &n); bzero(&h, sizeof(h));
if ((m = ParseHost(p, n, &h))) { p = luaL_checklstring(L, 1, &n);
lua_newtable(L); if ((m = ParseHost(p, n, &h))) {
LuaPushUrlView(L, &h.host); lua_newtable(L);
LuaPushUrlView(L, &h.port); LuaPushUrlView(L, &h.host);
free(m); LuaPushUrlView(L, &h.port);
return 1; free(m);
return 1;
} else {
luaL_error(L, "out of memory");
unreachable;
}
} else { } else {
luaL_error(L, "out of memory"); return lua_gettop(L);
unreachable;
} }
} }
@ -347,16 +355,20 @@ int LuaCrc32c(lua_State *L) {
int LuaIndentLines(lua_State *L) { int LuaIndentLines(lua_State *L) {
void *p; void *p;
size_t n, j; size_t n, j;
p = luaL_checklstring(L, 1, &n); if (!lua_isnoneornil(L, 1)) {
j = luaL_optinteger(L, 2, 1); p = luaL_checklstring(L, 1, &n);
if (!(0 <= j && j <= 65535)) { j = luaL_optinteger(L, 2, 1);
luaL_argerror(L, 2, "not in range 0..65535"); if (!(0 <= j && j <= 65535)) {
unreachable; 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) { int LuaGetMonospaceWidth(lua_State *L) {
@ -624,15 +636,19 @@ static dontinline int LuaCoderImpl(lua_State *L,
char *C(const char *, size_t, size_t *)) { char *C(const char *, size_t, size_t *)) {
void *p; void *p;
size_t n; size_t n;
p = luaL_checklstring(L, 1, &n); if (!lua_isnoneornil(L, 1)) {
if ((p = C(p, n, &n))) { p = luaL_checklstring(L, 1, &n);
lua_pushlstring(L, p, n); if ((p = C(p, n, &n))) {
free(p); lua_pushlstring(L, p, n);
free(p);
} else {
luaL_error(L, "out of memory");
unreachable;
}
return 1;
} else { } else {
luaL_error(L, "out of memory"); return lua_gettop(L);
unreachable;
} }
return 1;
} }
static dontinline int LuaCoder(lua_State *L, static dontinline int LuaCoder(lua_State *L,
@ -715,11 +731,15 @@ static dontinline int LuaHasherImpl(lua_State *L, size_t k,
void *p; void *p;
size_t n; size_t n;
uint8_t d[64]; uint8_t d[64];
p = luaL_checklstring(L, 1, &n); if (!lua_isnoneornil(L, 1)) {
H(p, n, d); p = luaL_checklstring(L, 1, &n);
lua_pushlstring(L, (void *)d, k); H(p, n, d);
mbedtls_platform_zeroize(d, sizeof(d)); lua_pushlstring(L, (void *)d, k);
return 1; mbedtls_platform_zeroize(d, sizeof(d));
return 1;
} else {
return lua_gettop(L);
}
} }
static dontinline int LuaHasher(lua_State *L, size_t k, static dontinline int LuaHasher(lua_State *L, size_t k,