Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…s-binding-plugin into one-char-JENKINS-72412
  • Loading branch information
jglick committed Apr 24, 2024
2 parents 50eda2a + 89bbe4e commit b3559cf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ public Collection<String> getBase64Forms(@NonNull String secret) {
String shiftedSecret = shift + secret;
String encoded = encoder.encodeToString(shiftedSecret.getBytes(StandardCharsets.UTF_8));
String processedEncoded = shift.length() > 0 ? encoded.substring(2 * shift.length()) : encoded;
result.add(processedEncoded);
result.add(removeTrailingEquals(processedEncoded));
if (!processedEncoded.isEmpty()) {
result.add(processedEncoded);
}
String processedWithoutTrailingEquals = removeTrailingEquals(processedEncoded);
if (!processedWithoutTrailingEquals.isEmpty()) {
result.add(removeTrailingEquals(processedEncoded));
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,25 @@ public void widerRequiredContext() throws Throwable {
});
}

@Issue("JENKINS-72412")
@Test public void maskingOfOneCharSecretShouldNotMangleOutput() throws Throwable {
rr.then(r -> {
String credentialsId = "creds";
String secret = "a";
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), new StringCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "sample", Secret.fromString(secret)));
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " withCredentials([string(credentialsId: '" + credentialsId + "', variable: 'SECRET')]) {\n"
// forgot set +x, ran /usr/bin/env, etc.
+ " if (isUnix()) {sh 'echo $SECRET > oops'} else {bat 'echo %SECRET% > oops'}\n"
+ " }\n"
+ "}", true));
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0).get());
r.assertLogContains("echo", b);
});
}

@Issue("JENKINS-30326")
@Test
public void testGlobalBindingWithAuthorization() throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Collection;
import java.util.List;

public class Base64SecretPatternFactoryTest {

@Rule
Expand Down Expand Up @@ -52,4 +56,16 @@ public void base64SecretsAreMaskedInLogs() throws Exception {
j.assertLogContains("****", run);
j.assertLogNotContains(SAMPLE_PASSWORD, run);
}

@Test
public void base64FormsAreNotEmpty() {
Base64SecretPatternFactory factory = new Base64SecretPatternFactory();
List<String> secrets = List.of("", "s", "s3", "s3cr3t");

secrets.forEach(secret -> {
Collection<String> base64Forms = factory.getBase64Forms(secret);
Assert.assertTrue("Failed for secret: '" + secret + "'. Method returned an empty string.",
base64Forms.stream().noneMatch(String::isEmpty));
});
}
}

0 comments on commit b3559cf

Please sign in to comment.