Browse Source

protocol: Do not print admin password hash

ipv6
Denis Drakhnia 1 year ago
parent
commit
35b4abbf15
  1. 10
      protocol/src/admin.rs
  2. 24
      protocol/src/types.rs

10
protocol/src/admin.rs

@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2023 Denis Drakhnia <numas13@gmail.com> // SPDX-FileCopyrightText: 2023 Denis Drakhnia <numas13@gmail.com>
use crate::cursor::{Cursor, CursorMut}; use crate::cursor::{Cursor, CursorMut};
use crate::types::Str; use crate::types::{Hide, Str};
use crate::Error; use crate::Error;
pub const HASH_LEN: usize = 64; pub const HASH_LEN: usize = 64;
@ -30,7 +30,7 @@ impl AdminChallenge {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct AdminCommand<'a> { pub struct AdminCommand<'a> {
pub hash: &'a [u8], pub hash: Hide<&'a [u8]>,
pub command: Str<&'a [u8]>, pub command: Str<&'a [u8]>,
} }
@ -39,7 +39,7 @@ impl<'a> AdminCommand<'a> {
pub fn new(hash: &'a [u8], command: &'a str) -> Self { pub fn new(hash: &'a [u8], command: &'a str) -> Self {
Self { Self {
hash, hash: Hide(hash),
command: Str(command.as_bytes()), command: Str(command.as_bytes()),
} }
} }
@ -47,7 +47,7 @@ impl<'a> AdminCommand<'a> {
pub fn decode_with_hash_len(hash_len: usize, src: &'a [u8]) -> Result<Self, Error> { pub fn decode_with_hash_len(hash_len: usize, src: &'a [u8]) -> Result<Self, Error> {
let mut cur = Cursor::new(src); let mut cur = Cursor::new(src);
cur.expect(Self::HEADER)?; cur.expect(Self::HEADER)?;
let hash = cur.get_bytes(hash_len)?; let hash = Hide(cur.get_bytes(hash_len)?);
let command = Str(cur.get_bytes(cur.remaining())?); let command = Str(cur.get_bytes(cur.remaining())?);
cur.expect_empty()?; cur.expect_empty()?;
Ok(Self { hash, command }) Ok(Self { hash, command })
@ -61,7 +61,7 @@ impl<'a> AdminCommand<'a> {
pub fn encode(&self, buf: &mut [u8]) -> Result<usize, Error> { pub fn encode(&self, buf: &mut [u8]) -> Result<usize, Error> {
Ok(CursorMut::new(buf) Ok(CursorMut::new(buf)
.put_bytes(Self::HEADER)? .put_bytes(Self::HEADER)?
.put_bytes(self.hash)? .put_bytes(&self.hash)?
.put_bytes(&self.command)? .put_bytes(&self.command)?
.pos()) .pos())
} }

24
protocol/src/types.rs

@ -49,3 +49,27 @@ impl<T> Deref for Str<T> {
&self.0 &self.0
} }
} }
/// Wrapper for slice of bytes without printing
#[derive(Copy, Clone, PartialEq, Eq, Default)]
pub struct Hide<T>(pub T);
impl<T> fmt::Debug for Hide<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "<hidden>")
}
}
impl<T> fmt::Display for Hide<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "<hidden>")
}
}
impl<T> Deref for Hide<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}

Loading…
Cancel
Save