mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 06:55:47 +01:00
Validate namespaces correctly
This commit is contained in:
@@ -268,7 +268,7 @@ class RepositoryForm extends React.Component<Props, State> {
|
||||
|
||||
handleNamespaceChange = (namespace: string) => {
|
||||
this.setState({
|
||||
namespaceValidationError: !validator.isNameValid(namespace),
|
||||
namespaceValidationError: !validator.isNamespaceValid(namespace),
|
||||
repository: {
|
||||
...this.state.repository,
|
||||
namespace
|
||||
|
||||
@@ -25,6 +25,11 @@
|
||||
import { validation } from "@scm-manager/ui-components";
|
||||
|
||||
const nameRegex = /(?!^\.\.$)(?!^\.$)(?!.*[.]git$)(?!.*[\\\[\]])^[A-Za-z0-9\.][A-Za-z0-9\.\-_]*$/;
|
||||
const namespaceExceptionsRegex = /^(([0-9]{1,3})|(create))$/;
|
||||
|
||||
export const isNamespaceValid = (name: string) => {
|
||||
return nameRegex.test(name) && !namespaceExceptionsRegex.test(name);
|
||||
};
|
||||
|
||||
export const isNameValid = (name: string) => {
|
||||
return nameRegex.test(name);
|
||||
|
||||
@@ -27,17 +27,25 @@ package sonia.scm.repository;
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.util.ValidationUtil;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static sonia.scm.ScmConstraintViolationException.Builder.doThrow;
|
||||
|
||||
@Extension
|
||||
public class CustomNamespaceStrategy implements NamespaceStrategy {
|
||||
|
||||
private static final Pattern ONE_TO_THREE_DIGITS = Pattern.compile("[0-9]{1,3}");
|
||||
|
||||
@Override
|
||||
public String createNamespace(Repository repository) {
|
||||
String namespace = repository.getNamespace();
|
||||
|
||||
doThrow()
|
||||
.violation("invalid namespace", "namespace")
|
||||
.when(!ValidationUtil.isRepositoryNameValid(namespace) || namespace.matches("[0-9]{1,3}"));
|
||||
.when(
|
||||
!ValidationUtil.isRepositoryNameValid(namespace)
|
||||
|| ONE_TO_THREE_DIGITS.matcher(namespace).matches()
|
||||
|| namespace.equals("create"));
|
||||
|
||||
return namespace;
|
||||
}
|
||||
|
||||
@@ -21,10 +21,11 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import sonia.scm.ScmConstraintViolationException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -34,19 +35,21 @@ class CustomNamespaceStrategyTest {
|
||||
|
||||
private final NamespaceStrategy namespaceStrategy = new CustomNamespaceStrategy();
|
||||
|
||||
@Test
|
||||
void shouldReturnNamespaceFromRepository() {
|
||||
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
|
||||
assertThat(namespaceStrategy.createNamespace(heartOfGold)).isEqualTo(RepositoryTestData.NAMESPACE);
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"valid", "123_", "something_valid", "1234", "create_it"})
|
||||
void shouldReturnNamespaceFromRepository(String expectedValidNamespace) {
|
||||
Repository repository = RepositoryTestData.createHeartOfGold();
|
||||
repository.setNamespace(expectedValidNamespace);
|
||||
|
||||
assertThat(namespaceStrategy.createNamespace(repository)).isEqualTo(expectedValidNamespace);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowAnValidationExceptionForAnInvalidNamespace() {
|
||||
Repository repository = new Repository();
|
||||
repository.setNamespace("..");
|
||||
repository.setName(".");
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"..", " ", "0", "123", "create"})
|
||||
void shouldThrowAnValidationExceptionForAnInvalidNamespace(String expectedAsInvalidNamespace) {
|
||||
Repository repository = RepositoryTestData.createHeartOfGold();
|
||||
repository.setNamespace(expectedAsInvalidNamespace);
|
||||
|
||||
assertThrows(ScmConstraintViolationException.class, () -> namespaceStrategy.createNamespace(repository));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user