diff --git a/ctor_test.go b/ctor_test.go index c26b0e4..f99a1c6 100644 --- a/ctor_test.go +++ b/ctor_test.go @@ -1399,3 +1399,58 @@ func TestModuleWithheldError(t *testing.T) { t.Error("undefined name wrongly classified as withheld") } } + +// TestStarlarkEvaluationModulePolicy keeps dynamic evaluation out of the +// default Safe/Network sets while checking explicit opt-in and Full use. +func TestStarlarkEvaluationModulePolicy(t *testing.T) { + if starlet.GetBuiltinModule("starlark") == nil { + t.Skip("the currently pinned Starlet does not register the starlark module") + } + for _, set := range []starbox.ModuleSetName{starbox.SafeModuleSet, starbox.NetworkModuleSet} { + t.Run(string(set)+"/withheld", func(t *testing.T) { + b := starbox.New("starlark-withheld") + b.SetPrintFunc(noopPrint) + b.SetModuleSet(set) + _, err := b.Run(`load("starlark", "eval")`) + var withheld starbox.ModuleWithheldError + if !errors.As(err, &withheld) { + t.Fatalf("want ModuleWithheldError, got %T: %v", err, err) + } + }) + } + for _, tc := range []struct { + name string + set starbox.ModuleSetName + add bool + }{ + {"full", starbox.FullModuleSet, false}, + {"safe-explicit", starbox.SafeModuleSet, true}, + } { + t.Run(tc.name, func(t *testing.T) { + b := starbox.New("starlark-allowed") + b.SetPrintFunc(noopPrint) + b.SetModuleSet(tc.set) + b.SetMaxExecutionSteps(5000) + if tc.add { + b.AddNamedModules("starlark") + } + out, err := b.Run(hereDoc(` + load("starlark", "eval", "try_eval") + def inspect(): + items = [1, 2] + found = eval("items.index(2)", vars={"items": items}) + value, error = try_eval("items.append(3)", vars={"items": items}) + items.append(4) + return (found, value == None, error != None, len(items)) + answer = inspect() + `)) + if err != nil { + t.Fatal(err) + } + want := []interface{}{int64(1), true, true, int64(3)} + if !reflect.DeepEqual(out["answer"], want) { + t.Fatalf("answer = %#v, want %#v", out["answer"], want) + } + }) + } +} diff --git a/module_test.go b/module_test.go index a54bb99..c19d8b5 100644 --- a/module_test.go +++ b/module_test.go @@ -75,10 +75,16 @@ func TestModuleSetsGolden(t *testing.T) { // 3. Fail-closed coverage: every starlet builtin module must be accounted // for by an explicit classification - Safe, the network extras, or the - // full-only filesystem/process modules. A new (or removed) starlet module + // full-only modules reviewed below. A new (or removed) starlet module // makes the union differ from the full set and trips this, forcing a // conscious decision instead of a silent change to Safe. fullOnly := []string{"file", "path", "runtime"} + // The data-only Starlark evaluator is deliberately full-only by default. + // Older supported Starlet versions do not register it. Recognize this one + // reviewed addition without admitting any other future module implicitly. + if starlet.GetBuiltinModule("starlark") != nil { + fullOnly = append(fullOnly, "starlark") + } classified := append(append(append([]string(nil), safeModuleNames...), networkExtraNames...), fullOnly...) if got, want := sorted(classified), sorted(fullModuleNames); !reflect.DeepEqual(got, want) { t.Errorf("module classification drifted from starlet's full set:\n classified: %v\n full: %v\n(classify the new/removed starlet module in module.go)", got, want)