|
| 1 | +package backplane |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestClient_ListReports_PathEscapesClusterID(t *testing.T) { |
| 15 | + var receivedPath string |
| 16 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | + receivedPath = r.URL.EscapedPath() |
| 18 | + w.Header().Set("Content-Type", "application/json") |
| 19 | + _ = json.NewEncoder(w).Encode(listReportsResponse{Reports: []ReportSummary{}}) |
| 20 | + })) |
| 21 | + defer server.Close() |
| 22 | + |
| 23 | + cfg := &Config{URL: server.URL} |
| 24 | + client := NewClient(cfg, func() (string, error) { return "test-token", nil }) |
| 25 | + |
| 26 | + _, err := client.ListReports(context.Background(), "../../admin/endpoint") |
| 27 | + require.NoError(t, err) |
| 28 | + |
| 29 | + assert.Equal(t, "/backplane/cluster/..%2F..%2Fadmin%2Fendpoint/reports", receivedPath, |
| 30 | + "clusterID with path traversal must be URL-escaped in the request path") |
| 31 | +} |
| 32 | + |
| 33 | +func TestClient_GetReport_PathEscapesClusterIDAndReportID(t *testing.T) { |
| 34 | + var receivedPath string |
| 35 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 36 | + receivedPath = r.URL.EscapedPath() |
| 37 | + w.Header().Set("Content-Type", "application/json") |
| 38 | + _ = json.NewEncoder(w).Encode(Report{ReportID: "rpt-1"}) |
| 39 | + })) |
| 40 | + defer server.Close() |
| 41 | + |
| 42 | + cfg := &Config{URL: server.URL} |
| 43 | + client := NewClient(cfg, func() (string, error) { return "test-token", nil }) |
| 44 | + |
| 45 | + _, err := client.GetReport(context.Background(), "../admin", "../../secret") |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + assert.Equal(t, "/backplane/cluster/..%2Fadmin/reports/..%2F..%2Fsecret", receivedPath, |
| 49 | + "both clusterID and reportID with path traversal must be URL-escaped") |
| 50 | +} |
| 51 | + |
| 52 | +func TestClient_ListReports_NormalClusterIDUnchanged(t *testing.T) { |
| 53 | + var receivedPath string |
| 54 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 55 | + receivedPath = r.URL.Path |
| 56 | + w.Header().Set("Content-Type", "application/json") |
| 57 | + _ = json.NewEncoder(w).Encode(listReportsResponse{Reports: []ReportSummary{}}) |
| 58 | + })) |
| 59 | + defer server.Close() |
| 60 | + |
| 61 | + cfg := &Config{URL: server.URL} |
| 62 | + client := NewClient(cfg, func() (string, error) { return "test-token", nil }) |
| 63 | + |
| 64 | + _, err := client.ListReports(context.Background(), "abc-123-def") |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + assert.Equal(t, "/backplane/cluster/abc-123-def/reports", receivedPath, |
| 68 | + "normal clusterID should pass through unchanged") |
| 69 | +} |
0 commit comments