From 9bc5f7e2856b4442d7f681953b4b6b7c3f64f85f Mon Sep 17 00:00:00 2001 From: Gianluca Mardente Date: Sat, 21 Nov 2026 18:29:10 +0100 Subject: [PATCH] fix: run user-supplied Lua in a sandboxed state Lua scripts coming from Sveltos CRs ran in a state with the io and os libraries fully open, so a script could read files or environment variables of the pod. Use libsveltos NewSandboxedState, which only keeps the base, package, table, string, math and coroutine libraries and the time related functions of os (time, date, clock, difftime). Add tests running scripts that call io.open and os.getenv through the real code paths and expect them to fail. --- controllers/mgmtcluster_classifier_test.go | 58 +++++++++++++++++++-- controllers/mgmtcluster_classifier_utils.go | 4 +- go.mod | 2 +- go.sum | 2 + 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/controllers/mgmtcluster_classifier_test.go b/controllers/mgmtcluster_classifier_test.go index d7a146a..3df32f9 100644 --- a/controllers/mgmtcluster_classifier_test.go +++ b/controllers/mgmtcluster_classifier_test.go @@ -38,9 +38,10 @@ import ( ) const ( - testLabelEnv = "env" - testLabelApp = "app" - testValueProd = "prod" + testLabelEnv = "env" + testLabelApp = "app" + testValueProd = "prod" + testKindConfigMap = "ConfigMap" ) var _ = Describe("ManagementClusterClassifier utils", func() { @@ -115,7 +116,7 @@ var _ = Describe("ManagementClusterClassifier utils", func() { rs := &libsveltosv1beta1.ResourceSelector{ Group: "", Version: corev1.SchemeGroupVersion.Version, - Kind: "ConfigMap", + Kind: testKindConfigMap, } resources, err := controllers.FetchResourcesForSelector(context.TODO(), fakeClient, rs, logger) @@ -130,6 +131,53 @@ var _ = Describe("ManagementClusterClassifier utils", func() { }) }) + Context("Lua sandbox", func() { + const readHostFileScript = ` +function evaluate(resources) + local f = io.open("/etc/hosts", "r") + return {matching=(f ~= nil), message=""} +end +` + + It("fetchResourcesForSelector does not let a per-resource script read host files", func() { + u := buildUnstructuredConfigMap("cm", map[string]string{testLabelEnv: testValueProd}) + fakeClient := fake.NewClientBuilder().WithScheme(mgmtTestScheme()).WithObjects(u).Build() + + rs := &libsveltosv1beta1.ResourceSelector{ + Version: corev1.SchemeGroupVersion.Version, + Kind: testKindConfigMap, + Evaluate: readHostFileScript, + } + + // The script fails for every resource, so none is a match. + resources, err := controllers.FetchResourcesForSelector(context.TODO(), fakeClient, rs, testEnv.GetLogger()) + Expect(err).ToNot(HaveOccurred()) + Expect(resources).To(BeEmpty()) + }) + + It("runClassificationLua does not let classificationLua read host files", func() { + script := ` +function evaluate(resources) + local f = io.open("/etc/hosts", "r") + return {} +end +` + _, err := controllers.RunClassificationLua(script, nil, testEnv.GetLogger()) + Expect(err).To(HaveOccurred()) + }) + + It("runClassificationLua does not let classificationLua read the environment", func() { + script := ` +function evaluate(resources) + local home = os.getenv("HOME") + return {} +end +` + _, err := controllers.RunClassificationLua(script, nil, testEnv.GetLogger()) + Expect(err).To(HaveOccurred()) + }) + }) + Context("runClassificationLua", func() { It("returns no clusters for empty resource list", func() { script := ` @@ -357,7 +405,7 @@ func buildUnstructuredConfigMap(name string, lbls map[string]string) *unstructur obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cm) Expect(err).ToNot(HaveOccurred()) u := &unstructured.Unstructured{Object: obj} - u.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap")) + u.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind(testKindConfigMap)) return u } diff --git a/controllers/mgmtcluster_classifier_utils.go b/controllers/mgmtcluster_classifier_utils.go index 1a8f62d..a1dce50 100644 --- a/controllers/mgmtcluster_classifier_utils.go +++ b/controllers/mgmtcluster_classifier_utils.go @@ -84,7 +84,7 @@ func fetchResourcesForSelector(ctx context.Context, c client.Client, // Compile per-resource Lua once so we don't re-parse it for every resource. var luaState *lua.LState if rs.Evaluate != "" { - luaState = lua.NewState() + luaState = sveltoslua.NewSandboxedState() sveltoslua.LoadModulesAndRegisterMethods(luaState) if err := luaState.DoString(rs.Evaluate); err != nil { luaState.Close() @@ -217,7 +217,7 @@ func isMatchForEvaluateScript(u *unstructured.Unstructured, l *lua.LState, logge func runClassificationLua(luaScript string, resources []*unstructured.Unstructured, logger logr.Logger) ([]clusterRef, error) { - l := lua.NewState() + l := sveltoslua.NewSandboxedState() defer l.Close() sveltoslua.LoadModulesAndRegisterMethods(l) diff --git a/go.mod b/go.mod index a2971f6..8d7b80b 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/onsi/ginkgo/v2 v2.34.0 github.com/onsi/gomega v1.44.0 github.com/pkg/errors v0.9.1 - github.com/projectsveltos/libsveltos v1.17.1-0.20261117192944-bd11a04a1e5f + github.com/projectsveltos/libsveltos v1.17.1-0.20261121162648-4c6efa5c8263 github.com/prometheus/client_golang v1.25.0 github.com/spf13/pflag v1.1.0 github.com/yuin/gopher-lua v1.1.2 diff --git a/go.sum b/go.sum index 1175364..04b57a2 100644 --- a/go.sum +++ b/go.sum @@ -210,6 +210,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/projectsveltos/libsveltos v1.17.1-0.20261117192944-bd11a04a1e5f h1:1Sly6XRWugk2dOwkQHgut9Aoahr97AoM3VnL3xlHW2o= github.com/projectsveltos/libsveltos v1.17.1-0.20261117192944-bd11a04a1e5f/go.mod h1:dHJrl1V5/WrNda5wzxbHs2dlsvoV/FUlwK1TXy2X3pA= +github.com/projectsveltos/libsveltos v1.17.1-0.20261121162648-4c6efa5c8263 h1:E2/jlu5M6aQIjsimBdajj/+qD6mlZw1N7dCR9Iuj++s= +github.com/projectsveltos/libsveltos v1.17.1-0.20261121162648-4c6efa5c8263/go.mod h1:dHJrl1V5/WrNda5wzxbHs2dlsvoV/FUlwK1TXy2X3pA= github.com/projectsveltos/lua-utils/glua-json v0.0.0-20251212200258-2b3cdcb7c0f5 h1:khnc+994UszxZYu69J+R5FKiLA/Nk1JQj0EYAkwTWz0= github.com/projectsveltos/lua-utils/glua-json v0.0.0-20251212200258-2b3cdcb7c0f5/go.mod h1:yVL8KQFa9tmcxgwl9nwIMtKgtmIVC1zaFRSCfOwYvPY= github.com/projectsveltos/lua-utils/glua-runes v0.0.0-20251212200258-2b3cdcb7c0f5 h1:YbsebwRwTRhV8QacvEAdFqxcxHdeu7JTVtsBovbkgos=