Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionList;
import hudson.Util;
import hudson.model.Failure;
import hudson.model.Item;
import hudson.model.ModelObject;
import hudson.model.User;
Expand Down Expand Up @@ -198,8 +199,10 @@ public final FormValidation doCheckId(@ContextInPath ModelObject context, @Query
if (value.isEmpty()) {
return FormValidation.ok();
}
if (!value.matches("[a-zA-Z0-9_.-]+")) { // anything else considered kosher?
return FormValidation.error("Unacceptable characters");
try {
Jenkins.checkGoodName(value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

other than changing the allowed character set this does nothing its the same result as before.

When saving the credentials it needs to call checkGoodName not just when validating it

} catch (Failure e) {
return FormValidation.error(e.getMessage());
}
FormValidation problem = checkForDuplicates(value, context, context);
if (problem != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@

package com.cloudbees.plugins.credentials.impl;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import org.htmlunit.html.HtmlButton;
Expand All @@ -53,6 +55,7 @@
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@WithJenkins
Expand All @@ -63,6 +66,8 @@ void doCheckIdSyntax(JenkinsRule r) {
assertDoCheckId(r, "", r.jenkins, OK);
assertDoCheckId(r, /* random UUID */IdCredentials.Helpers.fixEmptyId(null), r.jenkins, OK);
assertDoCheckId(r, "blah-blah", r.jenkins, OK);
assertDoCheckId(r, "contains space", r.jenkins, ERROR);
assertDoCheckId(r, "nested/path", r.jenkins, ERROR);
assertDoCheckId(r, "definitely\nscary", r.jenkins, ERROR);
}

Expand Down Expand Up @@ -136,6 +141,21 @@ void doCheckIdDuplication(JenkinsRule r) throws Exception {
// TODO could test the case that alice has Item.READ but not CredentialsProvider.VIEW on a folder, and mocks a web request passing that folder as context
}

@Test
void keepsLegacyStoredIdUntouched(JenkinsRule r) throws Exception {
CredentialsStore store = lookupStore(r.jenkins);
String legacyId = "legacy/id";
store.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, legacyId, null, "x", "y"));

Credentials legacy = CredentialsMatchers.firstOrNull(
store.getCredentials(Domain.global()),
CredentialsMatchers.withId(legacyId)
);
assertNotNull(legacy, "legacy credential ID should remain stored and readable");

assertDoCheckId(r, legacyId, r.jenkins, ERROR);
}

@Test
void noIDValidationMessageOnCredentialsUpdate(JenkinsRule r) throws Exception {
// create credentials with ID test
Expand Down