Skip to content

Commit fde9c42

Browse files
committed
[SECURITY-360] introduce a system switch to kill CLI
This basically is a convenient version of https://github.com/jenkinsci-cert/SECURITY-218. During the course of discussing how to fix SECURITY-360, it was agreed by the CERT team that we provide this switch.
1 parent cfc9491 commit fde9c42

5 files changed

Lines changed: 85 additions & 4 deletions

File tree

core/src/main/java/hudson/cli/CLIAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ public String getIconFileName() {
6262
}
6363

6464
public String getDisplayName() {
65-
6665
return "Jenkins CLI";
6766
}
6867

6968
public String getUrlName() {
70-
return "cli";
69+
return jenkins.CLI.DISABLED ? null : "cli";
7170
}
7271

7372
public void doCommand(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {

core/src/main/java/hudson/cli/CliProtocol.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class CliProtocol extends AgentProtocol {
3232

3333
@Override
3434
public String getName() {
35-
return "CLI-connect";
35+
return jenkins.CLI.DISABLED ? null : "CLI-connect";
3636
}
3737

3838
@Override

core/src/main/java/hudson/cli/CliProtocol2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class CliProtocol2 extends CliProtocol {
2525
@Override
2626
public String getName() {
27-
return "CLI2-connect";
27+
return jenkins.CLI.DISABLED ? null : "CLI2-connect";
2828
}
2929

3030
@Override
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package jenkins;
2+
3+
import org.kohsuke.accmod.Restricted;
4+
import org.kohsuke.accmod.restrictions.NoExternalUse;
5+
6+
/**
7+
* Kill switch to disable the entire Jenkins CLI system.
8+
*
9+
* Marked as no external use because the CLI subsystem is nearing EOL.
10+
*
11+
* @author Kohsuke Kawaguchi
12+
*/
13+
@Restricted(NoExternalUse.class)
14+
public class CLI {
15+
// non-final to allow setting from $JENKINS_HOME/init.groovy.d
16+
public static boolean DISABLED = Boolean.getBoolean(CLI.class.getName()+".disabled");
17+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package jenkins;
2+
3+
import hudson.cli.FullDuplexHttpStream;
4+
import hudson.model.Computer;
5+
import hudson.model.Failure;
6+
import hudson.remoting.Channel;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.jvnet.hudson.test.JenkinsRule;
10+
11+
import java.io.FileNotFoundException;
12+
import java.net.URL;
13+
14+
import static org.junit.Assert.*;
15+
16+
/**
17+
* @author Kohsuke Kawaguchi
18+
*/
19+
public class CLITest {
20+
@Rule
21+
public JenkinsRule j = new JenkinsRule();
22+
23+
/**
24+
* Checks if the kill switch works correctly
25+
*/
26+
@Test
27+
public void killSwitch() throws Exception {
28+
// this should succeed, as a control case
29+
makeHttpCall();
30+
makeJnlpCall();
31+
32+
CLI.DISABLED = true;
33+
try {
34+
try {
35+
makeHttpCall();
36+
fail("Should have been rejected");
37+
} catch (FileNotFoundException e) {
38+
// attempt to make a call should fail
39+
}
40+
try {
41+
makeJnlpCall();
42+
fail("Should have been rejected");
43+
} catch (Exception e) {
44+
// attempt to make a call should fail
45+
e.printStackTrace();
46+
47+
// the current expected failure mode is EOFException, though we don't really care how it fails
48+
}
49+
} finally {
50+
CLI.DISABLED = false;
51+
}
52+
}
53+
54+
private void makeHttpCall() throws Exception {
55+
FullDuplexHttpStream con = new FullDuplexHttpStream(new URL(j.getURL(), "cli"));
56+
Channel ch = new Channel("test connection", Computer.threadPoolForRemoting, con.getInputStream(), con.getOutputStream());
57+
ch.close();
58+
}
59+
60+
private void makeJnlpCall() throws Exception {
61+
int r = hudson.cli.CLI._main(new String[]{"-s",j.getURL().toString(), "version"});
62+
if (r!=0)
63+
throw new Failure("CLI failed");
64+
}
65+
}

0 commit comments

Comments
 (0)