Skip to content

Commit 09befbb

Browse files
olamydaniel-beck
authored andcommitted
[SECURITY-2499]
1 parent f7c7262 commit 09befbb

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/main/java/hudson/plugins/git/GitStatus.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.*;
1919
import java.util.logging.Level;
2020
import java.util.logging.Logger;
21+
import java.util.regex.Pattern;
2122
import javax.servlet.ServletException;
2223
import javax.servlet.http.HttpServletRequest;
2324

@@ -28,6 +29,7 @@
2829
import jenkins.triggers.SCMTriggerItem;
2930
import org.apache.commons.lang.StringUtils;
3031
import static org.apache.commons.lang.StringUtils.isNotEmpty;
32+
3133
import org.eclipse.jgit.transport.RemoteConfig;
3234
import org.eclipse.jgit.transport.URIish;
3335
import org.kohsuke.stapler.*;
@@ -115,7 +117,10 @@ public HttpResponse doNotifyCommit(HttpServletRequest request, @QueryParameter(r
115117
@QueryParameter(required=false) String sha1) throws ServletException, IOException {
116118
lastURL = url;
117119
lastBranches = branches;
118-
lastSHA1 = sha1;
120+
if(StringUtils.isNotBlank(sha1)&&!SHA1_PATTERN.matcher(sha1.trim()).matches()){
121+
return HttpResponses.error(SC_BAD_REQUEST, new IllegalArgumentException("Illegal SHA1"));
122+
}
123+
lastSHA1 = cleanupSha1(sha1);
119124
lastBuildParameters = null;
120125
GitStatus.clearLastStaticBuildParameters();
121126
URIish uri;
@@ -316,6 +321,7 @@ public static class JenkinsAbstractProjectListener extends Listener {
316321
*/
317322
@Override
318323
public List<ResponseContributor> onNotifyCommit(String origin, URIish uri, String sha1, List<ParameterValue> buildParameters, String... branches) {
324+
sha1 = cleanupSha1(sha1);
319325
if (LOGGER.isLoggable(Level.FINE)) {
320326
LOGGER.log(Level.FINE, "Received notification from {0} for uri = {1} ; sha1 = {2} ; branches = {3}",
321327
new Object[]{StringUtils.defaultIfBlank(origin, "?"), uri, sha1, Arrays.toString(branches)});
@@ -594,15 +600,27 @@ public static class CommitHookCause extends Cause {
594600
public final String sha1;
595601

596602
public CommitHookCause(String sha1) {
597-
this.sha1 = sha1;
603+
this.sha1 = cleanupSha1(sha1);
598604
}
599605

600606
@Override
601607
public String getShortDescription() {
602-
return "commit notification " + sha1;
608+
return "commit notification " + cleanupSha1(sha1);
603609
}
604610
}
605611

612+
public static final Pattern SHA1_PATTERN = Pattern.compile("[a-fA-F0-9]++"); // we should have {40} but some compact sha1
613+
614+
public static final Pattern CLEANER_SHA1_PATTERN = Pattern.compile("[^a-fA-F0-9]");
615+
616+
/**
617+
* @param sha1 the String to cleanup
618+
* @return the String with all non hexa characters removed
619+
*/
620+
private static String cleanupSha1(String sha1){
621+
return sha1 == null?null:CLEANER_SHA1_PATTERN.matcher(sha1.trim()).replaceAll("");
622+
}
623+
606624
private static final Logger LOGGER = Logger.getLogger(GitStatus.class.getName());
607625
private static final int MAX_REPORTED_CONTRIBUTORS = 10;
608626

src/test/java/hudson/plugins/git/GitStatusTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.logging.Level;
2424
import java.util.logging.Logger;
2525
import org.eclipse.jgit.transport.URIish;
26+
import org.kohsuke.stapler.HttpResponses;
2627
import org.mockito.Mockito;
2728
import static org.mockito.Mockito.mock;
2829
import static org.mockito.Mockito.when;
@@ -665,4 +666,19 @@ public void testDoNotifyCommitTriggeredHeadersLimited() throws Exception {
665666

666667
assertEquals("URL: a Branches: master", this.gitStatus.toString());
667668
}
669+
670+
@Test
671+
@Issue("SECURITY-2499")
672+
public void testDoNotifyCommitWithWrongSha1Content() throws Exception {
673+
setupProjectWithTrigger("a", "master", false);
674+
675+
String content = "<img src=onerror=alert(1)>";
676+
677+
HttpResponse rsp = this.gitStatus.doNotifyCommit(requestWithNoParameter, "a", "master", content);
678+
679+
HttpResponses.HttpResponseException responseException = ((HttpResponses.HttpResponseException) rsp);
680+
assertEquals(IllegalArgumentException.class, responseException.getCause().getClass());
681+
assertEquals("Illegal SHA1", responseException.getCause().getMessage());
682+
683+
}
668684
}

0 commit comments

Comments
 (0)