Skip to content

Commit 90dba1a

Browse files
fix(cli): reject malformed approval grant lifetimes (#133806)
* fix(cli): reject malformed approval grant lifetimes * 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]> --------- Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: qingminlong <[email protected]>
1 parent e4cf6f5 commit 90dba1a

2 files changed

Lines changed: 72 additions & 8 deletions

File tree

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

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

167+
it.each(["10junk", "1.5", "1e3", "", "0", "-1", "3651"])(
168+
"rejects an invalid grant lifetime without resolving the approval (%s)",
169+
async (expiresInDays) => {
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+
);
181+
182+
await expect(
183+
runApprovalsCommand([
184+
"approvals",
185+
"resolve",
186+
"lifetime-invalid",
187+
"allow-always",
188+
"--expires-in-days",
189+
expiresInDays,
190+
]),
191+
).rejects.toThrow("__exit__:1");
192+
193+
expect(runtimeErrors).toEqual([
194+
"--expires-in-days must be a whole number of days between 1 and 3650.",
195+
]);
196+
expect(callGatewayFromCli.mock.calls.map(([method]) => method)).toEqual(["approval.get"]);
197+
},
198+
);
199+
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+
);
214+
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+
);
234+
167235
it("renders pending approvals from all three approval kinds", async () => {
168236
const now = Date.now();
169237
callGatewayFromCli.mockImplementation(async (method: string) => {

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

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

752-
const expiresInDaysRaw =
753-
opts.expiresInDays === undefined ? undefined : Number.parseInt(opts.expiresInDays, 10);
754-
if (
755-
expiresInDaysRaw !== undefined &&
756-
(!Number.isInteger(expiresInDaysRaw) || expiresInDaysRaw < 1 || expiresInDaysRaw > 3650)
757-
) {
752+
const expiresInDays = parseStrictPositiveInteger(opts.expiresInDays);
753+
if (opts.expiresInDays !== undefined && (expiresInDays === undefined || expiresInDays > 3650)) {
758754
exitWithError("--expires-in-days must be a whole number of days between 1 and 3650.");
759755
}
760-
if (expiresInDaysRaw !== undefined && decision !== "allow-always") {
756+
if (expiresInDays !== undefined && decision !== "allow-always") {
761757
exitWithError("--expires-in-days only applies to allow-always.");
762758
}
763759
const result = (await callGatewayFromCli(
@@ -767,7 +763,7 @@ async function resolvePendingApproval(
767763
id,
768764
kind: current.presentation.kind,
769765
decision,
770-
...(expiresInDaysRaw !== undefined ? { grantExpiresInDays: expiresInDaysRaw } : {}),
766+
...(expiresInDays !== undefined ? { grantExpiresInDays: expiresInDays } : {}),
771767
},
772768
approvalCallOptions,
773769
)) as ApprovalResolveResult;

0 commit comments

Comments
 (0)