Skip to content

Commit a92f132

Browse files
committed
feat: Return more 404s vs 403s
1 parent 1470149 commit a92f132

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

coderd/files.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ func (api *API) fileByHash(rw http.ResponseWriter, r *http.Request) {
8686
}
8787
file, err := api.Database.GetFileByHash(r.Context(), hash)
8888
if errors.Is(err, sql.ErrNoRows) {
89-
httpapi.Forbidden(rw)
89+
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
90+
Message: fmt.Sprintf("File %q not found.", hash),
91+
})
9092
return
9193
}
9294
if err != nil {

coderd/parameters.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ func (api *API) parameterRBACResource(rw http.ResponseWriter, r *http.Request, s
223223
// Write error payload to rw if we cannot find the resource for the scope
224224
if err != nil {
225225
if xerrors.Is(err, sql.ErrNoRows) {
226-
httpapi.Forbidden(rw)
226+
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
227+
Message: fmt.Sprintf("Scope %q resource %q not found.", scope, scopeID),
228+
})
227229
} else {
228230
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
229231
Message: err.Error(),

coderd/templateversions.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ func (api *API) fetchTemplateVersionDryRunJob(rw http.ResponseWriter, r *http.Re
351351

352352
job, err := api.Database.GetProvisionerJobByID(r.Context(), jobUUID)
353353
if xerrors.Is(err, sql.ErrNoRows) {
354-
httpapi.Forbidden(rw)
354+
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
355+
Message: fmt.Sprintf("Provisioner job %q not found.", jobUUID),
356+
})
355357
return database.ProvisionerJob{}, false
356358
}
357359
if err != nil {

coderd/users.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,13 +604,16 @@ func (api *API) organizationByUserAndName(rw http.ResponseWriter, r *http.Reques
604604
organizationName := chi.URLParam(r, "organizationname")
605605
organization, err := api.Database.GetOrganizationByName(r.Context(), organizationName)
606606
if errors.Is(err, sql.ErrNoRows) {
607-
// Return unauthorized rather than a 404 to not leak if the organization
608-
// exists.
609-
httpapi.Forbidden(rw)
607+
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
608+
Message: fmt.Sprintf("Organization %q not found.", organizationName),
609+
})
610610
return
611611
}
612612
if err != nil {
613-
httpapi.Forbidden(rw)
613+
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
614+
Message: "Internal error fetching organization.",
615+
Detail: err.Error(),
616+
})
614617
return
615618
}
616619

coderd/workspaces.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
192192
})
193193
}
194194
if errors.Is(err, sql.ErrNoRows) {
195-
// Do not leak information if the workspace exists or not
196-
httpapi.Forbidden(rw)
195+
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
196+
Message: fmt.Sprintf("Workspace %q not found.", workspaceName),
197+
})
197198
return
198199
}
199200
if err != nil {

0 commit comments

Comments
 (0)