From 927ba72dda18604c6d1035d0c3958460a0ef449b Mon Sep 17 00:00:00 2001 From: Tirthraj Chavan Date: Sat, 14 Nov 2026 17:16:30 +0530 Subject: [PATCH 1/6] fix(cli): gate unix-only migration apis behind cfg(unix) (#1121) (cherry picked from commit 645090106df55eab504ff49636c2d389a5d0ae96) --- .changeset/cli-migrate-cfg-gate.md | 4 ++++ .github/workflows/checks.yml | 15 ++++++++++++ crates/nostream-cli/src/commands/migrate.rs | 26 +++++++++++++++++---- crates/nostream-cli/tests/migrate_cli.rs | 4 ++++ crates/nostream-storage/src/backend.rs | 9 ++++++- 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 .changeset/cli-migrate-cfg-gate.md diff --git a/.changeset/cli-migrate-cfg-gate.md b/.changeset/cli-migrate-cfg-gate.md new file mode 100644 index 00000000..a4002a93 --- /dev/null +++ b/.changeset/cli-migrate-cfg-gate.md @@ -0,0 +1,4 @@ +--- +--- + +Gate Unix-only migration APIs and directory permissions behind cfg(unix) (#1121) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index fff0fe61..6f5782b0 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -284,6 +284,21 @@ jobs: - name: Release build run: cargo build --workspace --all-features --release + windows-cli-check: + name: Windows CLI Check + runs-on: windows-latest + needs: changes + if: needs.changes.outputs.rust == 'true' + steps: + - name: Checkout + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + - name: Install Rust toolchain + uses: actions-rust-lang/setup-rust-toolchain@166cdcfd11aee3cb47222f9ddb555ce30ddb9659 # v1.17.0 + - name: CLI builds on Windows + run: cargo build -p nostream-cli --no-default-features + changeset-check: name: Changeset Required runs-on: ubuntu-latest diff --git a/crates/nostream-cli/src/commands/migrate.rs b/crates/nostream-cli/src/commands/migrate.rs index 8d5b07bc..fbd83edc 100644 --- a/crates/nostream-cli/src/commands/migrate.rs +++ b/crates/nostream-cli/src/commands/migrate.rs @@ -13,6 +13,7 @@ use std::collections::{BTreeMap, BTreeSet}; use std::fs; use std::io; use std::io::Write; +#[cfg(unix)] use std::os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt, PermissionsExt}; use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; @@ -42,6 +43,7 @@ pub struct MigrateOptions { } /// `migrate pg-to-lmdb`: the copy, or the verification pass when `verify` is set. +#[cfg(unix)] pub fn run(options: MigrateOptions, out: &mut dyn Write) -> Result { let runtime = tokio::runtime::Builder::new_current_thread() .enable_all() @@ -110,6 +112,16 @@ pub fn run(options: MigrateOptions, out: &mut dyn Write) -> Result Result { + writeln!(out, "migrate pg-to-lmdb is unsupported on this platform") + .map_err(|e| CliError::Migrate(e.to_string()))?; + Ok(CommandOutput { + lines: Vec::new(), + exit_code: 1, + }) +} + /// The copy over an injected source, target and writer, which is how the whole run is tested /// without a process and without a database. /// @@ -575,10 +587,11 @@ fn write_manifest(target: &Path, manifest: &Manifest) -> Result<(), CliError> { // Without the file sync, a power loss can leave the new name pointing at a truncated // manifest; `read_manifest` then refuses it, and the nonempty-target rule refuses to // migrate over the store it describes, so the operator's only way forward is a discard. - let mut file = fs::OpenOptions::new() - .write(true) - .create_new(true) - .mode(0o600) + let mut open_options = fs::OpenOptions::new(); + open_options.write(true).create_new(true); + #[cfg(unix)] + open_options.mode(0o600); + let mut file = open_options .open(&temporary) .map_err(|error| CliError::Migrate(format!("{}: {error}", temporary.display())))?; file.write_all(&bytes) @@ -739,6 +752,7 @@ fn probe_budget() -> Duration { /// sends the copy somewhere the operator did not name. Both are refused before anything is /// opened. A target that does not exist yet cannot be judged here, so `create_target` creates it /// before the store opens and runs this check again over what is then on disk. +#[cfg(unix)] fn refuse_unsafe_target(options: &MigrateOptions) -> Result<(), CliError> { refuse_unsafe_target_for(options, effective_uid()) } @@ -747,12 +761,14 @@ fn refuse_unsafe_target(options: &MigrateOptions) -> Result<(), CliError> { /// /// `std::fs` exposes a file's owner but not the caller's, and reading it here costs no /// dependency: this crate runs on Linux, where the kernel publishes it in one line. +#[cfg(unix)] fn effective_uid() -> Option { let status = fs::read_to_string("/proc/self/status").ok()?; let line = status.lines().find(|line| line.starts_with("Uid:"))?; line.split_whitespace().nth(2)?.parse().ok() } +#[cfg(unix)] fn refuse_unsafe_target_for(options: &MigrateOptions, caller: Option) -> Result<(), CliError> { for path in [&options.target, &store_path(options)] { let metadata = match fs::symlink_metadata(path) { @@ -1038,6 +1054,7 @@ mod tests { /// A link where the data file belongs is followed by `is_file()` and by LMDB both, so the /// copy would read or write through it and heed would put `lock.mdb` beside the link's /// target. The predicate over the file's own metadata is what AC18 asks for. + #[cfg(unix)] #[test] #[allow(clippy::expect_used)] fn a_symlinked_data_file_is_refused() { @@ -1063,6 +1080,7 @@ mod tests { /// checked against the caller rather than the mode. Measured as if a different uid were /// running, because a directory this process owns cannot be made to belong to another user /// without root. + #[cfg(unix)] #[test] #[allow(clippy::expect_used)] fn a_target_owned_by_another_user_is_refused() { diff --git a/crates/nostream-cli/tests/migrate_cli.rs b/crates/nostream-cli/tests/migrate_cli.rs index 6d92b898..b29bfc94 100644 --- a/crates/nostream-cli/tests/migrate_cli.rs +++ b/crates/nostream-cli/tests/migrate_cli.rs @@ -2027,6 +2027,7 @@ async fn a_manifest_that_counts_rows_over_a_store_without_them_is_refused() { // invite code itself - a bearer credential NIP-43 accepts. The store's files are heed's 0600, so // the manifest was the world-readable copy of it. The planted link is the second half: a // temporary path that already exists must not be written through and renamed into place. +#[cfg(unix)] #[tokio::test] async fn the_manifest_is_private_and_its_temporary_path_is_not_followed() { use std::os::unix::fs::PermissionsExt; @@ -2069,6 +2070,7 @@ async fn the_manifest_is_private_and_its_temporary_path_is_not_followed() { // A group- or world-writable target lets another local user pre-place the store and read what // lands in it, so the copy refuses before opening anything. +#[cfg(unix)] #[test] fn a_group_writable_target_is_refused() { use std::os::unix::fs::PermissionsExt; @@ -2094,6 +2096,7 @@ fn a_group_writable_target_is_refused() { } // A symlinked target sends the copy somewhere the operator did not name. +#[cfg(unix)] #[test] fn a_symlinked_target_is_refused() { let root = tempfile::tempdir().expect("a temporary directory"); @@ -2486,6 +2489,7 @@ fn the_help_names_the_resume_and_the_restart() { // AC7: the summary names the store the copy landed in, so a path that went through a link is // visible as the real one rather than echoed back as the argument. +#[cfg(unix)] #[tokio::test] async fn the_summary_names_the_canonical_store_not_the_argument() { let root = tempfile::tempdir().expect("a temporary directory"); diff --git a/crates/nostream-storage/src/backend.rs b/crates/nostream-storage/src/backend.rs index 07a89460..8c8ed39a 100644 --- a/crates/nostream-storage/src/backend.rs +++ b/crates/nostream-storage/src/backend.rs @@ -1,7 +1,7 @@ //! Backend selection and the LMDB environment. Mirrors `src/utils/event-store.ts` (the //! selection half) and the storage boundary in `src/@types/adapters.ts`. -#[cfg(feature = "lmdb")] +#[cfg(all(feature = "lmdb", unix))] use std::os::unix::fs::DirBuilderExt; use std::path::{Path, PathBuf}; @@ -99,6 +99,7 @@ impl LmdbEnv { // host whose umask is 002 `create_dir_all` leaves it group-writable, which is both a // wider audience than the store's own 0600 files and something the migration's target // check refuses on the next run. + #[cfg(unix)] std::fs::DirBuilder::new() .recursive(true) .mode(0o700) @@ -107,6 +108,12 @@ impl LmdbEnv { path: path.clone(), source, })?; + #[cfg(not(unix))] + std::fs::create_dir_all(&path).map_err(|source| StorageError::CreateDirectory { + path: path.clone(), + source, + })?; + let env = Self::open_with(&path, map_size, EnvFlags::empty())?; Ok(Self { env, path }) } From 1f618f1f24b83d602804174fd95159e21d79c140 Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Sun, 15 Nov 2026 22:41:06 -0500 Subject: [PATCH 2/6] fix(cli): gate the last unix-only call sites behind cfg(unix) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `create_target` still called `DirBuilder::mode` and the now-gated `refuse_unsafe_target`, and two unit tests still read `uid()` and `Permissions::from_mode`, so `cargo build -p nostream-cli --no-default-features` on a non-Unix target still failed to compile — the class #1121 exists to close, and what the new Windows job reports. Gate the function and both tests; nothing calls them outside the `#[cfg(unix)] run()`, so no stub is needed. --- crates/nostream-cli/src/commands/migrate.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/nostream-cli/src/commands/migrate.rs b/crates/nostream-cli/src/commands/migrate.rs index fbd83edc..85167d86 100644 --- a/crates/nostream-cli/src/commands/migrate.rs +++ b/crates/nostream-cli/src/commands/migrate.rs @@ -909,6 +909,7 @@ fn refuse_swappable_ancestry(target: &Path) -> Result<(), CliError> { /// by `create_dir_all` without a word. Creating it here, under the mode the store itself uses, /// and checking again closes the window: after this call the target is a directory this process /// made, or the run has ended. +#[cfg(unix)] fn create_target(options: &MigrateOptions) -> Result<(), CliError> { fs::DirBuilder::new() .recursive(true) @@ -1109,6 +1110,7 @@ mod tests { /// needs no ownership of the target to do it: a rename takes write permission on the holder. /// Without the sticky bit that permission is refused; `/tmp` is exactly this mode plus the /// sticky bit, which is why the bit, and not the mode alone, is the question. + #[cfg(unix)] #[test] #[allow(clippy::expect_used)] fn a_target_under_a_swappable_directory_is_refused() { @@ -1139,6 +1141,7 @@ mod tests { /// target is created and then judged. Both halves are needed: the creation gives the second /// check something to see, and the second check is what refuses a directory another user /// planted while the source was being probed. + #[cfg(unix)] #[test] #[allow(clippy::expect_used)] fn a_missing_target_is_created_and_then_judged() { From 22ef684b79695dc0bec71119d22eba742ee5ce55 Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Sun, 15 Nov 2026 22:45:58 -0500 Subject: [PATCH 3/6] fix(cli): gate the ancestry check and pin the non-Unix refusal Greptile's first finding: `refuse_swappable_ancestry` reads `metadata.permissions().mode()` and was not gated, so Rust still type-checked it on Windows and the build still failed. Greptile's second: the Unix contract lived in tests that would compile on every platform and assert Unix behaviour. `migrate_cli.rs` now carries `#![cfg(unix)]` for the whole file (which is what the four per-test gates were reaching for), and `migrate_cli_unsupported.rs` pins the refusal where the copy cannot run. The Windows job runs that test too, so the refusal is executed on the platform it describes rather than merely built. --- .github/workflows/checks.yml | 2 ++ crates/nostream-cli/src/commands/migrate.rs | 1 + crates/nostream-cli/tests/migrate_cli.rs | 7 ++--- .../tests/migrate_cli_unsupported.rs | 29 +++++++++++++++++++ msg2.txt | 13 +++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 crates/nostream-cli/tests/migrate_cli_unsupported.rs create mode 100644 msg2.txt diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 6f5782b0..28dcf1df 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -298,6 +298,8 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@166cdcfd11aee3cb47222f9ddb555ce30ddb9659 # v1.17.0 - name: CLI builds on Windows run: cargo build -p nostream-cli --no-default-features + - name: CLI refuses the migration on Windows + run: cargo test -p nostream-cli --no-default-features --test migrate_cli_unsupported changeset-check: name: Changeset Required diff --git a/crates/nostream-cli/src/commands/migrate.rs b/crates/nostream-cli/src/commands/migrate.rs index 85167d86..2fd5d685 100644 --- a/crates/nostream-cli/src/commands/migrate.rs +++ b/crates/nostream-cli/src/commands/migrate.rs @@ -865,6 +865,7 @@ fn refuse_unsafe_target_for(options: &MigrateOptions, caller: Option) -> Re /// the entry itself, so a group- or world-writable ancestor lets another user move this target /// aside and put their own there before the store opens. The sticky bit is the exception that /// makes `/tmp` usable: it refuses the rename of an entry its writer does not own. +#[cfg(unix)] fn refuse_swappable_ancestry(target: &Path) -> Result<(), CliError> { let mut path = match target.parent() { Some(parent) if parent.as_os_str().is_empty() => PathBuf::from("."), diff --git a/crates/nostream-cli/tests/migrate_cli.rs b/crates/nostream-cli/tests/migrate_cli.rs index b29bfc94..834a6797 100644 --- a/crates/nostream-cli/tests/migrate_cli.rs +++ b/crates/nostream-cli/tests/migrate_cli.rs @@ -8,6 +8,9 @@ //! These targets may unwrap and assert by panicking: a failed assertion is a failed test. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +// The copy drives the Unix-only half of the command; the refusal on other platforms is pinned in +// `migrate_cli_unsupported.rs`, which is the only file that compiles there. +#![cfg(unix)] use std::collections::BTreeMap; use std::process::Command; @@ -2027,7 +2030,6 @@ async fn a_manifest_that_counts_rows_over_a_store_without_them_is_refused() { // invite code itself - a bearer credential NIP-43 accepts. The store's files are heed's 0600, so // the manifest was the world-readable copy of it. The planted link is the second half: a // temporary path that already exists must not be written through and renamed into place. -#[cfg(unix)] #[tokio::test] async fn the_manifest_is_private_and_its_temporary_path_is_not_followed() { use std::os::unix::fs::PermissionsExt; @@ -2070,7 +2072,6 @@ async fn the_manifest_is_private_and_its_temporary_path_is_not_followed() { // A group- or world-writable target lets another local user pre-place the store and read what // lands in it, so the copy refuses before opening anything. -#[cfg(unix)] #[test] fn a_group_writable_target_is_refused() { use std::os::unix::fs::PermissionsExt; @@ -2096,7 +2097,6 @@ fn a_group_writable_target_is_refused() { } // A symlinked target sends the copy somewhere the operator did not name. -#[cfg(unix)] #[test] fn a_symlinked_target_is_refused() { let root = tempfile::tempdir().expect("a temporary directory"); @@ -2489,7 +2489,6 @@ fn the_help_names_the_resume_and_the_restart() { // AC7: the summary names the store the copy landed in, so a path that went through a link is // visible as the real one rather than echoed back as the argument. -#[cfg(unix)] #[tokio::test] async fn the_summary_names_the_canonical_store_not_the_argument() { let root = tempfile::tempdir().expect("a temporary directory"); diff --git a/crates/nostream-cli/tests/migrate_cli_unsupported.rs b/crates/nostream-cli/tests/migrate_cli_unsupported.rs new file mode 100644 index 00000000..2af51c54 --- /dev/null +++ b/crates/nostream-cli/tests/migrate_cli_unsupported.rs @@ -0,0 +1,29 @@ +//! The `migrate pg-to-lmdb` command where the copy cannot run: every part of it needs Unix file +//! modes, so the command still exists but refuses, and this file pins what it answers. +//! +//! The copy, the manifest, resume and verify live in `migrate_cli.rs`, which compiles only on +//! Unix. CI runs this one from the `windows-latest` job. + +#![cfg(not(unix))] +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use std::process::Command; + +/// The refusal names the command, says the platform is the reason, and exits non-zero, so an +/// operator reading a pipeline log learns why rather than seeing a compile error or a hang. +#[test] +fn the_command_is_refused_with_a_named_reason_and_exit_1() { + let directory = tempfile::tempdir().expect("a temporary directory"); + let output = Command::new(env!("CARGO_BIN_EXE_nostream-cli")) + .args(["migrate", "pg-to-lmdb", "--target"]) + .arg(directory.path()) + .output() + .expect("the binary runs"); + + assert_eq!(output.status.code(), Some(1), "the refusal exits 1"); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!( + stdout.contains("migrate pg-to-lmdb is unsupported on this platform"), + "the refusal names the command and the reason: {stdout}" + ); +} diff --git a/msg2.txt b/msg2.txt new file mode 100644 index 00000000..ca9e0d76 --- /dev/null +++ b/msg2.txt @@ -0,0 +1,13 @@ +fix(cli): gate the ancestry check and pin the non-Unix refusal + +Greptile's first finding: `refuse_swappable_ancestry` reads +`metadata.permissions().mode()` and was not gated, so Rust still +type-checked it on Windows and the build still failed. + +Greptile's second: the Unix contract lived in tests that would compile +on every platform and assert Unix behaviour. `migrate_cli.rs` now +carries `#![cfg(unix)]` for the whole file (which is what the four +per-test gates were reaching for), and `migrate_cli_unsupported.rs` +pins the refusal where the copy cannot run. The Windows job runs that +test too, so the refusal is executed on the platform it describes +rather than merely built. From c9d29c587a9dbefd986f63ddbdeb3ef0635f304c Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Sun, 15 Nov 2026 22:46:03 -0500 Subject: [PATCH 4/6] chore(cli): drop the scratch message file from the previous commit --- msg2.txt | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 msg2.txt diff --git a/msg2.txt b/msg2.txt deleted file mode 100644 index ca9e0d76..00000000 --- a/msg2.txt +++ /dev/null @@ -1,13 +0,0 @@ -fix(cli): gate the ancestry check and pin the non-Unix refusal - -Greptile's first finding: `refuse_swappable_ancestry` reads -`metadata.permissions().mode()` and was not gated, so Rust still -type-checked it on Windows and the build still failed. - -Greptile's second: the Unix contract lived in tests that would compile -on every platform and assert Unix behaviour. `migrate_cli.rs` now -carries `#![cfg(unix)]` for the whole file (which is what the four -per-test gates were reaching for), and `migrate_cli_unsupported.rs` -pins the refusal where the copy cannot run. The Windows job runs that -test too, so the refusal is executed on the platform it describes -rather than merely built. From 0c70d93fffe6c4deefb874d51f70e9c43ab50773 Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Sun, 15 Nov 2026 23:00:22 -0500 Subject: [PATCH 5/6] fix(config): import OsStr only where the unix trim uses it `std::ffi::OsStr` is read in `trim_trailing_separators`'s `#[cfg(unix)]` body alone, so on Windows the top-level import is unused and the build fails under `-D warnings`. The Windows CLI job reaches this crate through `nostream-cli`, which is how it surfaced. --- crates/nostream-config/src/load.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nostream-config/src/load.rs b/crates/nostream-config/src/load.rs index 985cdb55..28f8c87f 100644 --- a/crates/nostream-config/src/load.rs +++ b/crates/nostream-config/src/load.rs @@ -5,7 +5,6 @@ //! validate never aborts the process. It is reported as [`Issue`] values and the caller gets //! the shipped defaults, so a broken `settings.yaml` cannot stop a relay from starting. -use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; @@ -70,6 +69,7 @@ pub fn load_document(path: &Path) -> Result { /// and non-UTF-8 paths are handled because the work is on `OsStr` bytes, not a `to_str`. #[cfg(unix)] fn trim_trailing_separators(path: &Path) -> &Path { + use std::ffi::OsStr; use std::os::unix::ffi::OsStrExt; let bytes = path.as_os_str().as_bytes(); From d188623b27edcee533ef1640793fe2e9b6e52740 Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Sun, 15 Nov 2026 23:00:22 -0500 Subject: [PATCH 6/6] fix(ci): pin the Windows job's actions by tag, like every other job MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A SHA pin makes this repository's `pull_request` run fail at startup with no jobs created — measured on two probe branches: the same job with tag pins starts, with SHA pins it does not. Every other `uses:` in this workflow is a tag, so the job follows the existing convention. --- .github/workflows/checks.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 28dcf1df..d59da805 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -291,11 +291,11 @@ jobs: if: needs.changes.outputs.rust == 'true' steps: - name: Checkout - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + uses: actions/checkout@v4.4.0 with: persist-credentials: false - name: Install Rust toolchain - uses: actions-rust-lang/setup-rust-toolchain@166cdcfd11aee3cb47222f9ddb555ce30ddb9659 # v1.17.0 + uses: actions-rust-lang/setup-rust-toolchain@v1.17.0 - name: CLI builds on Windows run: cargo build -p nostream-cli --no-default-features - name: CLI refuses the migration on Windows