From 395e0ee3da03eeea250dc007fd33fa55733d547e Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Sat, 4 Nov 2023 20:16:15 +1100 Subject: [PATCH] [ENG-1344] Limit node name length (#1730) limit length --- core/src/api/nodes.rs | 2 +- core/src/node/config.rs | 19 +++++++++++-------- .../$libraryId/settings/client/general.tsx | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/core/src/api/nodes.rs b/core/src/api/nodes.rs index 1fbaa6249..e3cafc984 100644 --- a/core/src/api/nodes.rs +++ b/core/src/api/nodes.rs @@ -20,7 +20,7 @@ pub(crate) fn mount() -> AlphaRouter { } R.mutation(|node, args: ChangeNodeNameArgs| async move { if let Some(name) = &args.name { - if name.is_empty() || name.len() > 32 { + if name.is_empty() || name.len() > 250 { return Err(rspc::Error::new( ErrorCode::BadRequest, "invalid node name".into(), diff --git a/core/src/node/config.rs b/core/src/node/config.rs index 2da94aa45..64d1ce909 100644 --- a/core/src/node/config.rs +++ b/core/src/node/config.rs @@ -47,16 +47,19 @@ impl Migrate for NodeConfig { type Ctx = (); fn default(_path: PathBuf) -> Result { + let mut name = match hostname::get() { + // SAFETY: This is just for display purposes so it doesn't matter if it's lossy + Ok(hostname) => hostname.to_string_lossy().into_owned(), + Err(err) => { + eprintln!("Falling back to default node name as an error occurred getting your systems hostname: '{err}'"); + "my-spacedrive".into() + } + }; + name.truncate(250); + Ok(Self { id: Uuid::new_v4(), - name: match hostname::get() { - // SAFETY: This is just for display purposes so it doesn't matter if it's lossy - Ok(hostname) => hostname.to_string_lossy().into_owned(), - Err(err) => { - eprintln!("Falling back to default node name as an error occurred getting your systems hostname: '{err}'"); - "my-spacedrive".into() - } - }, + name, keypair: Keypair::generate(), p2p: Default::default(), features: vec![], diff --git a/interface/app/$libraryId/settings/client/general.tsx b/interface/app/$libraryId/settings/client/general.tsx index 94d44ce87..96bb16448 100644 --- a/interface/app/$libraryId/settings/client/general.tsx +++ b/interface/app/$libraryId/settings/client/general.tsx @@ -33,7 +33,7 @@ export const Component = () => { const form = useZodForm({ schema: z.object({ - name: z.string().min(1).optional(), + name: z.string().min(1).max(250).optional(), p2p_enabled: z.boolean().optional(), p2p_port: u16, customOrDefault: z.enum(['Custom', 'Default'])