You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2021/08/02 15:17:45 UTC

[sling-org-apache-sling-feature-analyser] branch master updated: SLING-10695 : Create analyser to check for paths in content packages

This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-analyser.git


The following commit(s) were added to refs/heads/master by this push:
     new 2abe9f0  SLING-10695 : Create analyser to check for paths in content packages
2abe9f0 is described below

commit 2abe9f06067211552c8feb17ae57dcd71f686e6b
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Mon Aug 2 17:17:23 2021 +0200

    SLING-10695 : Create analyser to check for paths in content packages
---
 readme.md                                          |  2 +-
 .../task/impl/CheckContentPackagesForPaths.java    | 10 +++++----
 .../impl/CheckContentPackagesForPathsTest.java     | 26 ++++++++++++++++++----
 3 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/readme.md b/readme.md
index 1c89641..f5167c6 100644
--- a/readme.md
+++ b/readme.md
@@ -81,7 +81,7 @@ This analyser checks for allowed and denied paths inside content packages. This
  Configuration key | Allowed values | Description
  ----- | ----- | -----
 `includes` | Content paths | A comma separated list of content paths. If this is specified all content in the content package must match at least one of these.
-`excludes` | Content paths | A comma separated list of content paths. If this is specified all content in the contant package must not match any of these.
+`excludes` | Content paths | A comma separated list of content paths. If this is specified all content in the content package must not match any of these - except it matches an include.
 
 ## `duplicate-symbolic-names`
 
diff --git a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesForPaths.java b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesForPaths.java
index 50243fc..d874fe5 100644
--- a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesForPaths.java
+++ b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesForPaths.java
@@ -44,13 +44,13 @@ public class CheckContentPackagesForPaths implements AnalyserTask {
     public void execute(final AnalyserTaskContext ctx)
     throws Exception {
         final Rules rules = getRules(ctx);
-        
+
         if (rules != null ) {
             for (final ArtifactDescriptor d : ctx.getFeatureDescriptor().getArtifactDescriptors()) {
                 if (d instanceof ContentPackageDescriptor) {
                     checkPackage(ctx, (ContentPackageDescriptor) d, rules);
                 }
-            }    
+            }
         } else {
             ctx.reportError("Configuration for task " + getId() + " is missing.");
         }
@@ -64,7 +64,7 @@ public class CheckContentPackagesForPaths implements AnalyserTask {
     Rules getRules(final AnalyserTaskContext ctx) {
         final String inc = ctx.getConfiguration().get(PROP_INCLUDES);
         final String exc = ctx.getConfiguration().get(PROP_EXCLUDES);
-        
+
         if ( inc != null || exc != null ) {
             final Rules r = new Rules();
             r.includes = inc == null ? null : inc.split(",");
@@ -86,17 +86,19 @@ public class CheckContentPackagesForPaths implements AnalyserTask {
     void checkPackage(final AnalyserTaskContext ctx, final ContentPackageDescriptor desc, final Rules rules) {
         for(final String path : desc.paths) {
             boolean isAllowed = rules.includes == null;
+            int matchLength = 0;
             if ( !isAllowed ) {
                 for(final String i : rules.includes) {
                     if ( path.equals(i) || path.startsWith(i.concat("/")) ) {
                         isAllowed = true;
+                        matchLength = i.length();
                         break;
                     }
                 }
             }
             if ( isAllowed && rules.excludes != null ) {
                 for(final String i : rules.excludes) {
-                    if ( path.equals(i) || path.startsWith(i.concat("/")) ) {
+                    if ( path.equals(i) || path.startsWith(i.concat("/")) && i.length() > matchLength ) {
                         isAllowed = false;
                         break;
                     }
diff --git a/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesForPathsTest.java b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesForPathsTest.java
index 57e57ef..a88834a 100644
--- a/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesForPathsTest.java
+++ b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckContentPackagesForPathsTest.java
@@ -31,11 +31,11 @@ import org.apache.sling.feature.scanner.impl.ContentPackageDescriptor;
 import org.junit.Test;
 
 public class CheckContentPackagesForPathsTest {
-    
+
     @Test public void testNoRulesConfiguration() {
         final CheckContentPackagesForPaths analyser = new CheckContentPackagesForPaths();
         final AnalyserTaskContext ctx = new AnalyserTaskContextImpl();
-        
+
         assertNull(analyser.getRules(ctx));
     }
 
@@ -70,7 +70,7 @@ public class CheckContentPackagesForPathsTest {
         ctx.getConfiguration().put("includes", "/ab,/b");
 
         final Rules r = analyser.getRules(ctx);
-        final ContentPackageDescriptor desc = new ContentPackageDescriptor("name", new Artifact(ArtifactId.parse("g:a:1")), 
+        final ContentPackageDescriptor desc = new ContentPackageDescriptor("name", new Artifact(ArtifactId.parse("g:a:1")),
             new URL("https://sling.apache.org"));
         desc.paths.add("/b/foo");
         desc.paths.add("/a");
@@ -80,10 +80,28 @@ public class CheckContentPackagesForPathsTest {
         desc.paths.add("/c");
 
         analyser.checkPackage(ctx, desc, r);
-        
+
         assertEquals(3, ctx.getErrors().size());
         assertTrue(ctx.getErrors().get(0), ctx.getErrors().get(0).endsWith(" /a"));
         assertTrue(ctx.getErrors().get(1), ctx.getErrors().get(1).endsWith(" /a/foo"));
         assertTrue(ctx.getErrors().get(2), ctx.getErrors().get(2).endsWith(" /c"));
     }
+
+    @Test public void testPathsCheckLongestMatching() throws IOException {
+        final CheckContentPackagesForPaths analyser = new CheckContentPackagesForPaths();
+        final AnalyserTaskContextImpl ctx = new AnalyserTaskContextImpl();
+        ctx.getConfiguration().put("excludes", "/a");
+        ctx.getConfiguration().put("includes", "/a/foo");
+
+        final Rules r = analyser.getRules(ctx);
+        final ContentPackageDescriptor desc = new ContentPackageDescriptor("name", new Artifact(ArtifactId.parse("g:a:1")),
+            new URL("https://sling.apache.org"));
+        desc.paths.add("/a/foo");
+        desc.paths.add("/a/bar");
+
+        analyser.checkPackage(ctx, desc, r);
+
+        assertEquals(1, ctx.getErrors().size());
+        assertTrue(ctx.getErrors().get(0), ctx.getErrors().get(0).endsWith(" /a/bar"));
+    }
 }