handleSearch runs the requested search query against the search handler, and if the results are within the domain allowed by the master query, forwards them back to the client.
(w http.ResponseWriter, r *http.Request)
| 185 | // if the results are within the domain allowed by the master query, forwards them |
| 186 | // back to the client. |
| 187 | func (a *Handler) handleSearch(w http.ResponseWriter, r *http.Request) { |
| 188 | if r.Method != http.MethodPost { |
| 189 | camhttputil.BadRequestError(w, "Invalid method %s", r.Method) |
| 190 | return |
| 191 | } |
| 192 | if a.sh == nil { |
| 193 | http.Error(w, "app proxy has no search handler", 500) |
| 194 | return |
| 195 | } |
| 196 | a.masterQueryMu.RLock() |
| 197 | if a.masterQuery == nil { |
| 198 | http.Error(w, "search is not allowed", http.StatusForbidden) |
| 199 | a.masterQueryMu.RUnlock() |
| 200 | return |
| 201 | } |
| 202 | a.masterQueryMu.RUnlock() |
| 203 | var sq search.SearchQuery |
| 204 | if err := sq.FromHTTP(r); err != nil { |
| 205 | camhttputil.ServeJSONError(w, err) |
| 206 | return |
| 207 | } |
| 208 | sr, err := a.sh.Query(r.Context(), &sq) |
| 209 | if err != nil { |
| 210 | camhttputil.ServeJSONError(w, err) |
| 211 | return |
| 212 | } |
| 213 | // check this search is in the allowed domain |
| 214 | if !a.allowProxySearchResponse(sr) { |
| 215 | // there's a chance our domainBlobs cache is expired so let's |
| 216 | // refresh it and retry, but no more than once per minute. |
| 217 | if err := a.refreshDomainBlobs(); err != nil { |
| 218 | http.Error(w, "search scope is forbidden", http.StatusForbidden) |
| 219 | return |
| 220 | } |
| 221 | if !a.allowProxySearchResponse(sr) { |
| 222 | http.Error(w, "search scope is forbidden", http.StatusForbidden) |
| 223 | return |
| 224 | } |
| 225 | } |
| 226 | camhttputil.ReturnJSON(w, sr) |
| 227 | } |
| 228 | |
| 229 | // allowProxySearchResponse checks whether the blobs in sr are within the domain |
| 230 | // defined by the masterQuery, and hence if the client is allowed to get that |
no test coverage detected