From 59c6567ff33f36863a123e2994d03747cd57a925 Mon Sep 17 00:00:00 2001 From: veeso Date: Thu, 15 Jul 2021 13:00:55 +0200 Subject: [PATCH] Auth view enhanchements: check if port and host are valid --- CHANGELOG.md | 3 +++ src/ui/activities/auth/misc.rs | 35 ++++++++++++++++++++++++++- src/ui/activities/auth/update.rs | 41 +++++++++++++------------------- src/ui/activities/auth/view.rs | 9 ++++--- 4 files changed, 60 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e69a9d..8faa6ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,10 +48,13 @@ Released on FIXME: ?? - Enhancements: - Show a "wait" message when deleting, copying and moving files and when executing commands - Replaced all `...` with `…` in texts + - Check if remote host is valid in authentication form + - Check if port number is valid in authentication form - Bugfix: - Fixed broken input cursor when typing UTF8 characters (tui-realm 0.3.2) - Fixed save bookmark dialog: you could switch out from dialog with `` - Fixed transfer interruption: it was not possible to abort a transfer if the size of the file was less than 65k + - Changed `Remote address` to `Remote host` in authentication form - Dependencies: - Added `argh 0.1.5` - Added `open 1.7.0` diff --git a/src/ui/activities/auth/misc.rs b/src/ui/activities/auth/misc.rs index d30b877..85e9545 100644 --- a/src/ui/activities/auth/misc.rs +++ b/src/ui/activities/auth/misc.rs @@ -25,7 +25,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -use super::{AuthActivity, FileTransferProtocol}; +use super::{AuthActivity, FileTransferParams, FileTransferProtocol}; impl AuthActivity { /// ### protocol_opt_to_enum @@ -80,4 +80,37 @@ impl AuthActivity { self.umount_size_err(); } } + + /// ### collect_host_params + /// + /// Get input values from fields or return an error if fields are invalid + pub(super) fn collect_host_params(&self) -> Result { + let (address, port, protocol, username, password): ( + String, + u16, + FileTransferProtocol, + String, + String, + ) = self.get_input(); + if address.is_empty() { + return Err("Invalid host"); + } + if port == 0 { + return Err("Invalid port"); + } + Ok(FileTransferParams { + address, + port, + protocol, + username: match username.is_empty() { + true => None, + false => Some(username), + }, + password: match password.is_empty() { + true => None, + false => Some(password), + }, + entry_directory: None, + }) + } } diff --git a/src/ui/activities/auth/update.rs b/src/ui/activities/auth/update.rs index 9b0bbd8..9c6822a 100644 --- a/src/ui/activities/auth/update.rs +++ b/src/ui/activities/auth/update.rs @@ -27,9 +27,9 @@ */ // locals use super::{ - AuthActivity, FileTransferParams, FileTransferProtocol, COMPONENT_BOOKMARKS_LIST, - COMPONENT_INPUT_ADDR, COMPONENT_INPUT_BOOKMARK_NAME, COMPONENT_INPUT_PASSWORD, - COMPONENT_INPUT_PORT, COMPONENT_INPUT_USERNAME, COMPONENT_RADIO_BOOKMARK_DEL_BOOKMARK, + AuthActivity, FileTransferProtocol, COMPONENT_BOOKMARKS_LIST, COMPONENT_INPUT_ADDR, + COMPONENT_INPUT_BOOKMARK_NAME, COMPONENT_INPUT_PASSWORD, COMPONENT_INPUT_PORT, + COMPONENT_INPUT_USERNAME, COMPONENT_RADIO_BOOKMARK_DEL_BOOKMARK, COMPONENT_RADIO_BOOKMARK_DEL_RECENT, COMPONENT_RADIO_BOOKMARK_SAVE_PWD, COMPONENT_RADIO_PROTOCOL, COMPONENT_RADIO_QUIT, COMPONENT_RECENTS_LIST, COMPONENT_TEXT_ERROR, COMPONENT_TEXT_HELP, COMPONENT_TEXT_NEW_VERSION_NOTES, COMPONENT_TEXT_SIZE_ERR, @@ -327,27 +327,20 @@ impl Update for AuthActivity { } // On submit on any unhandled (connect) (_, Msg::OnSubmit(_)) | (_, &MSG_KEY_ENTER) => { - // Match key for all other components - self.save_recent(); - let (address, port, protocol, username, password) = self.get_input(); - // Set file transfer params to context - let params: FileTransferParams = FileTransferParams { - address, - port, - protocol, - username: match username.is_empty() { - true => None, - false => Some(username), - }, - password: match password.is_empty() { - true => None, - false => Some(password), - }, - entry_directory: None, - }; - self.context_mut().set_ftparams(params); - // Set exit reason - self.exit_reason = Some(super::ExitReason::Connect); + // Validate fields + match self.collect_host_params() { + Err(err) => { + // mount error + self.mount_error(err); + } + Ok(params) => { + self.save_recent(); + // Set file transfer params to context + self.context_mut().set_ftparams(params); + // Set exit reason + self.exit_reason = Some(super::ExitReason::Connect); + } + } // Return None None } diff --git a/src/ui/activities/auth/view.rs b/src/ui/activities/auth/view.rs index 4d366da..8ec4763 100644 --- a/src/ui/activities/auth/view.rs +++ b/src/ui/activities/auth/view.rs @@ -138,7 +138,7 @@ impl AuthActivity { InputPropsBuilder::default() .with_foreground(addr_color) .with_borders(Borders::ALL, BorderType::Rounded, addr_color) - .with_label(String::from("Remote address")) + .with_label(String::from("Remote host")) .build(), )), ); @@ -823,8 +823,11 @@ impl AuthActivity { pub(super) fn get_input_port(&self) -> u16 { match self.view.get_state(super::COMPONENT_INPUT_PORT) { - Some(Payload::One(Value::Usize(x))) => x as u16, - _ => Self::get_default_port_for_protocol(FileTransferProtocol::Sftp), + Some(Payload::One(Value::Usize(x))) => match x > 65535 { + true => 0, + false => x as u16, + }, + _ => 0, } }