Skip to content

Refuse API keys when creating API keys - #2055

Merged
ericmj merged 6 commits into
mainfrom
keys-cannot-create-keys
Nov 1, 2026
Merged

ericmj merged 6 commits into
mainfrom
keys-cannot-create-keys

Conversation

@ericmj

@ericmj ericmj commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Creating a key through POST /api/keys or POST /api/orgs/:organization/keys needs a sign-in: a browser session, Basic auth, or an OAuth token from mix hex.user auth. An API key, or a token exchanged for one through the client credentials grant, gets a 403 that points to the dashboard for personal keys, and to the organization's dashboard or mix hex.organization key ORG generate for organization keys. The refusal runs before the organization is looked up, so it's the same for an organization that exists and one that doesn't. Every key then comes from a sign-in, so revoking a key leaves behind no key it created. Creating keys on the dashboard is unchanged.

The keys permission on a key, or on a token exchanged for one, now only allows revoking keys, and /docs/keys says so. KeyGrantor only sees an OAuth token or no credential, so its clauses for a key credential are gone and its refusal names the authenticating token.

Organization keys can't create keys any more, and neither can mix hex.organization key ORG generate when it runs with HEX_API_KEY. Hex 2.3 and earlier create the read and repositories keys of mix hex.user auth with the write key they created a moment before, and that flow stops working when Basic auth turns off on 2026-11-01, so this deploys after that date. The docs for mix hex.organization key generate now say to sign in first.

Creating a key through the API needs a sign-in: a browser session, Basic
auth, or an OAuth token from mix hex.user auth. An API key, or a token
exchanged for one through the client credentials grant, gets a 403. Every
key then comes from a sign-in, so revoking a key leaves behind no key it
created.

The docs for mix hex.organization key generate now say to sign in first.
The refusal ran after authorize, so a token exchanged for an API key got
the OAuth TOTP challenge first and the caller was asked for a code before
being told keys can't create keys. It now runs first.

The message points to the dashboard for personal keys, and to the
organization's dashboard or mix hex.organization key ORG generate for
organization keys, since mix hex.user auth creates no keys.
ericmj added a commit to hexpm/hex that referenced this pull request Oct 16, 2026
* Create keys in integration tests from a two-factor sign-in

hexpm refuses an API key, or a token exchanged for one, when creating
API keys (hexpm/hexpm#2055). The tests that created keys with a key now
sign in the way mix hex.user auth does: a user with two-factor
authentication and an OAuth token, with the TOTP code that OAuth writes
need. They pass against hexpm with and without that change.

* Sign in before organization auth in the deauth test

The test authorized the organization with a stored key, which hexpm#2055
refuses for key creation, and then passed because deauth leaves no repo
either way. It now signs in with two-factor and checks that auth added
the repo before deauth removes it.
ericmj added a commit to hexpm/hex that referenced this pull request Oct 16, 2026
hexpm refuses an API key, or a token exchanged for one, when creating
API keys (hexpm/hexpm#2055). The tests that created keys with a key now
sign in the way mix hex.user auth does: a user with two-factor
authentication and an OAuth token, with the TOTP code that OAuth writes
need. They pass against hexpm with and without that change.
ericmj added a commit to hexpm/hex that referenced this pull request Oct 16, 2026
hexpm refuses an API key, or a token exchanged for one, when creating
API keys (hexpm/hexpm#2055). The tests that created keys with a key now
sign in the way mix hex.user auth does: a user with two-factor
authentication and an OAuth token, with the TOTP code that OAuth writes
need. They pass against hexpm with and without that change.
@ericmj ericmj changed the title Refuse API keys when creating API keys DO NOT MERGE: Refuse API keys when creating API keys Oct 17, 2026
# Conflicts:
#	test/hexpm_web/controllers/api/key_controller_test.exs
# Conflicts:
#	lib/hexpm_web/controllers/api/key_controller.ex
#	lib/hexpm_web/templates/docs/publish.html.md
The refusal ran after fetch_organization, so a key creating keys in an
organization that exists got a 403 naming it and one that doesn't exist
got a 404. Every other answer on these routes treats the two the same.
It runs first now and takes the name from the path.
KeyGrantor only sees an OAuth token or no credential now that a key can't
create keys, so its clauses for a key never ran. Its refusal names the
authenticating token instead of a key.
@ericmj
ericmj marked this pull request as ready for review November 1, 2026 14:09
@ericmj ericmj changed the title DO NOT MERGE: Refuse API keys when creating API keys Refuse API keys when creating API keys Nov 1, 2026
@ericmj
ericmj merged commit 60cc3c7 into main Nov 1, 2026
14 checks passed
@ericmj
ericmj deleted the keys-cannot-create-keys branch November 1, 2026 14:10
ericmj added a commit that referenced this pull request Nov 4, 2026
A key, or a token exchanged for one, can no longer revoke keys, the same
way it can't create them since #2055. A leaked key with keys or api:write
could revoke every other key of its account or organization.

Keys can't hold the keys permission any more: the key form doesn't offer
it, write members asking for api:write get api:read and publish, and
creating a key with it fails validation. The keys OAuth scope keeps
working for signed-in users.
ericmj added a commit that referenced this pull request Nov 4, 2026
* Describe the Keys permission as revoking keys

A key can't create other keys since #2055, but the Generate New Key form
still described the Keys permission as creating keys with a subset of the
key's permissions.

The test that read-only organization members can't revoke keys matched any
"Revoke key" text on the page, which the new description contains, so it
now looks for the revoke button's aria-label.

* Only let a signed-in person revoke keys

A key, or a token exchanged for one, can no longer revoke keys, the same
way it can't create them since #2055. A leaked key with keys or api:write
could revoke every other key of its account or organization.

Keys can't hold the keys permission any more: the key form doesn't offer
it, write members asking for api:write get api:read and publish, and
creating a key with it fails validation. The keys OAuth scope keeps
working for signed-in users.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant