Title: Custom roles: return every ungrantable permission as validations, and render them in the role editor · Issue #32279 · coder/coder · GitHub
Open Graph Title: Custom roles: return every ungrantable permission as validations, and render them in the role editor · Issue #32279 · coder/coder
X Title: Custom roles: return every ungrantable permission as validations, and render them in the role editor · Issue #32279 · coder/coder
Description: Follow-up to #32263. That issue is about the role editor offering permissions nobody can grant. This one covers what happens when the API rejects permissions: the API should report every invalid permission at once, and the role editor ne...
Open Graph Description: Follow-up to #32263. That issue is about the role editor offering permissions nobody can grant. This one covers what happens when the API rejects permissions: the API should report every invalid pe...
X Description: Follow-up to #32263. That issue is about the role editor offering permissions nobody can grant. This one covers what happens when the API rejects permissions: the API should report every invalid pe...
Opengraph URL: https://github.com/coder/coder/issues/32279
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Custom roles: return every ungrantable permission as validations, and render them in the role editor","articleBody":"Follow-up to #32263. That issue is about the role editor offering permissions nobody can grant. This one covers what happens when the API rejects permissions: the API should report every invalid permission at once, and the role editor needs to render that response properly. Today it can't.\n\n### Background\n\nWhen a custom role contains permissions the caller can't grant, [`customRoleCheck`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/coderd/database/dbauthz/dbauthz.go#L1716-L1726) returns on the **first** failure, and [`postOrgRoles`/`putOrgRoles`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/enterprise/coderd/roles.go#L78-L84) send the raw wrapped Go error back as `detail`:\n\n```json\n{\"message\":\"Failed to update role permissions\",\"detail\":\"org=\\\"\u003corg-uuid>\\\": org: invalid permission for action=\\\"application_connect\\\" type=\\\"workspace_dormant\\\", not allowed to grant this permission\"}\n```\n\nSo the user fixes one permission per save. The UI also shows the same message twice: once in the form's `ErrorAlert` and once in a toast (see the screenshots on #32263).\n\n### Proposal: accumulate and return `validations`\n\n`customRoleCheck` isn't on a hot path. It only runs on custom role inserts and updates (admin API calls, plus system-role reconciliation, which returns before the escalation loop). There's also prior art for collecting errors and returning them as `codersdk.Response.Validations`:\n\n- `acl.Validate` and `patchWorkspaceACL` ([`updatevalidator.go`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/coderd/rbac/acl/updatevalidator.go#L33), [`workspaces.go`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/coderd/workspaces.go#L2589))\n- A typed validation error returned from a lower layer and turned into `Validations` with `errors.As`: `chatModelACLValidationError` ([`exp_chats_model_acl.go`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/coderd/exp_chats_model_acl.go#L82)) and `aiProviderValidationError`\n\nSuggested backend shape:\n\n- `customRoleCheck` collects every escalation failure, across the site, org, member and user loops, into a typed error such as `*dbauthz.InvalidRolePermissionsError{Permissions []rbac.Permission}`. `dbauthz` doesn't import `codersdk`, so the HTTP mapping belongs in the handler.\n- `postOrgRoles`/`putOrgRoles` use `errors.As` and respond `400` with a human-readable `message` and one `ValidationError` per permission. Give each permission a **unique** `field`, e.g. `organization_permissions.workspace_dormant:ssh`.\n- Keep filling `detail` with a joined summary for clients that only read `detail`. Keep the raw wrapped error in server logs only. No org UUIDs in user-facing text.\n\nI tried a rough version of this locally (diff below). The response then looks like this:\n\n```json\n{\n \"message\": \"This role includes permissions you can't grant.\",\n \"validations\": [\n {\"field\": \"organization_permissions.workspace_dormant:application_connect\", \"detail\": \"You can't grant workspace_dormant:application_connect because you don't have that permission.\"},\n {\"field\": \"organization_permissions.workspace_dormant:share\", \"detail\": \"You can't grant workspace_dormant:share because you don't have that permission.\"},\n {\"field\": \"organization_permissions.workspace_dormant:ssh\", \"detail\": \"You can't grant workspace_dormant:ssh because you don't have that permission.\"},\n {\"field\": \"organization_permissions.workspace_dormant:start\", \"detail\": \"You can't grant workspace_dormant:start because you don't have that permission.\"}\n ]\n}\n```\n\n### Frontend problem: the role editor can't render this response\n\nWith the backend change above, I reproduced the frontend behavior on `main` @ `b4f8b6756c6a7ddddeab1664b51dea1237740a6e`:\n\n1. **The in-form error disappears.** [`CreateEditRolePageView.tsx#L115`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/site/src/pages/OrganizationSettingsPage/CustomRolesPage/CreateEditRolePageView.tsx#L115) only renders `ErrorAlert` when `!isApiValidationError(error)`. It expects field helpers to show validation errors instead, but [`getFormHelpers`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/site/src/utils/formUtils.ts#L51-L62) is only wired to `name` and `display_name`. Nothing in the permissions table reads validations, so the form shows **no error at all**, and the selected checkboxes look valid.\n2. **The only signal is a toast with a run-on paragraph.** The toast description comes from `getErrorDetail` → [`getValidationErrorMessage`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/site/src/api/errors.ts#L99-L105), which joins the details with `\\n`. The toast doesn't preserve the newlines, so the four errors run together.\n3. **Shared field names would collapse.** [`mapApiErrorToFieldErrors`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/site/src/api/errors.ts#L54-L65) does `result[error.field] = detail`, so when several entries share a `field`, only the last one survives. That's why the backend needs one unique field per permission. `acl.Validate` already returns several entries under a single field (one for users, one for groups), so it's worth checking whether any ACL UI relies on this mapping.\n\n### Suggested frontend changes\n\n- Render permission-level validations in the permissions table: mark each offending checkbox and show its `detail` inline. Also show a short summary alert at the top of the form (e.g. \"4 permissions can't be granted\") so the error is visible without scrolling.\n- Drop the `error` case from the `toast.promise` in [`CreateEditRolePage.tsx#L82`](https://github.com/coder/coder/blob/b4f8b6756c6a7ddddeab1664b51dea1237740a6e/site/src/pages/OrganizationSettingsPage/CustomRolesPage/CreateEditRolePage.tsx#L82) so errors appear once, in the form. Keep its loading and success messages.\n- If a toast or alert still lists several validations, render them as a list instead of joining them with `\\n`.\n\n### Compatibility\n\nThis is an additive change, not a breaking one:\n\n- Same endpoints, same `400` status, same `codersdk.Response` type. `validations` is already an optional field in the response schema.\n- The Go SDK's `codersdk.Error.Error()` already prints every validation, so `coder organization roles create`/`edit` users get the full list without changes.\n- `message` and `detail` are human-readable strings, not a contract. Keeping a `detail` summary helps clients that do string matching. In-repo tests that match `\"not allowed to grant this permission\"` (`dbauthz/customroles_test.go`) need updating if that text changes.\n- The site ships inside the same binary, so there's no frontend/backend version skew. The frontend changes above must land in the same PR, or the role editor gets worse (problem 1).\n\n### Rough backend diff used for the repro\n\nRepro only, not production-ready: it only covers the org loop, and the error type and messages are placeholders.\n\n\u003cdetails>\u003csummary>diff\u003c/summary>\n\n```diff\ndiff --git a/coderd/database/dbauthz/dbauthz.go b/coderd/database/dbauthz/dbauthz.go\nindex 0812156967..68ebefb3aa 100644\n--- a/coderd/database/dbauthz/dbauthz.go\n+++ b/coderd/database/dbauthz/dbauthz.go\n@@ -1717,12 +1717,17 @@ func (q *querier) customRoleCheck(ctx context.Context, role database.CustomRole,\n \t}\n \n \tfor orgID, perms := range rbacRole.ByOrgID {\n+\t\t// REPRO-ONLY: accumulate escalation failures instead of returning the first.\n+\t\tvar invalid []rbac.Permission\n \t\tfor _, orgPerm := range perms.Org {\n \t\t\terr := q.customRoleEscalationCheck(ctx, act, orgPerm, rbac.Object{OrgID: orgID, Type: orgPerm.ResourceType})\n \t\t\tif err != nil {\n-\t\t\t\treturn xerrors.Errorf(\"org=%q: org: %w\", orgID, err)\n+\t\t\t\tinvalid = append(invalid, orgPerm)\n \t\t\t}\n \t\t}\n+\t\tif len(invalid) > 0 {\n+\t\t\treturn &InvalidRolePermissionsError{Permissions: invalid}\n+\t\t}\n \t\tfor _, memberPerm := range perms.Member {\n \t\t\t// The person giving the permission should still be required to have\n \t\t\t// the permissions throughout the org in order to give individuals the\n@@ -10098,3 +10103,12 @@ func (q *querier) GetAuthorizedMCPServerConfigs(ctx context.Context, organizatio\n func (q *querier) GetAuthorizedOrganizationSkillMetadata(ctx context.Context, organizationID uuid.UUID, prepared rbac.PreparedAuthorized) ([]database.ListOrganizationSkillMetadataByOrganizationIDRow, error) {\n \treturn q.db.GetAuthorizedOrganizationSkillMetadata(ctx, organizationID, prepared)\n }\n+\n+// InvalidRolePermissionsError is REPRO-ONLY: lists every permission the caller cannot grant.\n+type InvalidRolePermissionsError struct {\n+\tPermissions []rbac.Permission\n+}\n+\n+func (e *InvalidRolePermissionsError) Error() string {\n+\treturn xerrors.Errorf(\"%d permission(s) cannot be granted\", len(e.Permissions)).Error()\n+}\ndiff --git a/enterprise/coderd/roles.go b/enterprise/coderd/roles.go\nindex d33b4dd489..7fa44ec336 100644\n--- a/enterprise/coderd/roles.go\n+++ b/enterprise/coderd/roles.go\n@@ -2,6 +2,7 @@ package coderd\n \n import (\n \t\"context\"\n+\t\"errors\"\n \t\"fmt\"\n \t\"net/http\"\n \t\"strings\"\n@@ -12,6 +13,7 @@ import (\n \t\"github.com/coder/coder/v2/coderd/audit\"\n \t\"github.com/coder/coder/v2/coderd/database\"\n \t\"github.com/coder/coder/v2/coderd/database/db2sdk\"\n+\t\"github.com/coder/coder/v2/coderd/database/dbauthz\"\n \t\"github.com/coder/coder/v2/coderd/httpapi\"\n \t\"github.com/coder/coder/v2/coderd/httpmw\"\n \t\"github.com/coder/coder/v2/coderd/rbac\"\n@@ -76,6 +78,21 @@ func (api *API) postOrgRoles(rw http.ResponseWriter, r *http.Request) {\n \t\treturn\n \t}\n \tif err != nil {\n+\t\tvar invalidPerms *dbauthz.InvalidRolePermissionsError\n+\t\tif errors.As(err, &invalidPerms) {\n+\t\t\tvalidations := make([]codersdk.ValidationError, 0, len(invalidPerms.Permissions))\n+\t\t\tfor _, p := range invalidPerms.Permissions {\n+\t\t\t\tvalidations = append(validations, codersdk.ValidationError{\n+\t\t\t\t\tField: fmt.Sprintf(\"organization_permissions.%s:%s\", p.ResourceType, p.Action),\n+\t\t\t\t\tDetail: fmt.Sprintf(\"You can't grant %s:%s because you don't have that permission.\", p.ResourceType, p.Action),\n+\t\t\t\t})\n+\t\t\t}\n+\t\t\thttpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{\n+\t\t\t\tMessage: \"This role includes permissions you can't grant.\",\n+\t\t\t\tValidations: validations,\n+\t\t\t})\n+\t\t\treturn\n+\t\t}\n \t\thttpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{\n \t\t\tMessage: \"Failed to update role permissions\",\n \t\t\tDetail: err.Error(),\n@@ -167,6 +184,21 @@ func (api *API) putOrgRoles(rw http.ResponseWriter, r *http.Request) {\n \t\treturn\n \t}\n \tif err != nil {\n+\t\tvar invalidPerms *dbauthz.InvalidRolePermissionsError\n+\t\tif errors.As(err, &invalidPerms) {\n+\t\t\tvalidations := make([]codersdk.ValidationError, 0, len(invalidPerms.Permissions))\n+\t\t\tfor _, p := range invalidPerms.Permissions {\n+\t\t\t\tvalidations = append(validations, codersdk.ValidationError{\n+\t\t\t\t\tField: fmt.Sprintf(\"organization_permissions.%s:%s\", p.ResourceType, p.Action),\n+\t\t\t\t\tDetail: fmt.Sprintf(\"You can't grant %s:%s because you don't have that permission.\", p.ResourceType, p.Action),\n+\t\t\t\t})\n+\t\t\t}\n+\t\t\thttpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{\n+\t\t\t\tMessage: \"This role includes permissions you can't grant.\",\n+\t\t\t\tValidations: validations,\n+\t\t\t})\n+\t\t\treturn\n+\t\t}\n \t\thttpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{\n \t\t\tMessage: \"Failed to update role permissions\",\n \t\t\tDetail: err.Error(),\n```\n\n\u003c/details>\n\nCreated on behalf of @angrycub\n","author":{"url":"https://github.com/apps/coder-superblink","@type":"Person","name":"coder-superblink"},"datePublished":"2026-11-16T19:39:07Z","url":"https://github.com/coder/coder/issues/32279"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:27db3b98-1e13-593a-21b3-eb68c596de70 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B17A:29C09E:981E49:D5DC5F:6B042D8A |
| html-safe-nonce | de8bb6a13b8177b44cc15f8f83c7d42490a0c91d2122b3f3b834b37b166f01d0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMTdBOjI5QzA5RTo5ODFFNDk6RDVEQzVGOjZCMDQyRDhBIiwidmlzaXRvcl9pZCI6IjY4MjYyMTUwNjExOTYxMjM1MzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 12c83d54fa8b45fb2614787fb28d4998f7804eede94bf7badac1e1d2fbddce8b |
| hovercard-subject-tag | issue:6429204132 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/coder/coder/32279/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8115283f8dc906bcd24d12bb61e6dc3e85d6337c44d0abe0df3b480c05da19b4/coder/coder/issues/32279 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8115283f8dc906bcd24d12bb61e6dc3e85d6337c44d0abe0df3b480c05da19b4/coder/coder/issues/32279 |
| og:image:alt | Follow-up to #32263. That issue is about the role editor offering permissions nobody can grant. This one covers what happens when the API rejects permissions: the API should report every invalid pe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | coder-superblink[bot] |
| hostname | github.com |
| expected-hostname | github.com |
| None | b7a1d2b9101496921b08579fbee988d014345aef2f5bd660d1f8c7332d81560e |
| turbo-cache-control | no-preview |
| go-import | github.com/coder/coder git https://github.com/coder/coder.git |
| octolytics-dimension-user_id | 95932066 |
| octolytics-dimension-user_login | coder |
| octolytics-dimension-repository_id | 440752086 |
| octolytics-dimension-repository_nwo | coder/coder |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 440752086 |
| octolytics-dimension-repository_network_root_nwo | coder/coder |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c3dd496c3da6ea6b77e69c09279d2b81604654f1 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width