Skip to content

feat: Return more 404s vs 403s #2194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use %q over %s
  • Loading branch information
Emyrk committed Jun 10, 2022
commit 8f90af4acbaa78541291a77e1e4927fe0135847e
1 change: 1 addition & 0 deletions coderd/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func AuthorizeFilter[O rbac.Objecter](api *API, r *http.Request, action rbac.Act
// Eg:
// if !api.Authorize(...) {
// httpapi.Forbidden(rw)
// return
// }
func (api *API) Authorize(r *http.Request, action rbac.Action, object rbac.Objecter) bool {
roles := httpmw.AuthorizationUserRoles(r)
Expand Down
4 changes: 2 additions & 2 deletions coderd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (api *API) fileByHash(rw http.ResponseWriter, r *http.Request) {
}
file, err := api.Database.GetFileByHash(r.Context(), hash)
if errors.Is(err, sql.ErrNoRows) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("File %s", hash))
httpapi.ResourceNotFound(rw, fmt.Sprintf("File %q", hash))
return
}
if err != nil {
Expand All @@ -101,7 +101,7 @@ func (api *API) fileByHash(rw http.ResponseWriter, r *http.Request) {
if !api.Authorize(r, rbac.ActionRead,
rbac.ResourceFile.WithOwner(file.CreatedBy.String()).WithID(file.Hash)) {
// Return 404 to not leak the file exists
httpapi.ResourceNotFound(rw, fmt.Sprintf("File %s", hash))
httpapi.ResourceNotFound(rw, fmt.Sprintf("File %q", hash))
return
}

Expand Down
2 changes: 1 addition & 1 deletion coderd/httpmw/templateparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ExtractTemplateParam(db database.Store) func(http.Handler) http.Handler {
}

if template.Deleted {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %s", templateID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %q", templateID))
return
}

Expand Down
2 changes: 1 addition & 1 deletion coderd/httpmw/templateversionparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ExtractTemplateVersionParam(db database.Store) func(http.Handler) http.Hand
}
templateVersion, err := db.GetTemplateVersionByID(r.Context(), templateVersionID)
if errors.Is(err, sql.ErrNoRows) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersionID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersionID))
return
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion coderd/httpmw/workspaceparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ExtractWorkspaceParam(db database.Store) func(http.Handler) http.Handler {
}
workspace, err := db.GetWorkspaceByID(r.Context(), workspaceID)
if errors.Is(err, sql.ErrNoRows) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspaceID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspaceID))
return
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion coderd/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestOrganizationByUserAndName(t *testing.T) {
_, err = other.OrganizationByName(context.Background(), codersdk.Me, org.Name)
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusForbidden, apiErr.StatusCode())
require.Equal(t, http.StatusNotFound, apiErr.StatusCode())
})

t.Run("Valid", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions coderd/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (api *API) deleteParameter(rw http.ResponseWriter, r *http.Request) {
}
// A deleted param is still updating the underlying resource for the scope.
if !api.Authorize(r, rbac.ActionUpdate, obj) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Parameter %s with scope %s", scopeID, scope))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Parameter %q with scope %q", scopeID, scope))
return
}

Expand All @@ -135,7 +135,7 @@ func (api *API) deleteParameter(rw http.ResponseWriter, r *http.Request) {
Name: name,
})
if errors.Is(err, sql.ErrNoRows) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Parameter %s with scope %s", scopeID, scope))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Parameter %q with scope %q", scopeID, scope))
return
}
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
template := httpmw.TemplateParam(r)

if !api.Authorize(r, rbac.ActionRead, template) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %s", template.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %q", template.ID))
return
}

Expand All @@ -56,7 +56,7 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
template := httpmw.TemplateParam(r)
if !api.Authorize(r, rbac.ActionDelete, template) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %s", template.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %q", template.ID))
return
}

Expand Down Expand Up @@ -273,7 +273,7 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %s in organization %s", templateName, organization.Name))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %q in organization %q", templateName, organization.Name))
return
}

Expand All @@ -285,7 +285,7 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
}

if !api.Authorize(r, rbac.ActionRead, template) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %s in organization %s", templateName, organization.Name))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %q in organization %q", templateName, organization.Name))
return
}

Expand All @@ -312,7 +312,7 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
template := httpmw.TemplateParam(r)
if !api.Authorize(r, rbac.ActionUpdate, template) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %s", template.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %q", template.ID))
return
}

Expand Down
24 changes: 12 additions & 12 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func (api *API) templateVersion(rw http.ResponseWriter, r *http.Request) {
templateVersion := httpmw.TemplateVersionParam(r)
if !api.Authorize(r, rbac.ActionRead, templateVersion) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersion.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersion.ID))
return
}

Expand All @@ -42,7 +42,7 @@ func (api *API) templateVersion(rw http.ResponseWriter, r *http.Request) {
func (api *API) patchCancelTemplateVersion(rw http.ResponseWriter, r *http.Request) {
templateVersion := httpmw.TemplateVersionParam(r)
if !api.Authorize(r, rbac.ActionUpdate, templateVersion) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersion.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersion.ID))
return
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func (api *API) patchCancelTemplateVersion(rw http.ResponseWriter, r *http.Reque
func (api *API) templateVersionSchema(rw http.ResponseWriter, r *http.Request) {
templateVersion := httpmw.TemplateVersionParam(r)
if !api.Authorize(r, rbac.ActionRead, templateVersion) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersion.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersion.ID))
return
}

Expand Down Expand Up @@ -136,7 +136,7 @@ func (api *API) templateVersionParameters(rw http.ResponseWriter, r *http.Reques
apiKey := httpmw.APIKey(r)
templateVersion := httpmw.TemplateVersionParam(r)
if !api.Authorize(r, rbac.ActionRead, templateVersion) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersion.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersion.ID))
return
}

Expand Down Expand Up @@ -180,7 +180,7 @@ func (api *API) postTemplateVersionDryRun(rw http.ResponseWriter, r *http.Reques
apiKey := httpmw.APIKey(r)
templateVersion := httpmw.TemplateVersionParam(r)
if !api.Authorize(r, rbac.ActionRead, templateVersion) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersion.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersion.ID))
return
}
// We use the workspace RBAC check since we don't want to allow dry runs if
Expand Down Expand Up @@ -301,7 +301,7 @@ func (api *API) patchTemplateVersionDryRunCancel(rw http.ResponseWriter, r *http
}
if !api.Authorize(r, rbac.ActionUpdate,
rbac.ResourceWorkspace.InOrg(templateVersion.OrganizationID).WithOwner(job.InitiatorID.String())) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersion.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersion.ID))
return
}

Expand Down Expand Up @@ -344,7 +344,7 @@ func (api *API) fetchTemplateVersionDryRunJob(rw http.ResponseWriter, r *http.Re
jobID = chi.URLParam(r, "jobID")
)
if !api.Authorize(r, rbac.ActionRead, templateVersion) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersion.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersion.ID))
return database.ProvisionerJob{}, false
}

Expand Down Expand Up @@ -403,7 +403,7 @@ func (api *API) fetchTemplateVersionDryRunJob(rw http.ResponseWriter, r *http.Re
func (api *API) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Request) {
template := httpmw.TemplateParam(r)
if !api.Authorize(r, rbac.ActionRead, template) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %s", template.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %q", template.ID))
return
}

Expand Down Expand Up @@ -491,7 +491,7 @@ func (api *API) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Reque
func (api *API) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
template := httpmw.TemplateParam(r)
if !api.Authorize(r, rbac.ActionRead, template) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %s", template.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %q", template.ID))
return
}

Expand Down Expand Up @@ -531,7 +531,7 @@ func (api *API) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
func (api *API) patchActiveTemplateVersion(rw http.ResponseWriter, r *http.Request) {
template := httpmw.TemplateParam(r)
if !api.Authorize(r, rbac.ActionUpdate, template) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %s", template.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template %q", template.ID))
return
}

Expand Down Expand Up @@ -705,7 +705,7 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
func (api *API) templateVersionResources(rw http.ResponseWriter, r *http.Request) {
templateVersion := httpmw.TemplateVersionParam(r)
if !api.Authorize(r, rbac.ActionRead, templateVersion) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersion.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersion.ID))
return
}

Expand All @@ -727,7 +727,7 @@ func (api *API) templateVersionResources(rw http.ResponseWriter, r *http.Request
func (api *API) templateVersionLogs(rw http.ResponseWriter, r *http.Request) {
templateVersion := httpmw.TemplateVersionParam(r)
if !api.Authorize(r, rbac.ActionRead, templateVersion) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %s", templateVersion.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Template version %q", templateVersion.ID))
return
}

Expand Down
16 changes: 8 additions & 8 deletions coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (api *API) workspaceBuild(rw http.ResponseWriter, r *http.Request) {

if !api.Authorize(r, rbac.ActionRead, rbac.ResourceWorkspace.
InOrg(workspace.OrganizationID).WithOwner(workspace.OwnerID.String()).WithID(workspace.ID.String())) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %s", workspaceBuild.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %q", workspaceBuild.ID))
return
}

Expand Down Expand Up @@ -54,7 +54,7 @@ func (api *API) workspaceBuilds(rw http.ResponseWriter, r *http.Request) {
workspace := httpmw.WorkspaceParam(r)

if !api.Authorize(r, rbac.ActionRead, workspace) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspace.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspace.ID))
return
}

Expand Down Expand Up @@ -158,7 +158,7 @@ func (api *API) workspaceBuildByName(rw http.ResponseWriter, r *http.Request) {
workspaceBuildName := chi.URLParam(r, "workspacebuildname")
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceWorkspace.
InOrg(workspace.OrganizationID).WithOwner(workspace.OwnerID.String()).WithID(workspace.ID.String())) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %s", workspaceBuildName))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %q", workspaceBuildName))
return
}

Expand All @@ -167,7 +167,7 @@ func (api *API) workspaceBuildByName(rw http.ResponseWriter, r *http.Request) {
Name: workspaceBuildName,
})
if errors.Is(err, sql.ErrNoRows) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %s", workspaceBuildName))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %q", workspaceBuildName))
return
}
if err != nil {
Expand Down Expand Up @@ -220,7 +220,7 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
}
if !api.Authorize(r, action, rbac.ResourceWorkspace.
InOrg(workspace.OrganizationID).WithOwner(workspace.OwnerID.String()).WithID(workspace.ID.String())) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspace.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspace.ID))
return
}

Expand Down Expand Up @@ -449,7 +449,7 @@ func (api *API) workspaceBuildResources(rw http.ResponseWriter, r *http.Request)

if !api.Authorize(r, rbac.ActionRead, rbac.ResourceWorkspace.
InOrg(workspace.OrganizationID).WithOwner(workspace.OwnerID.String()).WithID(workspace.ID.String())) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %s", workspaceBuild.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %q", workspaceBuild.ID))
return
}

Expand All @@ -476,7 +476,7 @@ func (api *API) workspaceBuildLogs(rw http.ResponseWriter, r *http.Request) {

if !api.Authorize(r, rbac.ActionRead, rbac.ResourceWorkspace.
InOrg(workspace.OrganizationID).WithOwner(workspace.OwnerID.String()).WithID(workspace.ID.String())) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %s", workspaceBuild.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %q", workspaceBuild.ID))
return
}

Expand All @@ -502,7 +502,7 @@ func (api *API) workspaceBuildState(rw http.ResponseWriter, r *http.Request) {
}

if !api.Authorize(r, rbac.ActionRead, workspace) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %s", workspaceBuild.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace build %q", workspaceBuild.ID))
return
}

Expand Down
14 changes: 7 additions & 7 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
func (api *API) workspace(rw http.ResponseWriter, r *http.Request) {
workspace := httpmw.WorkspaceParam(r)
if !api.Authorize(r, rbac.ActionRead, workspace) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspace.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspace.ID))
return
}

Expand Down Expand Up @@ -193,7 +193,7 @@ func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
})
}
if errors.Is(err, sql.ErrNoRows) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspaceName))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspaceName))
return
}
if err != nil {
Expand All @@ -204,7 +204,7 @@ func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
return
}
if !api.Authorize(r, rbac.ActionRead, workspace) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspace.Name))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspace.Name))
return
}

Expand Down Expand Up @@ -482,7 +482,7 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
func (api *API) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) {
workspace := httpmw.WorkspaceParam(r)
if !api.Authorize(r, rbac.ActionUpdate, workspace) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspace.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspace.ID))
return
}

Expand Down Expand Up @@ -525,7 +525,7 @@ func (api *API) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) {
func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) {
workspace := httpmw.WorkspaceParam(r)
if !api.Authorize(r, rbac.ActionUpdate, workspace) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspace.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspace.ID))
return
}

Expand Down Expand Up @@ -574,7 +574,7 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) {
workspace := httpmw.WorkspaceParam(r)

if !api.Authorize(r, rbac.ActionUpdate, workspace) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspace.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspace.ID))
return
}

Expand Down Expand Up @@ -632,7 +632,7 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) {
func (api *API) watchWorkspace(rw http.ResponseWriter, r *http.Request) {
workspace := httpmw.WorkspaceParam(r)
if !api.Authorize(r, rbac.ActionRead, workspace) {
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %s", workspace.ID))
httpapi.ResourceNotFound(rw, fmt.Sprintf("Workspace %q", workspace.ID))
return
}

Expand Down