Files
coder/coderd
Michael Suchacz 9551fa3b3e fix(coderd): store chat ACL keys as canonical UUIDs (#31037)
`PATCH /api/v2/chats/{chat}/acl` stored user and group IDs as the
request spelled them. `uuid.Parse` accepts uppercase spellings, so such
a request passed validation and returned `204`, but RBAC matches ACL
keys as canonical UUID strings: the share granted nothing, and a
`deleted` role sent with another spelling left the stored entry in
place. This PR rekeys both role maps with `canonicalACLRoles`, as the
MCP server config and organization skill ACLs do, and returns `400` when
two spellings name one principal.

Releases v2.34.0 through v2.38.0 store keys as sent, so before it
applies a request, the handler also deletes stored keys that spell a
requested principal's ID in a noncanonical form: a `deleted` role then
removes every spelling, and a `read` role leaves one canonical entry.
Principals that the request does not name keep their stored keys, since
rekeying them would grant access that RBAC never enforced.

**Why it is safe:** a canonical ID maps to itself, so requests with
canonical IDs behave as before. The chat project ACL already
canonicalizes its keys.

**Red tests:** `TestChatACLNoncanonicalIDs` shares a chat with a user
and a group by uppercase IDs, checks that the user and a group member
can read it, then removes both by uppercase IDs and checks that they
cannot. It then writes user and group keys straight to the database in
each spelling that `uuid.Parse` accepts and older servers stored as
sent: uppercase, braced, `urn:uuid:`, and unhyphenated. For each
spelling, a `deleted` request with the canonical or the stored spelling
must leave no stored key, and a `read` request with the stored spelling
must leave only the canonical keys, let both readers in, and notify the
user. Two new `TestChatACLValidation` cases send one user ID or one
group ID in two spellings and expect a `400`. On `main`:

```text
TestChatACLNoncanonicalIDs: GET /api/v2/chats/<id>: unexpected status code 404: Resource not found or you do not have access to this resource
TestChatACLValidation/DuplicateUserSpellings: An error is expected but got nil.
TestChatACLValidation/DuplicateGroupSpellings: An error is expected but got nil.
```

With the rekey but without the cleanup (`79865b4afc9`), each uppercase
legacy case fails when run on its own:

```text
deleted, canonical spelling: Should be empty, but was map[<UPPERCASE ID>:{[read]}]
deleted, uppercase spelling: Should be empty, but was map[<UPPERCASE ID>:{[read]}]
read: Not equal: expected: database.ChatACL{"<canonical ID>": {[read]}}, actual: database.ChatACL{"<UPPERCASE ID>": {[read]}, "<canonical ID>": {[read]}}
```

These tests also fail on nine `go test -overlay` mutants. Seven skip the
rekey, the duplicate check, or the cleanup for users or for groups, or
run the cleanup after the `oldChat` snapshot. The other two lowercase
keys instead of parsing them, one in the cleanup and one in the rekey.
They passed while the legacy cases used only uppercase keys, and now
fail on the braced spelling:

```text
lowercasing cleanup: Should be empty, but was map[{<ID>}:{[read]}]
lowercasing rekey: Not equal: expected: database.ChatACL{"<ID>": {[read]}}, actual: database.ChatACL{"{<ID>}": {[read]}}
```

**Review focus:** the maps are rekeyed in place after `acl.Validate`,
which still reports IDs that do not parse, so the role loops below read
canonical keys. The cleanup runs before the `oldChat` snapshot, so the
share notification treats a user whose legacy entry granted nothing as
newly added. On `main`, that user is not notified again.

#31016 replaces those loops with `applyACLReadRoles`. The two merge
without conflicts and the merged loops read the rekeyed maps; whichever
merges second will merge `main` first.

The PR adds 153 lines and removes none: 23 in `coderd/exp_chats_acl.go`
and 130 in `coderd/exp_chats_acl_test.go`.

Validation: `go test ./coderd -run
'^(TestChatACLValidation|TestChatACLNoncanonicalIDs)$' -count=1` fails
on `main` and on `79865b4afc9` as above and passes here (9 tests with
subtests), as does `go test -race ./coderd -run
'^TestChatACLNoncanonicalIDs$' -count=10`. Also passing here: `go test
./coderd -run '(?i)acl|mcp|skill' -count=1` (65 with subtests), `go test
./coderd -run
'^(TestChatProjectSharing|TestChatSharingDisabled|TestListChatsSharedScope|TestSharedReaderStreamChat|TestChatFileDownloadURL)$'
-count=1` (22), and `go test ./enterprise/coderd -run
'(?i)acl|mcp|skill|TestEnterpriseAuditLogs' -count=1` (46). A local
merge with #31016 passes `go test ./coderd -run
'^(TestChatACLValidation|TestChatACLNoncanonicalIDs|TestChatACLSharingLifecycle|TestChatProjectSharing)$'
-count=1` (16 with subtests). `go vet ./coderd/` is clean on both, and
the pre-commit hook passed (`make gen`, formatting, golangci-lint, slim
build).

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
2026-10-27 16:18:00 +01:00
..