Skip to content

Commit ae1520e

Browse files
committed
prevent accidential admin auto-approval
1 parent 3d3347a commit ae1520e

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/main/java/org/jenkinsci/plugins/scriptsecurity/scripts/ScriptApproval.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,8 @@ boolean isEmpty() {
581581
pendingClasspathEntries.isEmpty();
582582
}
583583

584-
/**
584+
585+
/**
585586
* Used when someone is configuring a script.
586587
* Typically you would call this from a {@link DataBoundConstructor}.
587588
* It should also be called from a {@code readResolve} method (which may then simply return {@code this}),
@@ -594,15 +595,17 @@ boolean isEmpty() {
594595
* @param language the language in which it is written
595596
* @param context any additional information about how where or by whom this is being configured
596597
* @param approveIfAdmin indicates whether script should be approved if current user has admin permissions
598+
* @param ignoreAdmin indicates whether auto approval should be ignored, regardless of any configurations.
597599
* @return {@code script}, for convenience
598600
*/
599-
public synchronized String configuring(@NonNull String script, @NonNull Language language, @NonNull ApprovalContext context, boolean approveIfAdmin) {
601+
public synchronized String configuring(@NonNull String script, @NonNull Language language, @NonNull ApprovalContext context, boolean approveIfAdmin, boolean ignoreAdmin) {
600602
final ConversionCheckResult result = checkAndConvertApprovedScript(script, language);
601603
if (!result.approved) {
602-
if (!Jenkins.get().isUseSecurity() ||
604+
if (!Jenkins.get().isUseSecurity() ||
603605
(ALLOW_ADMIN_APPROVAL_ENABLED &&
604606
((Jenkins.getAuthentication2() != ACL.SYSTEM2 && Jenkins.get().hasPermission(Jenkins.ADMINISTER))
605-
&& (ADMIN_AUTO_APPROVAL_ENABLED || approveIfAdmin)))) {
607+
&& (ADMIN_AUTO_APPROVAL_ENABLED || approveIfAdmin)
608+
&& !ignoreAdmin))) {
606609
approvedScriptHashes.add(result.newHash);
607610
//Pending scripts are not stored with a precalculated hash, so no need to remove any old hashes
608611
removePendingScript(result.newHash);
@@ -618,6 +621,14 @@ public synchronized String configuring(@NonNull String script, @NonNull Language
618621
return script;
619622
}
620623

624+
/**
625+
* @deprecated Use {@link #configuring(String, Language, ApprovalContext, boolean, boolean)} instead
626+
*/
627+
@Deprecated
628+
public synchronized String configuring(@NonNull String script, @NonNull Language language, @NonNull ApprovalContext context, boolean approveIfAdmin) {
629+
return configuring(script, language, context, approveIfAdmin, false);
630+
}
631+
621632
/**
622633
* @deprecated Use {@link #configuring(String, Language, ApprovalContext, boolean)} instead
623634
*/
@@ -644,7 +655,9 @@ public synchronized String using(@NonNull String script, @NonNull Language langu
644655
// Usually. this method is called once the job configuration with the script is saved.
645656
// If a script was previously pending and is now deleted, however, it would require to re-configure the job.
646657
// That's why we call it again if it is unapproved in a running job.
647-
this.configuring(script, language, ApprovalContext.create(), false);
658+
// 'ignoreAdmin' is set to true, so that administrators
659+
// do not accidentally approve scripts when running a job.
660+
this.configuring(script, language, ApprovalContext.create(), false, true);
648661
throw new UnapprovedUsageException(result.newHash);
649662
}
650663
return script;

0 commit comments

Comments
 (0)