|
| 1 | +// Copyright Project Harbor Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package api |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "net/http" |
| 20 | + "net/http/httptest" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + |
| 25 | + "github.com/goharbor/harbor/src/jobservice/errs" |
| 26 | +) |
| 27 | + |
| 28 | +// routeProbe records which handler a request reached and the job ID it carried. |
| 29 | +type routeProbe struct { |
| 30 | + route string |
| 31 | + jobID string |
| 32 | + called bool |
| 33 | +} |
| 34 | + |
| 35 | +func (p *routeProbe) record(route string) http.HandlerFunc { |
| 36 | + return func(w http.ResponseWriter, req *http.Request) { |
| 37 | + p.route, p.jobID, p.called = route, req.PathValue("job_id"), true |
| 38 | + w.WriteHeader(http.StatusOK) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (p *routeProbe) HandleLaunchJobReq(w http.ResponseWriter, r *http.Request) { |
| 43 | + p.record("launch")(w, r) |
| 44 | +} |
| 45 | +func (p *routeProbe) HandleGetJobReq(w http.ResponseWriter, r *http.Request) { |
| 46 | + p.record("get-job")(w, r) |
| 47 | +} |
| 48 | +func (p *routeProbe) HandleJobActionReq(w http.ResponseWriter, r *http.Request) { |
| 49 | + p.record("job-action")(w, r) |
| 50 | +} |
| 51 | +func (p *routeProbe) HandleCheckStatusReq(w http.ResponseWriter, r *http.Request) { |
| 52 | + p.record("stats")(w, r) |
| 53 | +} |
| 54 | +func (p *routeProbe) HandleJobLogReq(w http.ResponseWriter, r *http.Request) { |
| 55 | + p.record("job-log")(w, r) |
| 56 | +} |
| 57 | +func (p *routeProbe) HandlePeriodicExecutions(w http.ResponseWriter, r *http.Request) { |
| 58 | + p.record("executions")(w, r) |
| 59 | +} |
| 60 | +func (p *routeProbe) HandleGetJobsReq(w http.ResponseWriter, r *http.Request) { |
| 61 | + p.record("get-jobs")(w, r) |
| 62 | +} |
| 63 | +func (p *routeProbe) HandleGetConfigReq(w http.ResponseWriter, r *http.Request) { |
| 64 | + p.record("config")(w, r) |
| 65 | +} |
| 66 | + |
| 67 | +// openAuthenticator lets every request through so the tests see routing alone. |
| 68 | +type openAuthenticator struct{} |
| 69 | + |
| 70 | +func (openAuthenticator) DoAuth(_ *http.Request) error { return nil } |
| 71 | + |
| 72 | +func serve(t *testing.T, method, target string) (*routeProbe, *httptest.ResponseRecorder) { |
| 73 | + t.Helper() |
| 74 | + |
| 75 | + probe := &routeProbe{} |
| 76 | + router := NewBaseRouter(probe, openAuthenticator{}) |
| 77 | + |
| 78 | + rec := httptest.NewRecorder() |
| 79 | + router.ServeHTTP(rec, httptest.NewRequest(method, target, nil)) |
| 80 | + |
| 81 | + return probe, rec |
| 82 | +} |
| 83 | + |
| 84 | +func TestRouterDispatch(t *testing.T) { |
| 85 | + base := fmt.Sprintf("%s/%s", baseRoute, apiVersion) |
| 86 | + |
| 87 | + cases := []struct { |
| 88 | + method string |
| 89 | + path string |
| 90 | + route string |
| 91 | + jobID string |
| 92 | + }{ |
| 93 | + {http.MethodPost, "/jobs", "launch", ""}, |
| 94 | + {http.MethodGet, "/jobs", "get-jobs", ""}, |
| 95 | + {http.MethodGet, "/jobs?page_number=2", "get-jobs", ""}, |
| 96 | + {http.MethodGet, "/jobs/abc123", "get-job", "abc123"}, |
| 97 | + {http.MethodPost, "/jobs/abc123", "job-action", "abc123"}, |
| 98 | + {http.MethodGet, "/jobs/abc123/log", "job-log", "abc123"}, |
| 99 | + {http.MethodGet, "/jobs/abc123/executions", "executions", "abc123"}, |
| 100 | + {http.MethodGet, "/stats", "stats", ""}, |
| 101 | + {http.MethodGet, "/config", "config", ""}, |
| 102 | + } |
| 103 | + |
| 104 | + for _, c := range cases { |
| 105 | + t.Run(c.method+" "+c.path, func(t *testing.T) { |
| 106 | + probe, rec := serve(t, c.method, base+c.path) |
| 107 | + |
| 108 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 109 | + assert.True(t, probe.called, "no handler was reached") |
| 110 | + assert.Equal(t, c.route, probe.route) |
| 111 | + assert.Equal(t, c.jobID, probe.jobID) |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// TestRouterRejectsJobIDsWithSeparators pins the routing contract gorilla/mux |
| 117 | +// enforced by matching the decoded path. ServeMux matches escaped segments, so |
| 118 | +// without withJobID these would reach a handler carrying "a/b" or "..". |
| 119 | +func TestRouterRejectsJobIDsWithSeparators(t *testing.T) { |
| 120 | + base := fmt.Sprintf("%s/%s", baseRoute, apiVersion) |
| 121 | + |
| 122 | + paths := []string{ |
| 123 | + "/jobs/a%2Fb", |
| 124 | + "/jobs/a%2Fb/log", |
| 125 | + "/jobs/a%2Fb/executions", |
| 126 | + "/jobs/%2e%2e", |
| 127 | + "/jobs/%2e%2e/log", |
| 128 | + "/jobs/%2e%2e/executions", |
| 129 | + "/jobs/%2e%2e%2f%2e%2e/log", |
| 130 | + } |
| 131 | + |
| 132 | + for _, p := range paths { |
| 133 | + t.Run(p, func(t *testing.T) { |
| 134 | + probe, rec := serve(t, http.MethodGet, base+p) |
| 135 | + |
| 136 | + assert.Equal(t, http.StatusNotFound, rec.Code) |
| 137 | + assert.False(t, probe.called, "handler was reached with job_id %q", probe.jobID) |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestRouterRejectsUnknownRoutes(t *testing.T) { |
| 143 | + base := fmt.Sprintf("%s/%s", baseRoute, apiVersion) |
| 144 | + |
| 145 | + probe, rec := serve(t, http.MethodGet, base+"/nope") |
| 146 | + |
| 147 | + assert.Equal(t, http.StatusNotFound, rec.Code) |
| 148 | + assert.False(t, probe.called) |
| 149 | +} |
| 150 | + |
| 151 | +// TestRouterRejectsWrongMethod records a deliberate change: gorilla/mux answered |
| 152 | +// 404 when no method matcher fired, ServeMux answers 405 and names the methods. |
| 153 | +func TestRouterRejectsWrongMethod(t *testing.T) { |
| 154 | + base := fmt.Sprintf("%s/%s", baseRoute, apiVersion) |
| 155 | + |
| 156 | + probe, rec := serve(t, http.MethodDelete, base+"/jobs") |
| 157 | + |
| 158 | + assert.Equal(t, http.StatusMethodNotAllowed, rec.Code) |
| 159 | + assert.Equal(t, "GET, HEAD, POST", rec.Header().Get("Allow")) |
| 160 | + assert.False(t, probe.called) |
| 161 | +} |
| 162 | + |
| 163 | +// TestRouterServesHeadOnGetRoutes records the other deliberate change: ServeMux |
| 164 | +// ties HEAD to GET, where gorilla/mux answered 405. /stats is the unauthenticated |
| 165 | +// health endpoint, so this exposes nothing a GET did not already. |
| 166 | +func TestRouterServesHeadOnGetRoutes(t *testing.T) { |
| 167 | + base := fmt.Sprintf("%s/%s", baseRoute, apiVersion) |
| 168 | + |
| 169 | + probe, rec := serve(t, http.MethodHead, base+"/stats") |
| 170 | + |
| 171 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 172 | + assert.True(t, probe.called) |
| 173 | + assert.Equal(t, "stats", probe.route) |
| 174 | +} |
| 175 | + |
| 176 | +// TestErrorResponseIsNotSniffable covers the reflected path value reaching the |
| 177 | +// response body: the type is named and sniffing is turned off, so an error text |
| 178 | +// carrying a job ID cannot be interpreted as HTML. |
| 179 | +func TestErrorResponseIsNotSniffable(t *testing.T) { |
| 180 | + fc := &fakeController{} |
| 181 | + fc.On("GetJob", "<img src=x onerror=alert(1)>"). |
| 182 | + Return(nil, errs.NoObjectFoundError("<img src=x onerror=alert(1)>")) |
| 183 | + |
| 184 | + handler := NewDefaultHandler(fc) |
| 185 | + rec := httptest.NewRecorder() |
| 186 | + req := httptest.NewRequest(http.MethodGet, "/api/v1/jobs/x", nil) |
| 187 | + req.SetPathValue("job_id", "<img src=x onerror=alert(1)>") |
| 188 | + |
| 189 | + handler.HandleGetJobReq(rec, req) |
| 190 | + |
| 191 | + assert.Equal(t, http.StatusNotFound, rec.Code) |
| 192 | + assert.Equal(t, "text/plain; charset=utf-8", rec.Header().Get("Content-Type")) |
| 193 | + assert.Equal(t, "nosniff", rec.Header().Get("X-Content-Type-Options")) |
| 194 | +} |
| 195 | + |
| 196 | +func TestRouterRequiresAuthExceptStats(t *testing.T) { |
| 197 | + base := fmt.Sprintf("%s/%s", baseRoute, apiVersion) |
| 198 | + |
| 199 | + probe := &routeProbe{} |
| 200 | + router := NewBaseRouter(probe, &SecretAuthenticator{}) |
| 201 | + |
| 202 | + rec := httptest.NewRecorder() |
| 203 | + router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, base+"/jobs", nil)) |
| 204 | + assert.Equal(t, http.StatusUnauthorized, rec.Code) |
| 205 | + assert.False(t, probe.called) |
| 206 | + |
| 207 | + rec = httptest.NewRecorder() |
| 208 | + router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, base+"/stats", nil)) |
| 209 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 210 | + assert.True(t, probe.called) |
| 211 | +} |
0 commit comments