Skip to content

Commit 1d80e65

Browse files
committed
Revert "Downgrading to a warning at @abayer’s request."
This reverts commit f3774c3.
1 parent 4b2f0e8 commit 1d80e65

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

src/main/java/org/jenkinsci/plugins/workflow/multibranch/SCMBinder.java

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@
2525
package org.jenkinsci.plugins.workflow.multibranch;
2626

2727
import edu.umd.cs.findbugs.annotations.NonNull;
28+
import hudson.AbortException;
2829
import hudson.Extension;
2930
import hudson.Functions;
30-
import hudson.MarkupText;
31-
import hudson.console.ConsoleAnnotationDescriptor;
32-
import hudson.console.ConsoleAnnotator;
33-
import hudson.console.ConsoleNote;
3431
import hudson.model.Action;
3532
import hudson.model.Descriptor;
3633
import hudson.model.DescriptorVisibilityFilter;
@@ -46,6 +43,7 @@
4643
import jenkins.scm.api.SCMRevision;
4744
import jenkins.scm.api.SCMRevisionAction;
4845
import jenkins.scm.api.SCMSource;
46+
import jenkins.util.SystemProperties;
4947
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
5048
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
5149
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
@@ -61,7 +59,11 @@
6159
class SCMBinder extends FlowDefinition {
6260

6361
/** Kill switch for JENKINS-33273 in case of problems. */
64-
static /* not final */ boolean USE_HEAVYWEIGHT_CHECKOUT = Boolean.getBoolean(SCMBinder.class.getName() + ".USE_HEAVYWEIGHT_CHECKOUT"); // TODO 2.4+ use SystemProperties
62+
static /* not final */ boolean USE_HEAVYWEIGHT_CHECKOUT = SystemProperties.getBoolean(SCMBinder.class.getName() + ".USE_HEAVYWEIGHT_CHECKOUT");
63+
64+
/** Kill switch for making this as strict as {@link ReadTrustedStep} about untrusted modifications. */
65+
static /* not final */ boolean IGNORE_UNTRUSTED_EDITS = SystemProperties.getBoolean(SCMBinder.class.getName() + ".IGNORE_UNTRUSTED_EDITS");
66+
6567
private String scriptPath = WorkflowBranchProjectFactory.SCRIPT;
6668

6769
public Object readResolve() {
@@ -111,10 +113,10 @@ public SCMBinder(String scriptPath) {
111113
listener.error("Could not do lightweight checkout, falling back to heavyweight").println(Functions.printThrowable(x).trim());
112114
}
113115
if (script != null) {
114-
if (!rev.equals(tip)) {
115-
// Print a warning in builds where an untrusted contributor has tried to edit Jenkinsfile.
116-
// If we fail to check this (e.g., due to heavyweight checkout), a warning will still be printed to the log
117-
// by the SCM, but that is less apparent.
116+
if (!IGNORE_UNTRUSTED_EDITS && !rev.equals(tip)) {
117+
// Make a best effort to abort builds where an untrusted contributor has tried to edit Jenkinsfile.
118+
// If we fail to check this (e.g., due to heavyweight checkout), a warning will be printed to the log
119+
// and the build will continue with the trusted variant, which is safe but confusing.
118120
SCMFileSystem tipFS = SCMFileSystem.of(scmSource, head, tip);
119121
if (tipFS != null) {
120122
String tipScript = null;
@@ -124,9 +126,7 @@ public SCMBinder(String scriptPath) {
124126
listener.error("Could not compare lightweight checkout of trusted revision").println(Functions.printThrowable(x).trim());
125127
}
126128
if (tipScript != null && !script.equals(tipScript)) {
127-
listener.annotate(new WarningNote());
128-
listener.getLogger().println(Messages.ReadTrustedStep__has_been_modified_in_an_untrusted_revis(scriptPath));
129-
// TODO JENKINS-45970 consider aborting instead, at least optionally
129+
throw new AbortException(Messages.ReadTrustedStep__has_been_modified_in_an_untrusted_revis(scriptPath));
130130
}
131131
}
132132
}
@@ -165,22 +165,4 @@ public SCMBinder(String scriptPath) {
165165

166166
}
167167

168-
// TODO seems there is no general-purpose ConsoleNote which simply wraps markup in specified HTML
169-
@SuppressWarnings("rawtypes")
170-
public static class WarningNote extends ConsoleNote {
171-
172-
@Override public ConsoleAnnotator annotate(Object context, MarkupText text, int charPos) {
173-
text.addMarkup(0, text.length(), "<span class='warning-inline'>", "</span>");
174-
return null;
175-
}
176-
177-
@Extension public static final class DescriptorImpl extends ConsoleAnnotationDescriptor {
178-
@NonNull
179-
@Override public String getDisplayName() {
180-
return "Multibranch warnings";
181-
}
182-
}
183-
184-
}
185-
186168
}

src/test/java/org/jenkinsci/plugins/workflow/multibranch/SCMBinderTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ public static void assertRevisionAction(WorkflowRun build) {
237237

238238
@Test public void untrustedRevisions() throws Exception {
239239
sampleGitRepo.init();
240-
sampleGitRepo.write("Jenkinsfile", "node {checkout scm; echo readFile('file')}");
240+
String masterJenkinsfile = "node {checkout scm; echo readFile('file')}";
241+
sampleGitRepo.write("Jenkinsfile", masterJenkinsfile);
241242
sampleGitRepo.write("file", "initial content");
242243
sampleGitRepo.git("add", "Jenkinsfile");
243244
sampleGitRepo.git("commit", "--all", "--message=flow");
@@ -262,8 +263,20 @@ public static void assertRevisionAction(WorkflowRun build) {
262263
assertNotNull(b);
263264
assertEquals(1, b.getNumber());
264265
assertRevisionAction(b);
265-
r.assertBuildStatusSuccess(b);
266+
r.assertBuildStatus(Result.FAILURE, b);
266267
r.assertLogContains(Messages.ReadTrustedStep__has_been_modified_in_an_untrusted_revis("Jenkinsfile"), b);
268+
r.assertLogContains("not trusting", b);
269+
SCMBinder.IGNORE_UNTRUSTED_EDITS = true;
270+
try {
271+
b = r.buildAndAssertSuccess(p);
272+
r.assertLogContains("subsequent content", b);
273+
r.assertLogContains("not trusting", b);
274+
} finally {
275+
SCMBinder.IGNORE_UNTRUSTED_EDITS = false;
276+
}
277+
sampleGitRepo.write("Jenkinsfile", masterJenkinsfile);
278+
sampleGitRepo.git("commit", "--all", "--message=meekly submitting");
279+
b = r.buildAndAssertSuccess(p);
267280
r.assertLogContains("subsequent content", b);
268281
r.assertLogContains("not trusting", b);
269282
}

0 commit comments

Comments
 (0)