From 14b33f0747d76d02d35d3e67ce5022911e7a0761 Mon Sep 17 00:00:00 2001 From: wingdeans <66850754+wingdeans@users.noreply.github.com> Date: Sun, 26 Nov 2023 21:55:55 -0500 Subject: [PATCH 1/8] Initial websocket experiment --- tool/net/redbean.c | 158 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index f7f934bf895..5cff7287d07 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -43,6 +43,7 @@ #include "libc/intrin/bits.h" #include "libc/intrin/bsr.h" #include "libc/intrin/likely.h" +#include "libc/intrin/newbie.h" #include "libc/intrin/nomultics.internal.h" #include "libc/intrin/safemacros.internal.h" #include "libc/log/appendresourcereport.internal.h" @@ -124,6 +125,7 @@ #include "third_party/mbedtls/net_sockets.h" #include "third_party/mbedtls/oid.h" #include "third_party/mbedtls/san.h" +#include "third_party/mbedtls/sha1.h" #include "third_party/mbedtls/ssl.h" #include "third_party/mbedtls/ssl_ticket.h" #include "third_party/mbedtls/x509.h" @@ -410,6 +412,7 @@ struct ClearedPerMessage { bool hascontenttype; bool gotcachecontrol; bool gotxcontenttypeoptions; + bool iswebsocket; int frags; int statuscode; int isyielding; @@ -5053,6 +5056,106 @@ static bool LuaRunAsset(const char *path, bool mandatory) { return !!a; } +static int LuaUpgradeWS(lua_State *L) { + size_t i; + char *p, *q; + bool haskey; + mbedtls_sha1_context ctx; + unsigned char hash[20]; + OnlyCallDuringRequest(L, "UpgradeWS"); + + haskey = true; + for (i = 0; i < cpm.msg.xheaders.n; ++i) { + if (SlicesEqualCase( + "Sec-WebSocket-Key", strlen("Sec-WebSocket-Key"), + inbuf.p + cpm.msg.xheaders.p[i].k.a, + cpm.msg.xheaders.p[i].k.b - cpm.msg.xheaders.p[i].k.a)) { + mbedtls_sha1_init(&ctx); + mbedtls_sha1_starts_ret(&ctx); + mbedtls_sha1_update_ret( + &ctx, (unsigned char *)inbuf.p + cpm.msg.xheaders.p[i].v.a, + cpm.msg.xheaders.p[i].v.b - cpm.msg.xheaders.p[i].v.a); + haskey = true; + break; + } + } + + if (!haskey) luaL_error(L, "No Sec-WebSocket-Key header"); + + p = SetStatus(101, "Switching Protocols"); + while (p - hdrbuf.p + (20 + 21 + (20 + 28 + 4)) + 512 > hdrbuf.n) { + hdrbuf.n += hdrbuf.n >> 1; + q = xrealloc(hdrbuf.p, hdrbuf.n); + cpm.luaheaderp = p = q + (p - hdrbuf.p); + hdrbuf.p = q; + } + + mbedtls_sha1_update_ret( + &ctx, (unsigned char *)"258EAFA5-E914-47DA-95CA-C5AB0DC85B11", 36); + mbedtls_sha1_finish_ret(&ctx, hash); + char *accept = EncodeBase64((char *)hash, 20, NULL); + + p = stpcpy(p, "Upgrade: websocket\r\n"); + p = stpcpy(p, "Connection: upgrade\r\n"); + p = AppendHeader(p, "Sec-WebSocket-Accept", accept); + + cpm.luaheaderp = p; + cpm.iswebsocket = true; + return 0; +} + +static int LuaReadWS(lua_State *L) { + ssize_t rc; + size_t i, got, amt; + unsigned char wshdr[10], wshdrlen, *extlen, *mask; + uint64_t len; + OnlyCallDuringRequest(L, "ReadWS"); + + got = 0; + do { + if ((rc = reader(client, wshdr + got, 2 - got)) == -1) + luaL_error(L, "Could not read WS header"); + } while ((got += rc) < 2); + + if (!(wshdr[1] | (1 << 7))) luaL_error(L, "Unmasked WS frame"); + + len = wshdr[1] & ~(1 << 7); + wshdrlen = 6; + if (len == 126) { + wshdrlen = 8; + } else if (len == 127) { + wshdrlen = 14; + } + + while (got < wshdrlen) { + if ((rc = reader(client, wshdr + got, wshdrlen - got)) == -1) + luaL_error(L, "Could not read WS extended length"); + got += rc; + } + + extlen = &wshdr[2]; + mask = &wshdr[wshdrlen - 4]; + if (len == 126) { + len = be16toh(*(uint16_t *)extlen); + } else if (len == 127) { + len = be64toh(*(uint64_t *)extlen); + } + + if (len >= inbuf.n - amtread) + luaL_error(L, "Required %d bytes to read WS frame, %d bytes available", len, + inbuf.n - amtread); + + for (got = 0, amt = amtread; got < len; got += rc, amt += rc) { + if ((rc = reader(client, inbuf.p + amt, len - got)) == -1) + luaL_error(L, "Could not read WS data"); + } + + for (i = 0, amt = amtread; i < got; ++i, ++amt) inbuf.p[amt] ^= mask[i & 0x3]; + + lua_pushlstring(L, inbuf.p + amtread, got); + return 1; +} + // // list of functions that can't be run from the repl static const char *const kDontAutoComplete[] = { @@ -5252,6 +5355,7 @@ static const luaL_Reg kLuaFuncs[] = { {"ProgramUid", LuaProgramUid}, // {"ProgramUniprocess", LuaProgramUniprocess}, // {"Rand64", LuaRand64}, // + {"ReadWS", LuaReadWS}, // undocumented {"Rdrand", LuaRdrand}, // {"Rdseed", LuaRdseed}, // {"Rdtsc", LuaRdtsc}, // @@ -5279,6 +5383,7 @@ static const luaL_Reg kLuaFuncs[] = { {"StoreAsset", LuaStoreAsset}, // {"Uncompress", LuaUncompress}, // {"Underlong", LuaUnderlong}, // + {"UpgradeWS", LuaUpgradeWS}, // undocumented {"VisualizeControlCodes", LuaVisualizeControlCodes}, // {"Write", LuaWrite}, // {"bin", LuaBin}, // @@ -6334,6 +6439,57 @@ static bool StreamResponse(char *p) { return true; } +static bool StreamWS(char *p) { + ssize_t rc; + struct iovec iov[4]; + char *s, wshdr[10], *extlen; + + p = AppendCrlf(p); + CHECK_LE(p - hdrbuf.p, hdrbuf.n); + if (logmessages) { + LogMessage("sending", hdrbuf.p, p - hdrbuf.p); + } + iov[0].iov_base = hdrbuf.p; + iov[0].iov_len = p - hdrbuf.p; + Send(iov, 1); + + bzero(iov, sizeof(iov)); + iov[0].iov_base = wshdr; + + wshdr[0] = 0x1 | (0x1 << 7); + extlen = &wshdr[2]; + + cpm.isyielding = 2; // skip first YieldGenerator + + for (;;) { + if ((rc = cpm.generator(iov + 1)) <= 0) break; + + if (rc < 126) { + wshdr[1] = rc; + iov[0].iov_len = 2; + } else if (rc <= 0xFFFF) { + wshdr[1] = 126; + *(uint16_t *)extlen = htobe16(rc); + iov[0].iov_len = 4; + } else { + wshdr[1] = 127; + *(uint64_t *)extlen = htobe64(rc); + iov[0].iov_len = 10; + } + if (Send(iov, 4) == -1) break; + } + + if (rc != -1) { + wshdr[0] = 0x8; + wshdr[1] = 0; + iov[0].iov_len = 2; + Send(iov, 1); + } else { + connectionclose = true; + } + return true; +} + static bool HandleMessageActual(void) { int rc; long reqtime, contime; @@ -6392,6 +6548,8 @@ static bool HandleMessageActual(void) { } if (!cpm.generator) { return TransmitResponse(p); + } else if (cpm.iswebsocket) { + return StreamWS(p); } else { return StreamResponse(p); } From 7b760da8b3a1a817184031b627f90fce4d6fb634 Mon Sep 17 00:00:00 2001 From: wingdeans <66850754+wingdeans@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:36:04 -0500 Subject: [PATCH 2/8] Always close WS, handle close message --- tool/net/redbean.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 5cff7287d07..4b79f8ca7c1 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -5150,6 +5150,9 @@ static int LuaReadWS(lua_State *L) { luaL_error(L, "Could not read WS data"); } + if ((wshdr[0] & 0xF) == 0x8) + luaL_error(L, "WS connection closed"); + for (i = 0, amt = amtread; i < got; ++i, ++amt) inbuf.p[amt] ^= mask[i & 0x3]; lua_pushlstring(L, inbuf.p + amtread, got); @@ -6479,14 +6482,12 @@ static bool StreamWS(char *p) { if (Send(iov, 4) == -1) break; } - if (rc != -1) { - wshdr[0] = 0x8; - wshdr[1] = 0; - iov[0].iov_len = 2; - Send(iov, 1); - } else { - connectionclose = true; - } + wshdr[0] = 0x8 | (1 << 7); + wshdr[1] = 0; + iov[0].iov_len = 2; + Send(iov, 1); + connectionclose = true; + return true; } From a3c060b2fff1cf3ef8a600f2034723a33ae45473 Mon Sep 17 00:00:00 2001 From: wingdeans <66850754+wingdeans@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:36:49 -0500 Subject: [PATCH 3/8] Support zero-length messages Copies part of YieldGenerator to StreamWS --- tool/net/redbean.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 4b79f8ca7c1..63a7a1e72ab 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -6444,8 +6444,9 @@ static bool StreamResponse(char *p) { static bool StreamWS(char *p) { ssize_t rc; - struct iovec iov[4]; + struct iovec iov[2]; char *s, wshdr[10], *extlen; + int nresults, status; p = AppendCrlf(p); CHECK_LE(p - hdrbuf.p, hdrbuf.n); @@ -6459,13 +6460,25 @@ static bool StreamWS(char *p) { bzero(iov, sizeof(iov)); iov[0].iov_base = wshdr; - wshdr[0] = 0x1 | (0x1 << 7); + wshdr[0] = 0x1 | (1 << 7); extlen = &wshdr[2]; - cpm.isyielding = 2; // skip first YieldGenerator - for (;;) { - if ((rc = cpm.generator(iov + 1)) <= 0) break; + if (!YL || lua_status(YL) != LUA_YIELD) break; // done yielding + cpm.contentlength = 0; + status = lua_resume(YL, NULL, 0, &nresults); + if (status != LUA_OK && status != LUA_YIELD) { + LogLuaError("resume", lua_tostring(YL, -1)); + lua_pop(YL, 1); + break; + } + lua_pop(YL, nresults); + if (!cpm.contentlength) UseOutput(); + + DEBUGF("(lua) ws yielded with %ld bytes generated", cpm.contentlength); + + iov[1].iov_base = cpm.content; + iov[1].iov_len = rc = cpm.contentlength; if (rc < 126) { wshdr[1] = rc; @@ -6479,7 +6492,7 @@ static bool StreamWS(char *p) { *(uint64_t *)extlen = htobe64(rc); iov[0].iov_len = 10; } - if (Send(iov, 4) == -1) break; + if (Send(iov, 2) == -1) break; } wshdr[0] = 0x8 | (1 << 7); From 988bbd2475402b5d4a647d0a556a1e9f905190f2 Mon Sep 17 00:00:00 2001 From: wingdeans <66850754+wingdeans@users.noreply.github.com> Date: Wed, 27 Dec 2023 22:21:38 -0500 Subject: [PATCH 4/8] Support pings, binary messages --- tool/net/redbean.c | 54 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 63a7a1e72ab..7cf5d37f273 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -412,7 +412,7 @@ struct ClearedPerMessage { bool hascontenttype; bool gotcachecontrol; bool gotxcontenttypeoptions; - bool iswebsocket; + char websockettype; int frags; int statuscode; int isyielding; @@ -5100,7 +5100,7 @@ static int LuaUpgradeWS(lua_State *L) { p = AppendHeader(p, "Sec-WebSocket-Accept", accept); cpm.luaheaderp = p; - cpm.iswebsocket = true; + cpm.websockettype = 1; return 0; } @@ -5109,6 +5109,7 @@ static int LuaReadWS(lua_State *L) { size_t i, got, amt; unsigned char wshdr[10], wshdrlen, *extlen, *mask; uint64_t len; + struct iovec iov[2]; OnlyCallDuringRequest(L, "ReadWS"); got = 0; @@ -5117,9 +5118,13 @@ static int LuaReadWS(lua_State *L) { luaL_error(L, "Could not read WS header"); } while ((got += rc) < 2); - if (!(wshdr[1] | (1 << 7))) luaL_error(L, "Unmasked WS frame"); + if (wshdr[0] & 0x70) goto close; // reserved bit set + if (!(wshdr[1] | (1 << 7))) goto close; // unmasked + if ((wshdr[0] & 0x7) >= 0x3) goto close; // reserved opcode len = wshdr[1] & ~(1 << 7); + if (wshdr[0] & 0x8 && len >= 126) goto close; // long control frame + wshdrlen = 6; if (len == 126) { wshdrlen = 8; @@ -5150,13 +5155,38 @@ static int LuaReadWS(lua_State *L) { luaL_error(L, "Could not read WS data"); } - if ((wshdr[0] & 0xF) == 0x8) - luaL_error(L, "WS connection closed"); - for (i = 0, amt = amtread; i < got; ++i, ++amt) inbuf.p[amt] ^= mask[i & 0x3]; + if ((wshdr[0] & 0xF) == 0x9) { + wshdr[0] = (wshdr[0] & ~0xF) | 0xA; + wshdr[1] = wshdr[1] & ~0x80; + iov[0].iov_base = wshdr; + iov[0].iov_len = wshdrlen - 4; + iov[1].iov_base = inbuf.p + amtread; + iov[1].iov_len = got; + Send(iov, 2); + } + lua_pushlstring(L, inbuf.p + amtread, got); - return 1; + lua_pushnumber(L, wshdr[0]); + + return 2; + +close: + lua_pushnil(L); + lua_pushnumber(L, 0x08); + return 2; +} + +static int LuaSetWSFlags(lua_State *L) { + OnlyCallDuringRequest(L, "SetWSFlags"); + char flags = lround(lua_tonumber(L, 1)); + if (flags & 0x01) { + cpm.websockettype = 1; + } else if (flags & 0x02) { + cpm.websockettype = 2; + } + return 0; } // @@ -5376,6 +5406,7 @@ static const luaL_Reg kLuaFuncs[] = { {"SetHeader", LuaSetHeader}, // {"SetLogLevel", LuaSetLogLevel}, // {"SetStatus", LuaSetStatus}, // + {"SetWSFlags", LuaSetWSFlags}, // undocumented {"Sha1", LuaSha1}, // {"Sha224", LuaSha224}, // {"Sha256", LuaSha256}, // @@ -6460,14 +6491,16 @@ static bool StreamWS(char *p) { bzero(iov, sizeof(iov)); iov[0].iov_base = wshdr; - wshdr[0] = 0x1 | (1 << 7); extlen = &wshdr[2]; for (;;) { if (!YL || lua_status(YL) != LUA_YIELD) break; // done yielding cpm.contentlength = 0; status = lua_resume(YL, NULL, 0, &nresults); - if (status != LUA_OK && status != LUA_YIELD) { + if (status == LUA_OK) { + lua_pop(YL, nresults); + break; + } else if (status != LUA_YIELD) { LogLuaError("resume", lua_tostring(YL, -1)); lua_pop(YL, 1); break; @@ -6492,6 +6525,7 @@ static bool StreamWS(char *p) { *(uint64_t *)extlen = htobe64(rc); iov[0].iov_len = 10; } + wshdr[0] = cpm.websockettype | (1 << 7); if (Send(iov, 2) == -1) break; } @@ -6562,7 +6596,7 @@ static bool HandleMessageActual(void) { } if (!cpm.generator) { return TransmitResponse(p); - } else if (cpm.iswebsocket) { + } else if (cpm.websockettype) { return StreamWS(p); } else { return StreamResponse(p); From 8a8fc9a65fbdd81bb51803d3805e70759f243b82 Mon Sep 17 00:00:00 2001 From: wingdeans <66850754+wingdeans@users.noreply.github.com> Date: Thu, 28 Dec 2023 00:49:52 -0500 Subject: [PATCH 5/8] Support fragmentation, utf-8 checks --- tool/net/redbean.c | 71 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 7cf5d37f273..dea0a1ee6d3 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -412,7 +412,7 @@ struct ClearedPerMessage { bool hascontenttype; bool gotcachecontrol; bool gotxcontenttypeoptions; - char websockettype; + char wstype; int frags; int statuscode; int isyielding; @@ -493,6 +493,8 @@ static uint8_t *zmap; static uint8_t *zcdir; static size_t hdrsize; static size_t amtread; +static size_t wsfragread; +static char wsfragtype; static reader_f reader; static writer_f writer; static char *extrahdrs; @@ -5100,14 +5102,15 @@ static int LuaUpgradeWS(lua_State *L) { p = AppendHeader(p, "Sec-WebSocket-Accept", accept); cpm.luaheaderp = p; - cpm.websockettype = 1; + cpm.wstype = 1; return 0; } static int LuaReadWS(lua_State *L) { ssize_t rc; - size_t i, got, amt; - unsigned char wshdr[10], wshdrlen, *extlen, *mask; + size_t i, got, amt, bufsize; + unsigned char wshdr[10], wshdrlen, *extlen, *mask, op; + char *bufstart; uint64_t len; struct iovec iov[2]; OnlyCallDuringRequest(L, "ReadWS"); @@ -5118,12 +5121,19 @@ static int LuaReadWS(lua_State *L) { luaL_error(L, "Could not read WS header"); } while ((got += rc) < 2); + op = wshdr[0] & 0xF; + if (wshdr[0] & 0x70) goto close; // reserved bit set if (!(wshdr[1] | (1 << 7))) goto close; // unmasked if ((wshdr[0] & 0x7) >= 0x3) goto close; // reserved opcode + if (!wsfragtype && !op) goto close; // not in continuation len = wshdr[1] & ~(1 << 7); - if (wshdr[0] & 0x8 && len >= 126) goto close; // long control frame + if (wshdr[0] & 0x8) { // control frame + if (!(wshdr[0] & 0x80) || len >= 126) goto close; // fragmented or too long + } else { + if (op && wsfragtype) goto close; // during fragmented seq + } wshdrlen = 6; if (len == 126) { @@ -5146,29 +5156,54 @@ static int LuaReadWS(lua_State *L) { len = be64toh(*(uint64_t *)extlen); } - if (len >= inbuf.n - amtread) + if (len >= inbuf.n - wsfragread) luaL_error(L, "Required %d bytes to read WS frame, %d bytes available", len, - inbuf.n - amtread); + inbuf.n - wsfragread); - for (got = 0, amt = amtread; got < len; got += rc, amt += rc) { + for (got = 0, amt = wsfragread; got < len; got += rc, amt += rc) { if ((rc = reader(client, inbuf.p + amt, len - got)) == -1) luaL_error(L, "Could not read WS data"); } - for (i = 0, amt = amtread; i < got; ++i, ++amt) inbuf.p[amt] ^= mask[i & 0x3]; + for (i = 0, amt = wsfragread; i < got; ++i, ++amt) + inbuf.p[amt] ^= mask[i & 0x3]; - if ((wshdr[0] & 0xF) == 0x9) { + if (op == 0x9) { wshdr[0] = (wshdr[0] & ~0xF) | 0xA; wshdr[1] = wshdr[1] & ~0x80; iov[0].iov_base = wshdr; iov[0].iov_len = wshdrlen - 4; - iov[1].iov_base = inbuf.p + amtread; + iov[1].iov_base = inbuf.p + wsfragread; iov[1].iov_len = got; Send(iov, 2); } - lua_pushlstring(L, inbuf.p + amtread, got); - lua_pushnumber(L, wshdr[0]); + if (wshdr[0] & 0x80) { + if (op) { + bufstart = inbuf.p + wsfragread; + bufsize = got; + + if (op == 0x1 && !isutf8(bufstart, bufsize)) goto close; + lua_pushlstring(L, bufstart, bufsize); + lua_pushnumber(L, wshdr[0]); + } else { + bufstart = inbuf.p + amtread; + bufsize = (wsfragread - amtread) + got; + + if (wsfragtype == 0x1 && !isutf8(bufstart, bufsize)) goto close; + lua_pushlstring(L, bufstart, bufsize); + lua_pushnumber(L, wsfragtype); + + wsfragread = amtread; + wsfragtype = 0; + } + } else { + lua_pushnil(L); + lua_pushnumber(L, 0); + + if (!wsfragtype) wsfragtype = op; + wsfragread += got; + } return 2; @@ -5182,9 +5217,9 @@ static int LuaSetWSFlags(lua_State *L) { OnlyCallDuringRequest(L, "SetWSFlags"); char flags = lround(lua_tonumber(L, 1)); if (flags & 0x01) { - cpm.websockettype = 1; + cpm.wstype = 1; } else if (flags & 0x02) { - cpm.websockettype = 2; + cpm.wstype = 2; } return 0; } @@ -6492,6 +6527,8 @@ static bool StreamWS(char *p) { iov[0].iov_base = wshdr; extlen = &wshdr[2]; + wsfragread = amtread; + wsfragtype = 0; for (;;) { if (!YL || lua_status(YL) != LUA_YIELD) break; // done yielding @@ -6525,7 +6562,7 @@ static bool StreamWS(char *p) { *(uint64_t *)extlen = htobe64(rc); iov[0].iov_len = 10; } - wshdr[0] = cpm.websockettype | (1 << 7); + wshdr[0] = cpm.wstype | (1 << 7); if (Send(iov, 2) == -1) break; } @@ -6596,7 +6633,7 @@ static bool HandleMessageActual(void) { } if (!cpm.generator) { return TransmitResponse(p); - } else if (cpm.websockettype) { + } else if (cpm.wstype) { return StreamWS(p); } else { return StreamResponse(p); From a39caefc8b621e1466d82e48bc686738aa239476 Mon Sep 17 00:00:00 2001 From: wingdeans <66850754+wingdeans@users.noreply.github.com> Date: Thu, 28 Dec 2023 15:41:29 -0500 Subject: [PATCH 6/8] isutf8: implement RFC 3629 reject surrogate pairs (U+D800 to U+DFFF) reject greater than U+10FFFF --- libc/str/isutf8.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/libc/str/isutf8.c b/libc/str/isutf8.c index 6c9a6334eaa..9dae24c6221 100644 --- a/libc/str/isutf8.c +++ b/libc/str/isutf8.c @@ -27,8 +27,8 @@ static const char kUtf8Dispatch[] = { 1, 1, 1, 1, 1, 1, 1, 1, // 0320 1, 1, 1, 1, 1, 1, 1, 1, // 0330 2, 3, 3, 3, 3, 3, 3, 3, // 0340 utf8-3 - 3, 3, 3, 3, 3, 3, 3, 3, // 0350 - 4, 5, 5, 5, 5, 0, 0, 0, // 0360 utf8-4 + 3, 3, 3, 3, 3, 4, 3, 3, // 0350 + 5, 6, 6, 6, 7, 0, 0, 0, // 0360 utf8-4 0, 0, 0, 0, 0, 0, 0, 0, // 0370 }; @@ -94,6 +94,7 @@ bool32 isutf8(const void *data, size_t size) { } // fallthrough case 3: + case3: if (p + 2 <= e && // (p[0] & 0300) == 0200 && // (p[1] & 0300) == 0200) { // @@ -103,11 +104,17 @@ bool32 isutf8(const void *data, size_t size) { return false; // missing cont } case 4: + if (p < e && (*p & 040)) { + return false; // utf-16 surrogate + } + goto case3; + case 5: if (p < e && (*p & 0377) < 0220) { return false; // overlong } // fallthrough - case 5: + case 6: + case6: if (p + 3 <= e && // (((uint32_t)(p[+2] & 0377) << 030 | // (uint32_t)(p[+1] & 0377) << 020 | // @@ -119,6 +126,11 @@ bool32 isutf8(const void *data, size_t size) { } else { return false; // missing cont } + case 7: + if (p < e && (*p & 0x3F) > 0xF) { + return false; // over limit + } + goto case6; default: __builtin_unreachable(); } From e759b8e38104f70779289621b80a364b30bee3aa Mon Sep 17 00:00:00 2001 From: wingdeans <66850754+wingdeans@users.noreply.github.com> Date: Thu, 28 Dec 2023 18:39:19 -0500 Subject: [PATCH 7/8] Add Sec-WebSocket-Key HTTP header --- net/http/gethttpheader.gperf | 1 + net/http/gethttpheader.inc | 7 +++++-- net/http/gethttpheadername.c | 2 ++ net/http/http.h | 3 ++- tool/net/redbean.c | 25 ++++++++----------------- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/net/http/gethttpheader.gperf b/net/http/gethttpheader.gperf index 26c3c2dd013..fad88e6bb15 100644 --- a/net/http/gethttpheader.gperf +++ b/net/http/gethttpheader.gperf @@ -104,3 +104,4 @@ CF-Visitor, kHttpCfVisitor CF-Connecting-IP, kHttpCfConnectingIp CF-IPCountry, kHttpCfIpcountry CDN-Loop, kHttpCdnLoop +Sec-WebSocket-Key, kHttpWebsocketKey diff --git a/net/http/gethttpheader.inc b/net/http/gethttpheader.inc index 72f3b7afe60..ae5a682a03d 100644 --- a/net/http/gethttpheader.inc +++ b/net/http/gethttpheader.inc @@ -39,7 +39,7 @@ #line 12 "gethttpheader.gperf" struct thatispacked HttpHeaderSlot { char *name; char code; }; -#define TOTAL_KEYWORDS 93 +#define TOTAL_KEYWORDS 94 #define MIN_WORD_LENGTH 2 #define MAX_WORD_LENGTH 32 #define MIN_HASH_VALUE 3 @@ -387,7 +387,10 @@ LookupHttpHeader (register const char *str, register size_t len) #line 87 "gethttpheader.gperf" {"Strict-Transport-Security", kHttpStrictTransportSecurity}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, - {""}, {""}, {""}, {""}, {""}, + {""}, {""}, +#line 107 "gethttpheader.gperf" + {"Sec-WebSocket-Key", kHttpWebsocketKey}, + {""}, {""}, #line 22 "gethttpheader.gperf" {"X-Forwarded-For", kHttpXForwardedFor}, {""}, diff --git a/net/http/gethttpheadername.c b/net/http/gethttpheadername.c index 898cf327a8d..b01f68e1fc7 100644 --- a/net/http/gethttpheadername.c +++ b/net/http/gethttpheadername.c @@ -206,6 +206,8 @@ const char *GetHttpHeaderName(int h) { return "CDN-Loop"; case kHttpSecChUaPlatform: return "Sec-CH-UA-Platform"; + case kHttpWebsocketKey: + return "Sec-WebSocket-Key"; default: return NULL; } diff --git a/net/http/http.h b/net/http/http.h index 5e70c0370fa..ee196399942 100644 --- a/net/http/http.h +++ b/net/http/http.h @@ -146,7 +146,8 @@ #define kHttpCfIpcountry 90 #define kHttpSecChUaPlatform 91 #define kHttpCdnLoop 92 -#define kHttpHeadersMax 93 +#define kHttpWebsocketKey 93 +#define kHttpHeadersMax 94 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ diff --git a/tool/net/redbean.c b/tool/net/redbean.c index dea0a1ee6d3..8cf8d221335 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -5066,23 +5066,14 @@ static int LuaUpgradeWS(lua_State *L) { unsigned char hash[20]; OnlyCallDuringRequest(L, "UpgradeWS"); - haskey = true; - for (i = 0; i < cpm.msg.xheaders.n; ++i) { - if (SlicesEqualCase( - "Sec-WebSocket-Key", strlen("Sec-WebSocket-Key"), - inbuf.p + cpm.msg.xheaders.p[i].k.a, - cpm.msg.xheaders.p[i].k.b - cpm.msg.xheaders.p[i].k.a)) { - mbedtls_sha1_init(&ctx); - mbedtls_sha1_starts_ret(&ctx); - mbedtls_sha1_update_ret( - &ctx, (unsigned char *)inbuf.p + cpm.msg.xheaders.p[i].v.a, - cpm.msg.xheaders.p[i].v.b - cpm.msg.xheaders.p[i].v.a); - haskey = true; - break; - } - } - - if (!haskey) luaL_error(L, "No Sec-WebSocket-Key header"); + if (!HasHeader(kHttpWebsocketKey)) + luaL_error(L, "No Sec-WebSocket-Key header"); + + mbedtls_sha1_init(&ctx); + mbedtls_sha1_starts_ret(&ctx); + mbedtls_sha1_update_ret(&ctx, (unsigned char*) + HeaderData(kHttpWebsocketKey), + HeaderLength(kHttpWebsocketKey)); p = SetStatus(101, "Switching Protocols"); while (p - hdrbuf.p + (20 + 21 + (20 + 28 + 4)) + 512 > hdrbuf.n) { From 20d28894f8409ba9fab4c715e501f16c73be19e5 Mon Sep 17 00:00:00 2001 From: wingdeans <66850754+wingdeans@users.noreply.github.com> Date: Thu, 28 Dec 2023 23:28:34 -0500 Subject: [PATCH 8/8] Implement new API --- tool/net/redbean.c | 66 +++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 8cf8d221335..22ea79c998b 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -5058,13 +5058,15 @@ static bool LuaRunAsset(const char *path, bool mandatory) { return !!a; } -static int LuaUpgradeWS(lua_State *L) { +static int LuaWSUpgrade(lua_State *L) { size_t i; char *p, *q; bool haskey; mbedtls_sha1_context ctx; unsigned char hash[20]; - OnlyCallDuringRequest(L, "UpgradeWS"); + + if (cpm.generator) + luaL_error(L, "Cannot upgrade to websocket after yielding normally"); if (!HasHeader(kHttpWebsocketKey)) luaL_error(L, "No Sec-WebSocket-Key header"); @@ -5094,17 +5096,18 @@ static int LuaUpgradeWS(lua_State *L) { cpm.luaheaderp = p; cpm.wstype = 1; + return 0; } -static int LuaReadWS(lua_State *L) { +static int LuaWSRead(lua_State *L) { ssize_t rc; size_t i, got, amt, bufsize; unsigned char wshdr[10], wshdrlen, *extlen, *mask, op; char *bufstart; uint64_t len; struct iovec iov[2]; - OnlyCallDuringRequest(L, "ReadWS"); + OnlyCallDuringRequest(L, "ws.Read"); got = 0; do { @@ -5176,21 +5179,21 @@ static int LuaReadWS(lua_State *L) { if (op == 0x1 && !isutf8(bufstart, bufsize)) goto close; lua_pushlstring(L, bufstart, bufsize); - lua_pushnumber(L, wshdr[0]); + lua_pushinteger(L, op); } else { bufstart = inbuf.p + amtread; bufsize = (wsfragread - amtread) + got; if (wsfragtype == 0x1 && !isutf8(bufstart, bufsize)) goto close; lua_pushlstring(L, bufstart, bufsize); - lua_pushnumber(L, wsfragtype); + lua_pushinteger(L, wsfragtype); wsfragread = amtread; wsfragtype = 0; } } else { lua_pushnil(L); - lua_pushnumber(L, 0); + lua_pushinteger(L, 0); if (!wsfragtype) wsfragtype = op; wsfragread += got; @@ -5200,21 +5203,50 @@ static int LuaReadWS(lua_State *L) { close: lua_pushnil(L); - lua_pushnumber(L, 0x08); + lua_pushinteger(L, 0x08); return 2; } -static int LuaSetWSFlags(lua_State *L) { - OnlyCallDuringRequest(L, "SetWSFlags"); - char flags = lround(lua_tonumber(L, 1)); - if (flags & 0x01) { - cpm.wstype = 1; - } else if (flags & 0x02) { - cpm.wstype = 2; +static int LuaWSWrite(lua_State *L) { + int type; + size_t size; + const char *data; + + OnlyCallDuringRequest(L, "ws.Write"); + if (!cpm.wstype) + LuaWSUpgrade(L); + + type = luaL_optinteger(L, 2, -1); + if (type == 1 || type == 2) { + cpm.wstype = type; + } else if (type != -1) { + luaL_error(L, "Invalid WS type"); + } + + if (!lua_isnil(L, 1)) { + data = luaL_checklstring(L, 1, &size); + appendd(&cpm.outbuf, data, size); } return 0; } +static const luaL_Reg kLuaWS[] = { + {"Read", LuaWSRead}, // + {"Write", LuaWSWrite}, // + {0} // +}; + +int LuaWS(lua_State *L) { + luaL_newlib(L, kLuaWS); + lua_pushinteger(L, 0); lua_setfield(L, -2, "CONT"); + lua_pushinteger(L, 1); lua_setfield(L, -2, "TEXT"); + lua_pushinteger(L, 2); lua_setfield(L, -2, "BIN"); + lua_pushinteger(L, 8); lua_setfield(L, -2, "CLOSE"); + lua_pushinteger(L, 9); lua_setfield(L, -2, "PING"); + lua_pushinteger(L, 10); lua_setfield(L, -2, "PONG"); + return 1; +} + // // list of functions that can't be run from the repl static const char *const kDontAutoComplete[] = { @@ -5414,7 +5446,6 @@ static const luaL_Reg kLuaFuncs[] = { {"ProgramUid", LuaProgramUid}, // {"ProgramUniprocess", LuaProgramUniprocess}, // {"Rand64", LuaRand64}, // - {"ReadWS", LuaReadWS}, // undocumented {"Rdrand", LuaRdrand}, // {"Rdseed", LuaRdseed}, // {"Rdtsc", LuaRdtsc}, // @@ -5432,7 +5463,6 @@ static const luaL_Reg kLuaFuncs[] = { {"SetHeader", LuaSetHeader}, // {"SetLogLevel", LuaSetLogLevel}, // {"SetStatus", LuaSetStatus}, // - {"SetWSFlags", LuaSetWSFlags}, // undocumented {"Sha1", LuaSha1}, // {"Sha224", LuaSha224}, // {"Sha256", LuaSha256}, // @@ -5443,7 +5473,6 @@ static const luaL_Reg kLuaFuncs[] = { {"StoreAsset", LuaStoreAsset}, // {"Uncompress", LuaUncompress}, // {"Underlong", LuaUnderlong}, // - {"UpgradeWS", LuaUpgradeWS}, // undocumented {"VisualizeControlCodes", LuaVisualizeControlCodes}, // {"Write", LuaWrite}, // {"bin", LuaBin}, // @@ -5482,6 +5511,7 @@ static const luaL_Reg kLuaLibs[] = { {"path", LuaPath}, // {"re", LuaRe}, // {"unix", LuaUnix}, // + {"ws", LuaWS} // }; static void LuaSetArgv(lua_State *L) {