diff --git a/.gitea/scripts/fuzz.sh b/.gitea/scripts/fuzz.sh new file mode 100644 index 0000000..5e5c63f --- /dev/null +++ b/.gitea/scripts/fuzz.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# The nightly fuzzing (.gitea/workflows/fuzz.yml); also runs on any Linux +# machine with rustup. +# bash .gitea/scripts/fuzz.sh setup nightly Rust and cargo-fuzz +# bash .gitea/scripts/fuzz.sh run each target for FUZZ_SECONDS (600) +# Targets: fuzz/fuzz_targets/, bodies in src/fuzzing.rs. The first corpus +# folder (fuzz/corpus/) takes what the run finds; the seeds and the +# cases that once broke a target are read as well. +set -euo pipefail +cd "$(dirname "$0")/../.." +export PATH="$HOME/.cargo/bin:$PATH" + +case "${1:?setup or run}" in +setup) + bash .gitea/scripts/rust-toolchain.sh stable >/dev/null + rustup toolchain install nightly --profile minimal + # a fixed version, so the cache keeps one build + command -v cargo-fuzz >/dev/null || cargo install cargo-fuzz --version 0.13.1 --locked + cargo +nightly --version + ;; +run) + seconds=${FUZZ_SECONDS:-600} + # the app's own lock file, so the targets build the versions it runs + cp Cargo.lock fuzz/Cargo.lock + # No sanitizer: the code is safe Rust, and without AddressSanitizer each + # target tries about three times as many inputs. Debug assertions stay + # on (overflow checks), as cargo-fuzz builds by default. + nice -n 19 cargo +nightly fuzz build -O -s none + failed=() + for target in $(cargo +nightly fuzz list); do + mkdir -p "fuzz/corpus/$target" "fuzz/artifacts/$target" + echo "== $target, ${seconds} s" + # -timeout: one input taking 10 s is a finding; -rss_limit_mb: so is + # one that needs 2 GB (cnx01 gives the app 1 GB in all) + if ! nice -n 19 cargo +nightly fuzz run -O -s none "$target" \ + "fuzz/corpus/$target" "fuzz/seeds/$target" "tests/fuzz_cases/$target" -- \ + -max_total_time="$seconds" -timeout=10 -rss_limit_mb=2048 -max_len=1048576 \ + -print_final_stats=1; then + failed+=("$target") + fi + done + du -sh fuzz/corpus/* || true + if [ ${#failed[@]} -gt 0 ]; then + echo "found something: ${failed[*]} (artifact fuzz-findings)" + ls -l fuzz/artifacts/*/ || true + exit 1 + fi + ;; +esac diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 242dd4f..8d995df 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -14,8 +14,11 @@ # x86_64 on Ubuntu 22.04 (glibc 2.35; .gitea/scripts/build-release.sh) # and signed with the repository secret RELEASE_SIGNING_KEY # (.gitea/scripts/sign-release.sh). cnx01 installs each release -# with that signature only, within 15 minutes, and restarts the app -# on it (cnx-network-clan modules/visa): a push to main is a deploy. +# with that signature only, seconds after the release webhook tells +# it (every 15 minutes it also looks), and restarts the app on it +# (cnx-network-clan modules/visa): a push to main is a deploy. +# Fuzzing runs every night in fuzz.yml; its findings are replayed here +# by tests/fuzz_cases.rs. # A commit message with [skip ci] skips the whole workflow. name: CI diff --git a/.gitea/workflows/fuzz.yml b/.gitea/workflows/fuzz.yml new file mode 100644 index 0000000..11d9e90 --- /dev/null +++ b/.gitea/workflows/fuzz.yml @@ -0,0 +1,63 @@ +# Nightly fuzzing (cargo-fuzz, libFuzzer) of the code that reads what +# reaches the app from outside: what people paste and type, uploaded +# images and PDFs, the drawn signature (src/fuzzing.rs, fuzz/). Only on +# runner02 on cnx01 (label cnx01, cnx-network-clan docs/src/ci-runner.md), +# at 03:00 in Chiang Mai and at the lowest CPU priority, so the services +# there keep first call. The corpus each run grows is kept in the cache +# for the next. A crash fails the job and leaves its input as the artifact +# fuzz-findings; it goes into tests/fuzz_cases// with the fix, +# which every push then replays. Never releases anything. +name: Fuzz + +on: + schedule: + - cron: "0 20 * * *" + workflow_dispatch: + inputs: + seconds: + description: Seconds per target + default: "600" + +concurrency: + group: fuzz + cancel-in-progress: false + +env: + CARGO_TERM_COLOR: always + CARGO_INCREMENTAL: "0" + +jobs: + fuzz: + name: fuzz + runs-on: cnx01 + timeout-minutes: 120 + env: + FUZZ_SECONDS: ${{ inputs.seconds || '600' }} + steps: + - uses: actions/checkout@v4 + + - name: Nightly Rust and cargo-fuzz + run: bash .gitea/scripts/fuzz.sh setup + + - uses: actions/cache@v4 + with: + path: | + ~/.cargo/bin/cargo-fuzz + ~/.cargo/registry/index + ~/.cargo/registry/cache + fuzz/target + fuzz/corpus + # a new key each run, so the grown corpus is saved every night + key: fuzz-${{ github.run_id }} + restore-keys: fuzz- + + - name: Fuzz + run: bash .gitea/scripts/fuzz.sh run + + - name: Keep the findings + if: failure() + uses: https://github.com/christopherHX/gitea-upload-artifact@v4 + with: + name: fuzz-findings + path: fuzz/artifacts/ + retention-days: 30 diff --git a/AGENTS.md b/AGENTS.md index e3b87fe..797c122 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -53,6 +53,13 @@ Lanna Desk (lannadesk.com, the app `visa`) holds passport data of real people. R smallest input and lands in `proptest-regressions/`: commit that file with the fix, so every run tries the case first. `PROPTEST_CASES=50000 cargo test --lib props` looks harder than the default 256. +- **Fuzzing** runs every night on cnx01 (`.gitea/workflows/fuzz.yml`, + `fuzz/`, bodies in `src/fuzzing.rs`). A finding arrives as the failed job's + artifact `fuzz-findings`: put the input into `tests/fuzz_cases//`, + named after what it broke, and fix the code until `cargo test` passes. A + new reader of outside input belongs in a target's body. On Linux: + `bash .gitea/scripts/fuzz.sh setup`, then `FUZZ_SECONDS=60 bash + .gitea/scripts/fuzz.sh run`. - **Writing** for people (pages, docs) follows the house style: plain American English in whole sentences, no em dashes, "Nov 17, 2026" in tables. diff --git a/docs/PLAN.md b/docs/PLAN.md index e8c8b4b..d93d621 100644 --- a/docs/PLAN.md +++ b/docs/PLAN.md @@ -736,16 +736,21 @@ cases in the tests. the contract diff, the journal hash, and the PDF's line wrap. Their first run found that the photo reader dropped a one-letter given name (the K of SMITH</` with its fix, which `cargo test` replays on + every push. The multipart reading itself is axum's (multer), so no + target of ours. +- [ ] The nightly job runs on runner02 on cnx01 (Alex, Nov 23, 2026), by + its own label `cnx01` (cnx-network-clan PR), at 03:00 and at the lowest + CPU priority (`nice`), so the app keeps first call on the CPU. ## Open questions diff --git a/fuzz/.gitignore b/fuzz/.gitignore new file mode 100644 index 0000000..577668c --- /dev/null +++ b/fuzz/.gitignore @@ -0,0 +1,6 @@ +/target/ +/corpus/ +/artifacts/ +/coverage/ +# a copy of ../Cargo.lock plus libfuzzer-sys, made before each build +/Cargo.lock diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 0000000..a243afe --- /dev/null +++ b/fuzz/Cargo.toml @@ -0,0 +1,41 @@ +# The fuzz targets (cargo-fuzz, nightly Rust): .gitea/workflows/fuzz.yml +# runs them every night on cnx01. Each one only calls its body in +# src/fuzzing.rs, which tests/fuzz_cases.rs replays on stable. +# Locally (Linux): cp Cargo.lock fuzz/ && cargo +nightly fuzz run text +[package] +name = "visa-fuzz" +version = "0.0.0" +edition = "2024" +publish = false + +[package.metadata] +cargo-fuzz = true + +[dependencies] +libfuzzer-sys = "0.4" +visa = { path = ".." } + +# its own workspace, so the app's cargo commands leave it alone +[workspace] +members = ["."] + +[[bin]] +name = "text" +path = "fuzz_targets/text.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "upload" +path = "fuzz_targets/upload.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "signed_copy" +path = "fuzz_targets/signed_copy.rs" +test = false +doc = false +bench = false diff --git a/fuzz/fuzz_targets/signed_copy.rs b/fuzz/fuzz_targets/signed_copy.rs new file mode 100644 index 0000000..1ea534e --- /dev/null +++ b/fuzz/fuzz_targets/signed_copy.rs @@ -0,0 +1,3 @@ +#![no_main] + +libfuzzer_sys::fuzz_target!(|data: &[u8]| visa::fuzzing::signed_copy(data)); diff --git a/fuzz/fuzz_targets/text.rs b/fuzz/fuzz_targets/text.rs new file mode 100644 index 0000000..534bd68 --- /dev/null +++ b/fuzz/fuzz_targets/text.rs @@ -0,0 +1,3 @@ +#![no_main] + +libfuzzer_sys::fuzz_target!(|data: &[u8]| visa::fuzzing::text(data)); diff --git a/fuzz/fuzz_targets/upload.rs b/fuzz/fuzz_targets/upload.rs new file mode 100644 index 0000000..8c15b57 --- /dev/null +++ b/fuzz/fuzz_targets/upload.rs @@ -0,0 +1,3 @@ +#![no_main] + +libfuzzer_sys::fuzz_target!(|data: &[u8]| visa::fuzzing::upload(data)); diff --git a/fuzz/seeds/signed_copy/agreement.txt b/fuzz/seeds/signed_copy/agreement.txt new file mode 100644 index 0000000..9d201c1 --- /dev/null +++ b/fuzz/seeds/signed_copy/agreement.txt @@ -0,0 +1,8 @@ +# 1. Purpose +Lanna Desk keeps your data for your stay. + +- name +- passport + +# 2. ข้อมูลส่วนบุคคล +ข้อมูลส่วนบุคคลของท่านจะถูกเก็บรักษาไว้อย่างปลอดภัย diff --git a/fuzz/seeds/signed_copy/drawn.png b/fuzz/seeds/signed_copy/drawn.png new file mode 100644 index 0000000..91099b7 Binary files /dev/null and b/fuzz/seeds/signed_copy/drawn.png differ diff --git a/fuzz/seeds/text/date.txt b/fuzz/seeds/text/date.txt new file mode 100644 index 0000000..af1de25 --- /dev/null +++ b/fuzz/seeds/text/date.txt @@ -0,0 +1 @@ +2026-11-23 \ No newline at end of file diff --git a/fuzz/seeds/text/dms.txt b/fuzz/seeds/text/dms.txt new file mode 100644 index 0000000..0618f77 --- /dev/null +++ b/fuzz/seeds/text/dms.txt @@ -0,0 +1 @@ +18°47'47.4"N 99°00'29.5"E \ No newline at end of file diff --git a/fuzz/seeds/text/maps.txt b/fuzz/seeds/text/maps.txt new file mode 100644 index 0000000..1f83712 --- /dev/null +++ b/fuzz/seeds/text/maps.txt @@ -0,0 +1 @@ +https://www.google.com/maps/@18.7965,99.0082,17z \ No newline at end of file diff --git a/fuzz/seeds/text/names.txt b/fuzz/seeds/text/names.txt new file mode 100644 index 0000000..03e0d61 Binary files /dev/null and b/fuzz/seeds/text/names.txt differ diff --git a/fuzz/seeds/text/ocr.txt b/fuzz/seeds/text/ocr.txt new file mode 100644 index 0000000..d66f413 --- /dev/null +++ b/fuzz/seeds/text/ocr.txt @@ -0,0 +1,3 @@ +REPUBLIC OF UTOPIA +p, String>| pack::pdf::Loaded { + name: "upload".into(), + content_type: content_type.into(), + kind: kind.into(), + back_of: None, + bytes, + }; + let _ = pack::pdf::sides(file(Ok(data.to_vec())), file(Err("none".into()))); +} + +/// A signed copy of an agreement: a drawn signature (a PNG, as the client +/// page takes it) or, for any other bytes, the agreement's text. +pub fn signed_copy(data: &[u8]) { + let png = files::sniff(data) == Some("image/png"); + if png && data.len() > agreements::DRAWING_LIMIT { + return; + } + let text = if png { + "# Purpose\nLanna Desk keeps your data.".to_string() + } else { + String::from_utf8_lossy(data).into_owned() + }; + let body = agreements::normalize(&text); + let sha = files::sha256_hex(body.as_bytes()); + let signed = agreements::pdf::Signed { + title: "Fuzz", + version: 1, + published: "Nov 23, 2026", + sha256: &sha, + name: "Erika Mustermann", + record: "MUSTERMANN, Erika", + at: "Nov 23, 2026, 03:00 (Chiang Mai time)", + through: "client link 1", + ip: "192.0.2.1", + user_agent: "fuzz", + drawing: png.then_some(data), + }; + let _ = agreements::pdf::render(&signed, &agreements::blocks(&body)); +} diff --git a/src/lib.rs b/src/lib.rs index 619da6f..abc873d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,8 @@ pub mod documents; pub mod embedded; pub mod files; pub mod forms; +#[doc(hidden)] +pub mod fuzzing; pub mod gps; pub mod journal; pub mod legal; diff --git a/src/web/client.rs b/src/web/client.rs index b17685a..b552763 100644 --- a/src/web/client.rs +++ b/src/web/client.rs @@ -988,9 +988,6 @@ pub async fn send( // ------------------------------------------------------------ agreements -/// The largest drawn signature taken, as a PNG. -const DRAWING_LIMIT: usize = 400 * 1024; - #[derive(Debug, Default, Serialize)] struct SignForm { name: String, @@ -1085,7 +1082,7 @@ fn drawing(f: &Fields) -> Result>, &'static str> { let bytes = base64::engine::general_purpose::STANDARD .decode(b64) .map_err(|_| "The drawn signature did not arrive whole. Clear it and sign again.")?; - if bytes.len() > DRAWING_LIMIT || crate::files::sniff(&bytes) != Some("image/png") { + if bytes.len() > agreements::DRAWING_LIMIT || crate::files::sniff(&bytes) != Some("image/png") { return Err("The drawn signature did not arrive whole. Clear it and sign again."); } Ok(Some(bytes)) diff --git a/tests/fuzz_cases.rs b/tests/fuzz_cases.rs new file mode 100644 index 0000000..1d97f3c --- /dev/null +++ b/tests/fuzz_cases.rs @@ -0,0 +1,53 @@ +//! Every input a fuzzer once broke a target with, replayed on every push +//! (src/fuzzing.rs, fuzz/). A crash the nightly fuzzing finds goes into +//! `tests/fuzz_cases//` with its fix, so it can never come back. + +use std::fs; +use std::path::Path; + +#[test] +fn the_inputs_the_fuzzers_found_stay_harmless() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fuzz_cases"); + let mut tried = 0; + for (name, target) in visa::fuzzing::TARGETS { + let dir = root.join(name); + let Ok(entries) = fs::read_dir(&dir) else { + continue; + }; + for e in entries { + let path = e.unwrap().path(); + if path.extension().is_some_and(|x| x == "md") { + continue; + } + let data = fs::read(&path).unwrap(); + let caught = std::panic::catch_unwind(|| target(&data)); + assert!(caught.is_ok(), "{name}: {} breaks it again", path.display()); + tried += 1; + } + } + // the folder of every target exists, so a renamed one is noticed + for (name, _) in visa::fuzzing::TARGETS { + assert!( + root.join(name).is_dir(), + "tests/fuzz_cases/{name} is missing" + ); + } + eprintln!("{tried} fuzz cases replayed"); +} + +#[test] +fn every_target_takes_its_seeds() { + // the seeds the nightly run starts from (fuzz/seeds/) go through too + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("fuzz/seeds"); + for (name, target) in visa::fuzzing::TARGETS { + let dir = root.join(name); + let files: Vec<_> = fs::read_dir(&dir) + .unwrap_or_else(|_| panic!("fuzz/seeds/{name} is missing")) + .map(|e| e.unwrap().path()) + .collect(); + assert!(!files.is_empty(), "fuzz/seeds/{name} is empty"); + for f in files { + target(&fs::read(&f).unwrap()); + } + } +} diff --git a/tests/fuzz_cases/signed_copy/README.md b/tests/fuzz_cases/signed_copy/README.md new file mode 100644 index 0000000..2aac595 --- /dev/null +++ b/tests/fuzz_cases/signed_copy/README.md @@ -0,0 +1,2 @@ +Inputs that once broke the fuzz target `signed_copy` (src/fuzzing.rs), replayed by +`tests/fuzz_cases.rs`. Name each after what it broke. diff --git a/tests/fuzz_cases/text/README.md b/tests/fuzz_cases/text/README.md new file mode 100644 index 0000000..42a8d00 --- /dev/null +++ b/tests/fuzz_cases/text/README.md @@ -0,0 +1,2 @@ +Inputs that once broke the fuzz target `text` (src/fuzzing.rs), replayed by +`tests/fuzz_cases.rs`. Name each after what it broke. diff --git a/tests/fuzz_cases/upload/README.md b/tests/fuzz_cases/upload/README.md new file mode 100644 index 0000000..2626f4f --- /dev/null +++ b/tests/fuzz_cases/upload/README.md @@ -0,0 +1,2 @@ +Inputs that once broke the fuzz target `upload` (src/fuzzing.rs), replayed by +`tests/fuzz_cases.rs`. Name each after what it broke. diff --git a/tests/main.rs b/tests/main.rs index 428ceeb..8551c0e 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -10,6 +10,7 @@ mod common; mod companies; mod deadlines; mod documents; +mod fuzz_cases; mod garage; mod legal; mod mrz_photo;