Skip to content

Commit 374502a

Browse files
fix(cli): validate whole approval grant lifetimes
Reuse the canonical strict parser and consolidate range validation without changing valid or omitted grant terms. Cover malformed and boundary input. Co-authored-by: qingminlong <[email protected]>
1 parent b0fea86 commit 374502a

2 files changed

Lines changed: 54 additions & 48 deletions

File tree

‎src/cli/exec-approvals-cli.pending-resolve.test.ts‎

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -164,60 +164,73 @@ describe("exec approvals pending and resolve CLI", () => {
164164
expect(call?.[2]).toEqual({ limit: 25 });
165165
});
166166

167-
it.each(["10junk", "1.5", "0", "-1"])(
168-
"rejects malformed approval grant lifetime before resolving (%s)",
167+
it.each(["10junk", "1.5", "1e3", "", "0", "-1", "3651"])(
168+
"rejects an invalid grant lifetime without resolving the approval (%s)",
169169
async (expiresInDays) => {
170-
callGatewayFromCli.mockResolvedValueOnce(pendingApprovalSnapshot({ id: "approval-expiry" }));
170+
callGatewayFromCli.mockImplementation(async (method: string) =>
171+
method === "approval.get"
172+
? pendingApprovalSnapshot({ id: "lifetime-invalid" })
173+
: {
174+
applied: true,
175+
approval: terminalApprovalSnapshot({
176+
id: "lifetime-invalid",
177+
decision: "allow-always",
178+
}),
179+
},
180+
);
171181

172182
await expect(
173183
runApprovalsCommand([
174184
"approvals",
175185
"resolve",
176-
"approval-expiry",
186+
"lifetime-invalid",
177187
"allow-always",
178188
"--expires-in-days",
179189
expiresInDays,
180190
]),
181191
).rejects.toThrow("__exit__:1");
182192

183-
expect(callGatewayFromCli).toHaveBeenCalledTimes(1);
184-
expect(runtimeErrors[0]).toContain(
193+
expect(runtimeErrors).toEqual([
185194
"--expires-in-days must be a whole number of days between 1 and 3650.",
186-
);
195+
]);
196+
expect(callGatewayFromCli.mock.calls.map(([method]) => method)).toEqual(["approval.get"]);
187197
},
188198
);
189199

190-
it("forwards a valid approval grant lifetime as a number", async () => {
191-
callGatewayFromCli.mockImplementation(async (method: string) => {
192-
if (method === "approval.get") {
193-
return pendingApprovalSnapshot({ id: "approval-expiry-valid" });
194-
}
195-
return {
196-
applied: true,
197-
approval: terminalApprovalSnapshot({
198-
id: "approval-expiry-valid",
199-
decision: "allow-always",
200-
}),
201-
};
202-
});
203-
204-
await runApprovalsCommand([
205-
"approvals",
206-
"resolve",
207-
"approval-expiry-valid",
208-
"allow-always",
209-
"--expires-in-days",
210-
"30",
211-
"--json",
212-
]);
200+
it.each([undefined, "1", "30", "3650"])(
201+
"preserves the numeric or absent grant lifetime (%s)",
202+
async (expiresInDays) => {
203+
callGatewayFromCli.mockImplementation(async (method: string) =>
204+
method === "approval.get"
205+
? pendingApprovalSnapshot({ id: "lifetime-valid" })
206+
: {
207+
applied: true,
208+
approval: terminalApprovalSnapshot({
209+
id: "lifetime-valid",
210+
decision: "allow-always",
211+
}),
212+
},
213+
);
213214

214-
expect(callGatewayFromCli.mock.calls[1]?.[2]).toEqual({
215-
id: "approval-expiry-valid",
216-
kind: "exec",
217-
decision: "allow-always",
218-
grantExpiresInDays: 30,
219-
});
220-
});
215+
await runApprovalsCommand([
216+
"approvals",
217+
"resolve",
218+
"lifetime-valid",
219+
"allow-always",
220+
...(expiresInDays === undefined ? [] : ["--expires-in-days", expiresInDays]),
221+
"--json",
222+
]);
223+
224+
expect(callGatewayFromCli.mock.calls[1]?.[0]).toBe("approval.resolve");
225+
expect(callGatewayFromCli.mock.calls[1]?.[2]).toEqual({
226+
id: "lifetime-valid",
227+
kind: "exec",
228+
decision: "allow-always",
229+
...(expiresInDays === undefined ? {} : { grantExpiresInDays: Number(expiresInDays) }),
230+
});
231+
expect(writtenJson()).toMatchObject({ applied: true, alreadyResolved: false });
232+
},
233+
);
221234

222235
it("renders pending approvals from all three approval kinds", async () => {
223236
const now = Date.now();

‎src/cli/exec-approvals-cli.ts‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -749,18 +749,11 @@ async function resolvePendingApproval(
749749
}
750750
}
751751

752-
const expiresInDaysRaw =
753-
opts.expiresInDays === undefined ? undefined : parseStrictPositiveInteger(opts.expiresInDays);
754-
if (opts.expiresInDays !== undefined && expiresInDaysRaw === undefined) {
752+
const expiresInDays = parseStrictPositiveInteger(opts.expiresInDays);
753+
if (opts.expiresInDays !== undefined && (expiresInDays === undefined || expiresInDays > 3650)) {
755754
exitWithError("--expires-in-days must be a whole number of days between 1 and 3650.");
756755
}
757-
if (
758-
expiresInDaysRaw !== undefined &&
759-
(!Number.isInteger(expiresInDaysRaw) || expiresInDaysRaw < 1 || expiresInDaysRaw > 3650)
760-
) {
761-
exitWithError("--expires-in-days must be a whole number of days between 1 and 3650.");
762-
}
763-
if (expiresInDaysRaw !== undefined && decision !== "allow-always") {
756+
if (expiresInDays !== undefined && decision !== "allow-always") {
764757
exitWithError("--expires-in-days only applies to allow-always.");
765758
}
766759
const result = (await callGatewayFromCli(
@@ -770,7 +763,7 @@ async function resolvePendingApproval(
770763
id,
771764
kind: current.presentation.kind,
772765
decision,
773-
...(expiresInDaysRaw !== undefined ? { grantExpiresInDays: expiresInDaysRaw } : {}),
766+
...(expiresInDays !== undefined ? { grantExpiresInDays: expiresInDays } : {}),
774767
},
775768
approvalCallOptions,
776769
)) as ApprovalResolveResult;

0 commit comments

Comments
 (0)