Repository navigation
fix(cli): don't provision STORAGE_ENCRYPTION_KEY on informational commands #3129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"]); | ||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This only skips Useful? React with 👍 / 👎. |
||
| return true; | ||
| } | ||
|
Comment on lines
+22
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation only checks 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;
} |
||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test case to verify that informational commands like
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| 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); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When users run
omniroute -v, Commander treats it as the version-only command becausebin/cli/program.mjsregisters.version(..., "-v, --version", ...), but this helper only recognizes-V. In that scenarioshouldProvisionStorageKey()returns true, so the read-only version command still createsDATA_DIR/.envand a storage key, which is the side effect this change is meant to prevent.Useful? React with 👍 / 👎.