| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func isolatedGitConfig(t *testing.T) string { |
| 11 | t.Helper() |
| 12 | dir := t.TempDir() |
| 13 | path := filepath.Join(dir, "gitconfig") |
| 14 | t.Setenv("GIT_CONFIG_GLOBAL", path) |
| 15 | t.Setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 16 | t.Setenv("GIT_CONFIG_COUNT", "0") |
| 17 | t.Setenv("GIT_CONFIG_PARAMETERS", "") |
| 18 | t.Setenv("XDG_CONFIG_HOME", dir) |
| 19 | if err := os.WriteFile(path, []byte("[user]\n\tname = Cage Test\n"), 0600); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | return path |
| 23 | } |
| 24 | |
| 25 | func TestHostGitRequiresExplicitAuthorizedConsent(t *testing.T) { |
| 26 | path := isolatedGitConfig(t) |
| 27 | s := testServer(t) |
| 28 | original, _ := os.ReadFile(path) |
| 29 | w := request(s, "GET", "/api/host/git", "", "", true) |
| 30 | if w.Code != 200 || !strings.Contains(w.Body.String(), `"can_enable":true`) { |
| 31 | t.Fatalf("expected missing protection: %d %s", w.Code, w.Body.String()) |
| 32 | } |
| 33 | for _, tc := range []struct { |
| 34 | origin, body string |
| 35 | auth bool |
| 36 | code int |
| 37 | }{ |
| 38 | {s.origin, `{"confirm":true}`, false, 401}, |
| 39 | {"https://example.com", `{"confirm":true}`, true, 403}, |
| 40 | {s.origin, `{}`, true, 400}, |
| 41 | {s.origin, `{"confirm":false}`, true, 400}, |
| 42 | } { |
| 43 | w = request(s, "POST", "/api/host/git", tc.origin, tc.body, tc.auth) |
| 44 | if w.Code != tc.code { |
| 45 | t.Fatalf("unauthorized consent: %d %s", w.Code, w.Body.String()) |
| 46 | } |
| 47 | } |
| 48 | unchanged, _ := os.ReadFile(path) |
| 49 | if string(unchanged) != string(original) { |
| 50 | t.Fatal("read or rejected request modified global configuration") |
| 51 | } |
| 52 | |
| 53 | if err := s.startEnvironment(true); err == nil || !strings.Contains(err.Error(), "Git 安全保护") { |
| 54 | t.Fatalf("first startup did not stop at host preflight: %v", err) |
| 55 | } |
| 56 | w = request(s, "POST", "/api/host/git", s.origin, `{"confirm":true}`, true) |
| 57 | if w.Code != 200 || !strings.Contains(w.Body.String(), `"ready":true`) { |
| 58 | t.Fatalf("explicit enable: %d %s", w.Code, w.Body.String()) |
| 59 | } |
| 60 | saved, _ := os.ReadFile(path) |
| 61 | if !strings.Contains(string(saved), "name = Cage Test") || !strings.Contains(string(saved), "bareRepository = explicit") { |
| 62 | t.Fatalf("existing settings lost or protection absent: %s", saved) |
| 63 | } |
| 64 | w = request(s, "GET", "/api/host/git", "", "", true) |
| 65 | if !strings.Contains(w.Body.String(), `"ready":true`) { |
| 66 | t.Fatal("protection not read back") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestHostGitWriteFailureRemainsBlocked(t *testing.T) { |
| 71 | path := isolatedGitConfig(t) |
| 72 | if err := os.Remove(path); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if err := os.Mkdir(path, 0700); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | s := testServer(t) |
| 79 | w := request(s, "POST", "/api/host/git", s.origin, `{"confirm":true}`, true) |
| 80 | if w.Code == 200 { |
| 81 | t.Fatal("unwritable Git settings accepted") |
| 82 | } |
| 83 | w = request(s, "GET", "/api/host/git", "", "", true) |
| 84 | if strings.Contains(w.Body.String(), `"ready":true`) { |
| 85 | t.Fatal("failed write allowed startup") |
| 86 | } |
| 87 | } |