2525package org .jenkinsci .plugins .workflow .multibranch ;
2626
2727import edu .umd .cs .findbugs .annotations .NonNull ;
28+ import hudson .AbortException ;
2829import hudson .Extension ;
2930import hudson .Functions ;
30- import hudson .MarkupText ;
31- import hudson .console .ConsoleAnnotationDescriptor ;
32- import hudson .console .ConsoleAnnotator ;
33- import hudson .console .ConsoleNote ;
3431import hudson .model .Action ;
3532import hudson .model .Descriptor ;
3633import hudson .model .DescriptorVisibilityFilter ;
4643import jenkins .scm .api .SCMRevision ;
4744import jenkins .scm .api .SCMRevisionAction ;
4845import jenkins .scm .api .SCMSource ;
46+ import jenkins .util .SystemProperties ;
4947import org .jenkinsci .plugins .workflow .cps .CpsFlowDefinition ;
5048import org .jenkinsci .plugins .workflow .cps .CpsScmFlowDefinition ;
5149import org .jenkinsci .plugins .workflow .flow .FlowDefinition ;
6159class 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}
0 commit comments