-
Notifications
You must be signed in to change notification settings - Fork 15
SLING-12739 - selectively hide scripts and servlets from the Sling resolver #58
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
bdelacretaz
wants to merge
7
commits into
master
Choose a base branch
from
issues/SLING-12739
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
15e6fca
SLING-12739 - initial implementation with incomplete tests
bdelacretaz 602d094
SLING-12739 - unit testing
bdelacretaz ee46e02
SLING-12739 add hidden servlet fallback tests
bdelacretaz 9a90570
SLING-12739 - add missing license
bdelacretaz 2ebbb8e
SLING-12739 - setup for external, independent tests
bdelacretaz d559fc0
SLING-12739 - remove ext-it tests
bdelacretaz 4d36bb8
SLING-12739 - rebase to master
bdelacretaz 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
123 changes: 123 additions & 0 deletions
123
...st/java/org/apache/sling/servlets/resolver/internal/resourcehiding/ServletHidingTest.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,123 @@ | ||
/* | ||
* 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.sling.servlets.resolver.internal.resourcehiding; | ||
|
||
import javax.servlet.Servlet; | ||
import javax.servlet.http.HttpServlet; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Predicate; | ||
|
||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.request.builder.Builders; | ||
import org.apache.sling.api.resource.PersistenceException; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ResourceResolver; | ||
import org.apache.sling.api.resource.ResourceUtil; | ||
import org.apache.sling.servlets.resolver.internal.SlingServletResolverTestBase; | ||
import org.apache.sling.servlets.resolver.internal.helper.HelperTestBase; | ||
import org.apache.sling.servlets.resolver.internal.resource.MockServletResource; | ||
import org.apache.sling.servlets.resolver.internal.resource.ServletResource; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.osgi.framework.Bundle; | ||
|
||
import static org.junit.Assert.fail; | ||
|
||
public class ServletHidingTest extends SlingServletResolverTestBase { | ||
|
||
private static final String SERVLET_EXTENSION = "html"; | ||
private static final String RESOURCE_TYPE = | ||
ServletHidingTest.class.getSimpleName() + "/" + UUID.randomUUID().toString(); | ||
|
||
protected static class TestServlet extends HttpServlet {} | ||
; | ||
|
||
private void setServletHidingFilter(Predicate<String> predicate) throws Exception { | ||
final Field predicateField = servletResolver.getClass().getDeclaredField("resourceHidingPredicate"); | ||
predicateField.setAccessible(true); | ||
predicateField.set(servletResolver, predicate); | ||
} | ||
|
||
@Override | ||
protected void defineTestServlets(Bundle bundle) { | ||
final TestServlet testServlet = new TestServlet(); | ||
String path = "/" + RESOURCE_TYPE + "/" + ResourceUtil.getName(RESOURCE_TYPE) + ".servlet"; | ||
Map<String, Object> props = new HashMap<>(); | ||
props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, path); | ||
props.put("sling:resourceSuperType", ServletResource.DEFAULT_RESOURCE_SUPER_TYPE); | ||
props.put(MockServletResource.PROP_SERVLET, testServlet); | ||
HelperTestBase.addOrReplaceResource(mockResourceResolver, path, props); | ||
try { | ||
// commit so the resource is visible to the script resource resolver | ||
// that is created later and can't see the temporary resources in | ||
// this resource resolver | ||
mockResourceResolver.commit(); | ||
} catch (PersistenceException e) { | ||
fail("Did not expect a persistence exception: " + e.getMessage()); | ||
} | ||
} | ||
|
||
private void assertResolvesToTestServlet(String info, boolean expectMatch) { | ||
final Resource resource = Mockito.mock(Resource.class); | ||
Mockito.when(resource.getResourceType()).thenReturn(RESOURCE_TYPE); | ||
Mockito.when(resource.getPath()).thenReturn("/" + RESOURCE_TYPE); | ||
|
||
final SlingHttpServletRequest request = Builders.newRequestBuilder(resource) | ||
.withExtension(SERVLET_EXTENSION) | ||
.build(); | ||
final Servlet s = servletResolver.resolveServlet(request); | ||
if (expectMatch != s.getClass().equals(TestServlet.class)) { | ||
if (expectMatch) { | ||
fail(info + ": expected to resolve to our test servlet, got " | ||
+ s.getClass().getName()); | ||
} else { | ||
fail(info + ": didn't expect to resolve to our test servlet"); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testHideAndSeek() throws Exception { | ||
final AtomicBoolean hide = new AtomicBoolean(); | ||
final Predicate<String> pred = (ignoredPath) -> hide.get(); | ||
|
||
// No filtering | ||
setServletHidingFilter(null); | ||
assertResolvesToTestServlet("before hiding", true); | ||
|
||
// Filter with our predicate | ||
setServletHidingFilter(pred); | ||
hide.set(true); | ||
assertResolvesToTestServlet("hidden by our Predicate", false); | ||
hide.set(false); | ||
assertResolvesToTestServlet("Predicate active but returns false", true); | ||
|
||
// Back to no filtering, (paranoid) check that it's really gone | ||
setServletHidingFilter(null); | ||
hide.set(false); | ||
assertResolvesToTestServlet("No Predicate set, hide=false", true); | ||
hide.set(true); | ||
assertResolvesToTestServlet("No Predicate set, hide=true", true); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...test/java/org/apache/sling/servlets/resolver/it/resourcehiding/BasicResourceHidingIT.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,49 @@ | ||
/* | ||
* 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.sling.servlets.resolver.it.resourcehiding; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.ops4j.pax.exam.junit.PaxExam; | ||
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; | ||
import org.ops4j.pax.exam.spi.reactors.PerClass; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(PaxExam.class) | ||
@ExamReactorStrategy(PerClass.class) | ||
public class BasicResourceHidingIT extends ResourceHidingITBase { | ||
|
||
@Before | ||
public void setupPredicate() { | ||
registerPredicate((path) -> path.contains(EXT_B)); | ||
} | ||
|
||
@Test | ||
public void testOnlyApresent() throws Exception { | ||
assertEquals(0, hiddenResourcesCount); | ||
assertTestServlet("/." + EXT_A, EXT_A); | ||
assertEquals(0, hiddenResourcesCount); | ||
assertTestServlet("/." + EXT_B, HttpServletResponse.SC_NOT_FOUND); | ||
assertEquals(1, hiddenResourcesCount); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...st/java/org/apache/sling/servlets/resolver/it/resourcehiding/HiddenServletFallbackIT.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,47 @@ | ||
/* | ||
* 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.sling.servlets.resolver.it.resourcehiding; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.ops4j.pax.exam.junit.PaxExam; | ||
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; | ||
import org.ops4j.pax.exam.spi.reactors.PerClass; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(PaxExam.class) | ||
@ExamReactorStrategy(PerClass.class) | ||
public class HiddenServletFallbackIT extends ResourceHidingITBase { | ||
|
||
@Before | ||
public void setupPredicate() { | ||
registerPredicate((path) -> path.contains(SEL_A)); | ||
} | ||
|
||
@Test | ||
public void testFallbackToExtA() throws Exception { | ||
assertEquals(0, hiddenResourcesCount); | ||
assertTestServlet("/." + SEL_A + "." + EXT_A, EXT_A); | ||
assertEquals(1, hiddenResourcesCount); | ||
assertTestServlet("/." + EXT_A, EXT_A); | ||
assertEquals(1, hiddenResourcesCount); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/org/apache/sling/servlets/resolver/it/resourcehiding/NoHidingIT.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,53 @@ | ||
/* | ||
* 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.sling.servlets.resolver.it.resourcehiding; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.ops4j.pax.exam.junit.PaxExam; | ||
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; | ||
import org.ops4j.pax.exam.spi.reactors.PerClass; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(PaxExam.class) | ||
@ExamReactorStrategy(PerClass.class) | ||
public class NoHidingIT extends ResourceHidingITBase { | ||
|
||
@After | ||
public void checkNothingHidden() { | ||
assertEquals(0, hiddenResourcesCount); | ||
} | ||
|
||
@Test | ||
public void testExtApresent() throws Exception { | ||
assertTestServlet("/." + EXT_A, EXT_A); | ||
} | ||
|
||
@Test | ||
public void testExtBpresent() throws Exception { | ||
assertTestServlet("/." + EXT_B, EXT_B); | ||
} | ||
|
||
@Test | ||
public void testSelApresent() throws Exception { | ||
assertTestServlet("/." + SEL_A + "." + EXT_A, SEL_A); | ||
} | ||
} |
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.
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.
I would explicitly mention that this method requires a non-null parameter (and that for this reason null-checks are missing).