Skip to content

Commit f52c59d

Browse files
Nik SamokhvalovNik Samokhvalov
authored andcommitted
fix(cli): validate init port option
- Reject non-numeric or out-of-range ports for psql-like -p/--port - Add unit test for invalid port
1 parent 24b81c5 commit f52c59d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

cli/lib/init.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,13 @@ export function resolveAdminConnection(opts: {
195195

196196
const cfg: PgClientConfig = {};
197197
if (opts.host) cfg.host = opts.host;
198-
if (opts.port !== undefined && opts.port !== "") cfg.port = Number(opts.port);
198+
if (opts.port !== undefined && opts.port !== "") {
199+
const p = Number(opts.port);
200+
if (!Number.isFinite(p) || !Number.isInteger(p) || p <= 0 || p > 65535) {
201+
throw new Error(`Invalid port value: ${String(opts.port)}`);
202+
}
203+
cfg.port = p;
204+
}
199205
if (opts.username) cfg.user = opts.username;
200206
if (opts.dbname) cfg.database = opts.dbname;
201207
if (opts.adminPassword) cfg.password = opts.adminPassword;

cli/test/init.test.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,11 @@ test("resolveAdminConnection accepts positional conninfo", () => {
6666
assert.equal(r.clientConfig.user, "alice");
6767
});
6868

69+
test("resolveAdminConnection rejects invalid psql-like port", () => {
70+
assert.throws(
71+
() => init.resolveAdminConnection({ host: "localhost", port: "abc", username: "u", dbname: "d" }),
72+
/Invalid port value/
73+
);
74+
});
75+
6976

0 commit comments

Comments
 (0)