Linear: [PLE-94](https://linear.app/codercom/issue/PLE-94). Dogfood Loki holds plaintext credentials because several log call sites emit secrets. This is one of three independent PRs split out of the original masking change. ## Root cause Both "requester is not authorized to access the object" warnings in `coderd/authorize.go` logged `slog.F("object", object)`, the raw `rbac.Objecter`. When the caller passes a database row, every field is dumped, e.g. `oauth2_client_secret` on `database.MCPServerConfig`; any Objecter row with a secret field leaks. `actor_name` was also the whole `rbac.Subject` (expanded roles, groups and email), which duplicated the `roles`, `actor_id` and `scope` fields; it dates to a mechanical rename of `roles.ActorName` in #12816. ## Fix Log `object.RBACObject()` (type, id, owner, org), and log `roles.FriendlyName` (the username, or the name of an internal actor) as `actor_name`. Sibling PRs (independent, disjoint files): - https://github.com/coder/coder/pull/32532 - https://github.com/coder/coder/pull/32533 ## Follow-ups not included - AI Gateway prompt, tool and thought content logged in `aibridgedserver.go` (~511-613) and `aibridge/recorder/log_recorder.go` - `rich_parameter_values` in `runner.go` (~443) - Proxy `response_body` in `aibridgeproxyd.go` (~1102) - Terraform `TF_LOG` output relayed to logs 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
326 lines
10 KiB
Go
326 lines
10 KiB
Go
package coderd_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd"
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/rbac"
|
|
"github.com/coder/coder/v2/coderd/rbac/policy"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestCheckPermissions(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
t.Cleanup(cancel)
|
|
|
|
adminClient := coderdtest.New(t, &coderdtest.Options{
|
|
IncludeProvisionerDaemon: true,
|
|
})
|
|
// Create adminClient, member, and org adminClient
|
|
adminUser := coderdtest.CreateFirstUser(t, adminClient)
|
|
memberClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
|
|
memberUser, err := memberClient.User(ctx, codersdk.Me)
|
|
require.NoError(t, err)
|
|
orgAdminClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID, rbac.ScopedRoleOrgAdmin(adminUser.OrganizationID))
|
|
orgAdminUser, err := orgAdminClient.User(ctx, codersdk.Me)
|
|
require.NoError(t, err)
|
|
|
|
version := coderdtest.CreateTemplateVersion(t, adminClient, adminUser.OrganizationID, nil)
|
|
coderdtest.AwaitTemplateVersionJobCompleted(t, adminClient, version.ID)
|
|
template := coderdtest.CreateTemplate(t, adminClient, adminUser.OrganizationID, version.ID)
|
|
|
|
// With admin, member, and org admin
|
|
const (
|
|
readAllUsers = "read-all-users"
|
|
readOrgWorkspaces = "read-org-workspaces"
|
|
readMyself = "read-myself"
|
|
readOwnWorkspaces = "read-own-workspaces"
|
|
updateSpecificTemplate = "update-specific-template"
|
|
)
|
|
params := map[string]codersdk.AuthorizationCheck{
|
|
readAllUsers: {
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceUser,
|
|
},
|
|
Action: "read",
|
|
},
|
|
readOrgWorkspaces: {
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceWorkspace,
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
|
},
|
|
Action: "read",
|
|
},
|
|
readMyself: {
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceUser,
|
|
OwnerID: "me",
|
|
},
|
|
Action: "read",
|
|
},
|
|
readOwnWorkspaces: {
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceWorkspace,
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
|
OwnerID: "me",
|
|
},
|
|
Action: "read",
|
|
},
|
|
updateSpecificTemplate: {
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceTemplate,
|
|
ResourceID: template.ID.String(),
|
|
},
|
|
Action: "update",
|
|
},
|
|
}
|
|
|
|
testCases := []struct {
|
|
Name string
|
|
Client *codersdk.Client
|
|
UserID uuid.UUID
|
|
Check codersdk.AuthorizationResponse
|
|
}{
|
|
{
|
|
Name: "Admin",
|
|
Client: adminClient,
|
|
UserID: adminUser.UserID,
|
|
Check: map[string]bool{
|
|
readAllUsers: true,
|
|
readOrgWorkspaces: true,
|
|
readMyself: true,
|
|
readOwnWorkspaces: true,
|
|
updateSpecificTemplate: true,
|
|
},
|
|
},
|
|
{
|
|
Name: "OrgAdmin",
|
|
Client: orgAdminClient,
|
|
UserID: orgAdminUser.ID,
|
|
Check: map[string]bool{
|
|
readAllUsers: true,
|
|
readOrgWorkspaces: true,
|
|
readMyself: true,
|
|
readOwnWorkspaces: true,
|
|
updateSpecificTemplate: true,
|
|
},
|
|
},
|
|
{
|
|
Name: "Member",
|
|
Client: memberClient,
|
|
UserID: memberUser.ID,
|
|
Check: map[string]bool{
|
|
readAllUsers: false,
|
|
readOrgWorkspaces: false,
|
|
readMyself: true,
|
|
readOwnWorkspaces: true,
|
|
updateSpecificTemplate: false,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, c := range testCases {
|
|
t.Run("CheckAuthorization/"+c.Name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
t.Cleanup(cancel)
|
|
|
|
resp, err := c.Client.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: params})
|
|
require.NoError(t, err, "check perms")
|
|
require.Equal(t, c.Check, resp)
|
|
})
|
|
}
|
|
|
|
// Enough same-typed checks in one request to push a group past the batching
|
|
// threshold, exercising the grouped partial-evaluation path and the key
|
|
// mapping. Reading org members is allowed for any member; updating them is
|
|
// admin-only, so the two actions must map back to their keys distinctly.
|
|
t.Run("CheckAuthorization/Grouped", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
t.Cleanup(cancel)
|
|
|
|
grouped := make(map[string]codersdk.AuthorizationCheck)
|
|
adminExpected := make(map[string]bool)
|
|
memberExpected := make(map[string]bool)
|
|
for i := 0; i < 55; i++ {
|
|
readKey := fmt.Sprintf("read-members-%d", i)
|
|
grouped[readKey] = codersdk.AuthorizationCheck{
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceOrganizationMember,
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
|
},
|
|
Action: "read",
|
|
}
|
|
adminExpected[readKey] = true
|
|
memberExpected[readKey] = true
|
|
|
|
updateKey := fmt.Sprintf("update-members-%d", i)
|
|
grouped[updateKey] = codersdk.AuthorizationCheck{
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceOrganizationMember,
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
|
},
|
|
Action: "update",
|
|
}
|
|
adminExpected[updateKey] = true
|
|
memberExpected[updateKey] = false
|
|
}
|
|
|
|
adminResp, err := adminClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
|
|
require.NoError(t, err)
|
|
require.Equal(t, adminExpected, map[string]bool(adminResp))
|
|
|
|
memberResp, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
|
|
require.NoError(t, err)
|
|
require.Equal(t, memberExpected, map[string]bool(memberResp))
|
|
})
|
|
|
|
// A member may create a workspace in an org it belongs to, so an
|
|
// any_org=true check is allowed. Repeating it past the batching threshold
|
|
// must not change the answer: AnyOrgOwner objects have no verified partial-
|
|
// evaluation semantics, so they must stay on the per-object path rather than
|
|
// being grouped into rbac.Filter's prepared query, which denies them.
|
|
t.Run("CheckAuthorization/AnyOrg", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
t.Cleanup(cancel)
|
|
|
|
check := codersdk.AuthorizationCheck{
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceWorkspace,
|
|
OwnerID: "me",
|
|
AnyOrgOwner: true,
|
|
},
|
|
Action: "create",
|
|
}
|
|
|
|
// Below the threshold: a single check is evaluated in full and allowed.
|
|
single, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{
|
|
Checks: map[string]codersdk.AuthorizationCheck{"create-any-org": check},
|
|
})
|
|
require.NoError(t, err)
|
|
require.True(t, single["create-any-org"], "single any_org check should be allowed")
|
|
|
|
// The same check repeated past the threshold must stay allowed.
|
|
grouped := make(map[string]codersdk.AuthorizationCheck)
|
|
expected := make(map[string]bool)
|
|
for i := 0; i < 55; i++ {
|
|
key := fmt.Sprintf("create-any-org-%d", i)
|
|
grouped[key] = check
|
|
expected[key] = true
|
|
}
|
|
resp, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
|
|
require.NoError(t, err)
|
|
require.Equal(t, expected, map[string]bool(resp))
|
|
})
|
|
|
|
// A single (read, workspace) group past the batching threshold mixing
|
|
// resource_id checks (concrete workspaces fetched via the maxFetch path)
|
|
// with org-scoped checks (synthetic org-level objects). The member owns the
|
|
// fetched workspace so those keys are allowed, but cannot read all org
|
|
// workspaces so the org-scoped keys are denied. Both object shapes must map
|
|
// back to the correct per-key verdict through the one prepared query.
|
|
t.Run("CheckAuthorization/MixedGroup", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
t.Cleanup(cancel)
|
|
|
|
workspace := coderdtest.CreateWorkspace(t, memberClient, template.ID)
|
|
coderdtest.AwaitWorkspaceBuildJobCompleted(t, memberClient, workspace.LatestBuild.ID)
|
|
|
|
grouped := make(map[string]codersdk.AuthorizationCheck)
|
|
expected := make(map[string]bool)
|
|
// resource_id checks are capped at maxFetch (10); the member owns the
|
|
// workspace, so reading it is allowed.
|
|
for i := 0; i < 10; i++ {
|
|
key := fmt.Sprintf("read-own-workspace-%d", i)
|
|
grouped[key] = codersdk.AuthorizationCheck{
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceWorkspace,
|
|
ResourceID: workspace.ID.String(),
|
|
},
|
|
Action: "read",
|
|
}
|
|
expected[key] = true
|
|
}
|
|
// Org-scoped checks push the (read, workspace) group past the threshold.
|
|
// A member cannot read every workspace in the org, so these are denied.
|
|
for i := 0; i < 45; i++ {
|
|
key := fmt.Sprintf("read-org-workspace-%d", i)
|
|
grouped[key] = codersdk.AuthorizationCheck{
|
|
Object: codersdk.AuthorizationObject{
|
|
ResourceType: codersdk.ResourceWorkspace,
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
|
},
|
|
Action: "read",
|
|
}
|
|
expected[key] = false
|
|
}
|
|
|
|
resp, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
|
|
require.NoError(t, err)
|
|
require.Equal(t, expected, map[string]bool(resp))
|
|
})
|
|
}
|
|
|
|
type secretObject struct {
|
|
Secret string
|
|
}
|
|
|
|
func (secretObject) RBACObject() rbac.Object {
|
|
return rbac.ResourceTemplate.WithID(uuid.New())
|
|
}
|
|
|
|
type denyAuthorizer struct{ rbac.Authorizer }
|
|
|
|
func (denyAuthorizer) Authorize(context.Context, rbac.Subject, policy.Action, rbac.Object) error {
|
|
return rbac.UnauthorizedError{}
|
|
}
|
|
|
|
func TestHTTPAuthorizer_DenialDoesNotLogSecrets(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const (
|
|
secret = "synthetic-oauth2-client-secret" //nolint:gosec // synthetic test value
|
|
email = "synthetic-user@example.invalid"
|
|
)
|
|
sink := testutil.NewFakeSink(t)
|
|
auth := &coderd.HTTPAuthorizer{Authorizer: denyAuthorizer{}, Logger: sink.Logger()}
|
|
ctx := dbauthz.As(context.Background(), rbac.Subject{
|
|
ID: uuid.NewString(),
|
|
FriendlyName: "synthetic",
|
|
Email: email,
|
|
Roles: rbac.RoleIdentifiers{rbac.RoleMember()},
|
|
Scope: rbac.ScopeAll,
|
|
})
|
|
|
|
require.False(t, auth.AuthorizeContext(ctx, policy.ActionRead, secretObject{Secret: secret}))
|
|
|
|
entries := sink.Entries()
|
|
require.Len(t, entries, 1)
|
|
var actorName any
|
|
for _, f := range entries[0].Fields {
|
|
v := fmt.Sprintf("%+v", f.Value)
|
|
require.NotContains(t, v, secret, "field %q leaks the object's secret", f.Name)
|
|
if f.Name == "actor_name" {
|
|
actorName = f.Value
|
|
}
|
|
}
|
|
require.Equal(t, "synthetic", actorName, "actor_name should be the subject's FriendlyName")
|
|
}
|