Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(connectors): reject invalid --limit and --window-days values on connectors sync (#5930)

garrytana4b44b4225b9
diff --git a/scripts/test-weights.json b/scripts/test-weights.json
index 3697c21a45..f2d0e9d524 100644
--- a/scripts/test-weights.json
+++ b/scripts/test-weights.json
@@ -352,6 +352,7 @@
"test/connectors-pkce.test.ts": 520,
"test/connectors-provider-pagination.test.ts": 18,
"test/connectors-providers.test.ts": 3,
+ "test/connectors-sync-flags.test.ts": 1000,
"test/consent.test.ts": 1,
"test/console-prefix.test.ts": 12,
"test/consolidate-valid-until.test.ts": 816,
diff --git a/scripts/ubicloud/weights.json b/scripts/ubicloud/weights.json
index a842f557cd..51730a2ffe 100644
--- a/scripts/ubicloud/weights.json
+++ b/scripts/ubicloud/weights.json
@@ -968,6 +968,7 @@
"unit:test/connectors-embed-kickoff.test.ts": 336,
"unit:test/connectors-pkce.test.ts": 701,
"unit:test/connectors-providers.test.ts": 258,
+ "unit:test/connectors-sync-flags.test.ts": 1000,
"unit:test/consent.test.ts": 600,
"unit:test/console-prefix.test.ts": 188,
"unit:test/consolidate-valid-until.test.ts": 2544,
diff --git a/src/commands/connectors/sync.ts b/src/commands/connectors/sync.ts
index db945ec2b7..915c76caf1 100644
--- a/src/commands/connectors/sync.ts
+++ b/src/commands/connectors/sync.ts
@@ -16,6 +16,7 @@ import { connectorProviderNames, isConnectorProviderName } from '../../core/conn
import { loadCredential } from '../../core/connectors/credentials.ts';
import { sourceIdKey } from '../../core/connectors/config-keys.ts';
import type { ConnectorProviderName } from '../../core/connectors/types.ts';
+import { intFlagValue } from '../../cli/flag-values.ts';
interface SyncFlags {
full: boolean;
@@ -40,8 +41,9 @@ function parseFlags(args: string[]): { provider: string; flags: SyncFlags } {
else if (a === '--embed') flags.embed = true;
else if (a === '--background') flags.background = true;
else if (a === '--json') flags.json = true;
- else if (a === '--limit') flags.limit = Number(args[++i]);
- else if (a === '--window-days') flags.windowDays = Number(args[++i]);
+ // #5930 (D4): decimals, zero limits and unsafe integers are usage errors (exit 2), not Number() coercions.
+ else if (a === '--limit') flags.limit = intFlagValue(args[++i], '--limit', { min: 1, example: 50 });
+ else if (a === '--window-days') flags.windowDays = intFlagValue(args[++i], '--window-days', { min: 0, example: 30 });
else if (a === '--source') flags.source = args[++i];
else if (!a.startsWith('-')) provider = a;
}
diff --git a/test/connectors-sync-flags.test.ts b/test/connectors-sync-flags.test.ts
new file mode 100644
index 0000000000..cd06a94e23
--- /dev/null
+++ b/test/connectors-sync-flags.test.ts
@@ -0,0 +1,76 @@
+import { describe, expect, test } from 'bun:test';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { OperationError } from '../src/core/ops/contract.ts';
+import { runConnectorSyncCmd } from '../src/commands/connectors/sync.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+// Absorbed in agent contract v1 D4 (#5930): the rejection is an invalid_params usage error (exit 2 via renderCliError).
+// Contract: connector sync numeric flags reject malformed or unsafe values and retain valid values.
+// Regression: Number() previously accepted decimals, zero for --limit, and unsafe integers.
+// Existing coverage exercises core sync behavior but not CLI flag parsing.
+// This uses the public command entry point and needs no production-only test seam.
+const stubEngine = { getConfig: async () => null } as unknown as BrainEngine;
+
+async function runWithoutCredentials(args: string[]): Promise<void> {
+ const testHome = mkdtempSync(join(tmpdir(), 'gbrain-connectors-sync-flags-'));
+ return withEnv({
+ GBRAIN_HOME: testHome,
+ GBRAIN_CONNECTOR_CHATGPT_COOKIE: undefined,
+ GBRAIN_CONNECTOR_CHATGPT_TOKEN: undefined,
+ }, async () => {
+ try {
+ await runConnectorSyncCmd(stubEngine, args);
+ } finally {
+ rmSync(testHome, { recursive: true, force: true });
+ }
+ });
+}
+
+async function expectInvalidParams(args: string[], flag: string): Promise<void> {
+ const result = runConnectorSyncCmd(stubEngine, args);
+ await expect(result).rejects.toBeInstanceOf(OperationError);
+ await expect(result).rejects.toMatchObject({
+ code: 'invalid_params',
+ message: expect.stringContaining(flag),
+ });
+}
+
+describe('connector sync flags', () => {
+ test('--limit requires a positive safe integer', async () => {
+ for (const args of [
+ ['chatgpt', '--limit', 'nope'],
+ ['chatgpt', '--limit', '1.5'],
+ ['chatgpt', '--limit', '0'],
+ ['chatgpt', '--limit', '9007199254740992'],
+ ['chatgpt', '--limit'],
+ ]) {
+ await expectInvalidParams(args, '--limit');
+ }
+ });
+
+ test('--limit accepts a positive safe integer', async () => {
+ await expect(runWithoutCredentials(['chatgpt', '--limit', '3'])).resolves.toBeUndefined();
+ });
+
+ test('--window-days requires a non-negative safe integer', async () => {
+ for (const args of [
+ ['chatgpt', '--window-days', 'nope'],
+ ['chatgpt', '--window-days', '1.5'],
+ ['chatgpt', '--window-days', '-1'],
+ ['chatgpt', '--window-days', '9007199254740992'],
+ ['chatgpt', '--window-days'],
+ ]) {
+ await expectInvalidParams(args, '--window-days');
+ }
+ });
+
+ test('--window-days accepts zero and positive safe integers', async () => {
+ // No credential in the isolated home makes the command stop normally after parsing.
+ for (const days of ['0', '3']) {
+ await expect(runWithoutCredentials(['chatgpt', '--window-days', days])).resolves.toBeUndefined();
+ }
+ });
+});