Lukasz Lenart created WW-5674:
---------------------------------
Summary: Make SecurityMemberAccess.isClassBelongsToPackages
allocation-free
Key: WW-5674
URL: https://issues.apache.org/jira/browse/WW-5674
Project: Struts 2
Issue Type: Sub-task
Reporter: Lukasz Lenart
Sub-task of WW-5667.
{{SecurityMemberAccess.isClassBelongsToPackages(Class, Set)}} runs on the OGNL
member-access hot path and allocates heavily on every call:
{code:java}
public static boolean isClassBelongsToPackages(Class[?] clazz, Set[String]
matchingPackages) {
List[String] packageParts = List.of(toPackageName(clazz).split("\\."));
return IntStream.range(0, packageParts.size())
.mapToObj(i -> String.join(".", packageParts.subList(0, i + 1)))
.anyMatch(matchingPackages::contains);
}
{code}
Per call this performs a regex-based {{String.split}}, wraps the result in a
{{List}}, and then builds a brand-new joined {{String}} for every package
prefix. For a class in {{org.apache.struts2.ognl}} that is one array plus four
{{String.join}} allocations.
It is invoked up to four times per {{isAccessible()}} call — once for the
excluded-package check and once for the allowlist check, for both the member's
declaring class and the target class (see {{isExcludedPackageNames}},
{{isClassAllowlisted}}).
This matches JFR sample 1 on the parent ticket:
{noformat}
java.lang.String.split(String)
SecurityMemberAccess.isClassBelongsToPackages(Class, Set) :390
SecurityMemberAccess.isExcludedPackageNames(Class) :386
SecurityMemberAccess.isPackageExcluded(Class) :371
{noformat}
The same prefix-matching can be done by walking the package name backwards from
each {{'.'}} and probing the set with a substring, with no array, stream, or
join allocations.
This is the OGNL security gate, so the change must preserve exact allow/deny
semantics. Equivalence tests are required for: the default package (empty
package name), single-segment packages, exact matches, sub-package matches, and
near-misses that share a character prefix but not a package boundary (e.g.
{{org.apache.struts2x}} must not match {{org.apache.struts2}}).
--
This message was sent by Atlassian Jira
(v8.20.10#820010)