Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-62708] Add JCasC support for approved script hashes #329

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -701,6 +702,27 @@ public synchronized String[] getAclApprovedSignatures() {
return aclApprovedSignatures.toArray(new String[aclApprovedSignatures.size()]);
}

@DataBoundSetter
public synchronized void setApprovedScriptHashes(String[] scriptHashes) throws IOException {
Jenkins.getInstance().checkPermission(Jenkins.RUN_SCRIPTS);
approvedScriptHashes.clear();
Pattern sha1Pattern = Pattern.compile("[a-fA-F0-9]{40}");
for (String scriptHash : scriptHashes) {
if (scriptHash != null && sha1Pattern.matcher(scriptHash).matches()) {
approvedScriptHashes.add(scriptHash);
} else {
LOG.warning(() -> "Ignoring malformed script hash: " + scriptHash);
}
}
save();
reconfigure();
}

@Restricted(NoExternalUse.class) // Jelly, implementation
public synchronized String[] getApprovedScriptHashes() {
return approvedScriptHashes.toArray(new String[approvedScriptHashes.size()]);
}

@Restricted(NoExternalUse.class) // implementation
@Extension public static final class ApprovedWhitelist extends ProxyWhitelist {
public ApprovedWhitelist() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public void smokeTestEntry() throws Exception {
String[] approved = ScriptApproval.get().getApprovedSignatures();
assertTrue(approved.length == 1);
assertEquals(approved[0], "method java.net.URI getHost");
String[] approvedScriptHashes = ScriptApproval.get().getApprovedScriptHashes();
assertTrue(approvedScriptHashes.length == 1);
assertEquals(approvedScriptHashes[0], "fccae58c5762bdd15daca97318e9d74333203106");
MarkRx marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,21 @@ public void malformedScriptApproval() throws Exception {
assertEquals(0, sa.getDangerousApprovedSignatures().length);
}

@Issue("JENKINS-57563")
@LocalData // Just a scriptApproval.xml that whitelists 'staticMethod jenkins.model.Jenkins getInstance'
@Issue({"JENKINS-57563", "JENKINS-62708"})
@LocalData // Just a scriptApproval.xml that whitelists 'staticMethod jenkins.model.Jenkins getInstance' and a script printing all labels
@Test
public void upgradeSmokes() throws Exception {
configureSecurity();
FreeStyleProject p = r.createFreeStyleProject();
p.getPublishersList().add(new TestGroovyRecorder(
new SecureGroovyScript("jenkins.model.Jenkins.instance", true, null)));
p.getPublishersList().add(new TestGroovyRecorder(
new SecureGroovyScript("println(jenkins.model.Jenkins.instance.getLabels())", false, null)));
r.assertLogNotContains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: "
+ "Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance",
r.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get()));
r.assertLogNotContains("org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedUsageException: script not yet approved for use",
r.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get()));
}

private Script script(String groovy) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<scriptApproval plugin="[email protected]">
<approvedScriptHashes/>
<approvedScriptHashes>
<string>fccae58c5762bdd15daca97318e9d74333203106</string>
</approvedScriptHashes>
<approvedSignatures>
<string>staticMethod jenkins.model.Jenkins getInstance</string>
</approvedSignatures>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ security:
scriptApproval:
approvedSignatures:
- method java.net.URI getHost
approvedScriptHashes:
- fccae58c5762bdd15daca97318e9d74333203106
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
approvedScriptHashes:
- "fccae58c5762bdd15daca97318e9d74333203106"
approvedSignatures:
- "method java.net.URI getHost"