Skip to content
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
20 changes: 20 additions & 0 deletions backend/migrations/20261106000000_blank_names.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
UPDATE servers SET name = 'Server'
WHERE name ~ '^[[:space:][:cntrl:]\u00AD\u034F\u115F\u1160\u180E\u200B-\u200F\u202A-\u202E\u2060-\u206F\u2800\u3164\uFEFF\uFFA0]*$';

UPDATE database_instances SET display = kind
WHERE display ~ '^[[:space:][:cntrl:]\u00AD\u034F\u115F\u1160\u180E\u200B-\u200F\u202A-\u202E\u2060-\u206F\u2800\u3164\uFEFF\uFFA0]*$';

UPDATE database_hosts SET display = left(public_host, 64)
WHERE display ~ '^[[:space:][:cntrl:]\u00AD\u034F\u115F\u1160\u180E\u200B-\u200F\u202A-\u202E\u2060-\u206F\u2800\u3164\uFEFF\uFFA0]*$';

UPDATE nodes SET display = 'Node'
WHERE display ~ '^[[:space:][:cntrl:]\u00AD\u034F\u115F\u1160\u180E\u200B-\u200F\u202A-\u202E\u2060-\u206F\u2800\u3164\uFEFF\uFFA0]*$';

UPDATE projects SET name = slug
WHERE name ~ '^[[:space:][:cntrl:]\u00AD\u034F\u115F\u1160\u180E\u200B-\u200F\u202A-\u202E\u2060-\u206F\u2800\u3164\uFEFF\uFFA0]*$';

UPDATE api_keys SET name = 'API key'
WHERE name ~ '^[[:space:][:cntrl:]\u00AD\u034F\u115F\u1160\u180E\u200B-\u200F\u202A-\u202E\u2060-\u206F\u2800\u3164\uFEFF\uFFA0]*$';

