package sandbox import ( "errors" "strings" "testing" ) func TestValidateEnvRefusesReservedNames(t *testing.T) { for _, k := range []string{"PATH", "path", "HTTP_PROXY", "https_proxy", "All_Proxy", "no_proxy", "LD_PRELOAD", "LD_LIBRARY_PATH", "ld_audit", "BASH_ENV", "env", "GCONV_PATH", "PythonPath", "SHELLOPTS", "PS4", "NODE_OPTIONS", "PERL5OPT", "GIT_SSH_COMMAND", "GLIBC_TUNABLES"} { if err := ValidateEnv(map[string]string{k: "x"}); !errors.Is(err, ErrBadRequest) { t.Errorf("ValidateEnv(%s) = %v", k, err) } // The panel drops them instead of sending a request the runner refuses. if got := FilterPanelEnv([]string{k + "=x", "KEEP=1"}); len(got) != 1 || got["KEEP"] != "1" { t.Errorf("FilterPanelEnv kept %s: %v", k, got) } } if err := ValidateEnv(map[string]string{"PYTHONUNBUFFERED": "1", "LANGUAGE": "en", "ENVIRONMENT": "x"}); err != nil { t.Fatalf("ordinary names refused: %v", err) } } func TestValidateExecRequestFiltersScopeEntries(t *testing.T) { req := ExecRequest{Argv: []string{"true"}, ScopeDomains: []string{"example.com", "a.example,evil.example", "*.example.org", "a.example evil", "", "example.com:8443", "https://x", "api-*.example.com", "xn--bcher-kva.example", "bücher.example", "host_1.lab.", strings.Repeat("a", 254)}, ScopeCIDRs: []string{"203.0.113.0/24", "1.2.3.4/32,10.0.0.0/8", "10.0.0.0/33", " 10.0.0.1", "198.51.100.7", "2001:db8::/32", "fe80::1%eth0,10.0.0.0/8", "::1%a,0.0.0.0/0", "fe80::1%eth0", "203.0.113.9/24", "::ffff:10.1.0.0/112", "::ffff:198.51.100.8"}} if err := ValidateExecRequest(&req); err != nil { t.Fatalf("invalid entries failed the request: %v", err) } // Invalid entries are dropped whole (never split at the comma); valid ones stay. if got := strings.Join(req.ScopeDomains, " "); got != "example.com *.example.org xn--bcher-kva.example bücher.example host_1.lab." { t.Errorf("domains = %q", got) } // An IPv6 zone may hold any text, commas included, so zoned addresses are // dropped; entries are passed on in canonical form, mapped ones as IPv4. if got := strings.Join(req.ScopeCIDRs, " "); got != "203.0.113.0/24 198.51.100.7 2001:db8::/32 203.0.113.0/24 10.1.0.0/16 198.51.100.8" { t.Errorf("CIDRs = %q", got) } long := make([]string, MaxScopeBytes/200) for i := range long { long[i] = strings.Repeat("a", 240) + ".example" } if err := ValidateExecRequest(&ExecRequest{Argv: []string{"true"}, ScopeDomains: long}); !errors.Is(err, ErrBadRequest) { t.Errorf("%d bytes of scope accepted: %v", joinedLen(long), err) } many := make([]string, MaxScopeEntries+1) for i := range many { many[i] = "a.example" } for _, req := range []ExecRequest{{ScopeDomains: many}, {ScopeCIDRs: many}} { req.Argv = []string{"true"} if err := ValidateExecRequest(&req); !errors.Is(err, ErrBadRequest) { t.Errorf("%d scope entries accepted: %v", len(many), err) } } }