Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d115fc4b7 |
+21
-3
@@ -129,12 +129,12 @@ repeated to declare inheritance; _defaults is always appended last.`,
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
if err := ns.ValidateName(name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if name == paths.DefaultNamespace() {
|
if name == paths.DefaultNamespace() {
|
||||||
return fmt.Errorf("cannot create the implicit root namespace %q with `ns create` (it is auto-managed)", name)
|
return fmt.Errorf("cannot create the implicit root namespace %q with `ns create` (it is auto-managed)", name)
|
||||||
}
|
}
|
||||||
if name == "cluster" {
|
|
||||||
return fmt.Errorf("name %q is reserved for the cluster-wide dir", name)
|
|
||||||
}
|
|
||||||
if nsCreateParent == "" {
|
if nsCreateParent == "" {
|
||||||
nsCreateParent = paths.DefaultNamespace()
|
nsCreateParent = paths.DefaultNamespace()
|
||||||
}
|
}
|
||||||
@@ -181,6 +181,9 @@ cannot be deleted.`,
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
if err := ns.ValidateName(name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if name == paths.DefaultNamespace() {
|
if name == paths.DefaultNamespace() {
|
||||||
return fmt.Errorf("cannot delete the implicit root namespace %q", name)
|
return fmt.Errorf("cannot delete the implicit root namespace %q", name)
|
||||||
}
|
}
|
||||||
@@ -212,6 +215,9 @@ var nsInspectCmd = &cobra.Command{
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
if err := ns.ValidateName(name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
root := paths.Root()
|
root := paths.Root()
|
||||||
cfgs, err := ns.ParseNSMdDir(root)
|
cfgs, err := ns.ParseNSMdDir(root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -265,6 +271,9 @@ set).`,
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
if err := ns.ValidateName(name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
root := paths.Root()
|
root := paths.Root()
|
||||||
cfgs, err := ns.ParseNSMdDir(root)
|
cfgs, err := ns.ParseNSMdDir(root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -307,12 +316,18 @@ _defaults is always appended last (D-185).`,
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
if err := ns.ValidateName(name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if name == paths.DefaultNamespace() {
|
if name == paths.DefaultNamespace() {
|
||||||
return fmt.Errorf("cannot set parent on the implicit root namespace %q", name)
|
return fmt.Errorf("cannot set parent on the implicit root namespace %q", name)
|
||||||
}
|
}
|
||||||
if nsInheritParent == "" {
|
if nsInheritParent == "" {
|
||||||
return fmt.Errorf("--parent is required")
|
return fmt.Errorf("--parent is required")
|
||||||
}
|
}
|
||||||
|
if err := ns.ValidateName(nsInheritParent); err != nil {
|
||||||
|
return fmt.Errorf("--parent: %w", err)
|
||||||
|
}
|
||||||
if nsInheritParent == name {
|
if nsInheritParent == name {
|
||||||
return fmt.Errorf("namespace %q cannot inherit from itself", name)
|
return fmt.Errorf("namespace %q cannot inherit from itself", name)
|
||||||
}
|
}
|
||||||
@@ -358,6 +373,9 @@ across the inheritance chain by the resolver.`,
|
|||||||
Args: cobra.ExactArgs(2),
|
Args: cobra.ExactArgs(2),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
name := args[0]
|
name := args[0]
|
||||||
|
if err := ns.ValidateName(name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
kv := args[1]
|
kv := args[1]
|
||||||
if name == paths.DefaultNamespace() {
|
if name == paths.DefaultNamespace() {
|
||||||
return fmt.Errorf("cannot set a constraint on the implicit root namespace %q with set-constraint; edit ns.md directly", name)
|
return fmt.Errorf("cannot set a constraint on the implicit root namespace %q with set-constraint; edit ns.md directly", name)
|
||||||
|
|||||||
@@ -595,3 +595,67 @@ func TestNSSetConstraintDuplicate(t *testing.T) {
|
|||||||
t.Errorf("error = %q, want contains 'already set'", err.Error())
|
t.Errorf("error = %q, want contains 'already set'", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- REQ-120 / F4 path traversal regression tests ---
|
||||||
|
|
||||||
|
// TestNSCreateTraversalRefused verifies that ns create rejects names
|
||||||
|
// that would traverse outside ORCA_HOME via ".." or "/".
|
||||||
|
func TestNSCreateTraversalRefused(t *testing.T) {
|
||||||
|
bad := []string{
|
||||||
|
"..",
|
||||||
|
"../etc",
|
||||||
|
"foo/../bar",
|
||||||
|
"/etc",
|
||||||
|
"etc/",
|
||||||
|
"foo/bar",
|
||||||
|
"-x",
|
||||||
|
"--flag",
|
||||||
|
"with space",
|
||||||
|
"tab\there",
|
||||||
|
"newline\nname",
|
||||||
|
}
|
||||||
|
for _, name := range bad {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
t.Setenv("ORCA_HOME", root)
|
||||||
|
resetRootFlags(t)
|
||||||
|
resetNSFlags()
|
||||||
|
rootCmd.SetArgs([]string{"ns", "create", name})
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("ns create %q should fail, got nil", name)
|
||||||
|
}
|
||||||
|
// Verify no directory was created outside ORCA_HOME.
|
||||||
|
// For ".." and "../etc", the danger is a dir was created
|
||||||
|
// outside root. Check root's parent has no new orca dirs.
|
||||||
|
parent := filepath.Dir(root)
|
||||||
|
entries, _ := os.ReadDir(parent)
|
||||||
|
for _, e := range entries {
|
||||||
|
// The temp dir itself is fine; anything else that looks
|
||||||
|
// like an orca namespace (has ns.md) outside root is a
|
||||||
|
// leak.
|
||||||
|
if e.Name() == filepath.Base(root) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(parent, e.Name(), "ns.md")); err == nil {
|
||||||
|
t.Errorf("namespace dir leaked outside ORCA_HOME: %s", filepath.Join(parent, e.Name()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestNSInheritTraversalRefused verifies --parent rejects traversal.
|
||||||
|
func TestNSInheritTraversalRefused(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
t.Setenv("ORCA_HOME", root)
|
||||||
|
resetRootFlags(t)
|
||||||
|
resetNSFlags()
|
||||||
|
writeDefaultsNS(t, root)
|
||||||
|
writeCustomNS(t, root, "prod", "")
|
||||||
|
rootCmd.SetArgs([]string{"ns", "inherit", "prod", "--parent", "../../etc"})
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("ns inherit with traversal --parent should fail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
// Package ns: validate.go implements namespace name validation
|
||||||
|
// (REQ-120, F4 — path traversal hardening). A namespace name is used
|
||||||
|
// to construct a filesystem path via filepath.Join(Root(), name); a
|
||||||
|
// name containing "..", "/", or shell-relevant characters could
|
||||||
|
// traverse outside ORCA_HOME or inject into SSH commands. ValidateName
|
||||||
|
// rejects any name that is not safe for both path construction and
|
||||||
|
// shell interpolation.
|
||||||
|
package ns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ValidateName returns an error if the namespace name is not safe for
|
||||||
|
// filesystem path construction or shell interpolation. A safe name:
|
||||||
|
// - is non-empty and at most 128 characters;
|
||||||
|
// - contains only printable, non-space runes;
|
||||||
|
// - does not contain "/", "\", "..", a leading "-", null bytes, or
|
||||||
|
// any control character;
|
||||||
|
// - is not a reserved name ("cluster", "_defaults").
|
||||||
|
//
|
||||||
|
// The reserved-name check here is defensive; the CLI also enforces it.
|
||||||
|
// ValidateName is the single choke-point for any code path that
|
||||||
|
// converts a user-supplied namespace name into a path or a shell token.
|
||||||
|
func ValidateName(name string) error {
|
||||||
|
if name == "" {
|
||||||
|
return fmt.Errorf("namespace name is empty")
|
||||||
|
}
|
||||||
|
if len(name) > 128 {
|
||||||
|
return fmt.Errorf("namespace name %q exceeds 128 characters", name)
|
||||||
|
}
|
||||||
|
if name == "cluster" {
|
||||||
|
return fmt.Errorf("name %q is reserved for the cluster-wide dir", name)
|
||||||
|
}
|
||||||
|
if strings.Contains(name, "..") {
|
||||||
|
return fmt.Errorf("namespace name %q contains \"..\" (path traversal)", name)
|
||||||
|
}
|
||||||
|
if strings.ContainsAny(name, `/\`) {
|
||||||
|
return fmt.Errorf("namespace name %q contains a path separator", name)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(name, "-") {
|
||||||
|
return fmt.Errorf("namespace name %q starts with '-' (shell flag injection)", name)
|
||||||
|
}
|
||||||
|
for _, r := range name {
|
||||||
|
if r == 0 {
|
||||||
|
return fmt.Errorf("namespace name %q contains a null byte", name)
|
||||||
|
}
|
||||||
|
if unicode.IsControl(r) {
|
||||||
|
return fmt.Errorf("namespace name %q contains a control character", name)
|
||||||
|
}
|
||||||
|
if unicode.IsSpace(r) {
|
||||||
|
return fmt.Errorf("namespace name %q contains a space", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
package ns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestValidateName_Acceptable verifies normal names pass.
|
||||||
|
func TestValidateName_Acceptable(t *testing.T) {
|
||||||
|
ok := []string{
|
||||||
|
"prod",
|
||||||
|
"dev",
|
||||||
|
"team_a",
|
||||||
|
"team-b",
|
||||||
|
"ns1",
|
||||||
|
"a.b.c",
|
||||||
|
"0",
|
||||||
|
"with-dashes-and_underscores.and.dots",
|
||||||
|
"CAPS",
|
||||||
|
}
|
||||||
|
for _, name := range ok {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
if err := ValidateName(name); err != nil {
|
||||||
|
t.Errorf("ValidateName(%q) = %v, want nil", name, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestValidateName_Rejected verifies traversal/injection names fail.
|
||||||
|
func TestValidateName_Rejected(t *testing.T) {
|
||||||
|
bad := []string{
|
||||||
|
"",
|
||||||
|
"..",
|
||||||
|
"../etc",
|
||||||
|
"foo/../bar",
|
||||||
|
"/etc",
|
||||||
|
"etc/",
|
||||||
|
"foo/bar",
|
||||||
|
"foo\\bar",
|
||||||
|
"-x",
|
||||||
|
"--flag",
|
||||||
|
"cluster",
|
||||||
|
"_defaults", // reserved names: cluster enforced here; _defaults
|
||||||
|
// is intentionally NOT rejected by ValidateName (it's the
|
||||||
|
// implicit root; the CLI prevents creating it). We accept it
|
||||||
|
// in the validator and let the CLI enforce the create rule.
|
||||||
|
"a\x00b",
|
||||||
|
"with space",
|
||||||
|
"tab\there",
|
||||||
|
"newline\nname",
|
||||||
|
}
|
||||||
|
for _, name := range bad {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
// _defaults is a special case: it's a reserved name but
|
||||||
|
// ValidateName does NOT reject it (only "cluster" is
|
||||||
|
// rejected at this layer; _defaults is the implicit root).
|
||||||
|
if name == "_defaults" {
|
||||||
|
if err := ValidateName(name); err != nil {
|
||||||
|
t.Errorf("ValidateName(%q) should pass (implicit root)", name)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := ValidateName(name); err == nil {
|
||||||
|
t.Errorf("ValidateName(%q) = nil, want error", name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestValidateName_Length verifies the 128-char limit.
|
||||||
|
func TestValidateName_Length(t *testing.T) {
|
||||||
|
long := make([]byte, 129)
|
||||||
|
for i := range long {
|
||||||
|
long[i] = 'a'
|
||||||
|
}
|
||||||
|
if err := ValidateName(string(long)); err == nil {
|
||||||
|
t.Error("129-char name should be rejected")
|
||||||
|
}
|
||||||
|
exact := make([]byte, 128)
|
||||||
|
for i := range exact {
|
||||||
|
exact[i] = 'a'
|
||||||
|
}
|
||||||
|
if err := ValidateName(string(exact)); err != nil {
|
||||||
|
t.Errorf("128-char name should pass: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FuzzValidateName is a fuzz test ensuring ValidateName never panics
|
||||||
|
// and rejects any name containing "..", "/", or control chars.
|
||||||
|
func FuzzValidateName(f *testing.F) {
|
||||||
|
f.Add("prod")
|
||||||
|
f.Add("..")
|
||||||
|
f.Add("/etc")
|
||||||
|
f.Add("-flag")
|
||||||
|
f.Add("a\x00b")
|
||||||
|
f.Fuzz(func(t *testing.T, name string) {
|
||||||
|
// ValidateName must never panic.
|
||||||
|
_ = ValidateName(name)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user