UPDATE user_ssh_keys SET name = 'SSH key'
WHERE name ~ '^[[:space:][:cntrl:]\u00AD\u034F\u115F\u1160\u180E\u200B-\u200F\u202A-\u202E\u2060-\u206F\u2800\u3164\uFEFF\uFFA0]*$';
3 changes: 2 additions & 1 deletion backend/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::service::minecraft::MinecraftService;
use crate::service::modpack::ModpackService;
use crate::service::profile::ProfileService;
use crate::service::project::ProjectService;
use crate::service::project_database::ProjectDatabaseService;
use crate::service::resource::ResourceService;
use crate::service::route::RouteService;
use crate::service::schedule::ScheduleService;
Expand All @@ -46,7 +47,7 @@ impl Backend {
#[openapi()]
struct ApiDoc;

let (default_router,) = framework_router!(Self self (default,) { ServerService, FileService, BackupService, ServerAdminService, NodeAdminService, UserAdminService, AuthService, ProfileService, ProjectService, TemplateService, InternalService, MinecraftService, ResourceService, ModpackService, ApiKeyService, ApiKeyAdminService, TemplateAdminService, RouteService, RouteAdminService, ProjectDomainService, ScheduleService, DatabaseService, DatabaseAdminService, IntegrityAdminService, InstanceService, GroupAdminService, DeployAdminService });
let (default_router,) = framework_router!(Self self (default,) { ServerService, FileService, BackupService, ServerAdminService, NodeAdminService, UserAdminService, AuthService, ProfileService, ProjectService, TemplateService, InternalService, MinecraftService, ResourceService, ModpackService, ApiKeyService, ApiKeyAdminService, TemplateAdminService, RouteService, RouteAdminService, ProjectDomainService, ScheduleService, DatabaseService, ProjectDatabaseService, DatabaseAdminService, IntegrityAdminService, InstanceService, GroupAdminService, DeployAdminService });
let (dev_auth_router,) = framework_router!(Self self (dev_auth,) { AuthService });

let router = if cfg!(feature = "auth_dev") {
Expand Down
7 changes: 6 additions & 1 deletion backend/src/service/admin/api_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::service::api_key::{
};
use crate::service::error_auth_permission_denied::FromAuthPermissionDenied;
use crate::service::*;
use crate::utils::name::display_name;
use crate::utils::{PageSplit, PageSplitExtension as _, Paged};
use sqlx::query;
use zelus::service;
Expand Down Expand Up @@ -77,7 +78,7 @@ pub(crate) impl ApiKeyAdminService for Backend {
/// Requires `ManagementPermissions::API_KEYS_MANAGE`. Both the key's current
/// permissions and the new ones may never exceed your own.
#[route("/{api_key_id}", method = PATCH)]
#[error(auth::{permission_denied, unreachable}, db::failure <- [sql:*], api_key::{not_found, permissions_exceeded})]
#[error(auth::{permission_denied, unreachable}, db::failure <- [sql:*], api_key::{not_found, permissions_exceeded}, input::name_invalid)]
async fn update_any_api_key(
&self,
#[special] auth: Authentication,
Expand All @@ -98,6 +99,10 @@ pub(crate) impl ApiKeyAdminService for Backend {
.await?
.ok_or(UpdateAnyApiKeyError::ApiKeyNotFound)?;

let name = name
.map(|name| display_name(&name).ok_or(UpdateAnyApiKeyError::InputNameInvalid))
.transpose()?;

if key.permissions & !grant_ceiling(access, key.project) != 0 {
return Err(<UpdateAnyApiKeyError as FromAuthPermissionDenied>::from());
}
Expand Down
36 changes: 28 additions & 8 deletions backend/src/service/admin/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::service::admin::database::database_admin_service_error::{
};
use crate::service::error_database_agent_error::FromDatabaseAgentError;
use crate::service::*;
use crate::utils::name::display_name;
use crate::utils::task::detached;
use core::str::FromStr as _;
use database_agent_common::service::instance::InstanceService as _;
Expand Down Expand Up @@ -207,17 +208,20 @@ pub(crate) impl DatabaseAdminService for Backend {
/// Requires [`ManagementPermissions::DATABASE_HOSTS_MANAGE`].
#[route("/host", method = PUT)]
#[error(auth::{permission_denied, unreachable}, os::failure,
group::not_found, db::failure <- [sql:*], cache::failure <- [redis:*])]
group::not_found, input::name_invalid, db::failure <- [sql:*], cache::failure <- [redis:*])]
async fn create_database_host(
&self,
#[special] auth: Authentication,
payload: CreateDatabaseHost,
mut payload: CreateDatabaseHost,
) -> Result<String, _> {
auth.require::<_, _, CreateDatabaseHostError>(
(),
ManagementPermissions::DATABASE_HOSTS_MANAGE,
)?;

payload.display =
display_name(&payload.display).ok_or(CreateDatabaseHostError::InputNameInvalid)?;

if !all_groups_exist(&*self.database, &payload.groups).await? {
return Err(CreateDatabaseHostError::GroupNotFound);
}
Expand Down Expand Up @@ -280,18 +284,21 @@ pub(crate) impl DatabaseAdminService for Backend {
/// Requires [`ManagementPermissions::DATABASE_HOSTS_MANAGE`].
#[route("/host/registrations/{code}", method = PATCH)]
#[error(auth::{permission_denied, unreachable}, database::host_not_found,
group::not_found, db::failure <- [sql:*], cache::failure <- [redis:*])]
group::not_found, input::name_invalid, db::failure <- [sql:*], cache::failure <- [redis:*])]
async fn update_database_host_registration(
&self,
#[special] auth: Authentication,
code: String,
payload: CreateDatabaseHost,
mut payload: CreateDatabaseHost,
) -> Result<(), _> {
auth.require::<_, _, UpdateDatabaseHostRegistrationError>(
(),
ManagementPermissions::DATABASE_HOSTS_MANAGE,
)?;

payload.display = display_name(&payload.display)
.ok_or(UpdateDatabaseHostRegistrationError::InputNameInvalid)?;

if !all_groups_exist(&*self.database, &payload.groups).await? {
return Err(UpdateDatabaseHostRegistrationError::GroupNotFound);
}
Expand Down Expand Up @@ -332,7 +339,7 @@ pub(crate) impl DatabaseAdminService for Backend {
/// Requires [`ManagementPermissions::DATABASE_HOSTS_MANAGE`].
#[route("/host/{host_id}", method = PATCH)]
#[error(auth::{permission_denied, unreachable}, database::host_not_found <- [sql:not_found],
group::not_found <- [sql:foreign_key], db::failure <- [sql:*])]
group::not_found <- [sql:foreign_key], input::name_invalid, db::failure <- [sql:*])]
async fn update_database_host(
&self,
#[special] auth: Authentication,
Expand All @@ -344,6 +351,11 @@ pub(crate) impl DatabaseAdminService for Backend {
ManagementPermissions::DATABASE_HOSTS_MANAGE,
)?;

let display = payload
.display
.map(|display| display_name(&display).ok_or(UpdateDatabaseHostError::InputNameInvalid))
.transpose()?;

query!(
"UPDATE database_hosts SET
display = COALESCE($2, display),
Expand All @@ -352,7 +364,7 @@ pub(crate) impl DatabaseAdminService for Backend {
maintenance = COALESCE($5, maintenance)
WHERE id = $1",
host_id,
payload.display,
display,
payload.url.map(|url| url.to_string()),
payload.public_host,
payload.maintenance
Expand Down Expand Up @@ -505,19 +517,27 @@ pub(crate) impl DatabaseAdminService for Backend {
/// Requires [`ManagementPermissions::DATABASE_HOSTS_MANAGE`].
#[route("/host/{host_id}/instance/{instance_id}", method = PATCH)]
#[error(auth::{permission_denied, unreachable},
database::{host_not_found, instance_not_found, agent_error}, db::failure <- [sql:*])]
database::{host_not_found, instance_not_found, agent_error}, input::name_invalid,
db::failure <- [sql:*])]
async fn update_database_instance(
&self,
#[special] auth: Authentication,
host_id: Uuid,
instance_id: Uuid,
payload: UpdateDatabaseInstance,
mut payload: UpdateDatabaseInstance,
) -> Result<(), _> {
auth.require::<_, _, UpdateDatabaseInstanceError>(
(),
ManagementPermissions::DATABASE_HOSTS_MANAGE,
)?;

payload.display = payload
.display
.map(|display| {
display_name(&display).ok_or(UpdateDatabaseInstanceError::InputNameInvalid)
})
.transpose()?;

let host = self
.database_host(host_id)
.await
Expand Down
5 changes: 4 additions & 1 deletion backend/src/service/admin/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::service::template::error_template_unavailable::FromTemplateUnavailabl
use crate::service::template::*;
use crate::service::*;
use crate::templates::{TemplateError, ThinTemplateError};
use crate::utils::name::display_name;
use daemon_common::service::server::ServerResourcePayload;
use redis::RedisError;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -198,7 +199,7 @@ pub(crate) impl DeployAdminService for Backend {
group::not_on_node,
port::{invalid, not_allocated},
template::{not_found, unavailable, corrupted, port_definition_unknown, input_invalid}, cache::failure <- [redis:*],
auth::{permission_denied, unreachable}, db::failure <- [sql:*])]
auth::{permission_denied, unreachable}, db::failure <- [sql:*], input::name_invalid)]
async fn deploy_server(
&self,
#[special] auth: Authentication,
Expand All @@ -209,6 +210,8 @@ pub(crate) impl DeployAdminService for Backend {
) -> Result<DeployedServer, _> {
auth.require::<_, _, DeployServerError>((), ManagementPermissions::SERVER_DEPLOY)?;

let name = display_name(&name).ok_or(DeployServerError::InputNameInvalid)?;

let project_exists = query_scalar!(
r#"SELECT EXISTS (SELECT 1 FROM projects WHERE id = $1) AS "exists!""#,
&project
Expand Down
9 changes: 7 additions & 2 deletions backend/src/service/admin/node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::cluster::SyncTarget;
use crate::utils::name::display_name;
use tracing::error;

use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -137,7 +138,7 @@ pub(crate) impl NodeAdminService for Backend {
/// # Permissions
/// Requires [`ManagementPermissions::NODES_MANAGE`].
#[route("", method = PUT)]
#[error(auth::{permission_denied, unreachable}, os::failure, node::invalid_host, db::failure <- [sql:*], cache::failure <- [redis:*])]
#[error(auth::{permission_denied, unreachable}, os::failure, node::invalid_host, input::name_invalid, db::failure <- [sql:*], cache::failure <- [redis:*])]
async fn create_node(
&self,
#[special] auth: Authentication,
Expand All @@ -146,6 +147,8 @@ pub(crate) impl NodeAdminService for Backend {
) -> Result<String, _> {
auth.require::<_, _, CreateNodeError>((), ManagementPermissions::NODES_MANAGE)?;

let name = display_name(&name).ok_or(CreateNodeError::InputNameInvalid)?;

let registration =
config.into_registration::<CreateNodeError>(name, &self.config.public_url)?;

Expand Down Expand Up @@ -304,7 +307,7 @@ pub(crate) impl NodeAdminService for Backend {
/// # Permissions
/// Requires [`ManagementPermissions::NODES_MANAGE`].
#[route("/registrations/{code}", method = PATCH)]
#[error(node::{not_found, invalid_host}, auth::{permission_denied, unreachable}, db::failure <- [sql:*], cache::failure <- [redis:*])]
#[error(node::{not_found, invalid_host}, auth::{permission_denied, unreachable}, input::name_invalid, db::failure <- [sql:*], cache::failure <- [redis:*])]
async fn update_node_registration(
&self,
#[special] auth: Authentication,
Expand All @@ -314,6 +317,8 @@ pub(crate) impl NodeAdminService for Backend {
) -> Result<(), _> {
auth.require::<_, _, UpdateNodeRegistrationError>((), ManagementPermissions::NODES_MANAGE)?;

let name = display_name(&name).ok_or(UpdateNodeRegistrationError::InputNameInvalid)?;

let registration = config
.into_registration::<UpdateNodeRegistrationError>(name, &self.config.public_url)?;

Expand Down
6 changes: 5 additions & 1 deletion backend/src/service/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::service::error_auth_permission_denied::FromAuthPermissionDenied;
use crate::service::template::*;
use crate::service::*;
use crate::templates::{TemplateError, ThinTemplateError};
use crate::utils::name::display_name;
use crate::utils::server::port_environment_key;
use crate::utils::task::detached;
use daemon_common::service::backup::backup_service_error::{
Expand Down Expand Up @@ -51,7 +52,8 @@ pub(crate) impl ServerAdminService for Backend {
node::{not_found, failure},
group::not_on_node,
port::{invalid, not_allocated},
template::{not_found, unavailable, corrupted, port_definition_unknown, input_invalid})]
template::{not_found, unavailable, corrupted, port_definition_unknown, input_invalid},
input::name_invalid)]
#[example("../examples/server.create.json")]
async fn create_server(
&self,
Expand All @@ -72,6 +74,8 @@ pub(crate) impl ServerAdminService for Backend {
return Err(<CreateServerError as FromAuthPermissionDenied>::from());
}

let name = display_name(&name).ok_or(CreateServerError::InputNameInvalid)?;

let project_exists = query_scalar!(
r#"SELECT (COUNT(*) > 0) AS "exists!" FROM projects WHERE id = $1"#,
&project
Expand Down
10 changes: 8 additions & 2 deletions backend/src/service/api_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::service::api_key::api_key_service_error::{
use crate::service::api_key::error_api_key_permissions_exceeded::FromApiKeyPermissionsExceeded;
use crate::service::error_auth_permission_denied::FromAuthPermissionDenied;
use crate::service::error_project_not_found::FromProjectNotFound;
use crate::utils::name::display_name;
use serde::Serialize;
use sqlx::query;
use zelus::{define_path_variable, service};
Expand Down Expand Up @@ -167,7 +168,7 @@ pub(crate) impl ApiKeyService for Backend {
/// key (`project` omitted).
/// The requested permissions may never exceed your own.
#[route("", method = PUT)]
#[error(db::failure <- [sql:*], auth::{permission_denied, unreachable}, project::not_found <- [sql:foreign_key], api_key::permissions_exceeded)]
#[error(db::failure <- [sql:*], auth::{permission_denied, unreachable}, project::not_found <- [sql:foreign_key], api_key::permissions_exceeded, input::name_invalid)]
async fn create_api_key(
&self,
#[special] auth: UserAuthentication,
Expand All @@ -177,6 +178,7 @@ pub(crate) impl ApiKeyService for Backend {
) -> Result<CreatedApiKey, _> {
ensure_scope_access::<CreateApiKeyError>(&auth, project)?;
let permissions = grantable_permissions::<CreateApiKeyError>(&auth, project, permissions)?;
let name = display_name(&name).ok_or(CreateApiKeyError::InputNameInvalid)?;

let token = ApiKeyToken::generate();
let record = query!(
Expand Down Expand Up @@ -207,7 +209,7 @@ pub(crate) impl ApiKeyService for Backend {
/// # Permissions
/// The permissions may never exceed your own.
#[route("/{api_key_id}", method = PATCH)]
#[error(db::failure <- [sql:*], auth::{permission_denied, unreachable}, project::not_found, api_key::{not_found, permissions_exceeded})]
#[error(db::failure <- [sql:*], auth::{permission_denied, unreachable}, project::not_found, api_key::{not_found, permissions_exceeded}, input::name_invalid)]
async fn update_api_key(
&self,
#[special] auth: UserAuthentication,
Expand All @@ -226,6 +228,10 @@ pub(crate) impl ApiKeyService for Backend {
return Err(UpdateApiKeyError::ApiKeyNotFound);
};

let name = name
.map(|name| display_name(&name).ok_or(UpdateApiKeyError::InputNameInvalid))
.transpose()?;

if let Some(permissions) = permissions {
ensure_scope_access::<UpdateApiKeyError>(&auth, key.project)?;
let permissions =
Expand Down
Loading
Loading