The multi-trait crate gives foundational traits for encoding and decoding
multiformats types. It uses unsigned varint encoding. This document describes
the security properties and the threat model of the crate.
- No
unsafecode.#![deny(unsafe_code)]is set at the crate root. The crate uses only safe Rust abstractions. - No buffer overflows. All buffer operations use safe indexing and slicing.
- No use-after-free. Lifetimes keep references valid.
- No data races. All types are
Send + Syncwith no shared mutable state.
- Malformed data rejection. Invalid varint encodings are detected and rejected with an error.
- Truncated data detection. Incomplete varints are rejected before processing.
- Boundary checking. All operations check buffer boundaries before access.
- Type-level validation.
EncodedBytesgives data validity at the type level.
- Excessive memory allocation for integer encoding. Integer encoding uses
at most 19 bytes (for
u128). - Stack overflow. No recursive operations. All encoding and decoding is iterative.
- Infinite loops. All loops have deterministic bounds set by the input size.
- Panic-based DoS. Tests check that no panic occurs on malicious input.
| Operation | Maximum Size | Notes |
|---|---|---|
EncodeInto for integers |
19 bytes | Maximum varint size for u128 |
EncodeInto for [u8; N] |
N bytes | Raw byte copy. Not a varint. |
EncodeIntoArray |
19 bytes | Stack-allocated. No heap. |
EncodeIntoBuffer |
19 bytes per integer | Appends to an existing buffer. |
TryDecodeFrom for integers |
Input-dependent | Zero allocation. Bounded by input. |
TryDecodeFrom for [u8; N] |
N bytes | Reads exactly N bytes from the input. |
EncodedBytes |
Arbitrary | Validated at construction. |
- No arithmetic overflow. All operations delegate to the
unsigned-varintcrate. It handles overflow. - Type-safe conversions. Decoding checks that values fit in the target type.
- Maximum value testing. Tests cover
TYPE::MAXvalues for all types.
The crate handles:
- Untrusted input data from network sources, files, or user input.
- Maliciously crafted varint sequences.
- Extreme values at type boundaries (0 and MAX).
- Truncated or incomplete data from interrupted transmissions.
- Concurrent access from multiple threads.
The crate does not protect against:
- Side-channel attacks. No constant-time guarantees. Timing may leak information.
- Cryptographic security. This is an encoding library, not a crypto library.
- Resource exhaustion. Callers must set rate limits and quotas.
- Semantic validation. Callers must check that decoded values are semantically correct.
- Physical attacks. No protection against hardware-level attacks.
- No panics on invalid input. All error conditions return
Result. - No buffer overflows. All indexing is bounds-checked.
- No undefined behavior.
#![deny(unsafe_code)]forbids unsafe code. - Deterministic behavior. The same input always gives the same output.
- Error transparency. All errors give source chains for debugging.
- Type safety. Invalid states are not representable.
- Constant-time operations. Encoding and decoding time may vary with the input.
- Resource limits. Callers must set quotas.
- Side-channel resistance. The crate is not for cryptographic use.
- Backward compatibility with bugs. Security fixes may break buggy code.
The crate depends on:
unsigned-varint(v0.8). Gives the varint encoding and decoding.thiserror(v2.0). Error type derivation.
Dev dependencies:
proptest(v1.4). Property-based testing.criterion(v0.5). Benchmarking.
- The
unsigned-varintcrate is used in the IPFS and libp2p ecosystem. - This crate relies on its security properties for varint parsing.
- Any security issue in
unsigned-varintaffects this crate.
If you find a security vulnerability in this crate, report it as follows:
- Do not open a public GitHub issue.
- Email the maintainers at the address in
Cargo.toml. - Include a description of the vulnerability.
- Include steps to reproduce it.
- Include the potential impact.
- Include a suggested fix if you have one.
- Initial response: within 48 hours.
- Vulnerability assessment: within 1 week.
- Fix development: depends on severity.
- Public disclosure: after the fix is released. Coordinated disclosure.
The crate has these security tests:
- 13 malicious input tests for attack vectors.
- 6 property tests with random inputs.
- Edge case tests for boundary conditions, max values, and empty inputs.
- Regression tests for all discovered bugs.
# Run all tests
cargo test
# Run only security tests
cargo test security_
# Run property tests with more cases
cargo test -- --ignored --test-threads=1All pull requests must pass:
- All unit tests.
- All integration tests.
- All property-based tests.
- Clippy with no warnings.
- Format check.
The crate assumes that callers will:
- Validate decoded values. Check that decoded values make sense.
- Set resource limits. Enforce quotas on input size and decode operations.
- Handle errors. Do not ignore or unwrap
Resultin production code. - Use the right types. Pick the smallest type that can hold the data.
- The Rust standard library is correct.
- The dependencies are secure.
- The compiler is correct.
- The platform is not compromised.
- Always handle errors. Do not unwrap in production code.
// Bad
let (value, _) = u32::try_decode_from(untrusted).unwrap();
// Good
let (value, _) = u32::try_decode_from(untrusted)?;- Validate semantic correctness. Type checking is not enough.
let (port, _) = u16::try_decode_from(data)?;
if port == 0 {
return Err("port cannot be zero");
}- Limit input size. Prevent resource exhaustion.
if input.len() > MAX_MESSAGE_SIZE {
return Err("input too large");
}- Use validated types. Use
EncodedBytesfor pre-validated data.
fn process(data: EncodedBytes) {
// The data is valid. No re-validation needed.
}- Do not use
unsafe. The crate enforces#![deny(unsafe_code)]. - Add tests for new features. Include security test cases.
- Document security implications. Explain potential misuse.
- Consider DoS vectors. Analyze every new feature for DoS potential.
- Rewrote documentation in ASD-STE100 strict mode.
- Fixed inaccuracies in
README.mdandSECURITY.md. - No known vulnerabilities.
- Removed the unmaintained
serde_cbordev-dependency. - Removed the redundant
unsafe impl Send/SyncforEncodedBytes. - No known vulnerabilities.
- Added
#![deny(unsafe_code)]and clippy lint configuration. - Added MSRV declaration and CI jobs for audit, fmt, and clippy.
- No known vulnerabilities.
- Initial production release.
- Validation in
EncodedBytes. - No known vulnerabilities.
This crate has not had a professional security audit. Review the code before you use it in security-critical applications.
Security issues are handled under the same Apache-2.0 license as the crate.