1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use borsh::{BorshDeserialize, BorshSerialize};
use zeroize::{Zeroize, ZeroizeOnDrop};

/// A string of bytes that is impossible to construct with any non-ASCII, non-printable, or
/// whitespace characters. This is mainly useful as a brute-force solution to avoid homoglyph
/// attacks.
#[derive(
    Clone,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Debug,
    Default,
    BorshSerialize,
    Zeroize,
    ZeroizeOnDrop,
)]
pub struct BoringAscii(Vec<u8>);

impl BoringAscii {
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    pub fn as_str(&self) -> &str {
        std::str::from_utf8(&self.0).expect("BoringAscii was somehow invalid")
    }

    pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
        for b in bytes.iter() {
            // Characters 31 and below are nonprintable
            // Character 32 is SPC
            // Character 127 is DEL
            // Characters 128 and above are control characters
            if *b < 33 || *b > 126 {
                return None;
            }
        }
        Some(BoringAscii(bytes.to_vec()))
    }
}

/* BEGIN IMPLS THAT CAN CONSTRUCT A PRINTABLEASCIISTRING */
// These should all (indirectly) call `from_bytes`.

impl std::str::FromStr for BoringAscii {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // from_bytes checks that bytes are inside the printable ASCII range, which excludes
        // multibyte codepoints, and thus anything unexpected.
        Self::from_bytes(s.as_bytes()).ok_or(())
    }
}

impl TryFrom<&str> for BoringAscii {
    type Error = <BoringAscii as std::str::FromStr>::Err;
    fn try_from(s: &str) -> Result<BoringAscii, Self::Error> {
        <BoringAscii as std::str::FromStr>::from_str(s)
    }
}

// We serialize to a series of bytes, which means that on deserialization we just have to do the
// exact same range check as we do on from_bytes.
impl BorshDeserialize for BoringAscii {
    fn deserialize_reader<R: std::io::Read>(
        r: &mut R,
    ) -> Result<BoringAscii, std::io::Error> {
        let bytes = <Vec<u8>>::deserialize_reader(r)?;
        BoringAscii::from_bytes(&bytes).ok_or(std::io::Error::new(
            std::io::ErrorKind::Other,
            "Unprintable characters when deserializing (supposedly) printable ascii string"
                .to_string(),
        ))
    }
}

/* END IMPLEMENTATIONS THAT CAN CONSTRUCT A PRINTABLEASCIISTRING */

impl std::fmt::Display for BoringAscii {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

// We implement Deref, Borrow, and AsRef, but never DerefMut, BorrowMut, or AsMut, to ensure that
// users can't change the bytes directly.
impl std::ops::Deref for BoringAscii {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl AsRef<str> for BoringAscii {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl AsRef<[u8]> for BoringAscii {
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl AsRef<Vec<u8>> for BoringAscii {
    fn as_ref(&self) -> &Vec<u8> {
        &self.0
    }
}

impl std::borrow::Borrow<str> for BoringAscii {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl std::borrow::Borrow<[u8]> for BoringAscii {
    fn borrow(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl std::borrow::Borrow<Vec<u8>> for BoringAscii {
    fn borrow(&self) -> &Vec<u8> {
        &self.0
    }
}

impl From<BoringAscii> for String {
    fn from(s: BoringAscii) -> String {
        String::from_utf8(s.0.clone())
            .expect("ASCII should always be valid UTF-8, but this failed to convert")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        assert!(BoringAscii::from_bytes("😊".as_bytes()) == None);
        assert!(BoringAscii::from_bytes(&[b'\0']) == None);
        assert!(BoringAscii::from_bytes(&[b'\x7f']) == None);
        assert!(BoringAscii::from_bytes(&[b'\x1f']) == None);
        assert!(BoringAscii::from_bytes(&[b' ']) == None);
        assert!(BoringAscii::from_bytes(&[b'\n']) == None);
        assert!(
            BoringAscii::from_bytes("Hi".as_bytes())
                == Some(BoringAscii(b"Hi".to_vec()))
        );
        assert!(
            BoringAscii::from_bytes(&[b'!']) == Some(BoringAscii(b"!".to_vec()))
        );
    }
}