From 972df7003b2500ad6620f06e0beb12bde4bd0791 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Sat, 3 May 2025 15:15:21 -0500 Subject: [PATCH] add session tools, password hashing, adjust create_table --- core/http/server.go | 11 +++- core/runner/password.go | 113 ++++++++++++++++++++++++++++++++++++++++ core/runner/sandbox.go | 4 ++ core/runner/sandbox.lua | 80 ++++++++++++++++++++++++++++ core/runner/sqlite.go | 22 +++++++- core/runner/sqlite.lua | 13 ++--- go.mod | 2 + go.sum | 42 +++++++++++++++ init.lua | 5 ++ luajit | 2 +- 10 files changed, 282 insertions(+), 12 deletions(-) create mode 100644 core/runner/password.go create mode 100644 init.lua diff --git a/core/http/server.go b/core/http/server.go index d2e5d55..f90002a 100644 --- a/core/http/server.go +++ b/core/http/server.go @@ -195,8 +195,17 @@ func (s *Server) handleLuaRoute(ctx *fasthttp.RequestCtx, bytecode []byte, scrip return } + if _, clearAll := response.SessionData["__clear_all"]; clearAll { + session.Clear() + delete(response.SessionData, "__clear_all") + } + for k, v := range response.SessionData { - session.Set(k, v) + if v == "__SESSION_DELETE_MARKER__" { + session.Delete(k) + } else { + session.Set(k, v) + } } s.sessionManager.ApplySessionCookie(ctx, session) diff --git a/core/runner/password.go b/core/runner/password.go new file mode 100644 index 0000000..754d32a --- /dev/null +++ b/core/runner/password.go @@ -0,0 +1,113 @@ +package runner + +import ( + "fmt" + + luajit "git.sharkk.net/Sky/LuaJIT-to-Go" + "github.com/alexedwards/argon2id" +) + +// RegisterPasswordFunctions registers password-related functions in the Lua state +func RegisterPasswordFunctions(state *luajit.State) error { + if err := state.RegisterGoFunction("__password_hash", passwordHash); err != nil { + return err + } + if err := state.RegisterGoFunction("__password_verify", passwordVerify); err != nil { + return err + } + return nil +} + +// passwordHash implements the Argon2id password hashing using alexedwards/argon2id +func passwordHash(state *luajit.State) int { + if !state.IsString(1) { + state.PushString("password_hash error: expected string password") + return 1 + } + + password := state.ToString(1) + + params := &argon2id.Params{ + Memory: 64 * 1024, + Iterations: 3, + Parallelism: 4, + SaltLength: 16, + KeyLength: 32, + } + + if state.IsTable(2) { + state.GetField(2, "memory") + if state.IsNumber(-1) { + params.Memory = uint32(state.ToNumber(-1)) + if params.Memory < 8*1024 { + params.Memory = 8 * 1024 // Minimum 8MB + } + } + state.Pop(1) + + state.GetField(2, "iterations") + if state.IsNumber(-1) { + params.Iterations = uint32(state.ToNumber(-1)) + if params.Iterations < 1 { + params.Iterations = 1 // Minimum 1 iteration + } + } + state.Pop(1) + + state.GetField(2, "parallelism") + if state.IsNumber(-1) { + params.Parallelism = uint8(state.ToNumber(-1)) + if params.Parallelism < 1 { + params.Parallelism = 1 // Minimum 1 thread + } + } + state.Pop(1) + + state.GetField(2, "salt_length") + if state.IsNumber(-1) { + params.SaltLength = uint32(state.ToNumber(-1)) + if params.SaltLength < 8 { + params.SaltLength = 8 // Minimum 8 bytes + } + } + state.Pop(1) + + state.GetField(2, "key_length") + if state.IsNumber(-1) { + params.KeyLength = uint32(state.ToNumber(-1)) + if params.KeyLength < 16 { + params.KeyLength = 16 // Minimum 16 bytes + } + } + state.Pop(1) + } + + hash, err := argon2id.CreateHash(password, params) + if err != nil { + state.PushString(fmt.Sprintf("password_hash error: %v", err)) + return 1 + } + + state.PushString(hash) + return 1 +} + +// passwordVerify verifies a password against a hash +func passwordVerify(state *luajit.State) int { + if !state.IsString(1) || !state.IsString(2) { + state.PushBoolean(false) + return 1 + } + + password := state.ToString(1) + hash := state.ToString(2) + + match, err := argon2id.ComparePasswordAndHash(password, hash) + if err != nil { + state.PushBoolean(false) + return 1 + } + + state.PushBoolean(match) + return 1 +} diff --git a/core/runner/sandbox.go b/core/runner/sandbox.go index 0ed0814..341bd98 100644 --- a/core/runner/sandbox.go +++ b/core/runner/sandbox.go @@ -109,6 +109,10 @@ func (s *Sandbox) registerCoreFunctions(state *luajit.State) error { return err } + if err := RegisterPasswordFunctions(state); err != nil { + return err + } + return nil } diff --git a/core/runner/sandbox.lua b/core/runner/sandbox.lua index 4103f61..c0cdb75 100644 --- a/core/runner/sandbox.lua +++ b/core/runner/sandbox.lua @@ -181,6 +181,22 @@ http.client.post = make_method("POST", true) http.client.put = make_method("PUT", true) http.client.patch = make_method("PATCH", true) +http.redirect = function(url, status) + if type(url) ~= "string" then + error("http.redirect: url must be a string", 2) + end + + status = status or 302 -- Default to temporary redirect + + local resp = __ensure_response() + resp.status = status + + resp.headers = resp.headers or {} + resp.headers["Location"] = url + + exit() +end + -- ====================================================================== -- COOKIE MODULE -- ====================================================================== @@ -321,6 +337,35 @@ local session = { end return nil + end, + + delete = function(key) + if type(key) ~= "string" then + error("session.delete: key must be a string", 2) + end + + local resp = __ensure_response() + resp.session = resp.session or {} + resp.session[key] = "__SESSION_DELETE_MARKER__" + + local env = getfenv(2) + if env.ctx and env.ctx.session and env.ctx.session.data then + env.ctx.session.data[key] = nil + end + end, + + clear = function() + local env = getfenv(2) + if env.ctx and env.ctx.session and env.ctx.session.data then + for k, _ in pairs(env.ctx.session.data) do + env.ctx.session.data[k] = nil + end + end + + local resp = __ensure_response() + resp.session = {} + + resp.session["__clear_all"] = true end } @@ -532,6 +577,40 @@ _G.render = function(template_str, env) return table.concat(output_buffer) end +-- ====================================================================== +-- PASSWORD MODULE +-- ====================================================================== + +local password = {} + +-- Hash a password using Argon2id +-- Options: +-- memory: Amount of memory to use in KB (default: 64MB) +-- iterations: Number of iterations (default: 3) +-- parallelism: Number of threads (default: 4) +-- salt_length: Length of salt in bytes (default: 16) +-- key_length: Length of the derived key in bytes (default: 32) +function password.hash(plain_password, options) + if type(plain_password) ~= "string" then + error("password.hash: expected string password", 2) + end + + return __password_hash(plain_password, options) +end + +-- Verify a password against a hash +function password.verify(plain_password, hash_string) + if type(plain_password) ~= "string" then + error("password.verify: expected string password", 2) + end + + if type(hash_string) ~= "string" then + error("password.verify: expected string hash", 2) + end + + return __password_verify(plain_password, hash_string) +end + -- ====================================================================== -- REGISTER MODULES GLOBALLY -- ====================================================================== @@ -541,3 +620,4 @@ _G.session = session _G.csrf = csrf _G.cookie = cookie _G.util = util +_G.password = password diff --git a/core/runner/sqlite.go b/core/runner/sqlite.go index 0f7fc6d..17fa80c 100644 --- a/core/runner/sqlite.go +++ b/core/runner/sqlite.go @@ -253,7 +253,7 @@ func luaSQLQuery(state *luajit.State) int { var rows []map[string]any err = sqlitex.Execute(conn, query, &sqlitex.ExecOptions{ - Named: params, // Using Named for named parameters + Named: prepareNamedParams(params), ResultFunc: func(stmt *sqlite.Stmt) error { row := make(map[string]any) columnCount := stmt.ColumnCount() @@ -363,7 +363,7 @@ func luaSQLExec(state *luajit.State) int { // Execute statement if params != nil { err = sqlitex.Execute(conn, query, &sqlitex.ExecOptions{ - Named: params, // Using Named for named parameters + Named: prepareNamedParams(params), }) } else { err = sqlitex.ExecScript(conn, query) @@ -391,3 +391,21 @@ func RegisterSQLiteFunctions(state *luajit.State) error { return nil } + +func prepareNamedParams(params map[string]any) map[string]any { + if params == nil { + return nil + } + + modified := make(map[string]any, len(params)) + + for key, value := range params { + if len(key) > 0 && key[0] != ':' { + modified[":"+key] = value + } else { + modified[key] = value + } + } + + return modified +} diff --git a/core/runner/sqlite.lua b/core/runner/sqlite.lua index a0f08b1..370bd67 100644 --- a/core/runner/sqlite.lua +++ b/core/runner/sqlite.lua @@ -20,14 +20,11 @@ local connection_mt = { end, -- Create a new table - create_table = function(self, table_name, schema) - if type(schema) ~= "table" then - error("connection:create_table: schema must be a table", 2) - end + create_table = function(self, table_name, ...) + local columns = {...} - local columns = {} - for name, definition in pairs(schema) do - table.insert(columns, name .. " " .. definition) + if #columns == 0 then + error("connection:create_table: no columns specified", 2) end local query = string.format("CREATE TABLE IF NOT EXISTS %s (%s)", @@ -51,7 +48,7 @@ local connection_mt = { for col, val in pairs(data) do table.insert(columns, col) table.insert(placeholders, ":" .. col) - params[col] = val + params[":" .. col] = val end local query = string.format( diff --git a/go.mod b/go.mod index 2b13fe5..fbb22e7 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.24.1 require ( git.sharkk.net/Sky/LuaJIT-to-Go v0.1.2 github.com/VictoriaMetrics/fastcache v1.12.2 + github.com/alexedwards/argon2id v1.0.0 github.com/deneonet/benc v1.1.7 github.com/goccy/go-json v0.10.5 github.com/matoous/go-nanoid/v2 v2.1.0 @@ -23,6 +24,7 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/crypto v0.37.0 // indirect golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect golang.org/x/sys v0.32.0 // indirect modernc.org/libc v1.65.0 // indirect diff --git a/go.sum b/go.sum index 01b1ecc..d6deb60 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI= github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkThDcMsQicp4xDukwJYI= +github.com/alexedwards/argon2id v1.0.0 h1:wJzDx66hqWX7siL/SRUmgz3F8YMrd/nfX/xHHcQQP0w= +github.com/alexedwards/argon2id v1.0.0/go.mod h1:tYKkqIjzXvZdzPvADMWOEZ+l6+BD6CtBXMj5fnJppiw= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= @@ -45,20 +47,60 @@ github.com/valyala/fasthttp v1.61.0 h1:VV08V0AfoRaFurP1EWKvQQdPTZHiUzaVoulX1aBDg github.com/valyala/fasthttp v1.61.0/go.mod h1:wRIV/4cMwUPWnRcDno9hGnYZGh78QzODFfo1LTUhBog= github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE= +golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc= golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM= golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0/go.mod h1:S9Xr4PYopiDyqSyp5NjCrhFrqg6A5zA2E/iPHPhqnS8= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610= golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20= golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0= golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.32.0 h1:Q7N1vhpkQv7ybVzLFtTjvQya2ewbwNDZzUgfXGqtMWU= golang.org/x/tools v0.32.0/go.mod h1:ZxrU41P/wAbZD8EDa6dDCa6XfpkhJ7HFMjHJXfBDu8s= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= modernc.org/cc/v4 v4.26.0 h1:QMYvbVduUGH0rrO+5mqF/PSPPRZNpRtg2CLELy7vUpA= diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..d3adca8 --- /dev/null +++ b/init.lua @@ -0,0 +1,5 @@ +sqlite("test"):create_table("users", + "id INTEGER PRIMARY KEY AUTOINCREMENT", + "username TEXT NOT NULL UNIQUE", + "password TEXT" +) diff --git a/luajit b/luajit index 6b9e2a0..231ffb0 160000 --- a/luajit +++ b/luajit @@ -1 +1 @@ -Subproject commit 6b9e2a0e201bdd34fba0972441434354aa5c67c5 +Subproject commit 231ffb0d297f47be485b1fe7549bef0cdd48d8d0