Skip to content

Add identifiable prefixes to API tokens#11187

Draft
daniel-beck wants to merge 1 commit intojenkinsci:masterfrom
daniel-beck:api-token-prefix
Draft

Add identifiable prefixes to API tokens#11187
daniel-beck wants to merge 1 commit intojenkinsci:masterfrom
daniel-beck:api-token-prefix

Conversation

@daniel-beck
Copy link
Member

Identifiable prefixes makes it easier to identify poor secret handling. See e.g. https://github.blog/engineering/platform-security/behind-githubs-new-authentication-token-formats/ for an (outdated at this point) description of benefits.

To really have users benefit from this, we can't allow the prefixes to be configurable though. WDYT?

Additionally, does it make sense to require prefixes by default already, or should that be done at a later point?


WIP, no real test coverage yet.

Testing done

Proposed changelog entries

  • human-readable text

Proposed changelog category

/label

Proposed upgrade guidelines

N/A

Submitter checklist

  • The Jira issue, if it exists, is well-described.
  • The changelog entries and upgrade guidelines are appropriate for the audience affected by the change (users or developers, depending on the change) and are in the imperative mood (see examples). Fill in the Proposed upgrade guidelines section only if there are breaking changes or changes that may require extra steps from users during upgrade.
  • There is automated testing or an explanation as to why this change has no tests.
  • New public classes, fields, and methods are annotated with @Restricted or have @since TODO Javadocs, as appropriate.
  • New deprecations are annotated with @Deprecated(since = "TODO") or @Deprecated(forRemoval = true, since = "TODO"), if applicable.
  • New or substantially changed JavaScript is not defined inline and does not call eval to ease future introduction of Content Security Policy (CSP) directives (see documentation).
  • For dependency updates, there are links to external changelogs and, if possible, full differentials.
  • For new APIs and extension points, there is a link to at least one consumer.

Desired reviewers

@mention

Before the changes are marked as ready-for-merge:

Maintainer checklist

  • There are at least two (2) approvals for the pull request and no outstanding requests for change.
  • Conversations in the pull request are over, or it is explicit that a reviewer is not blocking the change.
  • Changelog entries in the pull request title and/or Proposed changelog entries are accurate, human-readable, and in the imperative mood.
  • Proper changelog labels are set so that the changelog can be generated automatically.
  • If the change needs additional upgrade steps from users, the upgrade-guide-needed label is set and there is a Proposed upgrade guidelines section in the pull request title (see example).
  • If it would make sense to backport the change to LTS, a Jira issue must exist, be a Bug or Improvement, and be labeled as lts-candidate to be considered (see query).

@daniel-beck daniel-beck added work-in-progress The PR is under active development, not ready to the final review needs-testcase Test automation is required for this pull request labels Oct 10, 2025
Copy link
Member

@timja timja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think token prefix should probably be configurable for OEM, e.g. cloudbees CI. but no UI and there should be warnings about don't do this unless you know what you are doing?


private static final String HASH_ALGORITHM = "SHA-256";

private static final String TOKEN_PREFIX = "jenkins_apitoken_";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. GitHub

Have you checked what their new tokens look like recently? 😉 Their prefix is now github_pat_ instead of ghp.

I considered the prefix jio_ (for jenkins.io) for a while, then I saw GH made much longer token prefixes, so I thought why not. jio in isolation is not identifiable as a Jenkins API token. Neither might jenkins_ alone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe OT but we could rename the Jenkins feature in GUI and docs to personal access tokens for consistency with GH and to emphasize that these are bound to a “user” (regular principal). Thus, jenkins_pat_.

@daniel-beck daniel-beck added the rfe For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted label Oct 11, 2025
@daniel-beck
Copy link
Member Author

daniel-beck commented Oct 11, 2025

I think token prefix should probably be configurable for OEM, e.g. cloudbees CI. but no UI and there should be warnings about don't do this unless you know what you are doing?

I considered this, even had it in the code as a Java system property. The reason I removed it again is that I thought this could cause is that I do not really want prefixes to be optional besides a transitional period after introduction (the flag that currently exists).

With a user-configurable prefix, we end up with these options:

  • Allow any prefix on validation, e.g., only look at the token after the last _. So _abcdef1234567890 and wtf_abcdef1234567890 are OK, just like jenkins_apitoken_abcdef1234567890.
  • Basically soft revoke all API tokens if you restart Jenkins with a new prefix (until all secrets are updated with the new prefix).
  • Make it fairly complex to configure (the prefix to emit, and a list of prefixes to accept for "legacy" support)

I liked none of them a lot.

In terms of the OEM argument, I'll definitely check with colleagues what the situation with CloudBees CI should be before I finalize this PR. There may be a middle ground, like a resource file in src/filter, from which to get the prefix.

Comment on lines +273 to +276
if (REQUIRE_API_TOKEN_PREFIX && !token.startsWith(TOKEN_PREFIX)) {
// not allowed, so will not match
return null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will prevent people from upgrading unless I misunderstand?

IMO:

  • There is no need for a system property.
  • All new tokens should start with the new prefix.
  • Any existing tokens should still be honored (until regenerated).

@github-actions
Copy link
Contributor

Please take a moment and address the merge conflicts of your pull request. Thanks!

@github-actions github-actions bot added the unresolved-merge-conflict There is a merge conflict with the target branch. label Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-testcase Test automation is required for this pull request rfe For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted unresolved-merge-conflict There is a merge conflict with the target branch. work-in-progress The PR is under active development, not ready to the final review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants