package main // G3 hardening: the start flow used to read/write server.properties, // eula.txt and the generated runtime scaffold files by absolute name, so a // tenant symlink planted at one of these stable names redirected the daemon's // writes into a sibling server (no race needed) and a planted FIFO wedged the // start path. Every access now runs against the serverDir pinned by directory // fd, with the O_NONBLOCK + regular-file gate on reads and O_NOFOLLOW on // writes; normal files round-trip byte-identically. import ( "os" "path/filepath" "testing" ) func propsWithPortIP() string { return "motd=hello\nserver-port=30000\nserver-ip=1.2.3.4\nonline-mode=true\n" } func wantPropsWithPortIP() string { return "motd=hello\nserver-port=25565\nserver-ip=\nonline-mode=true\n" } // A normal server.properties round-trips byte-identically through the pinned // read-modify-write (port pinned, ip emptied, everything else untouched). func TestEnforceServerProps_NormalFileRoundTrip(t *testing.T) { dir := t.TempDir() props := filepath.Join(dir, "server.properties") if err := os.WriteFile(props, []byte(propsWithPortIP()), 0o644); err != nil { t.Fatal(err) } enforceServerProps(dir) data, err := os.ReadFile(props) if err != nil { t.Fatal(err) } if string(data) != wantPropsWithPortIP() { t.Fatalf("server.properties = %q, want %q", data, wantPropsWithPortIP()) } st, err := os.Stat(props) if err != nil { t.Fatal(err) } if st.Mode().Perm() != 0o644 { t.Fatalf("mode = %#o, want 644", st.Mode().Perm()) } } // Missing file: the defaults are created exactly as before. func TestEnforceServerProps_MissingCreatesDefaults(t *testing.T) { dir := t.TempDir() enforceServerProps(dir) data, err := os.ReadFile(filepath.Join(dir, "server.properties")) if err != nil { t.Fatal(err) } if string(data) != "server-port=25565\nserver-ip=\n" { t.Fatalf("defaults = %q", data) } } // A symlink planted at server.properties must not redirect the daemon's // write into the sibling directory, even when the write would otherwise // happen (the old os.WriteFile followed it blindly). func TestEnforceServerProps_PlantedSymlinkDoesNotEscape(t *testing.T) { dir := t.TempDir() sibling := t.TempDir() siblingProps := filepath.Join(sibling, "server.properties") siblingContent := "server-port=11111\nlevel-name=sibling\n" if err := os.WriteFile(siblingProps, []byte(siblingContent), 0o644); err != nil { t.Fatal(err) } if err := os.Symlink(siblingProps, filepath.Join(dir, "server.properties")); err != nil { t.Skipf("symlinks unsupported: %v", err) } enforceServerProps(dir) data, err := os.ReadFile(siblingProps) if err != nil { t.Fatal(err) } if string(data) != siblingContent { t.Fatalf("sibling server.properties was modified: %q", data) } // The planted link is still a link; nothing new was created through it. li, err := os.Lstat(filepath.Join(dir, "server.properties")) if err != nil || li.Mode()&os.ModeSymlink == 0 { t.Fatalf("planted symlink must be untouched, err=%v", err) } } // A planted FIFO cannot wedge the start flow: the refusal settles promptly // and the FIFO is left alone. func TestEnforceServerProps_PlantedFifoPrompt(t *testing.T) { dir := t.TempDir() mkfifoInRoot(t, filepath.Join(dir, "server.properties")) err := errSettleWithin(t, "enforceServerProps on planted FIFO", func() error { enforceServerProps(dir) return nil }) if err != nil { t.Fatal(err) } } // The eula read keeps its error convention ("not accepted") for a planted // FIFO: promptly, with no wedge. func TestMinecraftEulaAccepted_PlantedFifoNotAcceptedPrompt(t *testing.T) { dir := t.TempDir() mkfifoInRoot(t, filepath.Join(dir, "eula.txt")) err := errSettleWithin(t, "minecraftEulaAccepted on planted FIFO", func() error { if minecraftEulaAccepted(dir) { t.Fatalf("planted FIFO must count as not accepted") } return nil }) if err != nil { t.Fatal(err) } } // The eula read must not accept a EULA it read out of another tenant's // server through a symlink planted at the stable eula.txt name. func TestMinecraftEulaAccepted_SiblingSymlinkNotAccepted(t *testing.T) { dir := t.TempDir() sibling := t.TempDir() if err := os.WriteFile(filepath.Join(sibling, "eula.txt"), []byte("eula=true\n"), 0o644); err != nil { t.Fatal(err) } if err := os.Symlink(filepath.Join(sibling, "eula.txt"), filepath.Join(dir, "eula.txt")); err != nil { t.Skipf("symlinks unsupported: %v", err) } if minecraftEulaAccepted(dir) { t.Fatal("eula.txt symlink into a sibling server must not count as accepted") } } // writeGeneratedRuntimeFile keeps its create / legacy-replace / keep-custom // semantics for normal files, including nested paths. func TestWriteGeneratedRuntimeFile_NormalSemantics(t *testing.T) { dir := t.TempDir() // Missing: created with the content, 0644. writeGeneratedRuntimeFile(dir, filepath.Join(dir, "main.py"), []byte(pythonMainTemplate), isLegacyPythonScaffold) data, err := os.ReadFile(filepath.Join(dir, "main.py")) if err != nil || string(data) != pythonMainTemplate { t.Fatalf("scaffold not created: %v", err) } if st, serr := os.Stat(filepath.Join(dir, "main.py")); serr != nil || st.Mode().Perm() != 0o644 { t.Fatalf("scaffold mode = %v (%v), want 644", st.Mode().Perm(), serr) } // Legacy scaffold content: replaced by the current template (the exact // upgrade path the replace rule exists for). if err := os.WriteFile(filepath.Join(dir, "main.py"), []byte(legacyPythonMainTemplate), 0o644); err != nil { t.Fatal(err) } writeGeneratedRuntimeFile(dir, filepath.Join(dir, "main.py"), []byte(pythonMainTemplate), isLegacyPythonScaffold) data, err = os.ReadFile(filepath.Join(dir, "main.py")) if err != nil || string(data) != pythonMainTemplate { t.Fatalf("legacy scaffold not replaced: %v", err) } // Custom content: left alone (the replace rule only fires on the exact // legacy scaffold). if err := os.WriteFile(filepath.Join(dir, "main.py"), []byte("print('custom')\n"), 0o644); err != nil { t.Fatal(err) } writeGeneratedRuntimeFile(dir, filepath.Join(dir, "main.py"), []byte(pythonMainTemplate), isLegacyPythonScaffold) data, err = os.ReadFile(filepath.Join(dir, "main.py")) if err != nil || string(data) != "print('custom')\n" { t.Fatalf("custom content must be kept: %v (%q)", err, data) } // Nested path: parent dirs created like ensureDir did. nested := filepath.Join(dir, "app", "sub", "index.js") writeGeneratedRuntimeFile(dir, nested, []byte(buildNodeIndexTemplate(3001)), isLegacyNodeScaffold) if _, err := os.Stat(nested); err != nil { t.Fatalf("nested scaffold not created: %v", err) } } // A symlink planted at the scaffold name pointing into a sibling server must // not be written through - even when the sibling file holds exactly the // legacy scaffold the replace rule would have overwritten. func TestWriteGeneratedRuntimeFile_PlantedSymlinkDoesNotEscape(t *testing.T) { dir := t.TempDir() sibling := t.TempDir() siblingTarget := filepath.Join(sibling, "index.js") legacy := []byte(buildNodeIndexTemplate(3001)) if err := os.WriteFile(siblingTarget, legacy, 0o644); err != nil { t.Fatal(err) } if err := os.Symlink(siblingTarget, filepath.Join(dir, "index.js")); err != nil { t.Skipf("symlinks unsupported: %v", err) } newer := []byte(buildNodeIndexTemplate(4001)) writeGeneratedRuntimeFile(dir, filepath.Join(dir, "index.js"), newer, isLegacyNodeScaffold) data, err := os.ReadFile(siblingTarget) if err != nil { t.Fatal(err) } if string(data) != string(legacy) { t.Fatalf("sibling file was rewritten through the planted symlink") } li, err := os.Lstat(filepath.Join(dir, "index.js")) if err != nil || li.Mode()&os.ModeSymlink == 0 { t.Fatalf("planted symlink must be untouched, err=%v", err) } } // A planted FIFO at the scaffold name cannot wedge the start flow. func TestWriteGeneratedRuntimeFile_PlantedFifoPrompt(t *testing.T) { dir := t.TempDir() mkfifoInRoot(t, filepath.Join(dir, "index.js")) err := errSettleWithin(t, "writeGeneratedRuntimeFile on planted FIFO", func() error { writeGeneratedRuntimeFile(dir, filepath.Join(dir, "index.js"), []byte(buildNodeIndexTemplate(3001)), isLegacyNodeScaffold) return nil }) if err != nil { t.Fatal(err) } } // scaffoldNode's package.json creation follows the same discipline: a planted // symlink at package.json must not redirect the write into a sibling server. func TestScaffoldNode_PlantedPackageJsonSymlinkDoesNotEscape(t *testing.T) { dir := t.TempDir() sibling := t.TempDir() siblingPkg := filepath.Join(sibling, "package.json") if err := os.WriteFile(siblingPkg, []byte(`{"name":"sibling"}`), 0o644); err != nil { t.Fatal(err) } if err := os.Symlink(siblingPkg, filepath.Join(dir, "package.json")); err != nil { t.Skipf("symlinks unsupported: %v", err) } scaffoldNode(dir, filepath.Base(dir), "", 3001) data, err := os.ReadFile(siblingPkg) if err != nil { t.Fatal(err) } if string(data) != `{"name":"sibling"}` { t.Fatalf("sibling package.json was rewritten: %q", data) } // The scaffold entrypoint itself is still created. if _, err := os.Stat(filepath.Join(dir, "index.js")); err != nil { t.Fatalf("index.js scaffold not created: %v", err) } }