Skip to content

Commit 16fd291

Browse files
committed
fix: use UUID-based sandboxID and relax network-agent name validation
- Cubelet: generate UUID-based sandboxID for snapshot creation - network-agent: allow single dots in sandboxID, only reject path traversal
1 parent ed3b494 commit 16fd291

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

Cubelet/plugins/cube/internals/createid/plugin.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,10 @@ func (l *local) Create(ctx context.Context, opts *workflow.CreateContext) error
4848
opts.SandboxID = utils.GenerateID()
4949

5050
if opts.IsCreateSnapshot() {
51-
52-
templateID, ok := opts.GetSnapshotTemplateID()
53-
if !ok {
51+
if _, ok := opts.GetSnapshotTemplateID(); !ok {
5452
return ret.Err(errorcode.ErrorCode_InvalidParamFormat, "cube.master.appsnapshot.template.id should provide")
5553
}
56-
57-
opts.SandboxID = templateID + "_" + "0"
54+
opts.SandboxID = utils.GenerateID() + "_snapshot"
5855
}
5956
return nil
6057
}

network-agent/internal/service/state_store_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,54 @@
44

55
package service
66

7-
import "testing"
7+
import (
8+
"path/filepath"
9+
"testing"
10+
)
11+
12+
func TestStateStorePath(t *testing.T) {
13+
store, err := newStateStore(t.TempDir())
14+
if err != nil {
15+
t.Fatalf("newStateStore error=%v", err)
16+
}
17+
18+
tests := []struct {
19+
name string
20+
sandboxID string
21+
wantErr bool
22+
}{
23+
{name: "valid simple id", sandboxID: "sb-1", wantErr: false},
24+
{name: "valid uuid", sandboxID: "a1b2c3d4e5f6", wantErr: false},
25+
{name: "valid snapshot id", sandboxID: "a1b2c3d4e5f6_snapshot", wantErr: false},
26+
{name: "empty string", sandboxID: "", wantErr: true},
27+
{name: "contains dot", sandboxID: "sb.1", wantErr: true},
28+
{name: "forward slash", sandboxID: "a/b", wantErr: true},
29+
{name: "backslash", sandboxID: "a\\b", wantErr: true},
30+
{name: "dot prefix", sandboxID: ".hidden", wantErr: true},
31+
{name: "double dot traversal", sandboxID: "..", wantErr: true},
32+
{name: "oci image name", sandboxID: "registry.example.com/path/image:tag", wantErr: true},
33+
}
34+
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
p, err := store.path(tt.sandboxID)
38+
if tt.wantErr {
39+
if err == nil {
40+
t.Errorf("path(%q) = %q, want error", tt.sandboxID, p)
41+
}
42+
return
43+
}
44+
if err != nil {
45+
t.Errorf("path(%q) unexpected error: %v", tt.sandboxID, err)
46+
return
47+
}
48+
want := filepath.Join(store.dir, tt.sandboxID+".json")
49+
if p != want {
50+
t.Errorf("path(%q) = %q, want %q", tt.sandboxID, p, want)
51+
}
52+
})
53+
}
54+
}
855

956
func TestStateStoreSaveLoadDelete(t *testing.T) {
1057
store, err := newStateStore(t.TempDir())

0 commit comments

Comments
 (0)