TestIsLocalFileForUpdate_PathTraversal ensures that traversal attempts (e.g. "../../etc/passwd") are rejected even if the target path happens to exist.
(t *testing.T)
| 938 | // TestIsLocalFileForUpdate_PathTraversal ensures that traversal attempts (e.g. |
| 939 | // "../../etc/passwd") are rejected even if the target path happens to exist. |
| 940 | func TestIsLocalFileForUpdate_PathTraversal(t *testing.T) { |
| 941 | tmpDir := t.TempDir() |
| 942 | |
| 943 | // Traversal path that would escape tmpDir |
| 944 | traversal := "../../etc/passwd" |
| 945 | if isLocalFileForUpdate(tmpDir, traversal) { |
| 946 | t.Errorf("isLocalFileForUpdate should reject path traversal attempt: %s", traversal) |
| 947 | } |
| 948 | |
| 949 | // A normal path within tmpDir that doesn't exist should return false |
| 950 | if isLocalFileForUpdate(tmpDir, "nonexistent.md") { |
| 951 | t.Errorf("isLocalFileForUpdate should return false for non-existent file") |
| 952 | } |
| 953 | |
| 954 | // A normal path within tmpDir that DOES exist should return true |
| 955 | validFile := "shared/file.md" |
| 956 | if err := os.MkdirAll(filepath.Join(tmpDir, "shared"), 0755); err != nil { |
| 957 | t.Fatal(err) |
| 958 | } |
| 959 | if err := os.WriteFile(filepath.Join(tmpDir, validFile), []byte("content"), 0644); err != nil { |
| 960 | t.Fatal(err) |
| 961 | } |
| 962 | if !isLocalFileForUpdate(tmpDir, validFile) { |
| 963 | t.Errorf("isLocalFileForUpdate should return true for an existing file within tmpDir") |
| 964 | } |
| 965 | } |
nothing calls this directly
no test coverage detected