Skip to content

Commit

Permalink
fix(error-handling): Improved error handling for special characters i…
Browse files Browse the repository at this point in the history
…n fields (see #JENKINS-47858)

fixes https://issues.jenkins-ci.org/browse/JENKINS-47858
  • Loading branch information
hypery2k committed Nov 10, 2017
1 parent 2e79ca6 commit 2a3a542
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -8,14 +9,14 @@
</parent>

<properties>
<artifactSuffix />
<artifactSuffix/>
<jenkins.version>1.653</jenkins.version>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.level>7</java.level>
<failsafeArgLine />
<failsafeArgLine/>
<skipITs>false</skipITs>
<workflow.version>1.11</workflow.version>
<!-- Project Libs-->
Expand Down Expand Up @@ -86,7 +87,7 @@
<featureBranchPrefix>feature-</featureBranchPrefix>
<releaseBranchPrefix>release-</releaseBranchPrefix>
<hotfixBranchPrefix>hotfix-</hotfixBranchPrefix>
<versionTagPrefix />
<versionTagPrefix/>
</flowInitContext>
</configuration>
</plugin>
Expand Down Expand Up @@ -235,6 +236,11 @@
<artifactId>jna</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- for workflow support -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ protected Response buildCall(RocketChatRestApiV1 call, RocketChatQueryParams que

private void login() throws IOException {
HttpResponse<JsonNode> loginResult;
String apiURL = serverUrl + "v1/login";

try {
loginResult = Unirest.post(serverUrl + "v1/login").field("user", user).field("password", password).asJson();
loginResult = Unirest.post(apiURL).field("user", user).field("password", password).asJson();
} catch (UnirestException e) {
throw new IOException(e);
throw new IOException("Please check if the server API " + apiURL + " is correct");
} catch (Exception e) {
throw new IOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jenkins.plugins.rocketchatnotifier.model.Response;
import jenkins.plugins.rocketchatnotifier.model.Room;
import jenkins.plugins.rocketchatnotifier.model.User;
import org.json.simple.JSONValue;
import sun.security.validator.ValidatorException;

import java.io.IOException;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class RocketChatClientImpl implements RocketChatClient {
* @param password of the user to authenticate with
*/
public RocketChatClientImpl(String serverUrl, String user, String password) {
this.callBuilder = new RocketChatClientCallBuilder(serverUrl, user, password);
this.callBuilder = new RocketChatClientCallBuilder(serverUrl, JSONValue.escape(user), JSONValue.escape(password));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jenkins.plugins.rocketchatnotifier.rocket;

import org.junit.Test;

import java.io.IOException;

public class RocketChatClientCallBuilderTest {

@Test(expected = IOException.class)
public void shouldEscapeSpecialCharacters() throws Exception {
// given
RocketChatClientCallBuilder rocketCallBuilder = new RocketChatClientCallBuilder("https://open.rocket.chat/", "]\",", "]\",");
// when
rocketCallBuilder.buildCall(RocketChatRestApiV1.ChannelsList);
// then error
}

}

0 comments on commit 2a3a542

Please sign in to comment.