|
| 1 | +package huggingface |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/docker/model-runner/pkg/internal/archive" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCheckRelativeRejectsPathTraversal(t *testing.T) { |
| 11 | + baseDir := t.TempDir() |
| 12 | + |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + filePath string |
| 16 | + wantErr bool |
| 17 | + wantPath string // expected result when no error |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "simple filename", |
| 21 | + filePath: "model.safetensors", |
| 22 | + wantErr: false, |
| 23 | + wantPath: filepath.Join(baseDir, "model.safetensors"), |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "nested subdirectory", |
| 27 | + filePath: "subdir/model.gguf", |
| 28 | + wantErr: false, |
| 29 | + wantPath: filepath.Join(baseDir, "subdir/model.gguf"), |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "deeply nested path", |
| 33 | + filePath: "a/b/c/config.json", |
| 34 | + wantErr: false, |
| 35 | + wantPath: filepath.Join(baseDir, "a/b/c/config.json"), |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "path traversal with dot-dot", |
| 39 | + filePath: "../../etc/passwd", |
| 40 | + wantErr: true, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "path traversal single level", |
| 44 | + filePath: "../evil.txt", |
| 45 | + wantErr: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "path traversal embedded in subdir", |
| 49 | + filePath: "subdir/../../etc/shadow", |
| 50 | + wantErr: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "absolute path is rejected", |
| 54 | + filePath: "/etc/passwd", |
| 55 | + wantErr: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "dot-dot at boundary of base name", |
| 59 | + filePath: "../" + filepath.Base(baseDir) + "-other/evil.txt", |
| 60 | + wantErr: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "path with dot segment that stays inside", |
| 64 | + filePath: "subdir/../model.gguf", |
| 65 | + wantErr: false, |
| 66 | + wantPath: filepath.Join(baseDir, "model.gguf"), |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + got, err := archive.CheckRelative(baseDir, tt.filePath) |
| 73 | + if tt.wantErr { |
| 74 | + if err == nil { |
| 75 | + t.Errorf("CheckRelative(%q, %q) = %q, want error", baseDir, tt.filePath, got) |
| 76 | + } |
| 77 | + return |
| 78 | + } |
| 79 | + if err != nil { |
| 80 | + t.Errorf("CheckRelative(%q, %q) returned unexpected error: %v", baseDir, tt.filePath, err) |
| 81 | + return |
| 82 | + } |
| 83 | + if got != tt.wantPath { |
| 84 | + t.Errorf("CheckRelative(%q, %q) = %q, want %q", baseDir, tt.filePath, got, tt.wantPath) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
0 commit comments