From 476ac5a1757ef193e824bdf598a7cbe8fba8a2e5 Mon Sep 17 00:00:00 2001 From: Noah Date: Tue, 16 Apr 2024 10:00:08 -0500 Subject: [PATCH] Make sure Lua values are actually tables before accessing them (fix #44) --- config.go | 12 ++++++++++-- site.go | 26 ++++++++++++++++---------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index c611921..5af2d0b 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "errors" + "log" lua "github.com/yuin/gopher-lua" ) @@ -57,12 +58,16 @@ func getTableFromTable(L *lua.LState, lobj lua.LValue, key string, out *lua.LVal // parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config' func parseConfig(path string, config *Configuration) { + log.Printf("Loading config from : %s ...", path) // Lua state setup L := lua.NewState() defer L.Close() L.DoFile(path) - table := L.Get(-1) // Get the table returned by the config file + table := L.Get(-1) // Get the table returned by the config file + if table.Type().String() != "table" { // Make sure value returned is of type 'table' + log.Fatalf("config is of type : '%s', not 'table'", table.Type().String()) + } // Main config if _, err := getStringFromTable(L, table, "sites_dir", &config.sites_dir); err != nil { @@ -74,7 +79,10 @@ func parseConfig(path string, config *Configuration) { // Matrix config var matrix lua.LValue - getTableFromTable(L, table, "matrix", &matrix) + getTableFromTable(L, table, "matrix", &matrix) // Get the 'matrix' sub table + if matrix.Type().String() != "table" { // Make sure 'config.matrix' is of type 'table' + log.Fatalf("config.matrix is of type : '%s', not 'table'", matrix.Type().String()) + } if _, err := getStringFromTable(L, matrix, "homeserver", &config.matrix.homeserver); err != nil { panic(err) } diff --git a/site.go b/site.go index 92b3a4f..2dcdf7c 100644 --- a/site.go +++ b/site.go @@ -48,32 +48,38 @@ func getSite(name string) (Site, bool, error) { // getConfig parses a site's configuration file. func (site *Site) getConfig() (bool, error) { path := fmt.Sprintf("%s/%s/deploy_config.lua", configuration.sites_dir, site.name) + warning := fmt.Sprintf("%s%s : Site config warning : ", Circles["orange"], site.getName()) // Make sure config file exists if exists, _ := fileExists(path); !exists { return false, nil } + log.Printf("Loading per-site config %s", path) // Lua state setup L := lua.NewState() defer L.Close() - L.DoFile(path) // Load the config - table := L.Get(-1) // Get the table returned by the config file + L.DoFile(path) // Load the config + table := L.Get(-1) // Get the table returned by the config file + if table.Type().String() != "table" { // Make sure 'config' is of type 'table' + err := fmt.Sprintf("Value returned by config is of type '%s', not 'table'", table.Type().String()) + sendMessage(MatrixMessage{text: warning + err}) + return false, errors.New(err) + } // Parse the config config := SiteConfig{} - log.Printf("Loading per-site config %s", path) - warning := fmt.Sprintf("%s%s : Site config warning : ", Circles["orange"], site.getName()) - - if _, err := getStringFromTable(L, table, "name", &config.name); err != nil { // Name + if _, err := getStringFromTable(L, table, "name", &config.name); err != nil { // name sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)}) } - if _, err := getIntFromTable(L, table, "port", &config.port); err != nil { // Port + if _, err := getIntFromTable(L, table, "port", &config.port); err != nil { // port sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)}) } - raw_gitt := L.GetTable(table, lua.LString("git")) // Attempt to get 'git' table - if gitt, ok := raw_gitt.(*lua.LTable); ok { - getStringFromTable(L, gitt, "branch", &config.git.branch) + raw_gitt := L.GetTable(table, lua.LString("git")) // git + if gitt, ok := L.GetTable(table, lua.LString("git")).(*lua.LTable); ok { + if _, err := getStringFromTable(L, gitt, "branch", &config.git.branch); err != nil { // git.branch + sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)}) + } } else if _, ok := raw_gitt.(*lua.LNilType); !ok { // If 'git' is neither a table or nil (undefined), sendMessage(MatrixMessage{text: warning + "'git' is neither a table or nil!"}) }