mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 06:25:45 +01:00
Validate banch names
This commit is contained in:
@@ -28,11 +28,13 @@ package sonia.scm.repository;
|
|||||||
|
|
||||||
import com.google.common.base.MoreObjects;
|
import com.google.common.base.MoreObjects;
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
|
import sonia.scm.Validateable;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAccessType;
|
import javax.xml.bind.annotation.XmlAccessType;
|
||||||
import javax.xml.bind.annotation.XmlAccessorType;
|
import javax.xml.bind.annotation.XmlAccessorType;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
@@ -44,9 +46,14 @@ import java.io.Serializable;
|
|||||||
*/
|
*/
|
||||||
@XmlRootElement(name = "branch")
|
@XmlRootElement(name = "branch")
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
public final class Branch implements Serializable
|
public final class Branch implements Serializable, Validateable
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private static final String VALID_CHARACTERS_AT_START_AND_END = "\\w-,;\\]{}@&+=$#`|<>";
|
||||||
|
private static final String VALID_CHARACTERS = VALID_CHARACTERS_AT_START_AND_END + "/.";
|
||||||
|
public static final String VALID_BRANCH_NAMES = "[" + VALID_CHARACTERS_AT_START_AND_END + "]([" + VALID_CHARACTERS + "]*[" + VALID_CHARACTERS_AT_START_AND_END + "])?";
|
||||||
|
public static final Pattern VALID_BRANCH_NAME_PATTERN = Pattern.compile(VALID_BRANCH_NAMES);
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private static final long serialVersionUID = -4602244691711222413L;
|
private static final long serialVersionUID = -4602244691711222413L;
|
||||||
|
|
||||||
@@ -83,6 +90,11 @@ public final class Branch implements Serializable
|
|||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid() {
|
||||||
|
return VALID_BRANCH_NAME_PATTERN.matcher(name).matches();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -30,6 +30,11 @@ import lombok.Getter;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
import javax.validation.constraints.Pattern;
|
||||||
|
|
||||||
|
import static sonia.scm.repository.Branch.VALID_BRANCH_NAMES;
|
||||||
|
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@@ -41,6 +46,8 @@ public class GitConfigDto extends HalRepresentation {
|
|||||||
|
|
||||||
private boolean nonFastForwardDisallowed;
|
private boolean nonFastForwardDisallowed;
|
||||||
|
|
||||||
|
@NotEmpty
|
||||||
|
@Pattern(regexp = VALID_BRANCH_NAMES, message = "")
|
||||||
private String defaultBranch;
|
private String defaultBranch;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import sonia.scm.web.VndMediaType;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
|
import javax.validation.Valid;
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.PUT;
|
import javax.ws.rs.PUT;
|
||||||
@@ -126,7 +127,7 @@ public class GitConfigResource {
|
|||||||
mediaType = VndMediaType.ERROR_TYPE,
|
mediaType = VndMediaType.ERROR_TYPE,
|
||||||
schema = @Schema(implementation = ErrorDto.class)
|
schema = @Schema(implementation = ErrorDto.class)
|
||||||
))
|
))
|
||||||
public Response update(GitConfigDto configDto) {
|
public Response update(@Valid GitConfigDto configDto) {
|
||||||
|
|
||||||
GitConfig config = dtoToConfigMapper.map(configDto);
|
GitConfig config = dtoToConfigMapper.map(configDto);
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,12 @@ export const isNameValid = (name: string) => {
|
|||||||
return nameRegex.test(name);
|
return nameRegex.test(name);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const branchRegex = /^[\w-,;\]{}@&+=$#`|<>]([\w-,;\]{}@&+=$#`|<>/.]*[\w-,;\]{}@&+=$#`|<>])?$/;
|
||||||
|
|
||||||
|
export const isBranchValid = (name: string) => {
|
||||||
|
return branchRegex.test(name);
|
||||||
|
};
|
||||||
|
|
||||||
const mailRegex = /^[ -~]+@[A-Za-z0-9][\w\-.]*\.[A-Za-z0-9][A-Za-z0-9-]+$/;
|
const mailRegex = /^[ -~]+@[A-Za-z0-9][\w\-.]*\.[A-Za-z0-9][A-Za-z0-9-]+$/;
|
||||||
|
|
||||||
export const isMailValid = (mail: string) => {
|
export const isMailValid = (mail: string) => {
|
||||||
|
|||||||
@@ -124,15 +124,13 @@ class BranchForm extends React.Component<Props, State> {
|
|||||||
|
|
||||||
handleSourceChange = (source: string) => {
|
handleSourceChange = (source: string) => {
|
||||||
this.setState({
|
this.setState({
|
||||||
...this.state,
|
|
||||||
source
|
source
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
handleNameChange = (name: string) => {
|
handleNameChange = (name: string) => {
|
||||||
this.setState({
|
this.setState({
|
||||||
nameValidationError: !validator.isNameValid(name),
|
nameValidationError: !validator.isBranchValid(name),
|
||||||
...this.state,
|
|
||||||
name
|
name
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -35,16 +35,14 @@ import org.hibernate.validator.constraints.Length;
|
|||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
import javax.validation.constraints.Pattern;
|
import javax.validation.constraints.Pattern;
|
||||||
|
|
||||||
|
import static sonia.scm.repository.Branch.VALID_BRANCH_NAMES;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@SuppressWarnings("java:S2160") // we do not need this for dto
|
@SuppressWarnings("java:S2160") // we do not need this for dto
|
||||||
public class BranchDto extends HalRepresentation {
|
public class BranchDto extends HalRepresentation {
|
||||||
|
|
||||||
private static final String VALID_CHARACTERS_AT_START_AND_END = "\\w-,;\\]{}@&+=$#`|<>";
|
|
||||||
private static final String VALID_CHARACTERS = VALID_CHARACTERS_AT_START_AND_END + "/.";
|
|
||||||
static final String VALID_BRANCH_NAMES = "[" + VALID_CHARACTERS_AT_START_AND_END + "]([" + VALID_CHARACTERS + "]*[" + VALID_CHARACTERS_AT_START_AND_END + "])?";
|
|
||||||
|
|
||||||
@NotEmpty
|
@NotEmpty
|
||||||
@Length(min = 1, max = 100)
|
@Length(min = 1, max = 100)
|
||||||
@Pattern(regexp = VALID_BRANCH_NAMES)
|
@Pattern(regexp = VALID_BRANCH_NAMES)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import javax.validation.constraints.NotEmpty;
|
|||||||
|
|
||||||
import javax.validation.constraints.Pattern;
|
import javax.validation.constraints.Pattern;
|
||||||
|
|
||||||
import static sonia.scm.api.v2.resources.BranchDto.VALID_BRANCH_NAMES;
|
import static sonia.scm.repository.Branch.VALID_BRANCH_NAMES;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import org.junit.jupiter.params.provider.ValueSource;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static sonia.scm.repository.Branch.VALID_BRANCH_NAMES;
|
||||||
|
|
||||||
class BranchDtoTest {
|
class BranchDtoTest {
|
||||||
|
|
||||||
@@ -54,10 +55,11 @@ class BranchDtoTest {
|
|||||||
"val{d",
|
"val{d",
|
||||||
"val{}d",
|
"val{}d",
|
||||||
"val|kill",
|
"val|kill",
|
||||||
"val}"
|
"val}",
|
||||||
|
"va/li/d"
|
||||||
})
|
})
|
||||||
void shouldAcceptValidBranchName(String branchName) {
|
void shouldAcceptValidBranchName(String branchName) {
|
||||||
assertTrue(branchName.matches(BranchDto.VALID_BRANCH_NAMES));
|
assertTrue(branchName.matches(VALID_BRANCH_NAMES));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@@ -70,6 +72,6 @@ class BranchDtoTest {
|
|||||||
"val id"
|
"val id"
|
||||||
})
|
})
|
||||||
void shouldRejectInvalidBranchName(String branchName) {
|
void shouldRejectInvalidBranchName(String branchName) {
|
||||||
assertFalse(branchName.matches(BranchDto.VALID_BRANCH_NAMES));
|
assertFalse(branchName.matches(VALID_BRANCH_NAMES));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user