Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: temp validation of project name in gateway #534

Merged
merged 3 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion common/src/models/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,18 @@ impl From<ErrorKind> for ApiError {
ErrorKind::ProjectUnavailable => {
(StatusCode::BAD_GATEWAY, "project returned invalid response")
}
ErrorKind::InvalidProjectName => (StatusCode::BAD_REQUEST, "invalid project name"),
ErrorKind::InvalidProjectName => (
StatusCode::BAD_REQUEST,
r#"
Invalid project name. Project name must:
1. start and end with alphanumeric characters.
2. only contain lowercase characters.
3. only contain characters inside of the alphanumeric range, except for `-`.
4. not be empty.
5. be shorter than 63 characters.
6. not contain profanity.
7. not be a reserved word."#,
),
ErrorKind::InvalidOperation => (
StatusCode::BAD_REQUEST,
"the requested operation is invalid",
Expand Down
17 changes: 17 additions & 0 deletions gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ impl ProjectName {
pub fn as_str(&self) -> &str {
self.0.as_str()
}

pub fn is_valid(&self) -> bool {
let name = self.0.clone();

fn is_valid_char(byte: u8) -> bool {
matches!(byte, b'a'..=b'z' | b'0'..=b'9' | b'-')
}

// each label in a hostname can be between 1 and 63 chars
let is_invalid_length = name.len() > 63;

!(name.bytes().any(|byte| !is_valid_char(byte))
|| name.ends_with('-')
|| name.starts_with('-')
|| name.is_empty()
|| is_invalid_length)
}
}

impl<'de> Deserialize<'de> for ProjectName {
Expand Down
16 changes: 12 additions & 4 deletions gateway/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,18 @@ impl GatewayService {
Err(Error::from_kind(ErrorKind::ProjectAlreadyExists))
}
} else {
// Otherwise attempt to create a new one. This will fail
// outright if the project already exists (this happens if
// it belongs to another account).
self.insert_project(project_name, account_name).await
// Check if project name is valid according to new rules if it
// doesn't exist.
// TODO: remove this check when we update the project name rules
// in shuttle-common
if project_name.is_valid() {
// Otherwise attempt to create a new one. This will fail
// outright if the project already exists (this happens if
// it belongs to another account).
self.insert_project(project_name, account_name).await
} else {
Err(Error::from_kind(ErrorKind::InvalidProjectName))
}
}
}

Expand Down