Skip to content

Commit

Permalink
Merge branch 'main' into feat/mongodb-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
oddgrd authored Aug 15, 2022
2 parents 191efd0 + 306789b commit 4e2e9fe
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cargo-shuttle/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ShuttleInit for ShuttleInitAxum {
let router = Router::new().route("/hello", get(hello_world));
let sync_wrapper = SyncWrapper::new(router);
Ok(sync_wrapper){}
Ok(sync_wrapper)
}"#}
}
}
Expand Down
2 changes: 2 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ description = "Common library for the shuttle platform (https://www.shuttle.rs/)

[dependencies]
anyhow = "1.0.57"
censor = "0.2.0"
chrono = { version = "0.4.19", features = ["serde"] }
lazy_static = "1.4.0"
log = { version = "0.4.17", features = ["serde"] }
once_cell = "1.13.0"
rocket = "0.5.0-rc.2"
serde = "1.0.137"
serde_json = "1.0.81"
Expand Down
25 changes: 24 additions & 1 deletion common/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
Expand All @@ -6,12 +7,19 @@ use rocket::request::FromParam;
use serde::de::Error as DeError;
use serde::{Deserialize, Deserializer, Serialize};

use once_cell::sync::OnceCell;

/// Project names should conform to valid Host segments (or labels)
/// as per [IETF RFC 1123](https://datatracker.ietf.org/doc/html/rfc1123).
/// Initially we'll implement a strict subset of the IETF RFC 1123, concretely:
/// - It does not start or end with `-` or `_`.
/// - It does not contain any characters outside of the alphanumeric range, except for `-` or '_'.
/// - It is not empty.
/// - It does not contain profanity.
/// - It is not a reserved word.
///
use censor::Censor;

#[derive(Clone, Serialize, Debug, Eq, PartialEq)]
pub struct ProjectName(String);

Expand Down Expand Up @@ -45,9 +53,20 @@ impl ProjectName {
matches!(byte, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'-' | b'_')
}

fn is_profanity_free_and_not_reserved(hostname: &str) -> bool {
static INSTANCE: OnceCell<HashSet<String>> = OnceCell::new();
INSTANCE.get_or_init(|| HashSet::from(["Shuttle.rs".to_string()]));

let censor = Censor::Standard
+ Censor::Sex
+ Censor::Custom(INSTANCE.get().expect("Reserved words not set").clone());
!censor.check(hostname)
}

let separators = ['-', '_'];

!(hostname.bytes().any(|byte| !is_valid_char(byte))
|| !is_profanity_free_and_not_reserved(hostname)
|| hostname.ends_with(separators)
|| hostname.starts_with(separators)
|| hostname.is_empty())
Expand Down Expand Up @@ -89,7 +108,9 @@ impl Display for ProjectNameError {
`{}` is an invalid project name. project name must
1. start and end with alphanumeric characters.
2. only contain characters inside of the alphanumeric range, except for `-`, or `_`.
3. not be empty."#,
3. not be empty.,
4. not contain profanity.
5. not be a reserved word."#,
name
),
}
Expand Down Expand Up @@ -139,6 +160,8 @@ pub mod tests {
"__dunder_like__",
"__invalid",
"invalid__",
"test-crap-crap",
"shuttle.rs",
] {
let project_name = ProjectName::from_str(hostname);
assert!(project_name.is_err(), "{:?} was ok", hostname);
Expand Down

0 comments on commit 4e2e9fe

Please sign in to comment.