package main // Tests for the environment-variable invariants that protect every path from // untrusted template JSON to a container process environment. The two real // sinks are the Docker Engine API "KEY=VALUE" Env list (envMapToList) and // `docker run -e KEY=VALUE` argv for installers (dockerEnvArgs). Both split // each entry on the first "=", so the suite proves the core framing // invariant: for any input map, the key the container sees is exactly the // sanitized key from the panel's maps — never a smuggled rename — and no // emitted token can be mistaken for a CLI flag. import ( "fmt" "reflect" "strings" "testing" ) func TestValidEnvKey(t *testing.T) { valid := []string{ "FOO", "_BAR", "A1", "a", "my-key", "a.b.c", "K8S_STYLE_1", "UID", "LD_LIBRARY_PATH", "FOO-", "FOO.", strings.Repeat("K", envKeyMaxLen), } for _, key := range valid { if !validEnvKey(key) { t.Fatalf("expected valid env key %q", key) } } invalid := []string{ "", " ", "FOO=BAR", "=FOO", "FOO BAR", "FOO\tBAR", "FOO\nBAR", "FOO\rBAR", "FOO\x00BAR", "-FOO", ".FOO", "FOÖ", "FOO", "FOO\x01", "FOO\x7f", strings.Repeat("K", envKeyMaxLen+1), } for _, key := range invalid { if validEnvKey(key) { t.Fatalf("expected invalid env key %q", key) } } } func TestNormalizeEnvEntry(t *testing.T) { key, value, ok := normalizeEnvEntry(" MY_KEY ", "va\x00lue") if !ok || key != "MY_KEY" || value != "value" { t.Fatalf("expected trimmed key and NUL-stripped value, got %q/%q/%v", key, value, ok) } // A key containing "=" must be dropped, not split: the container-visible // name must never differ from the panel's key. for _, bad := range []string{"PATH=/evil", "A B", "", "A\tB"} { if _, _, ok := normalizeEnvEntry(bad, "x"); ok { t.Fatalf("expected entry with key %q to be rejected", bad) } } oversized := strings.Repeat("v", envValueMaxLen+1) if _, _, ok := normalizeEnvEntry("OK", oversized); ok { t.Fatalf("expected oversized value to be rejected") } // Exactly at the cap is accepted. if _, _, ok := normalizeEnvEntry("OK", strings.Repeat("v", envValueMaxLen)); !ok { t.Fatalf("expected value at cap to be accepted") } } func TestSanitizeEnvMapDropsUnsafeEntries(t *testing.T) { out := sanitizeEnvMap(map[string]string{ "GOOD": "ok", "PATH=/evil": "smuggled", "BAD KEY": "space", "BAD=KEY": "equals", "BAD\nKEY": "newline", "BAD\x00KEY": "nul", "": "empty", " ": "blank", "NUL\u0000VAL": "a\x00b", "my-key": "dashes survive", }) // NUL inside a KEY breaks the key charset: the whole entry is dropped, // only NUL inside a VALUE is stripped. want := map[string]string{"GOOD": "ok", "my-key": "dashes survive"} if !reflect.DeepEqual(out, want) { t.Fatalf("sanitizeEnvMap mismatch:\n got: %#v\nwant: %#v", out, want) } } func TestSanitizeEnvMapEntryCapIsDeterministic(t *testing.T) { env := map[string]string{} for i := 0; i < envMaxEntries+10; i++ { env[fmt.Sprintf("K%03d", i)] = "v" } out := sanitizeEnvMap(env) if len(out) != envMaxEntries { t.Fatalf("expected %d entries, got %d", envMaxEntries, len(out)) } // Sorted iteration: the first envMaxEntries keys in sorted order win, // independent of Go map iteration order. for i := 0; i < envMaxEntries; i++ { if _, ok := out[fmt.Sprintf("K%03d", i)]; !ok { t.Fatalf("expected deterministic survivor K%03d to be present", i) } } if _, ok := out[fmt.Sprintf("K%03d", envMaxEntries)]; ok { t.Fatalf("entry beyond the cap survived") } } func TestRuntimeEnvMapFiltersJSONShapedInput(t *testing.T) { // map[string]any is what template JSON decodes into. out := runtimeEnvMap(map[string]any{ "OK": 1, "SMUGGLE=KEY": "x", "SP ACE": "y", " ": "blank key", "NUL": "a\x00b", }) want := map[string]string{"OK": "1", "NUL": "ab"} if !reflect.DeepEqual(out, want) { t.Fatalf("runtimeEnvMap mismatch:\n got: %#v\nwant: %#v", out, want) } // Compat guard: non-identifier keys stay supported for the startup // renderer's quoted-splice path (see // TestRenderADPanelStartupTemplateNonIdentifierKeyQuotedSplice). out = runtimeEnvMap(map[string]any{"my-key": "a b; c"}) if len(out) != 1 || out["my-key"] != "a b; c" { t.Fatalf("my-key must survive runtimeEnvMap, got %#v", out) } for _, empty := range []any{nil, "string", []any{"a"}, map[string]any{}} { if got := runtimeEnvMap(empty); got != nil { t.Fatalf("expected nil for input %#v, got %#v", empty, got) } } } func TestEnvMapToListFramingInvariant(t *testing.T) { env := map[string]string{ "B": "2", "A": "1", "PATH=/evil": "smuggled", "C D": "space", "E\x00F": "nul", "": "empty", } got := envMapToList(env) want := []string{"A=1", "B=2"} if !reflect.DeepEqual(got, want) { t.Fatalf("envMapToList mismatch:\n got: %#v\nwant: %#v", got, want) } // Framing property, checked structurally: for ANY map, every emitted // entry must split into a valid env key that is the trimmed input key, // and the remainder must be the NUL-stripped value. A key containing "=" // can therefore never rename a variable at the Engine API boundary, and // flag-looking names can never reach the boundary either. hostile := map[string]string{ "PATH=/evil": "x", "LD_PRELOAD=/lib/x": "y", "--rm": "flagish", "GOOD": "a=b=c", "NUL": "a\x00b", } for _, entry := range envMapToList(hostile) { name, value, _ := strings.Cut(entry, "=") if !validEnvKey(name) { t.Fatalf("emitted entry %q has an invalid env name", entry) } if name == "GOOD" { if value != "a=b=c" { t.Fatalf("value must be preserved verbatim after NUL strip, got %q", value) } continue } if name != "NUL" || value != "ab" { t.Fatalf("unexpected survivor from hostile map: %q", entry) } } } func TestDockerEnvArgs(t *testing.T) { env := map[string]string{ "B": "2", "A": "1", "PATH=/evil": "smuggled", "E=\x00F": "nul", "C D": "space", } got := dockerEnvArgs(env) want := []string{"-e", "A=1", "-e", "B=2"} if !reflect.DeepEqual(got, want) { t.Fatalf("dockerEnvArgs mismatch:\n got: %#v\nwant: %#v", got, want) } // The value tokens are consumed by pflag as the -e argument // unconditionally, but as defense in depth none of them may ever look // like a CLI flag or carry a smuggled name. for i := 0; i < len(got); i += 2 { if got[i] != "-e" { t.Fatalf("expected -e flag at index %d, got %q", i, got[i]) } pair := got[i+1] if strings.HasPrefix(pair, "-") { t.Fatalf("env pair %q must never start with '-' (flag injection)", pair) } name, _, _ := strings.Cut(pair, "=") if !validEnvKey(name) { t.Fatalf("env pair %q has an invalid name", pair) } } if got := dockerEnvArgs(nil); got != nil { t.Fatalf("expected nil for nil env, got %#v", got) } if got := dockerEnvArgs(map[string]string{"BAD KEY": "x"}); got != nil { t.Fatalf("expected nil when no entries survive, got %#v", got) } } func TestMergeStringEnvEnforcesInvariants(t *testing.T) { dst := map[string]string{"KEEP": "me"} out := mergeStringEnv(dst, map[string]string{ "GOOD": "1", "BAD=KEY": "x", "BAD KEY": "x", "": "x", "NUL\x00": "x", "OVERSIZE": strings.Repeat("v", envValueMaxLen+1), }) if !reflect.DeepEqual(out, map[string]string{"KEEP": "me", "GOOD": "1"}) { t.Fatalf("mergeStringEnv mismatch: %#v", out) } // nil dst gets a fresh map (pre-existing contract). fresh := mergeStringEnv(nil, map[string]string{"A": "1"}) if !reflect.DeepEqual(fresh, map[string]string{"A": "1"}) { t.Fatalf("mergeStringEnv(nil, ...) mismatch: %#v", fresh) } } func TestNormalizeTemplateInstallConfigEnvInvariants(t *testing.T) { install := normalizeTemplateInstallConfig(&templateInstallConfig{ Image: "ghcr.io/pterodactyl/installers:debian", Script: "echo hi", Env: map[string]string{ "GOOD": "1", "PATH=/evil": "smuggled", "BAD KEY": "space", "E\x00F": "nul", }, }) if install == nil { t.Fatalf("expected install config to normalize") } want := map[string]string{"GOOD": "1"} if !reflect.DeepEqual(install.Env, want) { t.Fatalf("install env mismatch:\n got: %#v\nwant: %#v", install.Env, want) } } // TestEnvKeysStayStartupTemplateReferenceable pins the coupling between the // env key charset and the {{KEY}} tokenizer: every key that survives // sanitization must be referenceable from a startup template token, so a key // can never be dropped by one layer while remaining reachable in another. func TestEnvKeysStayStartupTemplateReferenceable(t *testing.T) { for _, key := range []string{"FOO", "my-key", "a.b", "A_1-x.y", "FOO-"} { if !validEnvKey(key) { t.Fatalf("expected %q to be a valid env key", key) } matched := startupTemplateTokenRE.FindStringSubmatch("{{ " + key + " }}") if len(matched) != 2 || strings.TrimSpace(matched[1]) != key { t.Fatalf("key %q must be referenceable from a {{KEY}} token", key) } } }