Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions bin/cli/utils/storageKeyProvision.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Decide whether a CLI invocation should provision (generate + persist) the
* STORAGE_ENCRYPTION_KEY into DATA_DIR/.env.
*
* Purely informational invocations must NOT create `~/.omniroute/.env` or write
* a key — they never touch encrypted storage. Generating a 32-byte key and a
* `.env` file just to print `omniroute --version` (or `--help`) is a surprising
* side effect: running a read-only command should not mutate the data dir.
*
* Returns false for: no command at all (bare `omniroute` → Commander prints
* help), `--version`/`-V` or `--help`/`-h` anywhere in the args, and the
* `help`/`completion` subcommands. Returns true for every real command
* (`serve`, `keys`, `providers`, …) so the encryption key is still provisioned
* before encrypted storage is accessed (preserves the #1622 persistence fix).
*
* @param {string[]} argv - process.argv (node + script + args).
* @returns {boolean}
*/
const INFO_FLAGS = new Set(["-h", "--help", "-V", "--version"]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add the actual -v version flag

When users run omniroute -v, Commander treats it as the version-only command because bin/cli/program.mjs registers .version(..., "-v, --version", ...), but this helper only recognizes -V. In that scenario shouldProvisionStorageKey() returns true, so the read-only version command still creates DATA_DIR/.env and a storage key, which is the side effect this change is meant to prevent.

Useful? React with 👍 / 👎.

const INFO_COMMANDS = new Set(["help", "completion"]);

export function shouldProvisionStorageKey(argv) {
const args = Array.isArray(argv) ? argv.slice(2) : [];
if (args.length === 0) return false;
if (args.some((a) => INFO_FLAGS.has(a))) return false;
if (INFO_COMMANDS.has(args[0])) return false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Detect help subcommands after global options

This only skips help/completion when they are the first CLI token, but Commander allows global options before the subcommand, e.g. omniroute --lang en help or omniroute --output json completion. Those informational invocations currently return true here and provision DATA_DIR/.env, so users can still get the unwanted mutation whenever they combine global options with help/completion.

Useful? React with 👍 / 👎.

return true;
}
Comment on lines +22 to +28

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation only checks args[0] to identify informational commands. However, if global options (such as --lang <lang> or --output <format>) are specified before the command (e.g., omniroute --lang en help), args[0] will be "--lang" instead of "help". This causes the function to return true and incorrectly provision a storage key for an informational command.

We should parse the arguments more robustly by skipping known options and their arguments to find the actual command.

export function shouldProvisionStorageKey(argv) {
  const args = Array.isArray(argv) ? argv.slice(2) : [];
  if (args.length === 0) return false;
  if (args.some((a) => INFO_FLAGS.has(a))) return false;

  // Find the first positional argument (the command) by skipping options
  let command = null;
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (arg.startsWith("-")) {
      // Skip the option's argument if it takes one
      if (arg === "--lang" || arg === "--output") {
        i++;
      }
      continue;
    }
    command = arg;
    break;
  }

  if (!command || INFO_COMMANDS.has(command)) return false;
  return true;
}

8 changes: 7 additions & 1 deletion bin/omniroute.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { homedir, platform } from "node:os";
import updateNotifier from "update-notifier";
import { isNativeBinaryCompatible } from "../scripts/build/native-binary-compat.mjs";
import { getNodeRuntimeSupport, getNodeRuntimeWarning } from "./nodeRuntimeSupport.mjs";
import { shouldProvisionStorageKey } from "./cli/utils/storageKeyProvision.mjs";

// Register tsx so dynamic imports of .ts source files (referenced as .js per
// TypeScript conventions) resolve correctly. The build never emits .js for
Expand Down Expand Up @@ -92,7 +93,12 @@ loadEnvFile();
// Generate STORAGE_ENCRYPTION_KEY if not set (persisted to ~/.omniroute/.env)
// This ensures the key survives across upgrades and is not regenerated on each install.
// See: https://github.com/diegosouzapw/OmniRoute/issues/1622
{
//
// Only provision for commands that actually touch encrypted storage. Purely
// informational invocations (`--version`, `--help`, `help`) must not create a
// key or write ~/.omniroute/.env — running a read-only command should never
// mutate the data dir.
if (shouldProvisionStorageKey(process.argv)) {
const { randomBytes } = await import("node:crypto");
const { existsSync, mkdirSync, readFileSync, writeFileSync } = await import("node:fs");
const { join } = await import("node:path");
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/cli-storage-key-provision.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import test from "node:test";
import assert from "node:assert/strict";
import { shouldProvisionStorageKey } from "../../bin/cli/utils/storageKeyProvision.mjs";

// argv shape is [node, script, ...args]
const argv = (...args: string[]) => ["node", "omniroute", ...args];

test("storage key: informational commands do NOT provision a key", () => {
assert.equal(shouldProvisionStorageKey(argv()), false); // bare omniroute → help
assert.equal(shouldProvisionStorageKey(argv("--version")), false);
assert.equal(shouldProvisionStorageKey(argv("-V")), false);
assert.equal(shouldProvisionStorageKey(argv("--help")), false);
assert.equal(shouldProvisionStorageKey(argv("-h")), false);
assert.equal(shouldProvisionStorageKey(argv("help")), false);
assert.equal(shouldProvisionStorageKey(argv("completion")), false);
});

test("storage key: --help/--version anywhere in the args still skips", () => {
assert.equal(shouldProvisionStorageKey(argv("serve", "--help")), false);
assert.equal(shouldProvisionStorageKey(argv("keys", "list", "-h")), false);
assert.equal(shouldProvisionStorageKey(argv("--lang", "en", "--version")), false);
});
Comment on lines +18 to +22

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add a test case to verify that informational commands like help are correctly skipped even when global options (such as --lang) are passed before them.

Suggested change
test("storage key: --help/--version anywhere in the args still skips", () => {
assert.equal(shouldProvisionStorageKey(argv("serve", "--help")), false);
assert.equal(shouldProvisionStorageKey(argv("keys", "list", "-h")), false);
assert.equal(shouldProvisionStorageKey(argv("--lang", "en", "--version")), false);
});
test("storage key: --help/--version anywhere in the args still skips", () => {
assert.equal(shouldProvisionStorageKey(argv("serve", "--help")), false);
assert.equal(shouldProvisionStorageKey(argv("keys", "list", "-h")), false);
assert.equal(shouldProvisionStorageKey(argv("--lang", "en", "--version")), false);
assert.equal(shouldProvisionStorageKey(argv("--lang", "en", "help")), false);
});


test("storage key: real commands DO provision (preserves #1622 persistence)", () => {
assert.equal(shouldProvisionStorageKey(argv("serve")), true);
assert.equal(shouldProvisionStorageKey(argv("keys", "list")), true);
assert.equal(shouldProvisionStorageKey(argv("providers")), true);
assert.equal(shouldProvisionStorageKey(argv("serve", "--port", "20128")), true);
// global --lang option before a real command must not suppress provisioning
assert.equal(shouldProvisionStorageKey(argv("--lang", "en", "serve")), true);
});

test("storage key: defensive on non-array input", () => {
// @ts-expect-error intentional bad input
assert.equal(shouldProvisionStorageKey(undefined), false);
});