-
Notifications
You must be signed in to change notification settings - Fork 196
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
Cache GroovySourceFileAllowlist
responses and remove entries from the default allowlist that are unrelated to WithScriptDescriptor.WithScriptAllowlist
#829
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,10 +37,11 @@ | |
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import jenkins.util.SystemProperties; | ||
|
@@ -138,12 +139,7 @@ public Enumeration<URL> getResources(String name) throws IOException { | |
|
||
private static boolean isAllowed(URL url) { | ||
String urlString = url.toString(); | ||
for (GroovySourceFileAllowlist allowlist : GroovySourceFileAllowlist.all()) { | ||
if (allowlist.isAllowed(urlString)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
return ExtensionList.lookupSingleton(AllowedGroovyResourcesCache.class).isAllowed(urlString); | ||
} | ||
|
||
private static boolean endsWithIgnoreCase(String value, String suffix) { | ||
|
@@ -152,6 +148,27 @@ private static boolean endsWithIgnoreCase(String value, String suffix) { | |
} | ||
} | ||
|
||
@Extension | ||
@SuppressFBWarnings(value = "NP_BOOLEAN_RETURN_NULL", justification = "intentionally not caching negative results") | ||
public static class AllowedGroovyResourcesCache { | ||
jglick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private final Map<String, Boolean> cache = new ConcurrentHashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At most there would be around 50 entries in the map if you used every single plugin that has Groovy source files, so I don't think we need anything fancy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually IDK if that's right, let me see if we get here with Groovy's speculative class loading as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never mind, we do really only get here for the weird Groovy DSL script files in Jenkins plugins or other attempts to directly load Groovy source files on the classpath. I guess that is obvious in hindsight, otherwise we would see a ton of log spam for otherwise non-existent classes. ( |
||
|
||
public boolean isAllowed(String groovySourceFileUrl) { | ||
Boolean cachedResult = cache.computeIfAbsent(groovySourceFileUrl, url -> { | ||
for (GroovySourceFileAllowlist allowlist : GroovySourceFileAllowlist.all()) { | ||
if (allowlist.isAllowed(url)) { | ||
return true; | ||
} | ||
} | ||
// In practice we should only get here with files that are allowed, so we don't cache negative | ||
// results in case it would cause problems with unusual Pipelines that reference Groovy source | ||
// files directly in combination with dynamically installed plugins. | ||
return null; | ||
}); | ||
return Boolean.TRUE.equals(cachedResult); | ||
} | ||
} | ||
|
||
/** | ||
* Allows Groovy source files used to implement DSLs in plugins that were created before | ||
* {@link GroovySourceFileAllowlist} was introduced. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
# This list is ordered from most popular to least popular plugin to minimize performance impact. | ||
# pipeline-model-definition | ||
/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/AnyScript.groovy | ||
/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/LabelScript.groovy | ||
/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/NoneScript.groovy | ||
/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AbstractChangelogConditionalScript.groovy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AllOfConditionalScript.groovy | ||
/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AnyOfConditionalScript.groovy | ||
/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditionalScript.groovy | ||
|
@@ -18,11 +16,7 @@ | |
/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/NotConditionalScript.groovy | ||
/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/TagConditionalScript.groovy | ||
/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/TriggeredByConditionalScript.groovy | ||
# pipeline-model-extensions | ||
/org/jenkinsci/plugins/pipeline/modeldefinition/agent/CheckoutScript.groovy | ||
Comment on lines
-21
to
-22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
# docker-workflow | ||
/org/jenkinsci/plugins/docker/workflow/Docker.groovy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/org/jenkinsci/plugins/docker/workflow/declarative/AbstractDockerPipelineScript.groovy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/org/jenkinsci/plugins/docker/workflow/declarative/DockerPipelineFromDockerfileScript.groovy | ||
/org/jenkinsci/plugins/docker/workflow/declarative/DockerPipelineScript.groovy | ||
# kubernetes | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why, but this annotation actually does have to be on the class. If it's on the
isAllowed
method, SpotBugs still complains.