Skip to content

Commit 55e00bc

Browse files
Wadecksanderv32
authored andcommitted
[SECURITY-1438] Add form secret encryption compatibility
- Bonus: reproduction test for the two case, in the HTML and in the file - with dom4j exclusion to be able to run tests!
1 parent 5409797 commit 55e00bc

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@
101101
<artifactId>jenkins-client</artifactId>
102102
<version>0.3.7</version>
103103
<scope>test</scope>
104+
<exclusions>
105+
<!-- a forked version already included by the core -->
106+
<exclusion>
107+
<groupId>dom4j</groupId>
108+
<artifactId>dom4j</artifactId>
109+
</exclusion>
110+
</exclusions>
104111
</dependency>
105112
<dependency>
106113
<groupId>org.jenkins-ci.plugins</groupId>

src/main/java/org/jenkinsci/plugins/gogs/GogsProjectProperty.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public class GogsProjectProperty extends JobProperty<Job<?, ?>> {
4242

4343
@Deprecated
4444
public GogsProjectProperty(String gogsSecret, boolean gogsUsePayload, String gogsBranchFilter) {
45-
this.gogsSecret = Secret.fromString(gogsSecret);
46-
this.gogsUsePayload = gogsUsePayload;
47-
this.gogsBranchFilter = gogsBranchFilter;
45+
this(Secret.fromString(gogsSecret), gogsUsePayload, gogsBranchFilter);
4846
}
4947

5048
@DataBoundConstructor
@@ -53,8 +51,9 @@ public GogsProjectProperty(Secret gogsSecret, boolean gogsUsePayload, String gog
5351
this.gogsUsePayload = gogsUsePayload;
5452
this.gogsBranchFilter = gogsBranchFilter;
5553
}
56-
public String getGogsSecret() {
57-
return Secret.toString(this.gogsSecret);
54+
55+
public Secret getGogsSecret() {
56+
return this.gogsSecret;
5857
}
5958

6059
public boolean getGogsUsePayload() {

src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ associated documentation files (the "Software"), to deal in the Software without
2727
import hudson.model.Job;
2828
import hudson.model.UnprotectedRootAction;
2929
import hudson.security.ACL;
30+
import hudson.util.Secret;
3031
import net.sf.json.JSONObject;
3132
import org.acegisecurity.context.SecurityContext;
3233
import org.acegisecurity.context.SecurityContextHolder;
@@ -187,7 +188,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
187188
/* secret is stored in the properties of Job */
188189
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
189190
if (property != null) { /* only if Gogs secret is defined on the job */
190-
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
191+
jSecret = Secret.toString(property.getGogsSecret()); /* Secret provided by Jenkins */
191192
isRefMatched = property.filterBranch(ref);
192193
}
193194
} else {
@@ -207,7 +208,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
207208
/* secret is stored in the properties of Job */
208209
final GogsProjectProperty property = (GogsProjectProperty) job.getProperty(GogsProjectProperty.class);
209210
if (property != null) { /* only if Gogs secret is defined on the job */
210-
jSecret = property.getGogsSecret(); /* Secret provided by Jenkins */
211+
jSecret = Secret.toString(property.getGogsSecret()); /* Secret provided by Jenkins */
211212
isRefMatched = property.filterBranch(ref);
212213
}
213214
}

src/test/java/org/jenkinsci/plugins/gogs/GogsWebHookJenkinsTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package org.jenkinsci.plugins.gogs;
22

3+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
4+
import hudson.model.FreeStyleProject;
35
import hudson.util.Secret;
6+
import org.apache.commons.io.FileUtils;
47
import org.junit.Rule;
58
import org.junit.Test;
9+
import org.jvnet.hudson.test.Issue;
610
import org.jvnet.hudson.test.JenkinsRule;
711
import org.slf4j.Logger;
812
import org.slf4j.LoggerFactory;
913

14+
import java.io.File;
15+
import java.nio.charset.StandardCharsets;
16+
17+
import static org.hamcrest.CoreMatchers.containsString;
18+
import static org.hamcrest.CoreMatchers.not;
1019
import static org.junit.Assert.assertSame;
20+
import static org.junit.Assert.assertThat;
1121

1222
public class GogsWebHookJenkinsTest {
1323
final Logger log = LoggerFactory.getLogger(GogsWebHookJenkinsTest.class);
@@ -42,4 +52,42 @@ public void whenJobBranchNotMatchMustReturnError() {
4252

4353
log.info("Test succeeded.");
4454
}
55+
56+
@Test
57+
@Issue("SECURITY-1438")
58+
public void ensureTheSecretIsEncryptedOnDisk() throws Exception {
59+
Secret secret = Secret.fromString("s3cr3t");
60+
FreeStyleProject p = prepareProjectWithGogsProperty(secret);
61+
62+
File configFile = p.getConfigFile().getFile();
63+
String configFileContent = FileUtils.readFileToString(configFile, StandardCharsets.UTF_8);
64+
assertThat(configFileContent, not(containsString(secret.getPlainText())));
65+
assertThat(configFileContent, containsString(secret.getEncryptedValue()));
66+
}
67+
68+
@Test
69+
@Issue("SECURITY-1438")
70+
public void ensureTheSecretIsEncryptedInHtml() throws Exception {
71+
Secret secret = Secret.fromString("s3cr3t");
72+
FreeStyleProject p = prepareProjectWithGogsProperty(secret);
73+
74+
JenkinsRule.WebClient wc = j.createWebClient();
75+
// there are some errors in the page and thus the status is 500 but the content is there
76+
wc.getOptions().setThrowExceptionOnFailingStatusCode(false);
77+
HtmlPage htmlPage = wc.goTo(p.getUrl() + "configure");
78+
String pageContent = htmlPage.getWebResponse().getContentAsString();
79+
assertThat(pageContent, not(containsString(secret.getPlainText())));
80+
assertThat(pageContent, containsString(secret.getEncryptedValue()));
81+
}
82+
83+
private FreeStyleProject prepareProjectWithGogsProperty(Secret secret) throws Exception {
84+
FreeStyleProject p = j.createFreeStyleProject();
85+
86+
GogsProjectProperty prop = new GogsProjectProperty(secret, true, "master");
87+
p.addProperty(prop);
88+
89+
p.save();
90+
91+
return p;
92+
}
4593
}

0 commit comments

Comments
 (0)