Commit 21ea6f77 authored by Nick Mathewson's avatar Nick Mathewson 🥔
Browse files

hscrypto, linkspec, llcrypto: Use new redaction helpers

This eliminates some string slicing.
parent e46e1297
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ use derive_deftly::Deftly;
use digest::Digest;
use itertools::{Itertools, chain};
use safelog::DisplayRedacted;
use safelog::util::write_end_redacted;
use subtle::ConstantTimeEq;
use thiserror::Error;
use tor_basic_utils::{StrExt as _, impl_debug_hex};
@@ -134,12 +135,9 @@ impl safelog::DisplayRedacted for HsId {
    // We here display some of the end.  We don't want to display the
    // *start* because vanity domains, which would perhaps suffer from
    // reduced deniability.
    #[allow(clippy::string_slice)] // TODO
    fn fmt_redacted(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let unredacted = self.display_unredacted().to_string();
        /// Length of the base32 data part of the address
        const DATA: usize = 56;
        assert_eq!(unredacted.len(), DATA + HSID_ONION_SUFFIX.len());
        assert!(unredacted.ends_with(HSID_ONION_SUFFIX));

        // We show this part of the domain:
        //     e     n     l     5     s     i     d     .onion
@@ -149,7 +147,7 @@ impl safelog::DisplayRedacted for HsId {
        // 8 of those bits are the version, which is currently always 0x03.
        // So we are showing 7 bits derived from the site key.

        write!(f, "[…]{}", &unredacted[DATA - 3..])
        write_end_redacted(f, &unredacted, 3 + HSID_ONION_SUFFIX.len(), "[…]")
    }
}

+5 −2
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use std::str::FromStr;

use itertools::Either;
use safelog::Redactable;
use safelog::util::write_start_redacted;
use serde::{Deserialize, Serialize};
use thiserror::Error;

@@ -423,11 +424,13 @@ impl Display for PtTargetAddr {
impl<SA: Debug + Redactable, HN: Debug + Display + AsRef<str>> Redactable
    for BridgeAddrInner<SA, HN>
{
    #[allow(clippy::string_slice)] // TODO
    fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BridgeAddrInner::IpPort(a) => a.display_redacted(f),
            BridgeAddrInner::HostPort(host, port) => write!(f, "{}…:{}", &host.as_ref()[..2], port),
            BridgeAddrInner::HostPort(host, port) => {
                write_start_redacted(f, host.as_ref(), 2, "…")?;
                write!(f, ":{port}")
            }
        }
    }
}
+2 −6
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
use base64ct::{Base64Unpadded, Encoding as _};
use curve25519_dalek::Scalar;
use derive_deftly::Deftly;
use safelog::util::write_start_redacted;
use std::fmt::{self, Debug, Display, Formatter};
use subtle::{Choice, ConstantTimeEq};

@@ -371,13 +372,8 @@ impl Debug for Ed25519Identity {
impl safelog::Redactable for Ed25519Identity {
    /// Warning: This displays 12 bits of the ed25519 identity, which is
    /// enough to narrow down a public relay by a great deal.
    #[allow(clippy::string_slice)] // TODO
    fn display_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}…",
            &Base64Unpadded::encode_string(self.id.as_ref())[..2]
        )
        write_start_redacted(f, &Base64Unpadded::encode_string(self.id.as_ref()), 2, "…")
    }

    fn debug_redacted(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {