You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2018/11/20 14:31:50 UTC
[sling-org-apache-sling-capabilities] 02/02: SLING-8120 - remove
path restrictions
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-capabilities.git
commit da49ea5616ac18ae3f7e21f929f5a856ab11f843
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Tue Nov 20 15:31:30 2018 +0100
SLING-8120 - remove path restrictions
---
.../capabilities/internal/CapabilitiesServlet.java | 34 ++----------------
.../internal/CapabilitesServletTest.java | 40 +++-------------------
2 files changed, 6 insertions(+), 68 deletions(-)
diff --git a/src/main/java/org/apache/sling/capabilities/internal/CapabilitiesServlet.java b/src/main/java/org/apache/sling/capabilities/internal/CapabilitiesServlet.java
index 8e7b64c..4e0ab9e 100644
--- a/src/main/java/org/apache/sling/capabilities/internal/CapabilitiesServlet.java
+++ b/src/main/java/org/apache/sling/capabilities/internal/CapabilitiesServlet.java
@@ -47,30 +47,11 @@ property = {
"sling.servlet.methods=GET",
"sling.servlet.extensions=json"
})
-@Designate(ocd = CapabilitiesServlet.Config.class)
public class CapabilitiesServlet extends SlingSafeMethodsServlet {
- @ObjectClassDefinition(
- name = "Apache Sling Capabilities Servlet",
- description = "Provides information about Sling capabilities"
- )
- public static @interface Config {
- @AttributeDefinition(
- name = "Resource Path Patterns",
- description = "A set of (java) regular expression patterns that the resource path must match for this servlet to execute"
- )
- String [] resourcePathPatterns() default {};
- }
-
private final List<CapabilitiesSource> sources = new CopyOnWriteArrayList<>();
- private RegexFilter pathFilter;
public static final String NAMESPACES_PROP = "namespace_patterns";
- @Activate
- protected void activate(Config cfg, ComponentContext ctx) {
- pathFilter = new RegexFilter(cfg.resourcePathPatterns());
- }
-
@Override
public String toString() {
return getClass().getSimpleName() + ": " + sources.size() + " " + CapabilitiesSource.class.getSimpleName() + " active";
@@ -81,19 +62,8 @@ public class CapabilitiesServlet extends SlingSafeMethodsServlet {
final Resource resource = request.getResource();
- // Resource Path must match a configurable set of patterns, to prevent
- // users from getting this information by just creating a resource anywhere
- // with our resource type. The idea is that those paths will have suitable
- // access control (readonly for users) to control exactly which capabilities
- // are exposed.
- final String path = resource.getPath();
- if(!pathFilter.accept(path)) {
- response.sendError(HttpServletResponse.SC_FORBIDDEN, "Invalid path " + path);
- return;
- }
-
- // Resource must define which namespaces are exposed, also for
- // security reasons, to make sure administrators think about
+ // Resource must define which namespaces are exposed,
+ // to make sure administrators think about
// what's exposed
final ValueMap m = resource.adaptTo(ValueMap.class);
final String [] namespacePatterns = m.get(NAMESPACES_PROP, String[].class);
diff --git a/src/test/java/org/apache/sling/capabilities/internal/CapabilitesServletTest.java b/src/test/java/org/apache/sling/capabilities/internal/CapabilitesServletTest.java
index 1d3546d..9534083 100644
--- a/src/test/java/org/apache/sling/capabilities/internal/CapabilitesServletTest.java
+++ b/src/test/java/org/apache/sling/capabilities/internal/CapabilitesServletTest.java
@@ -60,22 +60,12 @@ public class CapabilitesServletTest {
private BundleContext bundleContext;
private ResourceResolver resourceResolver;
- // The CapabilitiesServlet must reject resource paths which are outside of this
- private static final String [] AUTHORIZED_PATHS_PATTERNS = {
- ".*/ok$",
- "/var/.*"
- };
-
// The CapabilitiesServlet must omit capabilities outside of these namespaces
private static final String [] NAMESPACE_PATTERNS = {
"[EF]",
"G"
};
- private final String DENIED_PATH = "/denied";
- private final String OK_PATH = "/denied/but/ok";
- private final String VAR_PATH = "/var/something";
-
private static final CapabilitiesSource [] SOURCES = {
new MockSource("F", 2),
new MockSource("G", 43),
@@ -85,14 +75,6 @@ public class CapabilitesServletTest {
@Before
public void setup() throws IOException {
- // Configure allowed path patterns
- final ConfigurationAdmin ca = context.getService(ConfigurationAdmin.class);
- assertNotNull("Expecting a ConfigurationAdmin service", ca);
- final Configuration cfg = ca.getConfiguration(CapabilitiesServlet.class.getName());
- final Dictionary<String, Object> props = new Hashtable<>();
- props.put("resourcePathPatterns", AUTHORIZED_PATHS_PATTERNS);
- cfg.update(props);
-
servlet = new CapabilitiesServlet();
bundleContext = MockOsgi.newBundleContext();
resourceResolver = MockSling.newResourceResolver(bundleContext);
@@ -107,13 +89,13 @@ public class CapabilitesServletTest {
context.registerInjectActivateService(servlet);
}
- private MockSlingHttpServletRequest requestFor(String path, boolean withNamespacePatterns) {
+ private MockSlingHttpServletRequest testRequest(boolean withNamespacePatterns) {
final MockSlingHttpServletRequest req = new MockSlingHttpServletRequest(resourceResolver);
final Map<String, Object> props = new HashMap<>();
if(withNamespacePatterns) {
props.put(CapabilitiesServlet.NAMESPACES_PROP, NAMESPACE_PATTERNS);
}
- final MockResource res = new MockResource(path, props, resourceResolver);
+ final MockResource res = new MockResource("/", props, resourceResolver);
req.setResource(res);
return req;
}
@@ -132,30 +114,16 @@ public class CapabilitesServletTest {
}
@Test
- public void testDeniedPath() throws ServletException, IOException {
- MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse();
- servlet.service(requestFor(DENIED_PATH, true), resp);
- assertEquals(403, resp.getStatus());
- }
-
- @Test
- public void testOkPath() throws ServletException, IOException {
- MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse();
- servlet.service(requestFor(OK_PATH, true), resp);
- assertEquals(200, resp.getStatus());
- }
-
- @Test
public void testMissingNamespaceProperty() throws ServletException, IOException {
MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse();
- servlet.service(requestFor(OK_PATH, false), resp);
+ servlet.service(testRequest(false), resp);
assertEquals(403, resp.getStatus());
}
@Test
public void testServletResponse() throws ServletException, IOException {
MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse();
- servlet.service(requestFor(VAR_PATH, true), resp);
+ servlet.service(testRequest(true), resp);
assertEquals(200, resp.getStatus());
// Just verify that both sources are taken into account