diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..d257cfd --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,38 @@ +# Dependabot configuration (M021E). +# +# Nothing here auto-merges, and there is no auto-merge workflow in +# `.github/workflows/`. A dependency PR is a proposal: it must pass the full +# `verify` matrix, the boundary lanes, and the `dependency-policy` advisory +# gate, and it is reviewed like any other change. The transport artifacts +# pinned in the root `Cargo.toml` are bumped only through a plan with a closure +# record (see `docs/release-process.md`), so a Dependabot PR that touches one +# of them is a prompt for that process, not an instruction to merge. +version: 2 +updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly + # One grouped PR per week keeps SHA-pin churn reviewable as a single diff. + groups: + github-actions: + patterns: ["*"] + open-pull-requests-limit: 3 + commit-message: + prefix: "ci" + + - package-ecosystem: cargo + directory: / + schedule: + interval: weekly + # Only patch updates are grouped. Grouping minor/major bumps would produce + # exactly the broad lockfile update M020 forbids and could move a + # transport pin without a plan; those arrive as individual PRs instead. + # The open-PR limit bounds the churn either way. + groups: + cargo-patch: + patterns: ["*"] + update-types: ["patch"] + open-pull-requests-limit: 5 + commit-message: + prefix: "deps" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ba303c2..29b8f83 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,8 +1,22 @@ name: CI +# Third-party actions are pinned to immutable commit SHAs; the trailing comment +# names the release the SHA was resolved from. `dtolnay/rust-toolchain` +# publishes no releases, so its SHA is the reviewed tip of the `stable` branch +# and the toolchain itself is selected by the `toolchain` input (the action +# defaults to `stable` when the input is omitted). on: push: pull_request: + schedule: + # Weekly advisory refresh (M021E): a new RUSTSEC entry fails the + # `dependency-policy` job even when the tree has not changed. + - cron: '17 5 * * 1' + +# Least privilege: every job reads the repository and nothing else. No job +# needs a write scope, an Actions secret, or an OIDC token. +permissions: + contents: read jobs: verify: @@ -18,8 +32,8 @@ jobs: toolchain: "1.89.0" runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable with: toolchain: ${{ matrix.toolchain }} components: rustfmt, clippy @@ -48,14 +62,14 @@ jobs: toolchain: stable runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable with: toolchain: ${{ matrix.toolchain }} - - uses: actions/setup-python@v5 + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: ${{ matrix.python }} - - uses: astral-sh/setup-uv@v6 + - uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6.8.0 with: version: '0.8.22' - run: uv run --directory crates/eggreplay-python --with maturin==1.14.1 --with pytest==8.4.2 --with pytest-asyncio==1.2.0 --with pytest-xdist==3.8.0 maturin develop @@ -105,17 +119,17 @@ jobs: python-abi3-cross-version: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - - uses: actions/setup-python@v5 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: '3.11' - - uses: astral-sh/setup-uv@v6 + - uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6.8.0 with: version: '0.8.22' - name: Build abi3 wheel under CPython 3.11 run: uv run --directory crates/eggreplay-python --python 3.11 --with maturin==1.14.1 maturin build --locked --out $GITHUB_WORKSPACE/target/python-wheels - - uses: actions/setup-python@v5 + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: '3.14' - name: Install the same wheel under CPython 3.14 @@ -126,8 +140,8 @@ jobs: dependency-boundary: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable with: toolchain: stable # Core/store must not pull transport runtimes. @@ -162,8 +176,8 @@ jobs: # pipe/`&&`/`||` chains portable across runners. runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable with: toolchain: stable - name: Ordinary profiles exclude the EggServe Core multiprotocol closure @@ -366,6 +380,31 @@ jobs: fi echo "inbound H2 and the gRPC view are both opt-in" + dependency-policy: + # M021E supply-chain gate: advisories and yanked crates, licenses, + # duplicate crates (reported, not banned), and crate sources. Deliberately + # separate from `dependency-boundary`/`protocol-boundary`, which assert + # transport and capability boundaries rather than dependency policy. The + # weekly `schedule` trigger makes a new advisory fail an unchanged tree; + # the known exceptions are reasoned entries in `deny.toml`. + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable + with: + toolchain: stable + - name: Install pinned cargo-deny + # A tool update must be a reviewed change: no floating install and no + # `@latest` action. 0.20.2 is the version the policy in `deny.toml` + # was validated against. + run: cargo install cargo-deny --locked --version 0.20.2 + - name: Dependency policy (advisories, bans, licenses, sources) + # `deny.toml` sets `all-features = true`, so this covers the widest + # workspace graph without repeating the flag here. + run: cargo deny check advisories bans licenses sources + interception: # M013F qualification lane: deterministic local CA/CONNECT/MITM coverage # that the ordinary all-features matrix also runs, pinned here so the @@ -377,8 +416,8 @@ jobs: os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable with: components: rustfmt, clippy # Minimum-feature graphs: default builds stay interception-free (tree diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml new file mode 100644 index 0000000..fa3a683 --- /dev/null +++ b/.github/workflows/fuzz.yml @@ -0,0 +1,48 @@ +name: Fuzz smoke + +# Bounded parser fuzzing (M021C harnesses, M021E lane). This is deliberately +# NOT a pull-request gate: a nightly toolchain and sanitizer build are too slow +# and too variable to block every change. A crash uploads its reproducer and +# fails the job, so a green scheduled run is evidence, not a proof of absence. + +on: + workflow_dispatch: + schedule: + # Weekly, off the main CI schedule. + - cron: '41 4 * * 2' + +permissions: + contents: read + +jobs: + fuzz: + name: fuzz (${{ matrix.target }}) + runs-on: ubuntu-latest + permissions: + contents: read + strategy: + fail-fast: false + matrix: + target: [har_import, session_open, websocket_transcript] + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable + with: + toolchain: nightly + - name: Install pinned cargo-fuzz + # A tool update is a reviewed change, matching the dependency-policy + # job's pinned `cargo-deny`. + run: cargo install cargo-fuzz --locked --version 0.13.2 + - name: Fuzz ${{ matrix.target }} for 60 seconds + working-directory: fuzz + run: cargo fuzz run ${{ matrix.target }} -- -max_total_time=60 -max_len=4096 + - name: Upload corpus and crash artifacts + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: fuzz-${{ matrix.target }} + path: | + fuzz/corpus/${{ matrix.target }} + fuzz/artifacts/${{ matrix.target }} + if-no-files-found: ignore + retention-days: 14 diff --git a/.github/workflows/python-wheels.yml b/.github/workflows/python-wheels.yml index d57b288..986c9c7 100644 --- a/.github/workflows/python-wheels.yml +++ b/.github/workflows/python-wheels.yml @@ -1,5 +1,9 @@ name: Python wheel qualification +# Third-party actions are pinned to immutable commit SHAs; the trailing comment +# names the release the SHA was resolved from. `dtolnay/rust-toolchain` +# publishes no releases, so its SHA is the reviewed tip of the `stable` branch +# (the action installs `stable` when the `toolchain` input is omitted). on: workflow_dispatch: push: @@ -40,12 +44,12 @@ jobs: platform_tag: win_amd64 runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - - uses: actions/setup-python@v5 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: '3.11' - - uses: astral-sh/setup-uv@v6 + - uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6.8.0 with: version: '0.8.22' - name: Verify native runner architecture @@ -58,7 +62,7 @@ jobs: run: python tools/python/clean_wheel_smoke.py --wheel crates/eggreplay-python/target/python-release/${{ matrix.target }} --smoke-script tools/python/wheel_smoke.py - name: Record artifact hash run: python tools/python/write_artifact_manifest.py crates/eggreplay-python/target/python-release/${{ matrix.target }} --pattern '*.whl' - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: eggreplay-${{ matrix.target }} path: crates/eggreplay-python/target/python-release/${{ matrix.target }} @@ -68,12 +72,12 @@ jobs: source-distribution: runs-on: ubuntu-24.04 steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - - uses: actions/setup-python@v5 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: dtolnay/rust-toolchain@39008827e6b4319a349e9a42dfc90579dd25f1ca # stable + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: '3.11' - - uses: astral-sh/setup-uv@v6 + - uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6.8.0 with: version: '0.8.22' - name: Build source distribution and wheel from the sdist @@ -82,7 +86,7 @@ jobs: run: python tools/python/inspect_sdist.py crates/eggreplay-python/target/python-sdist-verify/*.tar.gz - name: Record source archive hash run: python tools/python/write_artifact_manifest.py crates/eggreplay-python/target/python-sdist-verify --pattern '*.tar.gz' - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: eggreplay-sdist path: | @@ -99,11 +103,11 @@ jobs: matrix: python: ['3.11', '3.12', '3.13', '3.14'] steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: ${{ matrix.python }} - - uses: actions/download-artifact@v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: eggreplay-manylinux-x86_64 path: target/python-wheel-smoke diff --git a/.gitignore b/.gitignore index 888afd5..20d6f18 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /target/ *.eggr/ +*.har .venv/ __pycache__/ .pytest_cache/ diff --git a/SECURITY.md b/SECURITY.md index 0c89e5d..971a5d4 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -1,12 +1,103 @@ # Security policy -`.eggr` fixtures are sensitive data. EggReplay redacts authorization, proxy -authorization, cookies, and set-cookie headers by default in the semantic -redaction profile; configured query keys and JSON pointer paths are also -supported. Redaction is not a guarantee for opaque binary payloads, so access -to fixture directories must still be restricted. - -Fixture readers reject future schemas, malformed or oversized JSONL, invalid -blob names, hash/length mismatches, symlinked blobs, and incomplete manifests. -Diagnostics avoid dumping bodies and route credentials. Report remaining -security issues privately to the maintainers before public disclosure. +EggReplay records, stores, and replays HTTP interactions. A fixture is a +transcript of real traffic, so it must be treated as sensitive data; a listener +that serves one is an unauthenticated surface unless the operator puts a real +proxy or gateway in front of it. + +## Reporting a vulnerability + +Use GitHub private vulnerability reporting: + + + +Do not open a public issue, discussion, or pull request for an unfixed +vulnerability. If the private form is unavailable (the repository setting has +not been enabled), open a minimal public issue that asks for a private channel +and contains no reproduction, affected version, or exploit detail. + +A useful report includes the affected revision or release, the command or API +surface, a minimal reproduction, and the impact you believe it has. We aim to: + +- acknowledge a report within five business days; +- provide an initial assessment and severity within ten business days; +- coordinate disclosure within ninety days, or sooner once a fix ships. + A reporter who needs a different timeline should say so in the report. + +## Supported versions + +| Version | Supported | +|---|---| +| `main` (unreleased) | yes, for security fixes | +| latest `0.1.x` release | yes, for security fixes | +| older releases | no; upgrade to the latest release | + +Security fixes are developed on `main`, backported to the latest release line +where the fix is compatible, and announced with the advisory. + +## Fixture and HAR confidentiality + +Redaction is policy-based, not semantic understanding: + +- The secure default profile redacts `authorization`, `proxy-authorization`, + `cookie`, `set-cookie`, `x-api-key`, `api-key`, `x-auth-token`, + `x-access-token`, `x-amz-security-token`, and `x-goog-api-key` by default. + Query keys and JSON Pointer body paths are opt-in. `x-csrf-token`, `location`, + and `referer` are deliberately not default-redacted; see + [`docs/configuration.md`](docs/configuration.md). +- Stream-event trailer values are always persisted as ``; the values + are non-authoritative. A pre-fix fixture can be remediated with + `eggreplay migrate --in-place --scrub-stream-trailers`. +- Opaque binary payloads are not semantically redacted. See + [`docs/non-goals.md`](docs/non-goals.md). +- HAR comments and `interop-provenance.json` metadata are user-supplied and are + not covered by header, query, or body redaction. + +Fixtures are published with `0700` directories and `0600` files on Unix. On +Windows, `set_private_permissions` is a deliberate no-op: mode bits are a Unix +contract, and secrecy depends on the containing directory's ACLs. CA files +share this caveat. Exported HAR files and `--loss-report` outputs contain +recorded bodies; on Unix they are created `0600`, and elsewhere they depend on +the destination ACLs. Never commit a fixture or an exported HAR. + +## Untrusted input + +Fixture readers reject future schemas, incomplete manifests, malformed or +oversized JSONL, invalid blob names, hash/length mismatches, symlinked blobs, +and oversized manifests or HAR documents. Reads are bounded before allocation, +and display truncation is UTF-8-safe. + +`Session::open_blob` checks `symlink_metadata` before opening a blob, then +verifies the opened file's length and streams the SHA-256 incrementally. The +check and the open are separate syscalls, so a local attacker **with write +access to an open fixture directory** could replace a blob with a symlink in +between (a TOCTOU race). A portable no-follow open requires a platform +dependency this corrective does not add, so the residual is accepted, not +hidden: such an attacker already controls fixture contents, the length/hash +check fails closed with an integrity error, and replaced bytes are never +returned as valid. Fixtures must not be mutated while a session or handle is +open; this is an immutability contract, not a sandbox for a compromised fixture +directory. + +## Destructive operations + +`--overwrite` replaces an existing **valid fixture or empty directory**, never +an arbitrary path. A symlink, a non-directory, a non-empty directory that does +not open as a fixture, and a future or incomplete fixture are refused before +anything is deleted. The new fixture is staged and validated first, so a failed +recording, import, or migration leaves the old fixture byte-identical. See +[`docs/cli.md`](docs/cli.md). + +## Listener exposure + +`serve` and `record` bind loopback by default. Binding any other address +requires `--allow-non-loopback` (or `allow_non_loopback=True` in the Python +lifecycle functions); the same rule applies to the explicit proxy. A replay or +recording listener has no authentication of its own, and TLS does not add +authentication. Put an authenticating proxy in front of any non-loopback +listener. + +Repository settings that back this policy — private vulnerability reporting, +secret scanning, Dependabot alerts, branch protection, and Actions access — are +operator actions recorded in +[`docs/release-process.md`](docs/release-process.md). diff --git a/crates/eggreplay-cli/src/intercept.rs b/crates/eggreplay-cli/src/intercept.rs index f00ed10..9210ddb 100644 --- a/crates/eggreplay-cli/src/intercept.rs +++ b/crates/eggreplay-cli/src/intercept.rs @@ -103,10 +103,11 @@ pub struct ProxyRecordArgs { /// Listener address (loopback by default). #[arg(long, default_value = "127.0.0.1:0")] pub listen: SocketAddr, - /// Destination fixture directory. + /// Destination fixture directory; may be absent, empty, or a valid fixture. #[arg(long)] pub fixture: PathBuf, - /// Replace an existing fixture directory. + /// Replace an existing fixture or empty directory. Any other existing path + /// is refused; a failed recording leaves the old fixture untouched. #[arg(long, default_value_t = false)] pub overwrite: bool, /// Outbound route: `direct` or a pproxy URI. Never falls back to direct. @@ -332,24 +333,29 @@ pub struct CaRotateArgs { /// Validate a proxy listener bind: loopback always passes, anything else /// requires the explicit remote opt-in. /// +/// The predicate is the shared CLI listener gate in the parent module, not a +/// second copy: `serve`, `record`, and the proxy must never disagree about +/// which addresses are allowed. Only the consequence text is proxy-specific, +/// because a public forward proxy is an open relay rather than one exposed +/// fixture. The parent gate lives outside this `intercept`-gated module +/// precisely so a default build cannot lose it. +/// /// Returns the failure-class/message pair on denial. #[cfg(any(test, feature = "intercept"))] pub fn check_listener_bind( bind: SocketAddr, allow_non_loopback: bool, ) -> Result<(), (String, String)> { - if bind.ip().is_loopback() || allow_non_loopback { - Ok(()) - } else { - Err(( - "configuration".to_owned(), + super::check_listener_bind(bind, allow_non_loopback).map_err(|(class, _)| { + ( + class, format!( "refusing non-loopback proxy bind on {bind}: remote listeners require \ --allow-non-loopback and an operator-managed ingress allow policy; \ remote exposure without proxy authentication creates an open forward proxy" ), - )) - } + ) + }) } /// Dispatch `eggreplay proxy ...`. @@ -511,23 +517,8 @@ async fn proxy_record(args: ProxyRecordArgs) -> Result<(), (String, String)> { }; check_listener_bind(args.listen, args.allow_non_loopback)?; - if args.fixture.exists() && !args.overwrite { - super::emit( - "proxy-record", - args.output.output, - false, - Some("configuration"), - serde_json::json!({"fixture": args.fixture}), - ); - return Err(( - "configuration".into(), - "fixture exists; pass --overwrite to replace it".to_owned(), - )); - } - if args.fixture.exists() { - std::fs::remove_dir_all(&args.fixture) - .map_err(|error| ("runtime".into(), error.to_string()))?; - } + // Policy, route, CA, and limit validation all happen before the + // replacement target is inspected or any directory is touched. let (file_policy, target_policy) = load_record_policy(&args)?; let default_action = file_policy.default_connect_action(); let (redaction, profile_id) = super::redaction_policy( @@ -622,8 +613,25 @@ async fn proxy_record(args: ProxyRecordArgs) -> Result<(), (String, String)> { None }; - let session = eggreplay_store::RecordingSession::create( + if let Err(error) = super::check_replacement_target(&args.fixture, args.overwrite, "fixture") { + super::emit( + "proxy-record", + args.output.output, + false, + Some("configuration"), + serde_json::json!({"fixture": args.fixture}), + ); + return Err(error); + } + // Record into a sibling staging destination; the target is swapped only + // after finalization and validation, and every early return removes the + // staging tree. + let mut staging = super::StagingGuard::new(super::sibling_transaction_path( &args.fixture, + "proxy-record", + )); + let session = eggreplay_store::RecordingSession::create( + staging.path(), eggreplay_core::SessionMetadata { capture_mode: "explicit-proxy".into(), target: Some(redacted_route.clone()), @@ -669,6 +677,11 @@ async fn proxy_record(args: ProxyRecordArgs) -> Result<(), (String, String)> { let recorded = eggreplay_http::recording::finish_recording_session(session) .await .map_err(|error| ("fixture".into(), error.to_string()))?; + let recorded_flows = recorded.manifest().flow_count; + drop(recorded); + super::replace_fixture_transactionally(staging.path(), &args.fixture) + .map_err(|error| ("runtime".into(), error))?; + staging.disarm(); let snapshot = stats.snapshot(); let payload = serde_json::json!({ "bind": bind.to_string(), @@ -682,7 +695,7 @@ async fn proxy_record(args: ProxyRecordArgs) -> Result<(), (String, String)> { "rejected": snapshot.rejected, "tunneled": snapshot.tunneled, "intercepted": snapshot.intercepted, - "recorded_flows": recorded.manifest().flow_count, + "recorded_flows": recorded_flows, "failures": snapshot.failures, "route": redacted_route, "fixture": args.fixture, diff --git a/crates/eggreplay-cli/src/main.rs b/crates/eggreplay-cli/src/main.rs index 0dc671f..365ae94 100644 --- a/crates/eggreplay-cli/src/main.rs +++ b/crates/eggreplay-cli/src/main.rs @@ -14,7 +14,7 @@ use serde::Serialize; use serde_json::json; use std::fmt::Write as _; use std::io::Read as _; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::ExitCode; mod intercept; @@ -166,10 +166,18 @@ struct RecordArgs { timeout: TimeoutArgs, #[arg(long)] listen: std::net::SocketAddr, + /// Bind the recording gateway beyond loopback. The listener is + /// unauthenticated: any client that can reach it can drive this upstream + /// through the recorder, and TLS does not add authentication. + #[arg(long, default_value_t = false)] + allow_non_loopback: bool, #[arg(long)] upstream: String, + /// Fixture directory; may be absent, empty, or a valid fixture. #[arg(long)] fixture: PathBuf, + /// Replace an existing fixture or empty directory. Any other existing path + /// is refused; a failed recording leaves the old fixture untouched. #[arg(long, default_value_t = false)] overwrite: bool, /// Additional sensitive header names (added to secure defaults unless replaced). @@ -212,11 +220,19 @@ struct ServeArgs { outbound_version: OutboundVersionArgs, #[command(flatten)] timeout: TimeoutArgs, + /// Fixture directory. `re-record` replaces an existing fixture or empty + /// directory and refuses any other existing path. #[arg(long)] fixture: PathBuf, #[arg(long, default_value = "127.0.0.1:0")] listen: std::net::SocketAddr, - /// Record behavior; defaults to sealed offline replay. + /// Bind the replay server beyond loopback. The listener is + /// unauthenticated: any client that can reach it can consume this + /// fixture, and TLS does not add authentication. + #[arg(long, default_value_t = false)] + allow_non_loopback: bool, + /// Record behavior; defaults to sealed offline replay. `re-record` stages + /// a replacement and swaps it only after the new fixture validates. #[arg(long, value_enum, default_value_t = ServeRecordMode::Sealed)] record_mode: ServeRecordMode, /// Required for network-capable record modes. @@ -612,8 +628,11 @@ enum HarAction { struct HarImportArgs { #[arg(long)] har: PathBuf, + /// Fixture directory; may be absent, empty, or a valid fixture. #[arg(long)] fixture: PathBuf, + /// Replace an existing fixture or empty directory. Any other existing path + /// is refused; a malformed HAR leaves the old fixture untouched. #[arg(long, default_value_t = false)] overwrite: bool, #[arg(long = "loss-report")] @@ -651,19 +670,27 @@ struct HarExportArgs { struct MigrateArgs { #[arg(long)] fixture: PathBuf, - /// Destination fixture directory. Mutually exclusive with `--in-place`. - /// Named `--to` (not `--output`) because `--output` selects the result - /// envelope format (`human|json|junit`) on every command. + /// Destination fixture directory; may be absent, empty, or a valid + /// fixture. Mutually exclusive with `--in-place`. Named `--to` (not + /// `--output`) because `--output` selects the result envelope format + /// (`human|json|junit`) on every command. #[arg(long)] to: Option, #[arg(long, default_value_t = false)] in_place: bool, + /// Replace an existing fixture or empty destination directory. Any other + /// existing path is refused; a failed migration leaves it untouched. #[arg(long, default_value_t = false)] overwrite: bool, /// Target session schema (defaults to current). Schema 1 fixtures upgrade /// to current; current-to-current migration must be idempotent. #[arg(long)] target_schema: Option, + /// Rewrite every `stream-events` trailer value to `` inside the + /// transactional migration. Opt-in remediation for fixtures recorded + /// before stream-event values were suppressed. + #[arg(long = "scrub-stream-trailers", default_value_t = false)] + scrub_stream_trailers: bool, #[command(flatten)] output: OutputArgs, } @@ -807,16 +834,100 @@ fn build_client_with_version( } } +/// Refuse to bind an unauthenticated listener beyond loopback. +/// +/// This is the single exposure gate for every listener the CLI starts: +/// `serve`, `record`, and — through the thin proxy wrapper in `intercept` — +/// the explicit proxy. Binding a replay server or a recording gateway to a +/// routable address publishes a fixture or an upstream to every client that +/// can reach it, and neither command has any notion of a caller identity. +/// TLS is not authentication: it protects bytes in transit, it does not tell +/// the listener *who* connected, so `--inbound-tls` never makes a public bind +/// safe. +/// +/// Loopback always passes. Every other address requires the explicit +/// `--allow-non-loopback` opt-in and is otherwise a `configuration` refusal +/// (exit `2`). +/// +/// This lives here, in an always-compiled module, and not in the +/// `intercept`-gated operator surface: `intercept` is an opt-in Cargo feature, +/// so a gate that lived behind that `cfg` would silently disappear from every +/// default build — the build that actually ships `serve` and `record`. +fn check_listener_bind( + bind: std::net::SocketAddr, + allow_non_loopback: bool, +) -> Result<(), (String, String)> { + if bind.ip().is_loopback() || allow_non_loopback { + return Ok(()); + } + Err(( + "configuration".to_owned(), + format!( + "refusing non-loopback listener bind on {bind}: the listener is \ + unauthenticated and TLS does not add authentication; pass \ + --allow-non-loopback to bind beyond loopback" + ), + )) +} + +/// Apply [`check_listener_bind`] to one command invocation. +/// +/// A refusal is reported the same way for every listener: a `configuration` +/// envelope on the machine output channel carries the class and the bind, and +/// the returned error becomes the `configuration: ...` stderr diagnostic. The +/// gate itself runs before any caller-side filesystem work, so emitting here +/// cannot report a fixture that was never touched. +fn enforce_listener_gate( + command: &str, + output: OutputChoice, + bind: std::net::SocketAddr, + allow_non_loopback: bool, +) -> Result<(), (String, String)> { + match check_listener_bind(bind, allow_non_loopback) { + Ok(()) => Ok(()), + Err(error) => { + emit( + command, + output, + false, + Some("configuration"), + json!({"listen": bind}), + ); + Err(error) + } + } +} + +// Deliberately one linear script: configuration resolution, then the exposure +// gate, then staging, then the listener, then finalization. That ordering is +// the contract (a bad config or a refused bind must never touch a fixture), so +// splitting the body for a line-count lint would obscure it. +#[allow(clippy::too_many_lines)] async fn record(args: RecordArgs) -> Result<(), (String, String)> { - // Same ordering rule as `serve`: a configuration mistake is reported - // before any filesystem precondition is evaluated. + // Same ordering rule as `serve`: every configuration input is resolved + // before any filesystem precondition is evaluated, and nothing is deleted, + // so a bad route or upstream can never damage an existing fixture. let inbound = args .inbound .resolve() .map_err(|error| ("configuration".into(), error))?; + // Exposure gate: upstream parsing, target classification, staging, and bind + // all come after it. + enforce_listener_gate( + "record", + args.output.output, + args.listen, + args.allow_non_loopback, + )?; let timeout = args.timeout.resolve(); - if args.fixture.exists() && !args.overwrite { - let message = "fixture exists; pass --overwrite to replace it".to_string(); + let upstream = args + .upstream + .parse() + .map_err(|error: http::uri::InvalidUri| ("configuration".into(), error.to_string()))?; + let (redaction, profile_id) = effective_redaction_policy(&args); + let warning_redaction = redaction.clone(); + let (client, physical_route) = args.outbound_version.client(&args.route, timeout)?; + if let Err(error) = check_replacement_target(&args.fixture, args.overwrite, "fixture") { emit( "record", args.output.output, @@ -824,16 +935,14 @@ async fn record(args: RecordArgs) -> Result<(), (String, String)> { Some("configuration"), json!({"fixture": args.fixture}), ); - return Err(("configuration".into(), message)); - } - if args.fixture.exists() { - std::fs::remove_dir_all(&args.fixture) - .map_err(|error| ("runtime".into(), error.to_string()))?; + return Err(error); } - let (redaction, profile_id) = effective_redaction_policy(&args); - let (client, physical_route) = args.outbound_version.client(&args.route, timeout)?; + // Record into a sibling staging destination. The target is only touched + // after the staged fixture has been finalized and re-opened, and a failed + // recording removes the staging tree without touching the target. + let mut staging = StagingGuard::new(sibling_transaction_path(&args.fixture, "record")); let session = RecordingSession::create( - &args.fixture, + staging.path(), SessionMetadata { capture_mode: "gateway".into(), target: Some(redact_url(&args.upstream)), @@ -843,10 +952,6 @@ async fn record(args: RecordArgs) -> Result<(), (String, String)> { StoreLimits::default(), ) .map_err(|error| ("fixture".into(), error.to_string()))?; - let upstream = args - .upstream - .parse() - .map_err(|error: http::uri::InvalidUri| ("configuration".into(), error.to_string()))?; let server = eggreplay_http::recording::start_recording_gateway_with_protocol( args.listen, upstream, @@ -888,15 +993,27 @@ async fn record(args: RecordArgs) -> Result<(), (String, String)> { session.shutdown(); eggreplay_http::recording::drain_active_blobs(&session).await; let websocket_count = session.websocket_conversation_count().unwrap_or(0); - eggreplay_http::recording::finish_recording_session(session) + let recorded = eggreplay_http::recording::finish_recording_session(session) .await .map_err(|error| ("fixture".into(), error.to_string()))?; - emit( + let warnings = session_redaction_warnings( + &recorded, + &warning_redaction, + args.websockets, + args.redact_websocket_text, + args.redact_websocket_binary, + ); + drop(recorded); + replace_fixture_transactionally(staging.path(), &args.fixture) + .map_err(|error| ("runtime".into(), error))?; + staging.disarm(); + emit_with_warnings( "record", args.output.output, true, None, json!({"fixture": args.fixture, "status": "finalized", "serving": serving, "websocket_acquisition_enabled": args.websockets, "websocket_conversations": websocket_count, "outbound_timeout": args.timeout.describe()}), + warnings, ); Ok(()) } @@ -909,6 +1026,17 @@ async fn serve(args: ServeArgs) -> Result<(), (String, String)> { .inbound .resolve() .map_err(|error| ("configuration".into(), error))?; + // Exposure gate, before fixture recovery and before the record mode is + // resolved. It applies to every mode — sealed, `once`, `append-new`, and + // `re-record` — because the bind is chosen by the operator, not by the + // mode, and no inbound protocol policy can make an unauthenticated + // listener safe. + enforce_listener_gate( + "serve", + args.output.output, + args.listen, + args.allow_non_loopback, + )?; recover_fixture_transactionally(&args.fixture).map_err(|error| ("fixture".into(), error))?; let policy = eggreplay_core::RecordMode::from(args.record_mode) .resolve(args.fixture.exists(), args.upstream.is_some()) @@ -1026,6 +1154,7 @@ async fn record_once_from_serve(args: ServeArgs) -> Result<(), (String, String)> let upstream = required_upstream(&args)?; let (client, physical_route) = args.outbound_version.client(&args.route, timeout)?; let (redaction, profile_id) = effective_serve_redaction_policy(&args); + let warning_redaction = redaction.clone(); let session = RecordingSession::create( &args.fixture, SessionMetadata { @@ -1081,15 +1210,24 @@ async fn record_once_from_serve(args: ServeArgs) -> Result<(), (String, String)> session.shutdown(); eggreplay_http::recording::drain_active_blobs(&session).await; let websocket_count = session.websocket_conversation_count().unwrap_or(0); - eggreplay_http::recording::finish_recording_session(session) + let recorded = eggreplay_http::recording::finish_recording_session(session) .await .map_err(|error| ("fixture".into(), error.to_string()))?; - emit( + let warnings = session_redaction_warnings( + &recorded, + &warning_redaction, + args.websockets, + args.redact_websocket_text, + args.redact_websocket_binary, + ); + drop(recorded); + emit_with_warnings( "serve", args.output.output, true, None, json!({"fixture": args.fixture, "record_mode": "once", "upstream_enabled": true, "serving": serving, "websocket_acquisition_enabled": args.websockets, "websocket_conversations": websocket_count, "matcher_profile": args.matcher_profile.as_str(), "redaction_profile": args.redaction_profile, "outbound_timeout": args.timeout.describe(), "status": "finalized"}), + warnings, ); Ok(()) } @@ -1109,6 +1247,7 @@ async fn serve_append_new(args: ServeArgs) -> Result<(), (String, String)> { .map_err(|error| ("fixture".into(), error.to_string()))?; let (client, physical_route) = args.outbound_version.client(&args.route, timeout)?; let (redaction, profile_id) = effective_serve_redaction_policy(&args); + let warning_redaction = redaction.clone(); if profile_id != source.manifest().metadata.redaction_profile { return Err(( "configuration".into(), @@ -1182,6 +1321,7 @@ async fn serve_append_new(args: ServeArgs) -> Result<(), (String, String)> { .await .map_err(|error| ("fixture".into(), error.to_string()))?; let added_flows = additional.manifest().flow_count; + let warnings = session_redaction_warnings(&additional, &warning_redaction, false, false, false); let merged = source .merge_to( &additional, @@ -1193,12 +1333,13 @@ async fn serve_append_new(args: ServeArgs) -> Result<(), (String, String)> { replace_fixture_transactionally(&combined, &args.fixture) .map_err(|error| ("runtime".into(), error))?; let _ = std::fs::remove_dir_all(&temporary); - emit( + emit_with_warnings( "serve", args.output.output, true, None, json!({"fixture": args.fixture, "record_mode": "append-new", "upstream_enabled": true, "serving": serving, "websocket_acquisition_enabled": false, "websocket_conversations": 0, "matcher_profile": args.matcher_profile.as_str(), "redaction_profile": profile_id, "new_flows": added_flows, "status": "finalized"}), + warnings, ); Ok(()) } @@ -1214,9 +1355,18 @@ async fn serve_re_record(args: ServeArgs) -> Result<(), (String, String)> { let upstream = required_upstream(&args)?; let (client, physical_route) = args.outbound_version.client(&args.route, timeout)?; let (redaction, profile_id) = effective_serve_redaction_policy(&args); - let temporary = sibling_transaction_path(&args.fixture, "rerecord"); + let warning_redaction = redaction.clone(); + let inbound = args + .inbound + .resolve() + .map_err(|error| ("configuration".into(), error))?; + // Re-record implicitly replaces its own fixture. Refuse a path that is not + // a valid fixture, an empty directory, or absent *before* binding the + // listener, so a typo never starts a recording that could delete data. + check_replacement_target(&args.fixture, true, "fixture")?; + let mut staging = StagingGuard::new(sibling_transaction_path(&args.fixture, "rerecord")); let session = RecordingSession::create( - &temporary, + staging.path(), SessionMetadata { capture_mode: "re-record".into(), target: Some(redact_url(&args.upstream.clone().unwrap_or_default())), @@ -1226,10 +1376,6 @@ async fn serve_re_record(args: ServeArgs) -> Result<(), (String, String)> { StoreLimits::default(), ) .map_err(|error| ("fixture".into(), error.to_string()))?; - let inbound = args - .inbound - .resolve() - .map_err(|error| ("configuration".into(), error))?; let server = eggreplay_http::recording::start_recording_gateway_with_protocol( args.listen, upstream, @@ -1272,36 +1418,224 @@ async fn serve_re_record(args: ServeArgs) -> Result<(), (String, String)> { .await .map_err(|error| ("fixture".into(), error.to_string()))?; let recorded_flows = recorded.manifest().flow_count; + let warnings = session_redaction_warnings( + &recorded, + &warning_redaction, + args.websockets, + args.redact_websocket_text, + args.redact_websocket_binary, + ); drop(recorded); - replace_fixture_transactionally(&temporary, &args.fixture) + // A failed recording never reaches this point: `finish` fails closed and + // the staging guard removes the partial tree. + replace_fixture_transactionally(staging.path(), &args.fixture) .map_err(|error| ("runtime".into(), error))?; - emit( + staging.disarm(); + emit_with_warnings( "serve", args.output.output, true, None, json!({"fixture": args.fixture, "record_mode": "re-record", "upstream_enabled": true, "serving": serving, "websocket_acquisition_enabled": args.websockets, "websocket_conversations": websocket_count, "matcher_profile": args.matcher_profile.as_str(), "redaction_profile": args.redaction_profile, "new_flows": recorded_flows, "outbound_timeout": args.timeout.describe(), "status": "finalized"}), + warnings, ); Ok(()) } +/// What an existing (or absent) replacement target actually is. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum ReplacementTarget { + /// Nothing exists at the path; it is safe to create. + Absent, + /// An empty directory; replacing it discards nothing. + EmptyDir, + /// A directory that opens as a valid fixture (`Session::open` succeeds). + ValidFixture, +} + +impl ReplacementTarget { + const fn exists(self) -> bool { + !matches!(self, Self::Absent) + } +} + +/// The single authority for "may this path be replaced?". +/// +/// Every destructive command routes through this classifier, and +/// [`replace_fixture_transactionally`] re-checks it as defense in depth. It +/// never follows a symlink, never treats an arbitrary non-empty directory as +/// disposable, and rejects `.`/`..`/filesystem roots lexically before any +/// filesystem call. An existing directory is disposable only when it is empty +/// or when `Session::open` with the default limits accepts it; a future +/// schema, an incomplete manifest, or malformed flows are refusals, not +/// permission to delete. +fn classify_replacement_target(path: &Path) -> Result { + use std::path::Component; + + if path.file_name().is_none() { + return Err( + "the path has no final component (it is a filesystem root, `.`, or `..`)".into(), + ); + } + // `./fixture.eggr` is an ordinary spelling and resolves to the same + // directory; a `..` component can escape the operator's intended parent, + // so it is refused lexically before any filesystem call. + if path + .components() + .any(|component| matches!(component, Component::ParentDir)) + { + return Err("the path contains a `..` component".into()); + } + let metadata = match std::fs::symlink_metadata(path) { + Ok(metadata) => metadata, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + return Ok(ReplacementTarget::Absent); + } + Err(error) => return Err(format!("cannot inspect the path: {error}")), + }; + if metadata.file_type().is_symlink() { + return Err("the path is a symbolic link; symlinks are never followed".into()); + } + if !metadata.is_dir() { + return Err("the path exists and is not a directory".into()); + } + let mut entries = + std::fs::read_dir(path).map_err(|error| format!("cannot read the directory: {error}"))?; + if entries.next().is_none() { + return Ok(ReplacementTarget::EmptyDir); + } + Session::open(path, StoreLimits::default()).map_err(|error| { + format!( + "the directory is not empty and does not open as a valid EggReplay fixture: {error}" + ) + })?; + Ok(ReplacementTarget::ValidFixture) +} + +/// Enforce `--overwrite` and the replacement-target rules in one place. +/// +/// `noun` is the operator-facing name for the target (`fixture` or +/// `destination`) and is used only in the missing-flag diagnostic. +fn check_replacement_target( + path: &Path, + overwrite: bool, + noun: &str, +) -> Result { + let target = classify_replacement_target(path).map_err(|reason| { + ( + "configuration".to_owned(), + format!("cannot replace {}: {reason}", path.display()), + ) + })?; + if target.exists() && !overwrite { + return Err(( + "configuration".to_owned(), + format!( + "{noun} exists; pass --overwrite to replace an existing fixture or empty directory" + ), + )); + } + Ok(target) +} + +/// Best-effort cleanup for a sibling staging destination. +/// +/// Armed while a staged operation is in flight. `Drop` removes the staging +/// destination plus any `.incomplete-*` directory the store writer created +/// under that name, so every early return cleans up without repeating the code +/// at each `?`. The success path calls [`StagingGuard::disarm`] after the +/// staged fixture is published. +pub(crate) struct StagingGuard { + destination: PathBuf, + armed: bool, +} + +impl StagingGuard { + pub(crate) fn new(destination: PathBuf) -> Self { + Self { + destination, + armed: true, + } + } + + pub(crate) fn path(&self) -> &Path { + &self.destination + } + + /// Keep the staging result; used only after a successful publish. + pub(crate) fn disarm(&mut self) { + self.armed = false; + } +} + +impl Drop for StagingGuard { + fn drop(&mut self) { + if self.armed { + remove_staged_fixture(&self.destination); + } + } +} + +/// Remove a staging destination and any hidden `.incomplete-*` directory a +/// store writer created for it. Failures are ignored: cleanup must never mask +/// the original error. +fn remove_staged_fixture(destination: &Path) { + let _ = std::fs::remove_dir_all(destination); + let Some(parent) = destination.parent() else { + return; + }; + let parent = if parent.as_os_str().is_empty() { + Path::new(".") + } else { + parent + }; + let Some(name) = destination.file_name().and_then(|name| name.to_str()) else { + return; + }; + let prefix = format!(".{name}.incomplete-"); + let Ok(entries) = std::fs::read_dir(parent) else { + return; + }; + for entry in entries.flatten() { + if entry.file_name().to_string_lossy().starts_with(&prefix) { + let _ = std::fs::remove_dir_all(entry.path()); + } + } +} + fn sibling_transaction_path(fixture: &std::path::Path, label: &str) -> PathBuf { - let nonce = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_nanos(); + static NONCE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); let name = fixture .file_name() .and_then(|name| name.to_str()) .unwrap_or("fixture.eggr"); - fixture.with_file_name(format!(".{name}.{label}-{}-{nonce}", std::process::id())) + loop { + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default() + .as_nanos(); + let sequence = NONCE.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let candidate = fixture.with_file_name(format!( + ".{name}.{label}-{}-{nanos}-{sequence}", + std::process::id() + )); + // Never rename over a path that already exists, even if it is stale + // residue from a crashed run: take another name instead. + if std::fs::symlink_metadata(&candidate).is_err() { + return candidate; + } + } } fn replace_fixture_transactionally( staged: &std::path::Path, target: &std::path::Path, ) -> Result<(), String> { - if !target.exists() { + // Defense in depth: callers validate first, but a future caller must not + // be able to bypass the replacement-target authority here. + let existing = classify_replacement_target(target) + .map_err(|reason| format!("cannot replace {}: {reason}", target.display()))?; + if existing == ReplacementTarget::Absent { return std::fs::rename(staged, target).map_err(|error| error.to_string()); } let backup = sibling_transaction_path(target, "backup"); @@ -2402,15 +2736,158 @@ fn redaction_policy( } } +/// Query keys that commonly carry credentials but are not default-redacted. +const CREDENTIAL_QUERY_KEYS: &[&str] = &[ + "token", + "access_token", + "api_key", + "apikey", + "password", + "secret", + "sig", + "signature", +]; + +/// Whether a request header name looks credential-bearing. +/// +/// Covers the curated default set, the deliberately-not-default +/// `x-csrf-token`, and the common suffix families. Matching is on the +/// lowercased name. +fn is_credential_header_name(name: &str) -> bool { + eggreplay_core::security::DEFAULT_SENSITIVE_HEADERS.contains(&name) + || name == "x-csrf-token" + || name.ends_with("-api-key") + || name.ends_with("-auth-token") + || name.ends_with("-access-token") +} + +/// Value-free, advisory redaction-completeness warnings for one recording. +/// +/// Names a header or query key only, never its value or a body. Warnings are +/// deduplicated per command and never change success, exit codes, or the +/// payload schema. +fn redaction_warnings( + flows: &[eggreplay_core::Flow], + redaction: &eggreplay_core::RedactionConfig, + websockets_enabled: bool, + redact_websocket_text: bool, + redact_websocket_binary: bool, +) -> Vec { + use std::collections::BTreeSet; + let mut warnings = Vec::new(); + let mut seen_headers = BTreeSet::new(); + let mut seen_queries = BTreeSet::new(); + for flow in flows { + for header in &flow.request.headers { + let name = header.name.to_ascii_lowercase(); + if is_credential_header_name(&name) + && !redaction.headers.contains(&name) + && seen_headers.insert(name.clone()) + { + warnings.push(format!( + "request header {name} may carry a credential and is not redacted; pass --redact-header {name} to redact it" + )); + } + } + for pair in &flow.request.query { + let key = pair.key.to_ascii_lowercase(); + if CREDENTIAL_QUERY_KEYS.contains(&key.as_str()) + && !redaction.query_keys.contains(&pair.key) + && seen_queries.insert(key.clone()) + { + warnings.push(format!( + "request query key {key} may carry a credential and is not redacted; pass --redact-query {key} to redact it" + )); + } + } + } + if websockets_enabled + && !redact_websocket_text + && !redact_websocket_binary + && redaction.json_paths.is_empty() + { + warnings.push( + "WebSocket acquisition is enabled without payload redaction; pass --redact-websocket-text, --redact-websocket-binary, or --redact-json-path" + .into(), + ); + } + warnings +} + +/// Run [`redaction_warnings`] over a finalized session. +/// +/// Scanning is advisory: an unreadable flow iterator yields no warnings rather +/// than failing a command whose recording already succeeded. +fn session_redaction_warnings( + session: &Session, + redaction: &eggreplay_core::RedactionConfig, + websockets_enabled: bool, + redact_websocket_text: bool, + redact_websocket_binary: bool, +) -> Vec { + let flows = session + .iter_flows() + .ok() + .map(|iter| iter.collect::, _>>().unwrap_or_default()) + .unwrap_or_default(); + redaction_warnings( + &flows, + redaction, + websockets_enabled, + redact_websocket_text, + redact_websocket_binary, + ) +} + +/// Warn about pre-fix stream-event trailer values without naming any value. +/// +/// Stream-event trailer values are non-authoritative and are always written as +/// `` by current recorders. A value other than `` means the +/// fixture predates that guarantee and needs the opt-in scrub migration. +fn stream_trailer_warnings(session: &Session) -> Vec { + let Some(bytes) = session.read_extension("stream-events").ok().flatten() else { + return Vec::new(); + }; + let Ok(events) = serde_json::from_slice::(&bytes) else { + return Vec::new(); + }; + let mut warnings = Vec::new(); + for flow in events.flows { + let count = flow + .request + .iter() + .chain(flow.response.iter()) + .filter_map(|event| match &event.event { + eggreplay_core::StreamEventKind::Trailers { fields } => Some( + fields + .iter() + .filter(|field| field.value != "") + .count(), + ), + _ => None, + }) + .sum::(); + if count > 0 { + warnings.push(format!( + "flow {} has {count} stream-event trailer field value(s) that are not ; run `eggreplay migrate --in-place --scrub-stream-trailers` to remediate", + flow.flow_id + )); + } + } + warnings +} + fn validate(args: &ValidateArgs) -> Result<(), (String, String)> { let session = Session::open(&args.fixture, StoreLimits::default()) .map_err(|error| ("fixture".into(), error.to_string()))?; - emit( + let warnings = stream_trailer_warnings(&session); + emit_with_warnings( "validate", args.output.output, true, None, json!({"fixture": args.fixture, "flow_count": session.manifest().flow_count, "schema_version": session.manifest().metadata.schema_version}), + warnings, ); Ok(()) } @@ -2425,8 +2902,85 @@ fn har_redaction_policy(args: &HarImportArgs) -> (eggreplay_core::RedactionConfi ) } +/// Write an output that contains recorded material. +/// +/// On Unix the file is created with mode `0600` *before* any bytes are written, +/// so it is never world-readable even for an instant. Replacing an existing +/// file also tightens a stale permissive mode before the write. Non-Unix keeps +/// the platform default; secrecy there depends on the destination directory +/// ACLs, matching the documented fixture caveat. +fn write_sensitive_file(path: &Path, bytes: &[u8]) -> std::io::Result<()> { + #[cfg(unix)] + { + use std::io::Write as _; + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + let mut file = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .mode(0o600) + .open(path)?; + file.set_permissions(std::fs::Permissions::from_mode(0o600))?; + file.write_all(bytes)?; + file.flush()?; + Ok(()) + } + #[cfg(not(unix))] + { + std::fs::write(path, bytes) + } +} + +/// Result of a bounded read of an untrusted input document. +enum BoundedInput { + /// The document fits the configured limit. + Read(Vec), + /// The document exceeds the configured limit. + TooLarge, +} + +/// Read an untrusted document with a hard byte bound. +/// +/// `File::metadata` gives an allocation-free early refusal. The +/// `take(limit + 1)` read then closes the race where a file grows between +/// the metadata check and the read, so no more than `limit + 1` bytes are +/// ever retained. The caller owns the failure diagnostic, which keeps the +/// CLI's existing `configuration` class and message for HAR import. +fn read_bounded_input(path: &Path, limit: usize) -> std::io::Result { + let file = std::fs::File::open(path)?; + if file.metadata()?.len() > limit as u64 { + return Ok(BoundedInput::TooLarge); + } + let mut bytes = Vec::new(); + file.take(limit as u64 + 1).read_to_end(&mut bytes)?; + if bytes.len() > limit { + return Ok(BoundedInput::TooLarge); + } + Ok(BoundedInput::Read(bytes)) +} + fn har_import(args: &HarImportArgs) -> Result<(), (String, String)> { - if args.fixture.exists() && !args.overwrite { + // Inputs first: read and parse the HAR into a staged writer before the + // target is replaced, so a malformed document cannot destroy the old + // fixture. The read is bounded before parsing: `std::fs::read` would + // allocate the whole file before the library's 32 MiB check. + let har_bytes = match read_bounded_input(&args.har, eggreplay_har::MAX_HAR_FILE_BYTES) { + Ok(BoundedInput::Read(bytes)) => bytes, + Ok(BoundedInput::TooLarge) => { + return Err(( + "configuration".into(), + "HAR document exceeds size limit".into(), + )); + } + Err(error) => { + return Err(( + "configuration".into(), + format!("cannot read HAR file: {error}"), + )); + } + }; + let (redaction, profile_id) = har_redaction_policy(args); + if let Err(error) = check_replacement_target(&args.fixture, args.overwrite, "fixture") { emit( "har-import", args.output.output, @@ -2434,24 +2988,11 @@ fn har_import(args: &HarImportArgs) -> Result<(), (String, String)> { Some("configuration"), json!({"fixture": args.fixture, "har": args.har}), ); - return Err(( - "configuration".into(), - "fixture exists; pass --overwrite to replace it".into(), - )); - } - let har_bytes = std::fs::read(&args.har).map_err(|error| { - ( - "configuration".into(), - format!("cannot read HAR file: {error}"), - ) - })?; - let (redaction, profile_id) = har_redaction_policy(args); - if args.fixture.exists() { - std::fs::remove_dir_all(&args.fixture) - .map_err(|error| ("runtime".into(), error.to_string()))?; + return Err(error); } + let mut staging = StagingGuard::new(sibling_transaction_path(&args.fixture, "har-import")); let mut writer = eggreplay_store::SessionWriter::create( - &args.fixture, + staging.path(), SessionMetadata { capture_mode: "har-import".into(), redaction_profile: profile_id.clone(), @@ -2480,6 +3021,11 @@ fn har_import(args: &HarImportArgs) -> Result<(), (String, String)> { let session = writer .finish() .map_err(|error| ("fixture".into(), error.to_string()))?; + let flow_count = session.manifest().flow_count; + let warnings = session_redaction_warnings(&session, &redaction, false, false, false); + drop(session); + // The loss report is an output of the completed staged fixture. Writing it + // before the swap keeps a write failure from replacing the target. if let Some(loss_path) = &args.loss_report { let loss_document = json!({ "command": "har-import", @@ -2489,14 +3035,17 @@ fn har_import(args: &HarImportArgs) -> Result<(), (String, String)> { "flows": report.flows, "losses": report.losses, }); - std::fs::write( + write_sensitive_file( loss_path, - serde_json::to_vec_pretty(&loss_document) + &serde_json::to_vec_pretty(&loss_document) .map_err(|error| ("runtime".into(), error.to_string()))?, ) .map_err(|error| ("runtime".into(), error.to_string()))?; } - emit( + replace_fixture_transactionally(staging.path(), &args.fixture) + .map_err(|error| ("runtime".into(), error))?; + staging.disarm(); + emit_with_warnings( "har-import", args.output.output, true, @@ -2504,11 +3053,12 @@ fn har_import(args: &HarImportArgs) -> Result<(), (String, String)> { json!({ "fixture": args.fixture, "har": args.har, - "flow_count": session.manifest().flow_count, + "flow_count": flow_count, "entries": report.entries, "loss_count": report.losses.len(), "loss_report": args.loss_report, }), + warnings, ); Ok(()) } @@ -2531,9 +3081,9 @@ fn har_export(args: &HarExportArgs) -> Result<(), (String, String)> { { std::fs::create_dir_all(parent).map_err(|error| ("runtime".into(), error.to_string()))?; } - std::fs::write( + write_sensitive_file( &args.har, - serde_json::to_vec_pretty(&har).map_err(|error| ("runtime".into(), error.to_string()))?, + &serde_json::to_vec_pretty(&har).map_err(|error| ("runtime".into(), error.to_string()))?, ) .map_err(|error| ("runtime".into(), error.to_string()))?; if let Some(loss_path) = &args.loss_report { @@ -2544,9 +3094,9 @@ fn har_export(args: &HarExportArgs) -> Result<(), (String, String)> { "entries": report.entries, "losses": report.losses, }); - std::fs::write( + write_sensitive_file( loss_path, - serde_json::to_vec_pretty(&loss_document) + &serde_json::to_vec_pretty(&loss_document) .map_err(|error| ("runtime".into(), error.to_string()))?, ) .map_err(|error| ("runtime".into(), error.to_string()))?; @@ -2592,29 +3142,41 @@ fn migrate(args: &MigrateArgs) -> Result<(), (String, String)> { format!("unsupported target session schema {target_schema}"), )); } + // Validate the destination before the source is even read. A non-fixture + // destination is refused here, so the transactional replace can never + // interpret `--overwrite` as permission to delete an arbitrary path. + let destination = match args.to.as_ref() { + Some(destination) => { + check_replacement_target(destination, args.overwrite, "destination")?; + Some(destination) + } + None => None, + }; let source = Session::open(&args.fixture, StoreLimits::default()) .map_err(|error| ("fixture".into(), error.to_string()))?; if args.in_place { // Transactional in-place: stage to a sibling, validate (migration // opens the staged copy), then atomically replace the source. The // source is never mutated before the new fixture validates. - let staged = sibling_transaction_path(&args.fixture, "migrate"); - let migrated = - eggreplay_har::migrate_session(&source, &staged, target_schema).map_err(|error| { - match error { - eggreplay_har::MigrationError::Blocked(message) => ("fixture".into(), message), - eggreplay_har::MigrationError::Store(inner) => { - ("fixture".into(), inner.to_string()) - } - eggreplay_har::MigrationError::Runtime(message) => ("runtime".into(), message), - } - })?; + let mut staging = StagingGuard::new(sibling_transaction_path(&args.fixture, "migrate")); + let migrated = eggreplay_har::migrate_session( + &source, + staging.path(), + target_schema, + args.scrub_stream_trailers, + ) + .map_err(|error| match error { + eggreplay_har::MigrationError::Blocked(message) => ("fixture".into(), message), + eggreplay_har::MigrationError::Store(inner) => ("fixture".into(), inner.to_string()), + eggreplay_har::MigrationError::Runtime(message) => ("runtime".into(), message), + })?; let flow_count = migrated.manifest().flow_count; let schema_version = migrated.manifest().metadata.schema_version; drop(migrated); drop(source); - replace_fixture_transactionally(&staged, &args.fixture) + replace_fixture_transactionally(staging.path(), &args.fixture) .map_err(|error| ("runtime".into(), error))?; + staging.disarm(); emit( "migrate", args.output.output, @@ -2624,32 +3186,27 @@ fn migrate(args: &MigrateArgs) -> Result<(), (String, String)> { ); Ok(()) } else { - let destination = args.to.as_ref().expect("destination checked above"); - if destination.exists() && !args.overwrite { - return Err(( - "configuration".into(), - "destination exists; pass --overwrite to replace it".into(), - )); - } + let destination = destination.expect("destination checked above"); // Transactional --to: migrate into a sibling staging directory, then // publish to the destination (replacing transactionally when it // exists). The source is never mutated. - let staged = sibling_transaction_path(destination, "migrate"); - let migrated = - eggreplay_har::migrate_session(&source, &staged, target_schema).map_err(|error| { - match error { - eggreplay_har::MigrationError::Blocked(message) => ("fixture".into(), message), - eggreplay_har::MigrationError::Store(inner) => { - ("fixture".into(), inner.to_string()) - } - eggreplay_har::MigrationError::Runtime(message) => ("runtime".into(), message), - } - })?; + let mut staging = StagingGuard::new(sibling_transaction_path(destination, "migrate")); + let migrated = eggreplay_har::migrate_session( + &source, + staging.path(), + target_schema, + args.scrub_stream_trailers, + ) + .map_err(|error| match error { + eggreplay_har::MigrationError::Blocked(message) => ("fixture".into(), message), + eggreplay_har::MigrationError::Store(inner) => ("fixture".into(), inner.to_string()), + eggreplay_har::MigrationError::Runtime(message) => ("runtime".into(), message), + })?; let flow_count = migrated.manifest().flow_count; let schema_version = migrated.manifest().metadata.schema_version; drop(migrated); if destination.exists() { - replace_fixture_transactionally(&staged, destination) + replace_fixture_transactionally(staging.path(), destination) .map_err(|error| ("runtime".into(), error))?; } else { if let Some(parent) = destination @@ -2659,9 +3216,10 @@ fn migrate(args: &MigrateArgs) -> Result<(), (String, String)> { std::fs::create_dir_all(parent) .map_err(|error| ("runtime".into(), error.to_string()))?; } - std::fs::rename(&staged, destination) + std::fs::rename(staging.path(), destination) .map_err(|error| ("runtime".into(), error.to_string()))?; } + staging.disarm(); emit( "migrate", args.output.output, @@ -2857,6 +3415,12 @@ fn emit_with_warnings( } } OutputChoice::Human => { + // Warnings are operational diagnostics: stderr in human mode, the + // envelope's `warnings` array in JSON mode. They never affect + // success or the exit code. + for warning in &warnings { + eprintln!("warning: {warning}"); + } // Human rendering is terminal text, never machine JSON. if success { println!("{command}: ok"); @@ -2867,9 +3431,70 @@ fn emit_with_warnings( } } +/// Listener exposure gate (M021D). +/// +/// These are unit tests for [`check_listener_bind`], which is the *only* +/// place the loopback policy is decided. They are compiled in every build — +/// including one without the `intercept` feature — because the gate itself +/// must be present in every build. +#[cfg(test)] +mod listener_gate_tests { + use super::check_listener_bind; + + fn bind(address: &str) -> std::net::SocketAddr { + address.parse().expect("socket address literal") + } + + #[test] + fn loopback_binds_always_pass_without_the_opt_in() { + for address in ["127.0.0.1:0", "127.0.0.1:8080", "[::1]:0", "[::1]:8080"] { + assert!( + check_listener_bind(bind(address), false).is_ok(), + "loopback {address} must never need the opt-in" + ); + } + } + + #[test] + fn wildcard_and_lan_binds_require_the_opt_in() { + // `0.0.0.0` and `::` are the two "every interface" addresses; the LAN + // address is the ordinary case of a routable bind. All three are + // unauthenticated listeners once reachable. + for address in ["0.0.0.0:0", "[::]:0", "192.168.1.20:8080"] { + let (class, message) = + check_listener_bind(bind(address), false).expect_err("must refuse"); + assert_eq!( + class, "configuration", + "the refusal class is the exit-code key" + ); + assert!( + message.contains(&bind(address).to_string()), + "the refusal must name the bind: {message}" + ); + assert!( + message.contains("unauthenticated"), + "the refusal must state the listener is unauthenticated: {message}" + ); + assert!( + message.contains("TLS does not add authentication"), + "the refusal must state that TLS is not authentication: {message}" + ); + assert!( + message.contains("--allow-non-loopback"), + "the refusal must name the opt-in flag: {message}" + ); + assert!( + check_listener_bind(bind(address), true).is_ok(), + "the explicit opt-in must permit {address}" + ); + } + } +} + #[cfg(test)] mod transaction_tests { use super::*; + use eggreplay_core::{BodyRef, Flow, FlowOutcome, HttpRequest, HttpResponse, Provenance}; fn temp_path(label: &str) -> PathBuf { std::env::temp_dir().join(format!( @@ -2882,40 +3507,125 @@ mod transaction_tests { )) } + #[test] + fn bounded_input_reads_exactly_the_limit_and_rejects_one_byte_over() { + let file = temp_path("bounded-input"); + std::fs::write(&file, b"12345678").unwrap(); + match read_bounded_input(&file, 8).unwrap() { + BoundedInput::Read(bytes) => assert_eq!(bytes, b"12345678"), + BoundedInput::TooLarge => panic!("exactly the limit must be accepted"), + } + std::fs::write(&file, b"123456789").unwrap(); + assert!( + matches!( + read_bounded_input(&file, 8).unwrap(), + BoundedInput::TooLarge + ), + "one byte over the limit must be refused" + ); + std::fs::remove_file(&file).unwrap(); + } + + fn minimal_flow(index: usize) -> Flow { + Flow { + schema_version: eggreplay_core::SCHEMA_VERSION, + id: format!("flow-{index}"), + started_at_ms: 1, + completed_at_ms: Some(2), + request: HttpRequest { + method: "GET".into(), + scheme: "http".into(), + authority: "example.test".into(), + path: format!("/item-{index}"), + query: vec![], + headers: vec![], + body: BodyRef::Empty, + trailers: vec![], + }, + outcome: FlowOutcome::Response(HttpResponse { + status: 200, + headers: vec![], + body: BodyRef::Empty, + trailers: vec![], + }), + physical_route: None, + provenance: Provenance { + mode: "test".into(), + observer: "test".into(), + }, + annotations: vec![], + redactions: vec![], + } + } + + /// Publish a valid fixture at `path`, which must not exist yet. + fn publish_fixture(path: &Path, schema_version: u16, flows: usize) { + let metadata = SessionMetadata { + schema_version, + ..SessionMetadata::default() + }; + let mut writer = + eggreplay_store::SessionWriter::create(path, metadata, StoreLimits::default()).unwrap(); + for index in 0..flows { + writer.append_flow(&minimal_flow(index)).unwrap(); + } + drop(writer.finish().unwrap()); + } + + fn open_flow_count(path: &Path) -> usize { + Session::open(path, StoreLimits::default()) + .unwrap() + .manifest() + .flow_count + } + #[test] fn replacement_publishes_complete_stage_and_rolls_back_failed_publish() { let target = temp_path("target"); let staged = temp_path("staged"); - std::fs::create_dir_all(&target).unwrap(); - std::fs::create_dir_all(&staged).unwrap(); - std::fs::write(target.join("value"), "old").unwrap(); - std::fs::write(staged.join("value"), "new").unwrap(); + publish_fixture(&target, 2, 0); + publish_fixture(&staged, 2, 2); replace_fixture_transactionally(&staged, &target).unwrap(); - assert_eq!( - std::fs::read_to_string(target.join("value")).unwrap(), - "new" - ); + assert_eq!(open_flow_count(&target), 2); assert!(!staged.exists()); let missing_stage = temp_path("missing-stage"); let result = replace_fixture_transactionally(&missing_stage, &target); assert!(result.is_err()); assert_eq!( - std::fs::read_to_string(target.join("value")).unwrap(), - "new" + open_flow_count(&target), + 2, + "failed publish must preserve the published fixture" ); std::fs::remove_dir_all(target).unwrap(); } + #[test] + fn replacement_refuses_a_non_fixture_target() { + let target = temp_path("refuse-target"); + std::fs::create_dir_all(&target).unwrap(); + std::fs::write(target.join("keep.txt"), "keep").unwrap(); + let staged = temp_path("refuse-staged"); + publish_fixture(&staged, 2, 1); + let error = replace_fixture_transactionally(&staged, &target).unwrap_err(); + assert!( + error.contains("cannot replace"), + "unexpected error: {error}" + ); + assert_eq!( + std::fs::read_to_string(target.join("keep.txt")).unwrap(), + "keep" + ); + assert!(staged.exists(), "refusal must not consume the staged tree"); + std::fs::remove_dir_all(target).unwrap(); + std::fs::remove_dir_all(staged).unwrap(); + } + #[test] fn interrupted_replacement_restores_the_last_valid_backup() { let target = temp_path("recover-target"); let staging = temp_path("recover-stage"); - let writer = - RecordingSession::create(&staging, SessionMetadata::default(), StoreLimits::default()) - .unwrap(); - writer.shutdown(); - writer.finish().unwrap(); + publish_fixture(&staging, 2, 0); let backup = sibling_transaction_path(&target, "backup"); std::fs::rename(&staging, &backup).unwrap(); recover_fixture_transactionally(&target).unwrap(); @@ -2923,4 +3633,244 @@ mod transaction_tests { assert!(!backup.exists()); std::fs::remove_dir_all(target).unwrap(); } + + // -- replacement-target authority ------------------------------------- + + fn classify(path: &Path) -> Result { + classify_replacement_target(path) + } + + #[test] + fn absent_path_is_safe_to_create() { + let dir = tempfile::tempdir().unwrap(); + let absent = dir.path().join("new.eggr"); + assert_eq!(classify(&absent).unwrap(), ReplacementTarget::Absent); + assert!(!absent.exists(), "classification must not create anything"); + } + + #[test] + fn empty_directory_is_replaceable() { + let dir = tempfile::tempdir().unwrap(); + let empty = dir.path().join("empty.eggr"); + std::fs::create_dir(&empty).unwrap(); + assert_eq!(classify(&empty).unwrap(), ReplacementTarget::EmptyDir); + } + + #[test] + fn valid_schema_one_and_two_fixtures_are_replaceable() { + let dir = tempfile::tempdir().unwrap(); + for schema in [1, 2] { + let fixture = dir.path().join(format!("schema-{schema}.eggr")); + publish_fixture(&fixture, schema, 1); + assert_eq!( + classify(&fixture).unwrap(), + ReplacementTarget::ValidFixture, + "schema {schema} fixture must be replaceable" + ); + } + } + + #[test] + fn fixture_with_an_extra_unrelated_file_is_still_accepted() { + let dir = tempfile::tempdir().unwrap(); + let fixture = dir.path().join("extended.eggr"); + publish_fixture(&fixture, 2, 1); + std::fs::write(fixture.join("notes.txt"), "extension file").unwrap(); + assert_eq!(classify(&fixture).unwrap(), ReplacementTarget::ValidFixture); + } + + #[test] + fn non_empty_unrelated_directory_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let unrelated = dir.path().join("unrelated"); + std::fs::create_dir(&unrelated).unwrap(); + std::fs::write(unrelated.join("keep.txt"), "keep").unwrap(); + let error = classify(&unrelated).unwrap_err(); + assert!( + error.contains("valid EggReplay fixture"), + "unexpected error: {error}" + ); + assert_eq!( + std::fs::read_to_string(unrelated.join("keep.txt")).unwrap(), + "keep" + ); + } + + #[test] + fn regular_file_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let file = dir.path().join("fixture.eggr"); + std::fs::write(&file, "not a directory").unwrap(); + let error = classify(&file).unwrap_err(); + assert!(error.contains("not a directory"), "unexpected: {error}"); + } + + #[test] + fn incomplete_fixture_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let incomplete = dir.path().join("incomplete.eggr"); + std::fs::create_dir(&incomplete).unwrap(); + std::fs::write(incomplete.join("flows.jsonl"), b"").unwrap(); + let error = classify(&incomplete).unwrap_err(); + assert!( + error.contains("valid EggReplay fixture"), + "unexpected error: {error}" + ); + } + + #[test] + fn future_schema_fixture_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let fixture = dir.path().join("future.eggr"); + publish_fixture(&fixture, 2, 0); + let manifest_path = fixture.join("manifest.json"); + let mut manifest: serde_json::Value = + serde_json::from_slice(&std::fs::read(&manifest_path).unwrap()).unwrap(); + manifest["schema_version"] = serde_json::json!(99); + std::fs::write( + &manifest_path, + serde_json::to_vec_pretty(&manifest).unwrap(), + ) + .unwrap(); + let error = classify(&fixture).unwrap_err(); + assert!( + error.contains("unsupported fixture schema 99"), + "unexpected error: {error}" + ); + assert!(fixture.exists(), "refusal must not delete the fixture"); + } + + #[test] + fn lexical_dot_and_root_paths_are_refused() { + for path in [".", "..", "fixture.eggr/..", "parent/../fixture.eggr"] { + let error = classify(Path::new(path)).unwrap_err(); + assert!( + error.contains("final component") || error.contains("contains a"), + "path {path:?} must be refused lexically, got {error}" + ); + } + #[cfg(unix)] + assert!(classify(Path::new("/")).is_err()); + #[cfg(windows)] + assert!(classify(Path::new(r"C:\")).is_err()); + } + + #[test] + fn current_dir_prefix_is_an_ordinary_spelling() { + // `./name` resolves to the same directory; only `..` can escape the + // operator's intended parent. + assert_eq!( + classify(Path::new("./eggreplay-m021b-absent.eggr")).unwrap(), + ReplacementTarget::Absent + ); + } + + #[cfg(unix)] + #[test] + fn symlink_target_is_refused_without_following_it() { + let dir = tempfile::tempdir().unwrap(); + let real = dir.path().join("real.eggr"); + publish_fixture(&real, 2, 0); + let link = dir.path().join("link.eggr"); + std::os::unix::fs::symlink(&real, &link).unwrap(); + let error = classify(&link).unwrap_err(); + assert!(error.contains("symbolic link"), "unexpected error: {error}"); + assert!( + std::fs::symlink_metadata(&link) + .unwrap() + .file_type() + .is_symlink(), + "the link must not be replaced or removed" + ); + Session::open(&real, StoreLimits::default()).unwrap(); + } + + #[test] + fn missing_overwrite_flag_is_a_configuration_refusal() { + let dir = tempfile::tempdir().unwrap(); + let fixture = dir.path().join("existing.eggr"); + publish_fixture(&fixture, 2, 0); + let (class, message) = check_replacement_target(&fixture, false, "fixture").unwrap_err(); + assert_eq!(class, "configuration"); + assert!(message.contains("--overwrite"), "unexpected: {message}"); + assert_eq!( + check_replacement_target(&fixture, true, "fixture").unwrap(), + ReplacementTarget::ValidFixture + ); + } + + #[test] + fn warning_scanner_is_value_free_and_deduplicates() { + let mut first = minimal_flow(0); + first.request.headers.push(eggreplay_core::HeaderEntry { + name: "X-Api-Key".into(), + value: "WARNING-SENTINEL-1".into(), + }); + first.request.headers.push(eggreplay_core::HeaderEntry { + name: "X-Csrf-Token".into(), + value: "WARNING-SENTINEL-2".into(), + }); + first.request.query.push(eggreplay_core::QueryPair { + key: "access_token".into(), + value: "WARNING-SENTINEL-3".into(), + }); + // A second flow repeats the same header; warnings deduplicate. + let mut second = minimal_flow(1); + second.request.headers.push(eggreplay_core::HeaderEntry { + name: "x-api-key".into(), + value: "WARNING-SENTINEL-4".into(), + }); + let warnings = redaction_warnings( + &[first, second], + &eggreplay_core::RedactionConfig::default(), + false, + false, + false, + ); + assert_eq!( + warnings.len(), + 3, + "one warning per distinct field: {warnings:?}" + ); + let joined = warnings.join("\n"); + for name in ["x-api-key", "x-csrf-token", "access_token"] { + assert!(joined.contains(name), "{joined}"); + } + for sentinel in [ + "WARNING-SENTINEL-1", + "WARNING-SENTINEL-2", + "WARNING-SENTINEL-3", + ] { + assert!( + !joined.contains(sentinel), + "warnings must be value-free: {joined}" + ); + } + } + + #[test] + fn websocket_acquisition_without_payload_redaction_warns_once() { + let config = eggreplay_core::RedactionConfig::default_secure(); + let warnings = redaction_warnings(&[], &config, true, false, false); + assert_eq!(warnings.len(), 1, "{warnings:?}"); + assert!(warnings[0].contains("WebSocket"), "{warnings:?}"); + assert!( + redaction_warnings(&[], &config, true, true, false).is_empty(), + "text redaction is sufficient" + ); + assert!( + redaction_warnings(&[], &config, true, false, true).is_empty(), + "binary redaction is sufficient" + ); + let mut with_json = config.clone(); + with_json.json_paths.insert("/secret".into()); + assert!( + redaction_warnings(&[], &with_json, true, false, false).is_empty(), + "a configured JSON pointer is sufficient" + ); + assert!( + redaction_warnings(&[], &config, false, false, false).is_empty(), + "no warning when acquisition is disabled" + ); + } } diff --git a/crates/eggreplay-cli/tests/har_migrate.rs b/crates/eggreplay-cli/tests/har_migrate.rs index 148f439..6a108ae 100644 --- a/crates/eggreplay-cli/tests/har_migrate.rs +++ b/crates/eggreplay-cli/tests/har_migrate.rs @@ -240,6 +240,109 @@ fn har_import_rejects_invalid_har_as_configuration() { std::fs::remove_dir_all(dir).ok(); } +#[test] +fn har_import_refuses_oversized_input_before_reading() { + let dir = temp_dir("import-oversized"); + let har = dir.join("oversized.har"); + // A sparse file beyond the 32 MiB bound: the metadata check must refuse it + // without allocating or parsing the document. + let file = std::fs::File::create(&har).unwrap(); + file.set_len(eggreplay_har::MAX_HAR_FILE_BYTES as u64 + 1) + .unwrap(); + drop(file); + let fixture = dir.join("out.eggr"); + let output = run_cli(&[ + "har", + "import", + "--har", + har.to_str().unwrap(), + "--fixture", + fixture.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!( + output.status.code(), + Some(2), + "oversized HAR must be configuration" + ); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stderr.contains("configuration"), "stderr={stderr}"); + assert!( + stderr.contains("HAR document exceeds size limit"), + "the bounded reader must keep the library diagnostic: {stderr}" + ); + assert!(!fixture.exists(), "no partial fixture may be published"); + std::fs::remove_dir_all(dir).ok(); +} + +#[test] +fn har_import_truncates_a_multibyte_comment_without_panicking() { + // Byte 512 falls inside a 3-byte `€`; the pre-fix `input[..512]` panicked + // and the process exited 101. + let comment = "€".repeat(200); + assert_eq!(comment.len(), 600); + let dir = temp_dir("import-multibyte-comment"); + let har = dir.join("multibyte.har"); + let document = serde_json::json!({ + "log": { + "version": "1.2", + "creator": {"name": "test", "version": "0"}, + "entries": [{ + "startedDateTime": "2026-01-01T00:00:00.000Z", + "time": 1, + "request": { + "method": "GET", + "url": "http://example.test/multibyte", + "httpVersion": "http/1.1", + "cookies": [], + "headers": [], + "queryString": [], + "comment": comment + }, + "response": { + "status": 200, + "statusText": "OK", + "httpVersion": "http/1.1", + "cookies": [], + "headers": [], + "content": {"size": 0, "mimeType": "text/plain", "text": ""} + }, + "cache": {}, + "timings": {"send": 0, "wait": 0, "receive": 0} + }] + } + }); + std::fs::write(&har, serde_json::to_vec(&document).unwrap()).unwrap(); + let fixture = dir.join("out.eggr"); + let output = run_cli(&[ + "har", + "import", + "--har", + har.to_str().unwrap(), + "--fixture", + fixture.to_str().unwrap(), + "--output", + "json", + ]); + let code = output.status.code(); + assert_ne!(code, Some(101), "the import must not panic"); + assert_eq!( + code, + Some(0), + "boundary-safe truncation keeps the import successful: {}", + String::from_utf8_lossy(&output.stderr) + ); + let flows = std::fs::read_to_string(fixture.join("flows.jsonl")).unwrap(); + let flow: serde_json::Value = serde_json::from_str(flows.trim()).unwrap(); + let annotation = flow["annotations"][0][1].as_str().unwrap(); + assert!(annotation.ends_with('…'), "annotation={annotation}"); + let prefix = annotation.strip_suffix('…').unwrap(); + assert_eq!(prefix, "€".repeat(170), "byte 512 rounds down to 510"); + assert!(comment.starts_with(prefix)); + std::fs::remove_dir_all(dir).ok(); +} + #[test] fn har_export_embeds_lossy_provenance_and_never_claims_lossless() { let dir = temp_dir("export-ok"); @@ -549,3 +652,731 @@ fn migrate_future_extension_fails_closed_without_mutating_source() { ); std::fs::remove_dir_all(dir).ok(); } + +// --------------------------------------------------------------------------- +// M021A: stream-trailer remediation, HAR confidentiality, warning surface +// --------------------------------------------------------------------------- + +fn write_raw_stream_trailer_fixture(dir: &Path, name: &str) -> PathBuf { + use eggreplay_core::{ + BodyRef, Flow, FlowOutcome, HeaderEntry, HttpRequest, HttpResponse, Provenance, + SCHEMA_VERSION, SessionMetadata, StreamEvent, StreamEventKind, StreamEvents, + }; + use eggreplay_store::{SessionWriter, StoreLimits}; + + let fixture = dir.join(name); + let mut writer = + SessionWriter::create(&fixture, SessionMetadata::default(), StoreLimits::default()) + .unwrap(); + let flow = Flow { + schema_version: SCHEMA_VERSION, + id: "flow-raw-trailers".into(), + started_at_ms: 1, + completed_at_ms: Some(2), + request: HttpRequest { + method: "POST".into(), + scheme: "http".into(), + authority: "example.test".into(), + path: "/upload".into(), + query: vec![], + headers: vec![], + body: BodyRef::Empty, + trailers: vec![], + }, + outcome: FlowOutcome::Response(HttpResponse { + status: 200, + headers: vec![], + body: BodyRef::Empty, + trailers: vec![], + }), + physical_route: None, + provenance: Provenance { + mode: "test".into(), + observer: "test".into(), + }, + annotations: vec![], + redactions: vec![], + }; + writer.append_flow(&flow).unwrap(); + writer + .append_stream_events(eggreplay_core::FlowStreamEvents { + flow_id: flow.id.clone(), + start_offset_ns: 7, + request: vec![StreamEvent { + delta_ns: 11, + event: StreamEventKind::Trailers { + fields: vec![ + HeaderEntry { + name: "x-req-secret".into(), + value: "RAW-REQUEST-TRAILER-VALUE".into(), + }, + HeaderEntry { + name: "x-req-dup".into(), + value: "first".into(), + }, + HeaderEntry { + name: "x-req-dup".into(), + value: "second".into(), + }, + ], + }, + }], + response: vec![ + StreamEvent { + delta_ns: 13, + event: StreamEventKind::Trailers { + fields: vec![ + HeaderEntry { + name: "x-secret".into(), + value: "RAW-RESPONSE-TRAILER-VALUE".into(), + }, + HeaderEntry { + name: "x-dup".into(), + value: "one".into(), + }, + HeaderEntry { + name: "x-dup".into(), + value: "two".into(), + }, + ], + }, + }, + StreamEvent { + delta_ns: 17, + event: StreamEventKind::End, + }, + ], + }) + .unwrap(); + writer + .write_extension("other", 1, "other.json", false, b"{\"keep\":true}") + .unwrap(); + let _ = StreamEvents::default(); + let _ = writer.finish().unwrap(); + fixture +} + +fn extension_bytes(fixture: &Path, name: &str) -> Vec { + let session = + eggreplay_store::Session::open(fixture, eggreplay_store::StoreLimits::default()).unwrap(); + session.read_extension(name).unwrap().unwrap() +} + +#[test] +fn validate_warns_on_raw_stream_trailers_without_naming_the_value() { + let dir = temp_dir("m021a-validate-warn"); + let fixture = write_raw_stream_trailer_fixture(&dir, "raw.eggr"); + let output = run_cli(&[ + "validate", + "--fixture", + fixture.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!( + output.status.code(), + Some(0), + "stderr={}", + String::from_utf8_lossy(&output.stderr) + ); + let envelope = stdout_json(&output); + assert_eq!(envelope["success"], true); + let warnings = envelope["warnings"].as_array().unwrap(); + let joined = warnings + .iter() + .map(|warning| warning.as_str().unwrap_or_default()) + .collect::>() + .join("\n"); + assert!(joined.contains("flow-raw-trailers"), "{joined}"); + assert!(joined.contains('6'), "expected the field count: {joined}"); + for value in ["RAW-REQUEST-TRAILER-VALUE", "RAW-RESPONSE-TRAILER-VALUE"] { + assert!( + !joined.contains(value), + "warning must be value-free: {joined}" + ); + } + std::fs::remove_dir_all(dir).ok(); +} + +// One end-to-end remediation sequence: preserve-bytes, scrub, and idempotency +// are assertions about the same fixture, and splitting them would duplicate the +// construction setup. +#[allow(clippy::too_many_lines)] +#[test] +fn migrate_scrub_stream_trailers_is_transactional_and_idempotent() { + let dir = temp_dir("m021a-migrate-scrub"); + let fixture = write_raw_stream_trailer_fixture(&dir, "raw.eggr"); + let other_before = extension_bytes(&fixture, "other"); + let flows_before = std::fs::read(fixture.join("flows.jsonl")).unwrap(); + + // Without the flag, the extension bytes are preserved verbatim. + let copied = dir.join("copied.eggr"); + let preserved = run_cli(&[ + "migrate", + "--fixture", + fixture.to_str().unwrap(), + "--to", + copied.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!( + preserved.status.code(), + Some(0), + "stderr={}", + String::from_utf8_lossy(&preserved.stderr) + ); + let raw_before = extension_bytes(&fixture, "stream-events"); + assert_eq!( + extension_bytes(&copied, "stream-events"), + raw_before, + "migrate without --scrub-stream-trailers must preserve bytes" + ); + + let scrubbed = run_cli(&[ + "migrate", + "--fixture", + fixture.to_str().unwrap(), + "--in-place", + "--scrub-stream-trailers", + "--output", + "json", + ]); + assert_eq!( + scrubbed.status.code(), + Some(0), + "stderr={}", + String::from_utf8_lossy(&scrubbed.stderr) + ); + let scrubbed_bytes = extension_bytes(&fixture, "stream-events"); + let events: eggreplay_core::StreamEvents = serde_json::from_slice(&scrubbed_bytes).unwrap(); + let flow = &events.flows[0]; + let request = match &flow.request[0].event { + eggreplay_core::StreamEventKind::Trailers { fields } => fields, + other => panic!("expected request trailers, got {other:?}"), + }; + assert_eq!( + request + .iter() + .map(|field| field.name.as_str()) + .collect::>(), + ["x-req-secret", "x-req-dup", "x-req-dup"] + ); + assert!(request.iter().all(|field| field.value == "")); + assert_eq!(flow.request[0].delta_ns, 11, "delays are untouched"); + let response = match &flow.response[0].event { + eggreplay_core::StreamEventKind::Trailers { fields } => fields, + other => panic!("expected response trailers, got {other:?}"), + }; + assert_eq!( + response + .iter() + .map(|field| field.name.as_str()) + .collect::>(), + ["x-secret", "x-dup", "x-dup"] + ); + assert!(response.iter().all(|field| field.value == "")); + assert!(matches!( + &flow.response[1].event, + eggreplay_core::StreamEventKind::End + )); + assert_eq!(flow.response[1].delta_ns, 17, "delays are untouched"); + assert_eq!( + extension_bytes(&fixture, "other"), + other_before, + "unrelated extensions are untouched" + ); + assert_eq!( + std::fs::read(fixture.join("flows.jsonl")).unwrap(), + flows_before, + "flows are untouched" + ); + + // A second scrub is a byte-for-byte no-op. + let second = run_cli(&[ + "migrate", + "--fixture", + fixture.to_str().unwrap(), + "--in-place", + "--scrub-stream-trailers", + "--output", + "json", + ]); + assert_eq!(second.status.code(), Some(0)); + assert_eq!( + extension_bytes(&fixture, "stream-events"), + scrubbed_bytes, + "a second scrub must be idempotent" + ); + + // validate no longer warns after remediation. + let check = run_cli(&[ + "validate", + "--fixture", + fixture.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!(check.status.code(), Some(0)); + assert!( + stdout_json(&check)["warnings"] + .as_array() + .unwrap() + .is_empty(), + "remediated fixture must not warn: {}", + stdout_json(&check)["warnings"] + ); + std::fs::remove_dir_all(dir).ok(); +} + +fn har_with_header(header: &str, sentinel: &str) -> String { + format!( + r#"{{"log": {{"version": "1.2", "creator": {{"name": "m021a", "version": "1"}}, "entries": [{{ + "startedDateTime": "2026-03-01T12:00:00.000Z", "time": 5, + "request": {{ + "method": "GET", "url": "http://example.test/path", + "headers": [{{"name": "{header}", "value": "{sentinel}"}}], + "queryString": [] + }}, + "response": {{"status": 200, "headers": [], "content": {{"size": 0}}}}, + "cache": {{}}, "timings": {{}} + }}]}}}}"# + ) +} + +fn har_with_credentials(header: &str, query: &str, sentinel: &str) -> String { + format!( + r#"{{"log": {{"version": "1.2", "creator": {{"name": "m021a", "version": "1"}}, "entries": [{{ + "startedDateTime": "2026-03-01T12:00:00.000Z", "time": 5, + "request": {{ + "method": "GET", "url": "http://example.test/path?{query}={sentinel}", + "headers": [{{"name": "{header}", "value": "{sentinel}"}}], + "queryString": [{{"name": "{query}", "value": "{sentinel}"}}] + }}, + "response": {{"status": 200, "headers": [], "content": {{"size": 0}}}}, + "cache": {{}}, "timings": {{}} + }}]}}}}"# + ) +} + +fn fixture_contains(fixture: &Path, needle: &str) -> bool { + walk(fixture).iter().any(|path| { + std::fs::read(path).is_ok_and(|bytes| { + bytes + .windows(needle.len()) + .any(|window| window == needle.as_bytes()) + }) + }) +} + +#[test] +fn curated_default_headers_are_redacted_and_absent_from_the_fixture() { + let dir = temp_dir("m021a-default-api-key"); + let sentinel = "M021A-API-KEY-SENTINEL-31ac"; + let har = har_with_header("X-Api-Key", sentinel); + let har_path = dir.join("input.har"); + std::fs::write(&har_path, har).unwrap(); + let fixture = dir.join("out.eggr"); + let output = run_cli(&[ + "har", + "import", + "--har", + har_path.to_str().unwrap(), + "--fixture", + fixture.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!( + output.status.code(), + Some(0), + "stderr={}", + String::from_utf8_lossy(&output.stderr) + ); + let envelope = stdout_json(&output); + assert!( + !envelope["warnings"] + .as_array() + .unwrap() + .iter() + .any(|warning| warning.as_str().unwrap_or_default().contains("x-api-key")), + "a default-redacted header must not warn: {envelope}" + ); + assert!(!fixture_contains(&fixture, sentinel)); + let session = + eggreplay_store::Session::open(&fixture, eggreplay_store::StoreLimits::default()).unwrap(); + let flow = session.iter_flows().unwrap().next().unwrap().unwrap(); + assert!(flow.request.headers.iter().any( + |header| header.name.eq_ignore_ascii_case("x-api-key") && header.value == "" + )); + assert!(flow.redactions.iter().any(|marker| { + marker + .field + .eq_ignore_ascii_case("request.headers.x-api-key") + })); + std::fs::remove_dir_all(dir).ok(); +} + +#[test] +fn unsafe_replace_records_the_sentinel_and_warns_value_free() { + let dir = temp_dir("m021a-unsafe-replace"); + let sentinel = "M021A-UNSAFE-SENTINEL-b7d2"; + let har_path = dir.join("input.har"); + std::fs::write(&har_path, har_with_header("X-Api-Key", sentinel)).unwrap(); + let fixture = dir.join("out.eggr"); + let output = run_cli(&[ + "har", + "import", + "--har", + har_path.to_str().unwrap(), + "--fixture", + fixture.to_str().unwrap(), + "--unsafe-replace-default-redaction", + "--output", + "json", + ]); + assert_eq!( + output.status.code(), + Some(0), + "stderr={}", + String::from_utf8_lossy(&output.stderr) + ); + let envelope = stdout_json(&output); + let warnings = envelope["warnings"] + .as_array() + .unwrap() + .iter() + .map(|warning| warning.as_str().unwrap_or_default()) + .collect::>() + .join("\n"); + assert!(warnings.contains("x-api-key"), "{warnings}"); + assert!(!warnings.contains(sentinel), "warnings must be value-free"); + assert!( + fixture_contains(&fixture, sentinel), + "the override explicitly replaces the default set" + ); + std::fs::remove_dir_all(dir).ok(); +} + +#[test] +fn csrf_header_warns_without_selector_and_redacts_with_one() { + let dir = temp_dir("m021a-csrf"); + let sentinel = "M021A-CSRF-SENTINEL-dc48"; + let har_path = dir.join("input.har"); + std::fs::write(&har_path, har_with_header("X-Csrf-Token", sentinel)).unwrap(); + + let unselected = dir.join("unselected.eggr"); + let output = run_cli(&[ + "har", + "import", + "--har", + har_path.to_str().unwrap(), + "--fixture", + unselected.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!(output.status.code(), Some(0)); + let envelope = stdout_json(&output); + let warnings = envelope["warnings"] + .as_array() + .unwrap() + .iter() + .map(|warning| warning.as_str().unwrap_or_default()) + .collect::>() + .join("\n"); + assert!(warnings.contains("x-csrf-token"), "{warnings}"); + assert!(!warnings.contains(sentinel)); + assert!( + fixture_contains(&unselected, sentinel), + "documented residual: x-csrf-token is recorded unless selected" + ); + + let selected = dir.join("selected.eggr"); + let output = run_cli(&[ + "har", + "import", + "--har", + har_path.to_str().unwrap(), + "--fixture", + selected.to_str().unwrap(), + "--redact-header", + "x-csrf-token", + "--output", + "json", + ]); + assert_eq!(output.status.code(), Some(0)); + let envelope = stdout_json(&output); + assert!( + !envelope["warnings"] + .as_array() + .unwrap() + .iter() + .any(|warning| warning + .as_str() + .unwrap_or_default() + .contains("x-csrf-token")), + "an explicit selector must suppress the warning: {envelope}" + ); + assert!(!fixture_contains(&selected, sentinel)); + std::fs::remove_dir_all(dir).ok(); +} + +#[test] +fn credential_query_warns_without_its_value() { + let dir = temp_dir("m021a-query-warning"); + let sentinel = "M021A-QUERY-SENTINEL-6c19"; + let har_path = dir.join("input.har"); + std::fs::write( + &har_path, + har_with_credentials("X-Ordinary", "access_token", sentinel), + ) + .unwrap(); + let fixture = dir.join("out.eggr"); + let output = run_cli(&[ + "har", + "import", + "--har", + har_path.to_str().unwrap(), + "--fixture", + fixture.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!(output.status.code(), Some(0)); + let envelope = stdout_json(&output); + let warnings = envelope["warnings"] + .as_array() + .unwrap() + .iter() + .map(|warning| warning.as_str().unwrap_or_default()) + .collect::>() + .join("\n"); + assert!(warnings.contains("access_token"), "{warnings}"); + assert!(!warnings.contains(sentinel), "warnings must be value-free"); + // Query defaults are unchanged: the value is recorded. + assert!(fixture_contains(&fixture, sentinel)); + std::fs::remove_dir_all(dir).ok(); +} + +#[cfg(unix)] +#[test] +fn har_export_and_loss_reports_are_private_on_unix() { + use std::os::unix::fs::PermissionsExt; + + let dir = temp_dir("m021a-har-permissions"); + let har_path = dir.join("out.har"); + let loss_path = dir.join("loss.json"); + let fixture = store_fixture("schema-2-stream"); + let output = run_cli(&[ + "har", + "export", + "--fixture", + fixture.to_str().unwrap(), + "--har", + har_path.to_str().unwrap(), + "--loss-report", + loss_path.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!( + output.status.code(), + Some(0), + "stderr={}", + String::from_utf8_lossy(&output.stderr) + ); + for path in [&har_path, &loss_path] { + let mode = std::fs::metadata(path).unwrap().permissions().mode() & 0o777; + assert_eq!( + mode, + 0o600, + "{} must be private, got {mode:o}", + path.display() + ); + } + + // Without --overwrite the existing HAR is refused and left untouched. + let before = std::fs::read(&har_path).unwrap(); + let refused = run_cli(&[ + "har", + "export", + "--fixture", + fixture.to_str().unwrap(), + "--har", + har_path.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!(refused.status.code(), Some(2)); + assert_eq!(std::fs::read(&har_path).unwrap(), before); + + // With --overwrite it is replaced and remains private. + let replaced = run_cli(&[ + "har", + "export", + "--fixture", + fixture.to_str().unwrap(), + "--har", + har_path.to_str().unwrap(), + "--overwrite", + "--output", + "json", + ]); + assert_eq!(replaced.status.code(), Some(0)); + let mode = std::fs::metadata(&har_path).unwrap().permissions().mode() & 0o777; + assert_eq!(mode, 0o600); + std::fs::remove_dir_all(dir).ok(); +} + +#[test] +fn exported_har_preserves_fixture_redaction_markers() { + let dir = temp_dir("m021a-har-redaction"); + let sentinel = "M021A-HAR-SENTINEL-e4b1"; + let har_path = dir.join("input.har"); + std::fs::write(&har_path, har_with_header("Authorization", sentinel)).unwrap(); + let fixture = dir.join("out.eggr"); + let import = run_cli(&[ + "har", + "import", + "--har", + har_path.to_str().unwrap(), + "--fixture", + fixture.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!(import.status.code(), Some(0)); + assert!(!fixture_contains(&fixture, sentinel)); + let exported = dir.join("out.har"); + let export = run_cli(&[ + "har", + "export", + "--fixture", + fixture.to_str().unwrap(), + "--har", + exported.to_str().unwrap(), + "--output", + "json", + ]); + assert_eq!(export.status.code(), Some(0)); + let text = std::fs::read_to_string(&exported).unwrap(); + assert!(text.contains(""), "markers are preserved"); + assert!( + !text.contains(sentinel), + "export must not reintroduce secrets" + ); + std::fs::remove_dir_all(dir).ok(); +} + +#[test] +fn har_comments_and_provenance_are_not_redacted_by_header_or_query_selectors() { + let dir = temp_dir("m021a-track-e"); + let comment = "TRACK-E-COMMENT-SENTINEL-8a3f"; + let creator = "TRACK-E-CREATOR-2d7c"; + let header_sentinel = "TRACK-E-HEADER-SENTINEL-50b9"; + let query_sentinel = "TRACK-E-QUERY-SENTINEL-f16a"; + let har = format!( + r#"{{"log": {{"version": "1.2", "creator": {{"name": "{creator}", "version": "9"}}, "entries": [{{ + "startedDateTime": "2026-03-01T12:00:00.000Z", "time": 5, + "request": {{ + "method": "GET", "url": "http://example.test/path?token={query_sentinel}", + "headers": [{{"name": "X-Track-E", "value": "{header_sentinel}"}}], + "queryString": [{{"name": "token", "value": "{query_sentinel}"}}], + "comment": "{comment}" + }}, + "response": {{"status": 200, "headers": [], "content": {{"size": 0}}, "comment": "{comment}"}}, + "cache": {{}}, "timings": {{}} + }}]}}}}"# + ); + let har_path = dir.join("input.har"); + std::fs::write(&har_path, har).unwrap(); + let fixture = dir.join("out.eggr"); + let output = run_cli(&[ + "har", + "import", + "--har", + har_path.to_str().unwrap(), + "--fixture", + fixture.to_str().unwrap(), + "--redact-header", + "x-track-e", + "--redact-query", + "token", + "--output", + "json", + ]); + assert_eq!( + output.status.code(), + Some(0), + "stderr={}", + String::from_utf8_lossy(&output.stderr) + ); + let session = + eggreplay_store::Session::open(&fixture, eggreplay_store::StoreLimits::default()).unwrap(); + let flow = session.iter_flows().unwrap().next().unwrap().unwrap(); + // Header/query selectors were applied... + assert!(flow.request.headers.iter().any( + |header| header.name.eq_ignore_ascii_case("x-track-e") && header.value == "" + )); + assert!( + flow.request + .query + .iter() + .any(|pair| pair.key == "token" && pair.value == "") + ); + // ...but user-supplied HAR metadata is outside that policy. + assert!( + flow.annotations + .iter() + .any(|(key, value)| key == "har.request.comment" && value.contains(comment)) + ); + assert!( + flow.annotations + .iter() + .any(|(key, value)| key == "har.response.comment" && value.contains(comment)) + ); + let provenance = String::from_utf8( + session + .read_extension("interop-provenance") + .unwrap() + .unwrap(), + ) + .unwrap(); + assert!(provenance.contains(creator), "{provenance}"); + std::fs::remove_dir_all(dir).ok(); +} + +#[test] +fn human_mode_emits_warnings_on_stderr_without_values() { + let dir = temp_dir("m021a-human-warnings"); + let sentinel = "M021A-HUMAN-SENTINEL-7c5e"; + let har_path = dir.join("input.har"); + std::fs::write(&har_path, har_with_header("X-Api-Key", sentinel)).unwrap(); + let fixture = dir.join("out.eggr"); + let output = run_cli(&[ + "har", + "import", + "--har", + har_path.to_str().unwrap(), + "--fixture", + fixture.to_str().unwrap(), + "--unsafe-replace-default-redaction", + ]); + assert_eq!( + output.status.code(), + Some(0), + "stderr={}", + String::from_utf8_lossy(&output.stderr) + ); + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stdout.contains("har-import: ok"), "{stdout}"); + assert!(stderr.contains("warning:"), "{stderr}"); + assert!(stderr.contains("x-api-key"), "{stderr}"); + assert!( + !stderr.contains(sentinel), + "human warnings must be value-free: {stderr}" + ); + std::fs::remove_dir_all(dir).ok(); +} diff --git a/crates/eggreplay-cli/tests/m021b_destructive_paths.rs b/crates/eggreplay-cli/tests/m021b_destructive_paths.rs new file mode 100644 index 0000000..6d359cd --- /dev/null +++ b/crates/eggreplay-cli/tests/m021b_destructive_paths.rs @@ -0,0 +1,492 @@ +//! M021B: destructive replacement targets are validated and staged. +//! +//! Every command that advertises `--overwrite` (or implicitly replaces its own +//! fixture, as `serve --record-mode re-record` does) must refuse a path that is +//! not an `EggReplay` fixture, an empty directory, or absent, and must leave the +//! old path byte-identical on every failure. + +use std::path::{Path, PathBuf}; +use std::process::{Command, Output}; + +fn binary() -> PathBuf { + PathBuf::from(env!("CARGO_BIN_EXE_eggreplay")) +} + +fn run_cli(args: &[&str]) -> Output { + Command::new(binary()) + .args(args) + .output() + .expect("CLI subprocess must run") +} + +fn stdout_json(output: &Output) -> serde_json::Value { + let stdout = String::from_utf8_lossy(&output.stdout); + serde_json::from_str(&stdout).expect("stdout must be a JSON envelope") +} + +fn temp_dir(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!( + "eggreplay-m021b-{name}-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +fn store_fixture(name: &str) -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../eggreplay-store/tests/fixtures") + .join(name) +} + +fn har_corpus(name: &str) -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../eggreplay-har/tests/corpus") + .join(name) +} + +/// Publish a valid fixture with a known flow count at `path` (must be absent). +fn write_fixture(path: &Path, schema_version: u16, flows: usize) { + use eggreplay_core::{ + BodyRef, Flow, FlowOutcome, HttpRequest, HttpResponse, Provenance, SCHEMA_VERSION, + SessionMetadata, + }; + use eggreplay_store::{SessionWriter, StoreLimits}; + + let metadata = SessionMetadata { + schema_version, + ..SessionMetadata::default() + }; + let mut writer = SessionWriter::create(path, metadata, StoreLimits::default()).unwrap(); + for index in 0..flows { + writer + .append_flow(&Flow { + schema_version: SCHEMA_VERSION, + id: format!("flow-{index}"), + started_at_ms: 1, + completed_at_ms: Some(2), + request: HttpRequest { + method: "GET".into(), + scheme: "http".into(), + authority: "example.test".into(), + path: format!("/item-{index}"), + query: vec![], + headers: vec![], + body: BodyRef::Empty, + trailers: vec![], + }, + outcome: FlowOutcome::Response(HttpResponse { + status: 200, + headers: vec![], + body: BodyRef::Empty, + trailers: vec![], + }), + physical_route: None, + provenance: Provenance { + mode: "test".into(), + observer: "test".into(), + }, + annotations: vec![], + redactions: vec![], + }) + .unwrap(); + } + drop(writer.finish().unwrap()); +} + +fn open_flow_count(path: &Path) -> usize { + eggreplay_store::Session::open(path, eggreplay_store::StoreLimits::default()) + .expect("fixture must still open") + .manifest() + .flow_count +} + +/// A deterministic byte snapshot of a directory tree. +fn snapshot_tree(root: &Path) -> Vec<(String, Vec)> { + fn walk(root: &Path, dir: &Path, entries: &mut Vec<(String, Vec)>) { + let mut children = std::fs::read_dir(dir) + .unwrap() + .collect::, _>>() + .unwrap(); + children.sort_by_key(std::fs::DirEntry::file_name); + for child in children { + let path = child.path(); + let relative = path + .strip_prefix(root) + .unwrap() + .to_string_lossy() + .into_owned(); + let metadata = std::fs::symlink_metadata(&path).unwrap(); + if metadata.is_dir() { + entries.push((format!("{relative}/"), Vec::new())); + walk(root, &path, entries); + } else { + entries.push((relative, std::fs::read(&path).unwrap())); + } + } + } + + let mut entries = Vec::new(); + walk(root, root, &mut entries); + entries +} + +/// No hidden `..*` staging/backup sibling may remain after an +/// operation. A store writer nests its own incomplete directory under the +/// hidden staging name (`...