Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions ctor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
8 changes: 7 additions & 1 deletion module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading