|
| 1 | +package admin |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// The policy-version interceptor reaches matchesAdminPath through |
| 11 | +// serviceUnavailableMiddleware and its own handler, so it needs the same |
| 12 | +// path-variant coverage the validate interceptor has — a normalization |
| 13 | +// regression in either wiring would otherwise leak variants to the rule |
| 14 | +// evaluator. |
| 15 | +func TestPolicyVersionInterceptorMatchesPathVariants(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + for _, variant := range []string{ |
| 18 | + testPolicyVersionPath + "/", |
| 19 | + "/admin/policy//version", |
| 20 | + "/admin/policy/./version", |
| 21 | + "/admin/policy/x/../version", |
| 22 | + } { |
| 23 | + t.Run(variant, func(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + v := NewPolicyVersioner() |
| 26 | + v.Update(PolicySnapshot{Rules: 1, Source: "startup"}) |
| 27 | + next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { |
| 28 | + t.Fatalf("path variant %q leaked past the policy-version interceptor", variant) |
| 29 | + }) |
| 30 | + handler := NewPolicyVersionInterceptor(PolicyVersionOptions{ |
| 31 | + Path: testPolicyVersionPath, |
| 32 | + Source: v.Snapshot, |
| 33 | + })(next) |
| 34 | + |
| 35 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 36 | + req.URL.Path = variant |
| 37 | + rec := httptest.NewRecorder() |
| 38 | + handler.ServeHTTP(rec, req) |
| 39 | + |
| 40 | + if rec.Code != http.StatusOK { |
| 41 | + t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK) |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// A double-encoded slash (%252f → literal "%2f" in the decoded path) must NOT |
| 48 | +// match the admin path: path.Clean does not percent-decode, so the request |
| 49 | +// falls through to the rule evaluator where default-deny rejects it. This |
| 50 | +// pins the fail-closed behavior for both interceptors. |
| 51 | +func TestAdminInterceptorsIgnoreEncodedSlashVariants(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + t.Run("validate", func(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + passedThrough := false |
| 56 | + next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 57 | + passedThrough = true |
| 58 | + w.WriteHeader(http.StatusForbidden) |
| 59 | + }) |
| 60 | + handler := NewValidateInterceptor(Options{Path: testPath, Validate: newOKValidator()})(next) |
| 61 | + |
| 62 | + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("rules: []")) |
| 63 | + req.URL.Path = "/admin%2fvalidate" |
| 64 | + rec := httptest.NewRecorder() |
| 65 | + handler.ServeHTTP(rec, req) |
| 66 | + |
| 67 | + if !passedThrough { |
| 68 | + t.Fatal("encoded-slash path was handled by the admin layer, want pass-through to rule evaluator") |
| 69 | + } |
| 70 | + }) |
| 71 | + t.Run("policy-version", func(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + passedThrough := false |
| 74 | + next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 75 | + passedThrough = true |
| 76 | + w.WriteHeader(http.StatusForbidden) |
| 77 | + }) |
| 78 | + v := NewPolicyVersioner() |
| 79 | + v.Update(PolicySnapshot{Rules: 1, Source: "startup"}) |
| 80 | + handler := NewPolicyVersionInterceptor(PolicyVersionOptions{ |
| 81 | + Path: testPolicyVersionPath, |
| 82 | + Source: v.Snapshot, |
| 83 | + })(next) |
| 84 | + |
| 85 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 86 | + req.URL.Path = "/admin/policy%2fversion" |
| 87 | + rec := httptest.NewRecorder() |
| 88 | + handler.ServeHTTP(rec, req) |
| 89 | + |
| 90 | + if !passedThrough { |
| 91 | + t.Fatal("encoded-slash path was handled by the admin layer, want pass-through to rule evaluator") |
| 92 | + } |
| 93 | + }) |
| 94 | +} |
0 commit comments