Skip to content

Commit ce790f3

Browse files
juliusmarmingecodex
andcommitted
fix(shared): align loopback port validation
Co-authored-by: codex <[email protected]>
1 parent bf8ca6a commit ce790f3

2 files changed

Lines changed: 35 additions & 26 deletions

File tree

‎packages/shared/src/Net.test.ts‎

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,38 @@ it.layer(NetService.layer)("NetService", (it) => {
102102
});
103103
});
104104

105-
it.effect("preserves address-read context when closing an unusable reservation", () => {
106-
const probe = NodeNet.createServer();
107-
const cause = new Error("close failed");
108-
probe.unref = (() => probe) as typeof probe.unref;
109-
probe.address = (() => null) as typeof probe.address;
110-
probe.listen = ((_port: number, _host: string, listeningListener: () => void) => {
111-
listeningListener();
112-
return probe;
113-
}) as typeof probe.listen;
114-
probe.close = (() => {
115-
probe.emit("error", cause);
116-
return probe;
117-
}) as typeof probe.close;
118-
const net = NetService.make({ createServer: () => probe });
105+
it.effect("preserves address context when an unusable reservation errors during close", () =>
106+
Effect.gen(function* () {
107+
for (const invalidPort of [null, 43.5, 65_536]) {
108+
const probe = NodeNet.createServer();
109+
const cause = new Error("close failed");
110+
probe.unref = (() => probe) as typeof probe.unref;
111+
probe.address = (() => ({
112+
address: "127.0.0.1",
113+
family: "IPv4",
114+
port: invalidPort,
115+
})) as unknown as typeof probe.address;
116+
probe.listen = ((_port: number, _host: string, listeningListener: () => void) => {
117+
listeningListener();
118+
return probe;
119+
}) as typeof probe.listen;
120+
probe.close = (() => {
121+
probe.emit("error", cause);
122+
return probe;
123+
}) as typeof probe.close;
124+
const net = NetService.make({ createServer: () => probe });
119125

120-
return Effect.gen(function* () {
121-
const error = yield* net.reserveLoopbackPort().pipe(Effect.flip);
126+
const error = yield* net.reserveLoopbackPort().pipe(Effect.flip);
122127

123-
assert(isLoopbackPortAddressUnavailableError(error));
124-
assert.equal(error.host, "127.0.0.1");
125-
assert.equal(error.address, null);
126-
assert.equal(error.family, null);
127-
assert.equal(error.port, null);
128-
assert.strictEqual(error.cause, cause);
129-
});
130-
});
128+
assert(isLoopbackPortAddressUnavailableError(error));
129+
assert.equal(error.host, "127.0.0.1");
130+
assert.equal(error.address, "127.0.0.1");
131+
assert.equal(error.family, "IPv4");
132+
assert.equal(error.port, invalidPort);
133+
assert.strictEqual(error.cause, cause);
134+
}
135+
}),
136+
);
131137

132138
it.effect("rejects missing and non-finite ports returned by the server", () =>
133139
Effect.gen(function* () {

‎packages/shared/src/Net.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ const isErrnoExceptionWithCode = (
6262
Predicate.hasProperty(cause, "code") &&
6363
Predicate.isString(cause.code);
6464

65+
const isUsablePort = (port: number | null): port is number =>
66+
port !== null && Number.isInteger(port) && port > 0 && port <= 65_535;
67+
6568
const closeServer = (server: NodeNet.Server) => {
6669
try {
6770
server.close();
@@ -218,7 +221,7 @@ export const make = (
218221
Effect.fail(
219222
addressDetails === undefined
220223
? new LoopbackPortListenError({ host, cause })
221-
: addressDetails.port !== null && addressDetails.port > 0
224+
: isUsablePort(addressDetails.port)
222225
? new LoopbackPortReleaseError({ host, port: addressDetails.port, cause })
223226
: new LoopbackPortAddressUnavailableError({
224227
host,
@@ -251,7 +254,7 @@ export const make = (
251254

252255
probe.close((cause) => {
253256
const port = resolvedAddressDetails.port;
254-
if (port === null || !Number.isInteger(port) || port <= 0 || port > 65_535) {
257+
if (!isUsablePort(port)) {
255258
settle(
256259
Effect.fail(
257260
new LoopbackPortAddressUnavailableError({

0 commit comments

Comments
 (0)