57cbca585f feat: negotiate and persist authorization scope (#28178)
**TL;DR**

Middle of the stack, split out of #28045, that makes an OAuth2 app's
scope allowlist actually restrict what a client can be granted. The
authorize endpoint has always parsed `scope` and then thrown it away.

| PR | What it does |
|---|---|
| #28007 | Schema: `codes.scope` and `tokens.scope`, so a negotiated
scope has somewhere to live. Every code is still stamped unrestricted. |
| #28167 | `ScopesCover` compares an allowlist against a request by
permission coverage rather than by name, and fails closed on any shape
it does not model. Added with no production callers. |
| **#28178 (this)** | The authorize endpoint checks the request against
the scope catalog, grants it only if the app's filtered allowlist covers
it, and persists the result on the code. Both GET and POST negotiate, so
a doomed request fails before the consent page renders. |
| #28179 | The consent page states what was negotiated, and
`invalid_scope` is delivered to the client's own callback per RFC 6749
§4.1.2.1 instead of a Coder error page. |

- Each earlier PR is inert alone: the columns carry a placeholder, the
coverage rules have no caller.
- Visible on upgrade, unlike the rest of the stack. A scope name outside
the catalog, OIDC vocabulary included, now gets `invalid_scope`, on
admin-created apps as well as DCR ones.
- Tokens are unaffected. This changes which authorization requests
succeed, not what a token can do.

**Where in the flow**

- Authorize endpoint only, both legs: GET before the consent page
renders, POST before the code is minted. The token exchange, refresh,
and revocation are untouched.
- The result is persisted on the authorization code. The exchange copies
it onto the token row, but the API key it mints still carries no scope,
so nothing is enforced at request time yet.
- Registration and discovery are unchanged. The catalog checked against
is the `scopes_supported` both discovery documents already advertise.

**What it satisfies**

- OAuth 2.1 §1.4.1 — the server "MAY fully or partially ignore the scope
requested by the client, based on the authorization server policy". The
app's allowlist is that policy; until now the server ignored scope in
every direction.
- OAuth 2.1 §1.4.1 — on an omitted scope it "MUST either process the
request using a pre-defined default value or fail the request". The
filtered allowlist is that default; an allowlist filtering to nothing
takes the second branch. Same rule as RFC 6749 §3.3.
- OAuth 2.1 §1.4.1 — scope values "are defined by the authorization
server", and OIDC is named there as an extension that defines its own.
Hence `invalid_scope` for openid/profile/email rather than silent
acceptance.
- OAuth 2.1 §4.1.2.1 — `invalid_scope` is "invalid, unknown, or
malformed", which now covers an uncatalogued name and an over-broad one
alike. Delivery is interim here (static page on GET, error body on
POST); §4.1.2.1 wants it redirected to the client's callback, which is
#28179.
- OAuth 2.1 §4.1.1 — `scope` stays OPTIONAL. Omitting it selects a
default, never an error.
- RFC 7591 §2 — a registered `scope` is the list "the client can use
when requesting access tokens", a ceiling rather than a menu. Enforced
here for the first time; a client that registered an unsupportable one
repairs it via RFC 7592.
- RFC 8414 §2 — `scopes_supported` is that catalog, advertised since
before this stack, so a conformant client had no grounds to send a name
outside it.
- Not yet: §1.4.1 also requires `scope` in the token response when the
grant differs from the request. Tokens are untouched here, so that lands
with enforcement.

---

Split out of #28045
([PLAT-479](https://linear.app/codercom/issue/PLAT-479)). #28167 has
merged, so this diff is against main and reviews on its own.

The authorize endpoint parsed scope and threw it away, so an app's
allowlist never restricted anything and a client asking for too much was
never told no. Phase 1 added the columns that carry a negotiated scope
from a code to the token, but nothing wrote one.

**What changes**

- Requested names must be in the external scope catalog. The app's
stored allowlist is filtered through that same catalog, which only ever
narrows.
- The allowlist bounds authority, not spelling. A request is granted
when every permission it grants is also granted by the allowlist, named
or not, so an app allowed coder:workspaces.access can approve a client
asking only for workspace:ssh.
- Omitting scope grants the filtered allowlist (RFC 6749 §3.3).
- Both handlers negotiate, so a request that cannot succeed fails before
the consent page renders rather than after the user clicks Allow.
- Two paths produce an empty result and stay distinct. No allowlist and
no request keeps the previous unrestricted grant, written as an explicit
sentinel because the column is NOT NULL with a non-empty CHECK. An
allowlist filtering to nothing is rejected, since falling back would
grant more than it ever permitted.

**Accepted compatibility break, and it is wider than DCR.** The catalog
check runs before the allowlist is consulted, so it covers admin-created
apps too.

- Admin-created apps store no allowlist and had scope discarded
outright. A client sending OIDC vocabulary (openid, profile, email) now
gets invalid_scope on upgrade.
- DCR apps registered with openid, read or admin also hold allowlists
this server cannot grant from, and fail in both directions.
- scopes_supported has always been advertised on both discovery
documents, predating this stack, so a conformant client had no grounds
to send an uncatalogued name. RFC 6749 §4.1.2.1 makes invalid_scope the
answer for one that does.
- Grandfathering would seed the enforcement path with values it cannot
evaluate: a visible error now, a silent hole later.
- The rejection names the registered scopes and the remedy. Pinned in
TestOAuth2AuthorizeDCRScopeCompatibility.

**Interim error delivery, replaced in PR 3.** Rejections go out however
each handler already reports errors: a static error page on GET, an
OAuth2 error body on POST. #28179 delivers them to the client's own
callback (RFC 6749 §4.1.2.1) and rewrites the requireInvalidScope
helper. About 25 lines here are interim on purpose and flagged as such
in that helper.

**Not changed.** Issued tokens are still unrestricted. The exchange
copies the negotiated scope onto the token record, but the API key it
mints carries no scope. This changes which authorization requests
succeed, not what a token can do.

Stack: #28167 (merged), this PR, then #28179 for the consent page and
the invalid_scope redirect.

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Steven Masley <Emyrk@users.noreply.github.com>
2026-08-31 09:29:27 -07:00
…
…
…

Coder Logo Light Coder Logo Dark

Self-Hosted Cloud Development Environments and AI Agents

Coder Banner Light Coder Banner Dark

Quickstart | Docs | Why Coder | Premium

discord release godoc OpenSSF Best Practices OpenSSF Scorecard license

Coder is a self-hosted platform for cloud development environments and AI coding agents. Workspaces are defined with Terraform, connected through a secure Wireguard® tunnel, and automatically shut down when not used. Coder Agents runs a native AI coding agent whose loop executes in the control plane on your infrastructure, with no API keys in workspaces.

  • Define cloud development environments in Terraform
    • EC2 VMs, Kubernetes Pods, Docker Containers, etc.
  • Automatically shutdown idle resources to save on costs
  • Onboard developers in seconds instead of days
  • Delegate coding work to AI agents on your infrastructure
    • Bring any model (Anthropic, OpenAI, Google, Bedrock, self-hosted)
    • No LLM credentials in workspaces, user identity on every action
    • Centralized model governance, cost tracking, and audit logging

Coder platform showing templates and a running workspace

Quickstart

Check out our install guides for other methods and a complete tutorial.

Try Coder with the install script on Linux and macOS, or grab the latest binary or installer from GitHub Releases on Windows:

curl -L https://coder.com/install.sh | sh

Start the server and open http://localhost:3000 to create your initial user, create a Docker template, and provision your first workspace:

coder server

For a production deployment, add a PostgreSQL database (version 13 or later) and an external access URL, and see our validated architectures for sizing and infrastructure guidance:

coder server --postgres-url <url> --access-url <url>

Without these flags, Coder uses a built-in database and sets up a *.try.coder.app access URL for evaluation. Use coder --help for the full list of flags and environment variables.

Documentation

Browse the documentation or visit a specific section below:

  • Workspaces: Workspaces contain the IDEs, dependencies, and configuration information needed for software development
  • Templates: Templates are written in Terraform and describe the infrastructure for workspaces
  • Coder Agents: Delegate coding work to AI agents running on your self-hosted infrastructure
  • AI Gateway: Centralize authentication, auditing, and cost controls for AI tooling
  • Administration: Learn how to operate Coder
  • Premium: Learn about paid features built for large teams
  • IDEs: Connect your existing editor to a workspace

Support

Open an issue for bugs and feature requests.

For community support, showcasing what you built, and feedback on in-progress features, join our Discord or GitHub Discussions.

Dedicated support is included in Coder Premium. For more information, visit coder.com/pricing.

Integrations

New integrations are always in progress. Open an issue to request one. Contributions are welcome in any official or community repository.

Official

Community

Contributing

New contributors are always welcome. If you are new to the Coder codebase, see the contribution guide to get started.

Hiring

Apply on the careers page if you are interested in joining the team.

S
Description
Provision remote development environments via Terraform
Readme AGPL-3.0
1,022 MiB
Languages
Go 74.9%
TypeScript 23.1%
Shell 0.8%
HCL 0.3%
PLpgSQL 0.3%
Other 0.4%