Skip to content

Make WithScriptAllowlist look up instances of WithScriptDescriptor dynamically instead of registering allowed scripts directly #686

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 @@ -29,26 +29,24 @@
import org.jenkinsci.plugins.structs.describable.DescribableModel;
import org.jenkinsci.plugins.workflow.cps.GroovySourceFileAllowlist;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.ExtensionList;
import java.net.URL;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Descriptor for {@link WithScriptDescribable}.
*
* @author Andrew Bayer
*/
public abstract class WithScriptDescriptor<T extends WithScriptDescribable<T>> extends Descriptor<T> {

protected WithScriptDescriptor() {
ExtensionList.lookupSingleton(WithScriptAllowlist.class).registerScript(getScriptResource());
}
private static final Logger LOGGER = Logger.getLogger(WithScriptDescriptor.class.getName());

/**
* The name for this type. Defaults to the first string in the {@code Symbol} on the class.
Expand All @@ -75,10 +73,9 @@ protected WithScriptDescriptor() {
}

@SuppressFBWarnings(value = "UI_INHERITANCE_UNSAFE_GETRESOURCE", justification = "We intentionally want to use the class loader for the subclass in this case")
private @NonNull URL getScriptResource() {
private @CheckForNull URL getScriptResource() {
String scriptFile = '/' + getScriptClass().replace('.', '/') + ".groovy";
return Objects.requireNonNull(getClass().getResource(scriptFile),
() -> "Unable to find resource file: " + scriptFile);
return getClass().getResource(scriptFile);
}

/**
Expand All @@ -104,15 +101,17 @@ public T newInstance() throws Exception {

@Extension
public static class WithScriptAllowlist extends GroovySourceFileAllowlist {
private final Set<String> scriptUrls = new HashSet<>();

private void registerScript(URL scriptUrl) {
scriptUrls.add(scriptUrl.toString());
}

@Override
public boolean isAllowed(String groovyResourceUrl) {
return scriptUrls.contains(groovyResourceUrl);
for (WithScriptDescriptor descriptor : ExtensionList.lookup(WithScriptDescriptor.class)) {
URL scriptResource = descriptor.getScriptResource();
Comment on lines +106 to +107
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be slower than before. I suspect it would be ok as-is, but jenkinsci/workflow-cps-plugin#829 will cache all of the allowlist responses, which should make performance a non-issue.

if (scriptResource != null && scriptResource.toString().equals(groovyResourceUrl)) {
return true;
} else {
LOGGER.log(Level.WARNING, () -> "Unable to find Groovy source file for: " + descriptor.getScriptClass());
}
}
return false;
}
}
}