Four-dimension review (correctness/security/reliability/maintainability) of today's console/bus/login/dashboard/image work found no Critical/High; the HTTP parser, login gate (un-bypassable), bus semantics and the HMAC/PBKDF2/SHA-256 math were verified correct. The Medium findings are fixed here. Security: - Token-gate /api/files and /api/file (were session-only despite the docs/Security page claiming token-gated - arbitrary root-readable file exfiltration). - Neutralize settings-redirect header-splitting: __section/__tab are constrained to safe slugs before going into the Location header (CRLF no longer injects). - One token source (auth::rand_hex): fail loud + non-constant fallback instead of the published "dev-csrf-token" / second-granular wsid when /dev/urandom is unreadable. - Constant-time UI-token compare (auth::ct_eq); write auth.json 0600, dir 0700. - Cap concurrent connections (64, 503 beyond) + 10s head-read deadline, bounding the thread-per-connection flood on the open :80. Reliability/correctness: - Settings persistence now propagates its write error (was swallowed and reported as success with a misleading comment) and writes atomically (temp+rename); deck save atomic too. Auth::login returns None if the session insert failed. - TuningLoop::set_target resets the PID accumulator on a mode change (the old target's integral is in the old units) while keeping the operating point. - Editable-deck JS: monotonic new-block ids (no collision after remove+add), default source frequency (was power_w, not in the live map), symmetric drag-drop placement, re-render after save. Drivers "stopped" shows the bare reason, not JSON-quoted. Maintainability: - New `form` module consolidates the three divergent percent_decode/field copies (web/service/dashboard). Added a test asserting published state topics match the registry contract. Report in code-review-report.md (verdict, accepted-by-design posture incl. the no-TLS / factory-creds residual risk, and a deferred list: ui.rs dead code, deck assets extraction, target_label dedup). 206 lib tests + integration pass, clippy -D warnings clean, no emojis. Image rebuilt with the fixed binary. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BdKQBPjAXpCwmigUov5eQk
67 lines
2.1 KiB
Rust
67 lines
2.1 KiB
Rust
// SPDX-License-Identifier: MIT
|
|
//! Shared `application/x-www-form-urlencoded` / query decoding for the HTTP edge.
|
|
//!
|
|
//! One implementation, used by the web server, the miner service and the
|
|
//! dashboard, so a fix or a bug cannot diverge between copies.
|
|
|
|
/// Percent-decode a form or query token (`%XX` escapes and `+` as space).
|
|
pub fn percent_decode(text: &str) -> String {
|
|
let bytes = text.as_bytes();
|
|
let mut out = Vec::with_capacity(bytes.len());
|
|
let mut i = 0;
|
|
while i < bytes.len() {
|
|
match bytes[i] {
|
|
b'%' if i + 2 < bytes.len() => {
|
|
let hex = std::str::from_utf8(&bytes[i + 1..i + 3]).unwrap_or("");
|
|
match u8::from_str_radix(hex, 16) {
|
|
Ok(byte) => {
|
|
out.push(byte);
|
|
i += 3;
|
|
}
|
|
Err(_) => {
|
|
out.push(bytes[i]);
|
|
i += 1;
|
|
}
|
|
}
|
|
}
|
|
b'+' => {
|
|
out.push(b' ');
|
|
i += 1;
|
|
}
|
|
byte => {
|
|
out.push(byte);
|
|
i += 1;
|
|
}
|
|
}
|
|
}
|
|
String::from_utf8_lossy(&out).into_owned()
|
|
}
|
|
|
|
/// Read one field from a urlencoded body by key (first match), percent-decoded.
|
|
pub fn field(body: &str, key: &str) -> Option<String> {
|
|
body.split('&').find_map(|pair| {
|
|
let (k, v) = pair.split_once('=')?;
|
|
(percent_decode(k) == key).then(|| percent_decode(v))
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn decodes_escapes_and_plus() {
|
|
assert_eq!(percent_decode("a+b%2Fc"), "a b/c");
|
|
assert_eq!(percent_decode("%0d%0a"), "\r\n");
|
|
// A truncated trailing escape is passed through, not dropped.
|
|
assert_eq!(percent_decode("x%2"), "x%2");
|
|
}
|
|
|
|
#[test]
|
|
fn field_reads_first_match() {
|
|
assert_eq!(field("a=1&b=two&a=3", "b").as_deref(), Some("two"));
|
|
assert_eq!(field("a=1", "missing"), None);
|
|
assert_eq!(field("csrf=tok&user=admin", "csrf").as_deref(), Some("tok"));
|
|
}
|
|
}
|