| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // TestLsRemoteAllowProtocol: with GIT_ALLOW_PROTOCOL restricted, an ext:: |
| 11 | // repo runs nothing, even where git config would allow the transport. |
| 12 | func TestLsRemoteAllowProtocol(t *testing.T) { |
| 13 | marker := filepath.Join(t.TempDir(), "ran") |
| 14 | t.Setenv("GIT_CONFIG_COUNT", "1") |
| 15 | t.Setenv("GIT_CONFIG_KEY_0", "protocol.ext.allow") |
| 16 | t.Setenv("GIT_CONFIG_VALUE_0", "always") |
| 17 | repo := "ext::touch " + marker |
| 18 | if _, err := LsRemote(context.Background(), repo, "refs/heads/main", "GIT_ALLOW_PROTOCOL=https:ssh:git:file"); err == nil { |
| 19 | t.Error("ls-remote of an ext:: repo succeeded") |
| 20 | } |
| 21 | if _, err := os.Stat(marker); err == nil { |
| 22 | t.Fatal("git ran the ext:: command despite GIT_ALLOW_PROTOCOL") |
| 23 | } |
| 24 | // Without the restriction the same repo does run: the check above is real. |
| 25 | _, _ = LsRemote(context.Background(), repo, "refs/heads/main") |
| 26 | if _, err := os.Stat(marker); err != nil { |
| 27 | if os.Getenv("KLAUS_TEST_TOOLS") == "required" { |
| 28 | t.Fatalf("this git does not run ext:: even when allowed (%v); the restriction is untested here", err) |
| 29 | } |
| 30 | t.Skipf("this git does not run ext:: even when allowed (%v); the restriction is untested here", err) |
| 31 | } |
| 32 | } |