-
Notifications
You must be signed in to change notification settings - Fork 861
WW-5674 Cut the per-call allocations in SecurityMemberAccess package matching #1830
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
Open
lukaszlenart
wants to merge
10
commits into
main
Choose a base branch
from
WW-5674-isclassbelongstopackages-allocation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,287
−10
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7c20023
WW-5674 docs(ognl): add design for allocation-free isClassBelongsToPa…
lukaszlenart 05a9368
WW-5674 docs(ognl): add implementation plan for allocation-free packa…
lukaszlenart 197071d
WW-5674 test(ognl): characterise SecurityMemberAccess package matching
lukaszlenart a8f6b4c
WW-5674 perf(ognl): resolve package names via cached Class.getPackage…
lukaszlenart 8237a1f
WW-5674 perf(ognl): walk package names in place instead of building p…
lukaszlenart 34e5cf8
WW-5674 perf(ognl): match both allowlist package sets in one walk
lukaszlenart 866722b
WW-5674 test(ognl): assert package-boundary case against the live gate
lukaszlenart 2543dd8
WW-5674 docs(ognl): align the design doc with the package-private ove…
lukaszlenart 25ac949
WW-5674 docs(ognl): correct overstated claims and record the trailing…
lukaszlenart 33627a9
WW-5674 docs(ognl): note the naming cleanup deferred to WW-5678
lukaszlenart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
224 changes: 224 additions & 0 deletions
224
core/src/test/java/org/apache/struts2/ognl/SecurityMemberAccessPackageMatchingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.struts2.ognl; | ||
|
|
||
| import org.apache.struts2.util.ConfigParseUtil; | ||
| import org.junit.Test; | ||
|
|
||
| import java.lang.reflect.Proxy; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static java.util.Collections.emptySet; | ||
| import static org.apache.struts2.ognl.SecurityMemberAccess.isClassBelongsToPackages; | ||
| import static org.apache.struts2.ognl.SecurityMemberAccess.toPackageName; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Characterisation and equivalence tests for the static package-matching helpers in | ||
| * {@link SecurityMemberAccess}, covering WW-5674. | ||
| * <p> | ||
| * These helpers gate OGNL member access, so the rewrite in WW-5674 must be exactly | ||
| * behaviour-preserving. That is proven here by running the replaced implementation | ||
| * side by side with the new one over a matrix of inputs. | ||
| */ | ||
| public class SecurityMemberAccessPackageMatchingTest { | ||
|
|
||
| /** | ||
| * The implementation replaced by WW-5674, retained verbatim apart from taking the package | ||
| * name directly instead of a {@link Class}. Used as the reference oracle for the rewrite. | ||
| */ | ||
| private static boolean legacyPrefixMatch(String packageName, Set<String> matchingPackages) { | ||
| List<String> packageParts = List.of(packageName.split("\\.")); | ||
| return IntStream.range(0, packageParts.size()) | ||
| .mapToObj(i -> String.join(".", packageParts.subList(0, i + 1))) | ||
| .anyMatch(matchingPackages::contains); | ||
| } | ||
|
|
||
| /** | ||
| * The {@code toPackageName} implementation replaced by WW-5674, retained as the reference oracle. | ||
| */ | ||
| private static String legacyToPackageName(Class<?> clazz) { | ||
| if (clazz.getPackage() == null) { | ||
| return ""; | ||
| } | ||
| return clazz.getPackage().getName(); | ||
| } | ||
|
|
||
| /** | ||
| * Package-name shapes. Deliberately excludes trailing-dot inputs such as {@code "a.b."}: | ||
| * {@code split} drops trailing empty segments where an index walk would not, and | ||
| * {@code Class.getPackage().getName()} cannot produce a trailing dot, so the shape is | ||
| * unreachable through every caller. See the spec's "Verified current semantics" section. | ||
| */ | ||
| private static final List<String> PACKAGE_NAMES = List.of( | ||
| "", | ||
| "java", | ||
| "a.b.c", | ||
| "a..b", | ||
| ".a", | ||
| "org.apache.struts2", | ||
| "org.apache.struts2.ognl", | ||
| "org.apache.struts2x", | ||
| "java.io", | ||
| "java.io.tmp", | ||
| "javax.servlet.http"); | ||
|
|
||
| private static final List<Set<String>> CANDIDATE_SETS = List.of( | ||
| emptySet(), | ||
| Set.of(""), | ||
| Set.of("java"), | ||
| Set.of("java.io"), | ||
| Set.of("org.apache.struts2"), | ||
| Set.of("a"), | ||
| Set.of("a."), | ||
| Set.of("a.b"), | ||
| Set.of("zzz.not.matching"), | ||
| Set.of("java.io", "org.apache.struts2", "javax")); | ||
|
|
||
| private static List<Class<?>> classShapes() throws Exception { | ||
| return List.of( | ||
| String.class, | ||
| Map.Entry.class, | ||
| SecurityMemberAccess.class, | ||
| Class.forName("PackagelessAction"), | ||
| int.class, | ||
| void.class, | ||
| int[].class, | ||
| String[].class, | ||
| String[][].class, | ||
| ((Runnable) () -> { | ||
| }).getClass(), | ||
| Proxy.newProxyInstance( | ||
| SecurityMemberAccessPackageMatchingTest.class.getClassLoader(), | ||
| new Class<?>[]{Runnable.class}, | ||
| (proxy, method, args) -> null).getClass()); | ||
| } | ||
|
|
||
| @Test | ||
| public void siblingPackageWithSharedCharacterPrefixDoesNotMatch() { | ||
| Set<String> excluded = Set.of("org.apache.struts2"); | ||
|
|
||
| assertThat(SecurityMemberAccess.isPackageBelongsToPackages("org.apache.struts2x", excluded, emptySet())) | ||
| .as("a sibling package sharing a character prefix must not match (production)") | ||
| .isFalse(); | ||
| assertThat(legacyPrefixMatch("org.apache.struts2x", excluded)) | ||
| .as("a sibling package sharing a character prefix must not match (legacy oracle)") | ||
| .isFalse(); | ||
|
|
||
| assertThat(SecurityMemberAccess.isPackageBelongsToPackages("org.apache.struts2", excluded, emptySet())) | ||
| .as("an exact match must match (production)") | ||
| .isTrue(); | ||
| assertThat(legacyPrefixMatch("org.apache.struts2", excluded)) | ||
| .as("an exact match must match (legacy oracle)") | ||
| .isTrue(); | ||
|
|
||
| assertThat(SecurityMemberAccess.isPackageBelongsToPackages("org.apache.struts2.ognl", excluded, emptySet())) | ||
| .as("a sub-package must match (production)") | ||
| .isTrue(); | ||
| assertThat(legacyPrefixMatch("org.apache.struts2.ognl", excluded)) | ||
| .as("a sub-package must match (legacy oracle)") | ||
| .isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void dotOnlyConfigurationYieldsEmptyStringPackageName() { | ||
| assertThat(ConfigParseUtil.toPackageNamesSet(".")) | ||
| .as("struts.excludedPackageNames=\".\" strips to the empty string") | ||
| .containsExactly(""); | ||
| } | ||
|
|
||
| @Test | ||
| public void defaultPackageMatchesOnlyWhenEmptyStringConfigured() throws Exception { | ||
| Class<?> packageless = Class.forName("PackagelessAction"); | ||
|
|
||
| assertThat(toPackageName(packageless)).isEmpty(); | ||
| assertThat(isClassBelongsToPackages(packageless, Set.of(""))) | ||
| .as("a default-package class is matched by the empty-string entry") | ||
| .isTrue(); | ||
| assertThat(isClassBelongsToPackages(packageless, Set.of("java"))) | ||
| .as("a default-package class is not matched by an unrelated entry") | ||
| .isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void toPackageNameMatchesLegacyAcrossClassShapes() throws Exception { | ||
| for (Class<?> clazz : classShapes()) { | ||
| assertThat(toPackageName(clazz)) | ||
| .as("toPackageName(%s)", clazz.getName()) | ||
| .isEqualTo(legacyToPackageName(clazz)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void arraysAndPrimitivesResolveToTheEmptyPackage() { | ||
| assertThat(toPackageName(int.class)).isEmpty(); | ||
| assertThat(toPackageName(void.class)).isEmpty(); | ||
| assertThat(toPackageName(int[].class)).isEmpty(); | ||
| assertThat(toPackageName(String[].class)).isEmpty(); | ||
| assertThat(toPackageName(String[][].class)).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void classEntryPointMatchesLegacyAcrossCandidateSets() throws Exception { | ||
| for (Class<?> clazz : classShapes()) { | ||
| for (Set<String> candidates : CANDIDATE_SETS) { | ||
| assertThat(isClassBelongsToPackages(clazz, candidates)) | ||
| .as("clazz=[%s] candidates=%s", clazz.getName(), candidates) | ||
| .isEqualTo(legacyPrefixMatch(legacyToPackageName(clazz), candidates)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void indexWalkMatchesLegacyAcrossPackageNameShapes() { | ||
| for (String packageName : PACKAGE_NAMES) { | ||
| for (Set<String> candidates : CANDIDATE_SETS) { | ||
| assertThat(SecurityMemberAccess.isPackageBelongsToPackages(packageName, candidates, emptySet())) | ||
| .as("packageName=[%s] candidates=%s", packageName, candidates) | ||
| .isEqualTo(legacyPrefixMatch(packageName, candidates)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void bothSetsEmptyShortCircuitsToFalse() { | ||
| for (String packageName : PACKAGE_NAMES) { | ||
| assertThat(SecurityMemberAccess.isPackageBelongsToPackages(packageName, emptySet(), emptySet())) | ||
| .as("packageName=[%s] with no configured packages", packageName) | ||
| .isFalse(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void twoSetOverloadEqualsDisjunctionOfSingleSetCalls() throws Exception { | ||
| for (Class<?> clazz : classShapes()) { | ||
| for (Set<String> first : CANDIDATE_SETS) { | ||
| for (Set<String> second : CANDIDATE_SETS) { | ||
| assertThat(isClassBelongsToPackages(clazz, first, second)) | ||
| .as("clazz=[%s] first=%s second=%s", clazz.getName(), first, second) | ||
| .isEqualTo(isClassBelongsToPackages(clazz, first) | ||
| || isClassBelongsToPackages(clazz, second)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.