Rust library for safely and explicitly handling untrusted aka dangerous data
Documentation hosted on docs.rs.
toml
dangerous = "0.2"
no-std / suitable for embedded.[1] Panics due to OOM are out-of-scope. Disable heap-allocations if this is
a concern.
[2] Zero dependencies when both unicode and bytecount
features are disabled.
[3] Zero heap-allocations when both box-expected and full-context
features are disabled.
This library's intentions are to provide a simple interface for explicitly
parsing untrusted data safely. dangerous really shines with parsing binary or
simple text data formats and protocols. It is not a deserialisation library like
what serde provides, but you could write a parser with dangerous that could
be used within a deserialiser.
Panics and unhandled/unacknowledged data are two footguns this library seeks to prevent. An optional, but solid, debugging interface with sane input formatting and helpful errors is included to weed out problems before, or after they arise in production.
```rust
fn decodemessage<'i, E>(r: &mut Reader<'i, E>) -> Result
let input = dangerous::input(/* data */); let result: Result<_, Invalid> = input.readall(decodemessage); ```
Custom errors for protocols often do not provide much context around why and
where a specific problem occurs within input. Passing down errors as simple as
core::str::Utf8Error may be useful enough to debug while in development,
however when just written into logs without the input/context, often amount to
noise. At this stage you are almost better off with a simple input error.
This problem is amplified with any trivial recursive-descent parser as the
context around a sub-slice is lost, rendering any error offsets useless when
passed back up to the root. dangerous fixes this by capturing the context
around and above the error.
Ever tried working backwards from something like this?
Wouldn't it be better if this was the alternative?
```
['h' 'e' ff 'l' 'o'] ^^ additional: error offset: 2, input length: 5 backtrace: 1.
read all2.read(expected message) 3.read(expected body) 4.convert input to str(expected utf-8 code point) ```
This library has a instance of unsafe required for wrapping a byte slice into
the Input DST and multiple instances required for str::from_utf8_unchecked
used in the display section module.
This project was originally inspired by untrusted.