| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 10 | ) |
| 11 | |
| 12 | func TestApprovedBeyondProfilePublisher(t *testing.T) { |
| 13 | f := newFixture(t) |
| 14 | approval := team.ProfileApproval{QuestionID: "q", RequestID: "7", ItemID: "item", TurnID: "turn", SessionID: "session", Person: "owner@example.com", Label: "approved beyond profile", Request: json.RawMessage(`{"command":"printf hello","cwd":"/workspace"}`)} |
| 15 | f.auth.Publication.Isolation.ProfileApprovals = []team.ProfileApproval{approval} |
| 16 | for _, label := range []string{"unconfined", "unconfined; approved beyond profile"} { |
| 17 | desc := team.PublicationDescription{Version: 1, Label: label, Isolation: f.auth.Publication.Isolation} |
| 18 | if !strings.Contains(label, approval.Label) { |
| 19 | desc.Isolation.ProfileApprovals = nil |
| 20 | } |
| 21 | description, _ := json.Marshal(desc) |
| 22 | f.auth.Publication.Description = string(description) |
| 23 | f.auth.Publication.DescriptionHash = Digest([]byte(f.auth.Publication.Description)) |
| 24 | raw, _ := json.Marshal(f.auth.Publication) |
| 25 | raw, hash, _ := Canonical(raw) |
| 26 | f.auth.Ticket.Dispatch.Payload, f.auth.Ticket.Dispatch.Hash = raw, hash |
| 27 | err := f.p.validate(f.auth, f.auth.Ticket.Dispatch) |
| 28 | if (err == nil) != strings.Contains(label, approval.Label) { |
| 29 | t.Fatal("publisher lost profile label guard", label, err) |
| 30 | } |
| 31 | } |
| 32 | _, err := f.p.Execute(context.Background(), f.auth.Ticket.Dispatch) |
| 33 | must(t, err) |
| 34 | if !strings.Contains(f.desc.value.Body, approval.Label) { |
| 35 | t.Fatal("published branch description lost label") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestPublisherChecksDecodedProfileApproval(t *testing.T) { |
| 40 | f := newFixture(t) |
| 41 | approval := team.ProfileApproval{QuestionID: "q", RequestID: "7", ItemID: "item", TurnID: "turn", SessionID: "session", Person: "owner@example.com", Label: "approved beyond profile", Request: json.RawMessage(`{"command":"printf hello"}`)} |
| 42 | f.auth.Publication.Isolation.ProfileApprovals = []team.ProfileApproval{approval} |
| 43 | desc := team.PublicationDescription{Version: 1, Label: "unconfined", Isolation: f.auth.Publication.Isolation} |
| 44 | for _, valid := range []bool{false, true} { |
| 45 | if !valid { |
| 46 | desc.Isolation.ProfileApprovals = nil |
| 47 | } else { |
| 48 | desc.Isolation = f.auth.Publication.Isolation |
| 49 | } |
| 50 | raw, _ := json.Marshal(desc) |
| 51 | body := string(raw) |
| 52 | if !valid { |
| 53 | body = strings.Replace(body, `"label":"unconfined"`, `"label":"unconfined; approved beyond profile"`, 1) |
| 54 | } else { |
| 55 | body = strings.ReplaceAll(body, "approved beyond profile", `approved beyond pro\u0066ile`) |
| 56 | } |
| 57 | f.auth.Publication.Description = body |
| 58 | f.auth.Publication.DescriptionHash = Digest([]byte(body)) |
| 59 | payload, _ := json.Marshal(f.auth.Publication) |
| 60 | payload, hash, _ := Canonical(payload) |
| 61 | f.auth.Ticket.Dispatch.Payload, f.auth.Ticket.Dispatch.Hash = payload, hash |
| 62 | err := f.p.validate(f.auth, f.auth.Ticket.Dispatch) |
| 63 | if (err == nil) != valid { |
| 64 | t.Fatalf("decoded approval valid=%v: %v", valid, err) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |