Skip to content

chore: Rbac errors should be returned, and not hidden behind 404 #7122

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 5 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 3 additions & 5 deletions coderd/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package coderd
import (
"context"
"crypto/sha256"
"database/sql"
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -167,7 +165,7 @@ func (api *API) apiKeyByID(rw http.ResponseWriter, r *http.Request) {

keyID := chi.URLParam(r, "keyid")
key, err := api.Database.GetAPIKeyByID(ctx, keyID)
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down Expand Up @@ -202,7 +200,7 @@ func (api *API) apiKeyByName(rw http.ResponseWriter, r *http.Request) {
TokenName: tokenName,
UserID: user.ID,
})
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down Expand Up @@ -323,7 +321,7 @@ func (api *API) deleteAPIKey(rw http.ResponseWriter, r *http.Request) {
defer commitAudit()

err = api.Database.DeleteAPIKeyByID(ctx, keyID)
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
4 changes: 2 additions & 2 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (e NotAuthorizedError) Error() string {

// Unwrap will always unwrap to a sql.ErrNoRows so the API returns a 404.
// So 'errors.Is(err, sql.ErrNoRows)' will always be true.
func (NotAuthorizedError) Unwrap() error {
return sql.ErrNoRows
func (e NotAuthorizedError) Unwrap() error {
return e.Err
}

func IsNotAuthorizedError(err error) bool {
Expand Down
2 changes: 0 additions & 2 deletions coderd/database/dbauthz/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dbauthz_test

import (
"context"
"database/sql"
"fmt"
"reflect"
"sort"
Expand Down Expand Up @@ -219,7 +218,6 @@ func (s *MethodTestSuite) NotAuthorizedErrorTest(ctx context.Context, az *coderd
if err != nil || !hasEmptySliceResponse(resp) {
s.ErrorContainsf(err, "unauthorized", "error string should have a good message")
s.Errorf(err, "method should an error with disallow authz")
s.ErrorIsf(err, sql.ErrNoRows, "error should match sql.ErrNoRows")
s.ErrorAs(err, &dbauthz.NotAuthorizedError{}, "error should be NotAuthorizedError")
}
})
Expand Down
2 changes: 1 addition & 1 deletion coderd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (api *API) fileByID(rw http.ResponseWriter, r *http.Request) {
}

file, err := api.Database.GetFileByID(ctx, id)
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
13 changes: 13 additions & 0 deletions coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpapi
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"flag"
Expand All @@ -15,6 +16,8 @@ import (
"github.com/go-playground/validator/v10"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/coderd/tracing"
"github.com/coder/coder/codersdk"
)
Expand Down Expand Up @@ -80,6 +83,16 @@ func init() {
}
}

// Is404Error returns true if the given error should return a 404 status code.
// Both actual 404s and unauthorized errors should return 404s to not leak
// information about the existence of resources.
func Is404Error(err error) bool {
if err == nil {
return false
}
return xerrors.Is(err, sql.ErrNoRows) || dbauthz.IsNotAuthorizedError(err) || rbac.IsUnauthorizedError(err)
}

// Convenience error functions don't take contexts since their responses are
// static, it doesn't make much sense to trace them.

Expand Down
7 changes: 2 additions & 5 deletions coderd/httpmw/groupparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package httpmw

import (
"context"
"database/sql"
"errors"
"net/http"

"github.com/go-chi/chi/v5"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
Expand Down Expand Up @@ -45,7 +42,7 @@ func ExtractGroupByNameParam(db database.Store) func(http.Handler) http.Handler
OrganizationID: org.ID,
Name: name,
})
if xerrors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down Expand Up @@ -73,7 +70,7 @@ func ExtractGroupParam(db database.Store) func(http.Handler) http.Handler {
}

group, err := db.GetGroupByID(r.Context(), groupID)
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
6 changes: 2 additions & 4 deletions coderd/httpmw/organizationparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package httpmw

import (
"context"
"database/sql"
"errors"
"net/http"

"github.com/coder/coder/coderd/database"
Expand Down Expand Up @@ -47,7 +45,7 @@ func ExtractOrganizationParam(db database.Store) func(http.Handler) http.Handler
}

organization, err := db.GetOrganizationByID(ctx, orgID)
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down Expand Up @@ -77,7 +75,7 @@ func ExtractOrganizationMemberParam(db database.Store) func(http.Handler) http.H
OrganizationID: organization.ID,
UserID: user.ID,
})
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
4 changes: 1 addition & 3 deletions coderd/httpmw/templateparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package httpmw

import (
"context"
"database/sql"
"errors"
"net/http"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -34,7 +32,7 @@ func ExtractTemplateParam(db database.Store) func(http.Handler) http.Handler {
return
}
template, err := db.GetTemplateByID(r.Context(), templateID)
if errors.Is(err, sql.ErrNoRows) || (err == nil && template.Deleted) {
if httpapi.Is404Error(err) || (err == nil && template.Deleted) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
3 changes: 1 addition & 2 deletions coderd/httpmw/templateversionparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package httpmw
import (
"context"
"database/sql"
"errors"
"net/http"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -35,7 +34,7 @@ func ExtractTemplateVersionParam(db database.Store) func(http.Handler) http.Hand
return
}
templateVersion, err := db.GetTemplateVersionByID(ctx, templateVersionID)
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
5 changes: 1 addition & 4 deletions coderd/httpmw/userparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package httpmw

import (
"context"
"database/sql"
"net/http"

"golang.org/x/xerrors"

"github.com/go-chi/chi/v5"
"github.com/google/uuid"

Expand Down Expand Up @@ -71,7 +68,7 @@ func ExtractUserParam(db database.Store, redirectToLoginOnMe bool) func(http.Han
}
//nolint:gocritic // System needs to be able to get user from param.
user, err = db.GetUserByID(dbauthz.AsSystemRestricted(ctx), apiKey.UserID)
if xerrors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
4 changes: 1 addition & 3 deletions coderd/httpmw/workspacebuildparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package httpmw

import (
"context"
"database/sql"
"errors"
"net/http"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -34,7 +32,7 @@ func ExtractWorkspaceBuildParam(db database.Store) func(http.Handler) http.Handl
return
}
workspaceBuild, err := db.GetWorkspaceBuildByID(ctx, workspaceBuildID)
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
6 changes: 2 additions & 4 deletions coderd/httpmw/workspaceparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package httpmw

import (
"context"
"database/sql"
"errors"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -37,7 +35,7 @@ func ExtractWorkspaceParam(db database.Store) func(http.Handler) http.Handler {
return
}
workspace, err := db.GetWorkspaceByID(ctx, workspaceID)
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down Expand Up @@ -74,7 +72,7 @@ func ExtractWorkspaceAndAgentParam(db database.Store) func(http.Handler) http.Ha
Name: workspaceParts[0],
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (api *API) deleteParameter(rw http.ResponseWriter, r *http.Request) {
ScopeID: scopeID,
Name: name,
})
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/provisionerjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (api *API) provisionerJobResources(rw http.ResponseWriter, r *http.Request,
}

// nolint:gocritic // GetWorkspaceAppsByAgentIDs is a system function.
apps, err := api.Database.GetWorkspaceAppsByAgentIDs(ctx, resourceAgentIDs)
apps, err := api.Database.GetWorkspaceAppsByAgentIDs(dbauthz.AsSystemRestricted(ctx), resourceAgentIDs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops, I missed this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sql.ErrNoRows hid it ☹️

if errors.Is(err, sql.ErrNoRows) {
err = nil
}
Expand Down
11 changes: 1 addition & 10 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
Name: templateName,
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
Expand All @@ -419,11 +419,6 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
return
}

if !api.Authorize(r, rbac.ActionRead, template) {
httpapi.ResourceNotFound(rw)
return
}

createdByNameMap, err := getCreatedByNamesByTemplateIDs(ctx, api.Database, []database.Template{template})
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Expand Down Expand Up @@ -583,10 +578,6 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
func (api *API) templateDAUs(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
template := httpmw.TemplateParam(r)
if !api.Authorize(r, rbac.ActionRead, template) {
httpapi.ResourceNotFound(rw)
return
}

resp, _ := api.metricsCache.TemplateDAUs(template.ID)
if resp == nil || resp.Entries == nil {
Expand Down
Loading