| 1 | |
| 2 | |
| 3 | |
| 4 | package limatmpl |
| 5 | |
| 6 | import ( |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "gotest.tools/v3/assert" |
| 11 | ) |
| 12 | |
| 13 | func TestSymlinkTarget(t *testing.T) { |
| 14 | t.Run("target in the same directory", func(t *testing.T) { |
| 15 | got, err := symlinkTarget("dir/config.yaml", "other.yaml") |
| 16 | assert.NilError(t, err) |
| 17 | assert.Equal(t, got, "dir/other.yaml") |
| 18 | }) |
| 19 | t.Run("target relative to the repo root", func(t *testing.T) { |
| 20 | got, err := symlinkTarget(".lima.yaml", "sub/x.yaml") |
| 21 | assert.NilError(t, err) |
| 22 | assert.Equal(t, got, "sub/x.yaml") |
| 23 | }) |
| 24 | t.Run("parent that stays within the repo", func(t *testing.T) { |
| 25 | got, err := symlinkTarget("dir/config.yaml", "../top.yaml") |
| 26 | assert.NilError(t, err) |
| 27 | assert.Equal(t, got, "top.yaml") |
| 28 | }) |
| 29 | t.Run("parent from the repo root is rejected", func(t *testing.T) { |
| 30 | _, err := symlinkTarget(".lima.yaml", "../evil.yaml") |
| 31 | assert.ErrorContains(t, err, "escapes the repository") |
| 32 | }) |
| 33 | t.Run("traversal to another repo is rejected", func(t *testing.T) { |
| 34 | _, err := symlinkTarget("dir/config.yaml", "../../../../torvalds/linux/master/README") |
| 35 | assert.ErrorContains(t, err, "escapes the repository") |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | func TestEscapesRepo(t *testing.T) { |
| 40 | for _, tc := range []struct { |
| 41 | path string |
| 42 | want bool |
| 43 | }{ |
| 44 | {"", false}, |
| 45 | {"x.yaml", false}, |
| 46 | {"dir/../x.yaml", false}, |
| 47 | {"..", true}, |
| 48 | {"../x.yaml", true}, |
| 49 | {"dir/../../x.yaml", true}, |
| 50 | } { |
| 51 | assert.Equal(t, escapesRepo(tc.path), tc.want, "path %#q", tc.path) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | func TestTransformGitHubURLContainment(t *testing.T) { |
| 58 | t.Run("path climbing out of the repo is rejected", func(t *testing.T) { |
| 59 | _, err := transformGitHubURL(t.Context(), "lima-vm/lima/../../../torvalds/linux/master/evil.yaml") |
| 60 | assert.ErrorContains(t, err, "escapes the repository") |
| 61 | }) |
| 62 | t.Run("repo of .. is rejected", func(t *testing.T) { |
| 63 | _, err := transformGitHubURL(t.Context(), "lima-vm/../../torvalds/linux/master/evil.yaml") |
| 64 | assert.ErrorContains(t, err, "escapes the org") |
| 65 | }) |
| 66 | t.Run("branch climbing out of the repo is rejected", func(t *testing.T) { |
| 67 | _, err := transformGitHubURL(t.Context(), "lima-vm/lima/README.md@../../torvalds/linux/master") |
| 68 | assert.ErrorContains(t, err, "escapes the repository") |
| 69 | }) |
| 70 | t.Run("redirect body leaving the org is rejected on re-entry", func(t *testing.T) { |
| 71 | redirect, err := validateGitHubRedirect("github:lima-vm/../../torvalds/linux/master/evil.yaml", "lima-vm", "main", "https://example.com/x") |
| 72 | assert.NilError(t, err) |
| 73 | |
| 74 | _, err = transformGitHubURL(t.Context(), strings.TrimPrefix(redirect, "github:")) |
| 75 | assert.ErrorContains(t, err, "escapes the org") |
| 76 | }) |
| 77 | t.Run("parent that stays within the repo is allowed", func(t *testing.T) { |
| 78 | got, err := transformGitHubURL(t.Context(), "lima-vm/lima/docs/../README.md@main") |
| 79 | assert.NilError(t, err) |
| 80 | assert.Equal(t, got, "https://raw.githubusercontent.com/lima-vm/lima/main/README.md") |
| 81 | }) |
| 82 | } |