Security notes
vld is usually the first code that touches untrusted input. These are the places where a schema alone is not enough or where the defaults matter.
Filesystem paths and files
vld::path() and vld::file() check the input against the server's filesystem. Without a base directory:
- any absolute path or
..is accepted; exists(),file()anddir()reveal to the client whether arbitrary paths exist;vld::file()opens and reads whatever file the client names ({"path": "/etc/passwd"}).
Always confine user-supplied paths:
let avatar = vld::file()
.within("/srv/uploads")
.max_size(5 * 1024 * 1024)
.media_types(&["image/png", "image/jpeg"]);
within(base) resolves ., .. and the symlinks of existing ancestors before checking containment, so "../etc/x", "/srv/uploads/../etc/x" and a symlink pointing outside are rejected (path_outside_base), even when the target does not exist. It returns base.join(input), so later file operations use the checked path. File reads stop after max_size + 1 bytes.
within is a lexical check plus symlink resolution at validation time. If an attacker can create symlinks inside base between validation and use, open the file with OS-level protections (openat2 with RESOLVE_BENEATH, a chroot or a container) as well.URLs
string().url() accepts any http / https URL, including http://localhost and http://10.0.0.1. If your server fetches the URL (webhooks, link previews, image proxies), use url_strict() (feature string-advanced): it rejects localhost, private, loopback, link-local and unspecified addresses, including IPv4-mapped IPv6 forms such as http://[::ffff:127.0.0.1].
url_strict() checks the URL text. A public hostname can still resolve to a private address (DNS rebinding), so an SSRF-sensitive fetcher should also check the resolved IP before connecting.
Request bodies
Web adapters cap bodies by default (body limits). Set limits inside the body too: vld::array(..).max_len(n), vld::record(..).max_keys(n), vld::string().max(n), vld::json_value().max_depth(n); a 2 MiB body can still hold a million tiny array items.
Headers and cookies
The adapters ignore header names with _ and take the first value of a repeated cookie; see headers and cookies.
Error output
Issues carry the received value (truncated to 100 characters, cut on character boundaries). The default web error body contains only path, message and code, not the value, but VldError's Display / Debug (what servers log) and a serialized VldError / ValidationIssue include received. Mark secrets with redact().
Secrets in errors
.redact() works on any schema. Every issue it reports — nested ones in an object or array included — keeps path, code and message, and its received becomes "[redacted]" (vld::error::REDACTED). So Display, Debug, a serialized error, prettify_error, translations and HTTP error bodies never contain the value. Built-in messages and with_messages templates never quote the input; a custom message that does (a try_transform error formatting the value, say) has each input string of 4+ characters in it replaced by the marker.
let login = vld::object()
.field("user", vld::string().min(1))
.field("password", vld::string().min(12).require_digit().redact());
let err = login.parse(r#"{"user": "ada", "password": "hunter2"}"#).unwrap_err();
assert_eq!(err.issues[0].path_string(), ".password");
assert_eq!(err.issues[0].received, Some(serde_json::json!("[redacted]")));
assert!(!err.to_string().contains("hunter2"));
In #[derive(Validate)] and schema!, chain it in the field's schema: #[vld(vld::string().min(12).redact())], pub password: String => vld::string().min(12).redact().
Redact the outermost schema whose issues can carry the secret. A check of an enclosing schema reports the value it saw: vld::array(vld::string().redact()).unique() puts the duplicate item in not_unique's received, and a union with no matching variant reports the whole input. Redact the array or union itself (vld::array(vld::string()).unique().redact()).
Redaction covers errors only: the parsed output is the plain value, so keep it out of your own logs (a newtype with a masking Debug helps).
Passwords
Use the password rules (require_lowercase, require_uppercase, require_digit, require_symbol, max_repeat, and with the password-strength feature password_strength(score) backed by zxcvbn) together with min / max and redact(). Each failed rule is its own issue, so a sign-up form can show all of them at once; prefer a length minimum and a strength score over many composition rules. Validation does not replace hashing: store passwords only as a slow salted hash (argon2, scrypt, bcrypt).
Regex
pattern() uses regex-lite (linear time), so a pattern cannot be used for ReDoS; the built-in formats use no regex (patterns).