oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/equipo"
10 "github.com/go-chi/chi/v5"
11)
12
13func TestRunnerAppCSRF(t *testing.T) {
14 f := runnerFixtureNew(t, false)
15 token := runnerBearerFor(t, f)
16 cfg := testConfig(testOrigin)
17 cfg.ExecutionEnabled = true
18 app, err := equipo.AppWithExecution(f.d, cfg, &capturingMailer{}, nil, f.x)
19 executionMust(t, err)
20 request := func(path, body, bearer, cookie, origin string) *httptest.ResponseRecorder {
21 t.Helper()
22 req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
23 req.Header.Set("Content-Type", "application/json")
24 if bearer != "" {
25 req.Header.Set("Authorization", "Bearer "+bearer)
26 }
27 if cookie != "" {
28 req.Header.Set("Cookie", cookie)
29 }
30 if origin != "" {
31 req.Header.Set("Origin", origin)
32 }
33 response := httptest.NewRecorder()
34 app.ServeHTTP(response, req)
35 return response
36 }
37 executionMust(t, chi.Walk(f.s.RunnerHandler().(chi.Routes), func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error {
38 if method != http.MethodPost {
39 return nil
40 }
41 t.Run(route, func(t *testing.T) {
42 response := request("/api/runner/v2"+route, "{", token, "", "")
43 if response.Code != http.StatusUnprocessableEntity || !strings.Contains(response.Body.String(), "form.invalid") {
44 t.Fatalf("bearer request blocked before runner body validation: %d %s", response.Code, response.Body)
45 }
46 for _, bearer := range []string{"", token} {
47 response = request("/api/runner/v2"+route, "{}", bearer, "rastrillo_session=browser", "")
48 if response.Code != http.StatusForbidden || !strings.Contains(response.Body.String(), "form.forbidden") {
49 t.Fatalf("cookie request bypassed runner authentication: %d %s", response.Code, response.Body)
50 }
51 }
52 })
53 return nil
54 }))
55 t.Run("heartbeat", func(t *testing.T) {
56 response := request("/api/runner/v2/heartbeat", `{"version":2,"request_id":"csrf-heartbeat"}`, token, "", "")
57 if response.Code != http.StatusOK {
58 t.Fatalf("heartbeat: %d %s", response.Code, response.Body)
59 }
60 })
61 for _, path := range []string{"/projects", "/auth/begin", "/runners/enrol", "/api/runner/v2/poll/extra"} {
62 t.Run("browser"+path, func(t *testing.T) {
63 response := request(path, "{}", "", "rastrillo_session=browser", "https://other.example")
64 if response.Code != http.StatusForbidden || !strings.Contains(response.Body.String(), "cross-origin form submission refused") {
65 t.Fatalf("cross-origin form accepted: %d %s", response.Code, response.Body)
66 }
67 })
68 }
69 for _, path := range []string{"/api/enrolments", "/api/enrolments/token"} {
70 t.Run(path, func(t *testing.T) {
71 response := request(path, "{", "", "", "")
72 if response.Code != http.StatusUnprocessableEntity {
73 t.Fatalf("device client blocked: %d %s", response.Code, response.Body)
74 }
75 })
76 }
77}
78