-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Add identifiable prefixes to API tokens #11187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
daniel-beck
wants to merge
1
commit into
jenkinsci:master
Choose a base branch
from
daniel-beck:api-token-prefix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ | |
| import java.util.regex.Pattern; | ||
| import java.util.stream.Collectors; | ||
| import jenkins.security.Messages; | ||
| import jenkins.util.SystemProperties; | ||
| import net.jcip.annotations.Immutable; | ||
| import net.sf.json.JSONObject; | ||
| import org.kohsuke.accmod.Restricted; | ||
|
|
@@ -69,6 +70,10 @@ | |
|
|
||
| private static final String HASH_ALGORITHM = "SHA-256"; | ||
|
|
||
| private static final String TOKEN_PREFIX = "jenkins_apitoken_"; | ||
|
|
||
| private static /* non-final for script console */ boolean REQUIRE_API_TOKEN_PREFIX = SystemProperties.getBoolean(ApiTokenStore.class.getName() + ".REQUIRE_API_TOKEN_PREFIX", true); | ||
|
|
||
| private List<HashedToken> tokenList; | ||
|
|
||
| public ApiTokenStore() { | ||
|
|
@@ -177,8 +182,8 @@ | |
| RANDOM.nextBytes(random); | ||
| // 32-char in hex | ||
| String secretValue = Util.toHexString(random); | ||
| String tokenTheUserWillUse = HASH_VERSION + secretValue; | ||
| assert tokenTheUserWillUse.length() == 2 + 32; | ||
| String tokenTheUserWillUse = TOKEN_PREFIX + HASH_VERSION + secretValue; | ||
| assert tokenTheUserWillUse.length() == TOKEN_PREFIX.length() + 2 + 32; | ||
|
|
||
| HashedToken token = prepareAndStoreToken(name, secretValue); | ||
|
|
||
|
|
@@ -195,9 +200,19 @@ | |
| */ | ||
| @SuppressFBWarnings(value = "UNSAFE_HASH_EQUALS", justification = "Comparison only validates version of the specified token") | ||
| public synchronized @NonNull String addFixedNewToken(@NonNull String name, @NonNull String tokenPlainValue) { | ||
| if (REQUIRE_API_TOKEN_PREFIX && !tokenPlainValue.startsWith(TOKEN_PREFIX)) { | ||
| throw new IllegalArgumentException("The token must consist of: the prefix '" + TOKEN_PREFIX + "', 2 characters for the version, and 32 hex-characters for the secret"); | ||
| } | ||
| if (tokenPlainValue.startsWith(TOKEN_PREFIX)) { | ||
| tokenPlainValue = tokenPlainValue.substring(TOKEN_PREFIX.length()); | ||
| } | ||
| if (tokenPlainValue.length() != VERSION_LENGTH + HEX_CHAR_LENGTH) { | ||
| LOGGER.log(Level.INFO, "addFixedNewToken, length received: {0}" + tokenPlainValue.length()); | ||
| throw new IllegalArgumentException("The token must consist of 2 characters for the version and 32 hex-characters for the secret"); | ||
| // TODO clean this logic up | ||
| if (REQUIRE_API_TOKEN_PREFIX) { | ||
| throw new IllegalArgumentException("The token must consist of: the prefix '" + TOKEN_PREFIX + "', 2 characters for the version, and 32 hex-characters for the secret"); | ||
| } | ||
| throw new IllegalArgumentException("The token must consist of: the prefix '" + TOKEN_PREFIX + "' (optional), 2 characters for the version, and 32 hex-characters for the secret"); | ||
| } | ||
|
|
||
| String hashVersion = tokenPlainValue.substring(0, VERSION_LENGTH); | ||
|
|
@@ -255,6 +270,14 @@ | |
| if (isLegacyToken(token)) { | ||
| plainToken = token; | ||
| } else { | ||
| if (REQUIRE_API_TOKEN_PREFIX && !token.startsWith(TOKEN_PREFIX)) { | ||
| // not allowed, so will not match | ||
| return null; | ||
| } | ||
|
Comment on lines
+273
to
+276
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will prevent people from upgrading unless I misunderstand? IMO:
|
||
| if (token.startsWith(TOKEN_PREFIX)) { | ||
| // Identifiable Jenkins API token prefix | ||
| token = token.substring(TOKEN_PREFIX.length()); | ||
| } | ||
| plainToken = getHashOfToken(token); | ||
| } | ||
|
|
||
|
|
@@ -265,7 +288,7 @@ | |
| * Determine if the given token was generated by the legacy system or the new one | ||
| */ | ||
| private boolean isLegacyToken(@NonNull String token) { | ||
| return token.length() != TOKEN_LENGTH_V2; | ||
| return token.length() != TOKEN_LENGTH_V2 && token.length() != TOKEN_PREFIX.length() + TOKEN_LENGTH_V2; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems very long.
Have you done research into what other vendors use?
e.g. GitHub:
https://github.blog/engineering/platform-security/behind-githubs-new-authentication-token-formats/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you checked what their new tokens look like recently? 😉 Their prefix is now
github_pat_instead ofghp.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.jioin isolation is not identifiable as a Jenkins API token. Neither mightjenkins_alone.There was a problem hiding this comment.
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_.