electron_hardener/
error.rs

1use std::fmt;
2
3/// An error that the provided binary didn't contain the required information for
4/// an operation on it.
5#[derive(Debug, PartialEq)]
6#[non_exhaustive]
7pub enum BinaryError {
8    /// No [sentinel byte marker]() could be found in the binary.
9    ///
10    /// [sentinel byte marker]: https://www.electronjs.org/docs/tutorial/fuses#quick-glossary
11    NoSentinel,
12    /// No fuse version was found in the binary.
13    NoFuseVersion,
14    /// The length of the fuse was not found in the binary.
15    NoFuseLength,
16    /// The requested fuse to be modifed wasn't present in the fuse wire.
17    FuseDoesNotExist(crate::Fuse),
18    /// An unknown fuse status was encountered.
19    ///
20    /// The Electron project may have made a breaking change to the fuse format if
21    /// this is returned.
22    UnknownFuse {
23        /// The fuse that returned an unknown value.
24        fuse: crate::Fuse,
25        /// The value found querying the fuse.
26        value: u8,
27    },
28    #[allow(deprecated)]
29    /// The Node.JS command line flag attempted to be disabled wasn't present.
30    NodeJsFlagNotPresent(crate::patcher::NodeJsCommandLineFlag),
31    /// The Electron command line flag attempted to be disabled wasn't present.
32    ElectronOptionNotPresent(crate::patcher::ElectronOption),
33    #[allow(deprecated)]
34    /// The Node.JS debugging message attempted to be disabled wasn't present.
35    MessageNotPresent(crate::patcher::DevToolsMessage),
36}
37
38impl fmt::Display for BinaryError {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            BinaryError::NoSentinel => f.write_str("No fuse sentinel found"),
42            BinaryError::NoFuseVersion => f.write_str("Fuse had no version present"),
43            BinaryError::NoFuseLength => f.write_str("Fuse had no length specified"),
44            BinaryError::FuseDoesNotExist(fuse) => write!(f, "The {:?} fuse wasn't present", fuse),
45            BinaryError::UnknownFuse { fuse, value } => write!(
46                f,
47                "The {:?} fuse returned an unknown value of '{}'",
48                fuse, value
49            ),
50            BinaryError::NodeJsFlagNotPresent(flag) => {
51                write!(f, "The {:?} debugging flag wasn't present", flag)
52            }
53            BinaryError::ElectronOptionNotPresent(opt) => {
54                write!(f, "The Electron option for {:?} wasn't present", opt)
55            }
56            BinaryError::MessageNotPresent(msg) => {
57                write!(f, "The DevTools message {:?} wasn't present", msg)
58            }
59        }
60    }
61}
62
63impl std::error::Error for BinaryError {}
64
65/// An error that can result from parsing an Electron binary and attempting to modify it.
66#[derive(Debug, PartialEq)]
67#[non_exhaustive]
68pub enum PatcherError {
69    /// A part of the provided binary's contents was invalid.
70    Binary(BinaryError),
71    /// A different fuse schema version was found then what the library supports.
72    FuseVersion {
73        /// The supported version of the Electron fuse schema by this library.
74        expected: u8,
75        /// The Electron fuse schema version found in the provided application binary.
76        found: u8,
77    },
78    /// An attempt was made to modify a fuse which has been removed from the Electron schema.
79    ///
80    /// This is an error because modifying a removed fuse has no effect, so this may lead to unexpected behavior.
81    RemovedFuse(crate::Fuse),
82}
83
84impl From<BinaryError> for PatcherError {
85    fn from(e: BinaryError) -> Self {
86        PatcherError::Binary(e)
87    }
88}
89
90impl fmt::Display for PatcherError {
91    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92        match self {
93            PatcherError::Binary(e) => write!(f, "{}", e),
94            PatcherError::FuseVersion { expected, found } => write!(
95                f,
96                "Unknown fuse version found. Expected {}, but found {}",
97                expected, found
98            ),
99            PatcherError::RemovedFuse(fuse) => write!(
100                f,
101                "Failed to modify the {:?} fuse because it is marked as removed",
102                fuse
103            ),
104        }
105    }
106}
107
108impl std::error::Error for PatcherError {}