Make sure Lua values are actually tables before accessing them (fix #44)

This commit is contained in:
Noah 2024-04-16 10:00:08 -05:00
parent 698d472ca1
commit 476ac5a175
2 changed files with 26 additions and 12 deletions

View File

@ -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
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)
}

22
site.go
View File

@ -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
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")) // 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)})
}
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)
} 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!"})
}