|
| 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 runSynapseModule(t *testing.T, status int, body string) *modules.Result { |
| 14 | + t.Helper() |
| 15 | + def, err := modules.ParseYAMLModule("../../modules/recon/synapse-federation-version-exposure.yaml") |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("parse synapse module: %v", err) |
| 18 | + } |
| 19 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *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 synapse module: %v", err) |
| 31 | + } |
| 32 | + return res |
| 33 | +} |
| 34 | + |
| 35 | +func synapseExtract(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 TestSynapseFederationVersionExposureModule(t *testing.T) { |
| 45 | + // shape from element-hq/synapse FederationVersionServlet.on_GET, the |
| 46 | + // matrix server-server federation version endpoint (unauthenticated by spec) |
| 47 | + synapseBody := `{"server": {"name": "Synapse", "version": "1.99.0"}}` |
| 48 | + |
| 49 | + t.Run("an exposed synapse federation version endpoint is flagged and versioned", func(t *testing.T) { |
| 50 | + res := runSynapseModule(t, 200, synapseBody) |
| 51 | + if len(res.Findings) == 0 { |
| 52 | + t.Fatal("expected a synapse finding") |
| 53 | + } |
| 54 | + if v := synapseExtract(res, "synapse_version"); v != "1.99.0" { |
| 55 | + t.Errorf("synapse_version=%q, want 1.99.0", v) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("a dendrite homeserver on the same federation endpoint is not flagged as synapse", func(t *testing.T) { |
| 60 | + // dendrite implements the same open matrix federation version api |
| 61 | + // with the same json shape but its own implementation name |
| 62 | + body := `{"server": {"name": "Dendrite", "version": "0.13.7"}}` |
| 63 | + if res := runSynapseModule(t, 200, body); len(res.Findings) > 0 { |
| 64 | + t.Errorf("a dendrite homeserver should not match synapse, got %d findings", len(res.Findings)) |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("a conduit homeserver on the same federation endpoint is not flagged as synapse", func(t *testing.T) { |
| 69 | + body := `{"server": {"name": "Conduit", "version": "0.9.0"}}` |
| 70 | + if res := runSynapseModule(t, 200, body); len(res.Findings) > 0 { |
| 71 | + t.Errorf("a conduit homeserver should not match synapse, got %d findings", len(res.Findings)) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("version still extracts when json keys are reordered", func(t *testing.T) { |
| 76 | + // json object member order is not significant; the extractor must not |
| 77 | + // depend on name preceding version |
| 78 | + body := `{"server": {"version": "1.99.0", "name": "Synapse"}}` |
| 79 | + res := runSynapseModule(t, 200, body) |
| 80 | + if len(res.Findings) == 0 { |
| 81 | + t.Fatal("expected a synapse finding on reordered keys") |
| 82 | + } |
| 83 | + if v := synapseExtract(res, "synapse_version"); v != "1.99.0" { |
| 84 | + t.Errorf("synapse_version=%q on reordered keys, want 1.99.0", v) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("a 404 is not a leak", func(t *testing.T) { |
| 89 | + if res := runSynapseModule(t, 404, "not found"); len(res.Findings) > 0 { |
| 90 | + t.Errorf("a 404 should not match, got %d findings", len(res.Findings)) |
| 91 | + } |
| 92 | + }) |
| 93 | +} |
0 commit comments