|
| 1 | +package modules_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/vmfunc/sif/internal/modules" |
| 11 | +) |
| 12 | + |
| 13 | +func runMLPipelineModule(t *testing.T, file string, status int, body string) *modules.Result { |
| 14 | + t.Helper() |
| 15 | + def, err := modules.ParseYAMLModule(file) |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("parse %s: %v", file, err) |
| 18 | + } |
| 19 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | + w.WriteHeader(status) |
| 21 | + _, _ = w.Write([]byte(body)) |
| 22 | + })) |
| 23 | + defer srv.Close() |
| 24 | + |
| 25 | + res, err := modules.ExecuteHTTPModule(context.Background(), srv.URL, def, modules.Options{ |
| 26 | + Timeout: 5 * time.Second, |
| 27 | + Threads: 2, |
| 28 | + }) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("execute %s: %v", file, err) |
| 31 | + } |
| 32 | + return res |
| 33 | +} |
| 34 | + |
| 35 | +func mlPipelineExtract(res *modules.Result, key string) string { |
| 36 | + for _, f := range res.Findings { |
| 37 | + if v := f.Extracted[key]; v != "" { |
| 38 | + return v |
| 39 | + } |
| 40 | + } |
| 41 | + return "" |
| 42 | +} |
| 43 | + |
| 44 | +func TestMLPipelineExposureModules(t *testing.T) { |
| 45 | + const kubeflow = "../../modules/recon/kubeflow-pipelines-exposure.yaml" |
| 46 | + const metaflow = "../../modules/recon/metaflow-metadata-exposure.yaml" |
| 47 | + |
| 48 | + t.Run("a kubeflow pipelines list is flagged with its pipeline count", func(t *testing.T) { |
| 49 | + body := `{"pipelines":[{"id":"a1b2c3","created_at":"2026-01-01T00:00:00Z","name":"training-pipeline",` + |
| 50 | + `"parameters":[],"resource_references":[]}],"total_size":1,"next_page_token":""}` |
| 51 | + res := runMLPipelineModule(t, kubeflow, 200, body) |
| 52 | + if len(res.Findings) == 0 { |
| 53 | + t.Fatal("expected a kubeflow finding") |
| 54 | + } |
| 55 | + if v := mlPipelineExtract(res, "kubeflow_pipeline_count"); v != "1" { |
| 56 | + t.Errorf("kubeflow_pipeline_count=%q, want 1", v) |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("a generic pipelines-word page is not flagged as kubeflow", func(t *testing.T) { |
| 61 | + // shares the bare word "pipelines" (a CI product's dashboard prose) but not the |
| 62 | + // kubeflow pagination shape. |
| 63 | + body := `{"message":"see your pipelines dashboard","pipelines_url":"https://ci.example.com/pipelines"}` |
| 64 | + if res := runMLPipelineModule(t, kubeflow, 200, body); len(res.Findings) > 0 { |
| 65 | + t.Errorf("a page merely mentioning pipelines should not match, got %d findings", len(res.Findings)) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("a metaflow flows list is flagged with a flow id", func(t *testing.T) { |
| 70 | + body := `[{"flow_id":"TrainingFlow","user_name":"data-eng","ts_epoch":1735689600000,` + |
| 71 | + `"tags":null,"system_tags":["production"]}]` |
| 72 | + res := runMLPipelineModule(t, metaflow, 200, body) |
| 73 | + if len(res.Findings) == 0 { |
| 74 | + t.Fatal("expected a metaflow finding") |
| 75 | + } |
| 76 | + if v := mlPipelineExtract(res, "metaflow_flow_id"); v != "TrainingFlow" { |
| 77 | + t.Errorf("metaflow_flow_id=%q, want TrainingFlow", v) |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("a generic user/timestamp array is not flagged as metaflow", func(t *testing.T) { |
| 82 | + body := `[{"user_name":"alice","ts_epoch":1735689600000,"role":"admin"}]` |
| 83 | + if res := runMLPipelineModule(t, metaflow, 200, body); len(res.Findings) > 0 { |
| 84 | + t.Errorf("a body missing flow_id should not match, got %d findings", len(res.Findings)) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("a plain 200 body is not a leak", func(t *testing.T) { |
| 89 | + for _, file := range []string{kubeflow, metaflow} { |
| 90 | + if res := runMLPipelineModule(t, file, 200, "ok"); len(res.Findings) > 0 { |
| 91 | + t.Errorf("%s: a plain 200 body should not match, got %d findings", file, len(res.Findings)) |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("a 401 is not a leak", func(t *testing.T) { |
| 97 | + for _, file := range []string{kubeflow, metaflow} { |
| 98 | + if res := runMLPipelineModule(t, file, 401, `{"error":"unauthorized"}`); len(res.Findings) > 0 { |
| 99 | + t.Errorf("%s: a 401 should not match, got %d findings", file, len(res.Findings)) |
| 100 | + } |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("a 404 is not a leak", func(t *testing.T) { |
| 105 | + for _, file := range []string{kubeflow, metaflow} { |
| 106 | + if res := runMLPipelineModule(t, file, 404, "not found"); len(res.Findings) > 0 { |
| 107 | + t.Errorf("%s: a 404 should not match, got %d findings", file, len(res.Findings)) |
| 108 | + } |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + t.Run("a different product's version api is not flagged", func(t *testing.T) { |
| 113 | + // argocd's /api/version shape: shares no kubeflow/metaflow anchors. |
| 114 | + body := `{"Version":"v2.9.3","KustomizeVersion":"v5.3.0","HelmVersion":"v3.14.0"}` |
| 115 | + for _, file := range []string{kubeflow, metaflow} { |
| 116 | + if res := runMLPipelineModule(t, file, 200, body); len(res.Findings) > 0 { |
| 117 | + t.Errorf("%s: a different product's version body should not match, got %d findings", file, len(res.Findings)) |
| 118 | + } |
| 119 | + } |
| 120 | + }) |
| 121 | +} |
0 commit comments