Commits

Sebastian Sdorra committed e297401

improve validation of repository name

  • Participants
  • Parent commits e384a49
  • Branches issue-142

Comments (0)

Files changed (2)

File scm-core/src/main/java/sonia/scm/util/ValidationUtil.java

   private static final String REGEX_NAME = "^[A-z0-9\\.\\-_]+$";
 
   /** Field description */
-  private static final String REGEX_REPOSITORYNAME = "^[A-z0-9\\.\\-_/]+$";
+  private static final String REGEX_REPOSITORYNAME =
+    "^[A-z0-9][A-z0-9\\.\\-_/]*$";
 
   /** Field description */
-  private static final String REGEX_USERNAME = "^[A-z0-9\\.\\-_@]|[^ ]([A-z0-9\\.\\-_@ ]*[A-z0-9\\.\\-_@]|[^ ])?$";
+  private static final String REGEX_USERNAME =
+    "^[A-z0-9\\.\\-_@]|[^ ]([A-z0-9\\.\\-_@ ]*[A-z0-9\\.\\-_@]|[^ ])?$";
 
   //~--- get methods ----------------------------------------------------------
 
    */
   public static boolean isRepositoryNameValid(String name)
   {
-    return Util.isNotEmpty(name) && name.matches(REGEX_REPOSITORYNAME);
+    return Util.isNotEmpty(name) && name.matches(REGEX_REPOSITORYNAME)
+           &&!name.contains("..") &&!name.endsWith("/.") &&!name.endsWith(".");
   }
 
   /**

File scm-core/src/test/java/sonia/scm/util/ValidationUtilTest.java

    *
    */
   @Test
+  public void testIsRepositoryNameValid()
+  {
+    assertTrue(ValidationUtil.isRepositoryNameValid("scm"));
+    assertTrue(ValidationUtil.isRepositoryNameValid("scm/main"));
+    assertTrue(ValidationUtil.isRepositoryNameValid("scm/plugins/git-plugin"));
+
+    // issue 142
+    assertFalse(ValidationUtil.isRepositoryNameValid("."));
+    assertFalse(ValidationUtil.isRepositoryNameValid(".scm/plugins"));
+    assertTrue(ValidationUtil.isRepositoryNameValid("scm/plugins/."));
+    assertFalse(ValidationUtil.isRepositoryNameValid("scm/../plugins"));
+  }
+
+  /**
+   * Method description
+   *
+   */
+  @Test
   public void testIsUsernameValid()
   {