Skip to content

Commit 1914778

Browse files
committed
fix: address review feedback on thread services
- Name services with the text-generation model only for subscribers holding orchestration:operate, and only for threads such a subscriber is watching. - Stop a service missing from the cached list through a fresh lookup instead of reporting it stopped, and validate the service id before any lookup. - Identify a process by start time plus command, or by an identical start time and the same parent when it rewrote its own args. - Detect HTTPS dev servers with self-signed certificates through a loopback-only TLS probe that skips verification for that probe alone. - Mask the whole value after a secret-named assignment, like flags. - Copy and open a port the client can reach when a service mixes them. - Reuse ThreadDetailsListRowActions in the Lineage workflow row and tag the subscription span with its thread id.
1 parent dbeb4a3 commit 1914778

11 files changed

Lines changed: 686 additions & 120 deletions

‎apps/server/src/preview/PortScanner.test.ts‎

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as NodeNet from "node:net";
2+
import * as NodeTls from "node:tls";
23

34
import { it as effectIt } from "@effect/vitest";
45
import {
@@ -1316,36 +1317,88 @@ effectIt.effect("probes both loopback addresses over HTTP before trying HTTPS",
13161317
}).pipe(Effect.provide(layer));
13171318
});
13181319

1319-
effectIt.effect(
1320-
"finds an HTTPS-only server and reports a non-web port after both protocols",
1321-
() => {
1322-
const requests: string[] = [];
1323-
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
1324-
const url = String(input);
1325-
requests.push(url);
1326-
if (url.startsWith("http:")) return Promise.reject(new TypeError("TLS only"));
1327-
return Promise.resolve(
1328-
url.includes(":8443")
1329-
? new Response("app", { headers: { "content-type": "text/html" } })
1330-
: new Response("{}", { headers: { "content-type": "application/json" } }),
1320+
// Test-only self-signed P-256 certificate for localhost, valid until 2126; not a secret. Node
1321+
// cannot mint X.509 certificates without a library, and the point is a certificate fetch rejects.
1322+
const SELF_SIGNED_KEY = `-----BEGIN PRIVATE KEY-----
1323+
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg/j1BIGpQgxT01P5y
1324+
RSkrRe0dvapxAvVjOu5XKIAMc6ihRANCAATtUyra0sW8ghP54f0kVE7VP/CdVgjf
1325+
INH+ccMeF45Zpz1izEehFdORN5OsIWRw8OXjgaV4RQKYfn1PfUEdc/Hk
1326+
-----END PRIVATE KEY-----
1327+
`;
1328+
const SELF_SIGNED_CERT = `-----BEGIN CERTIFICATE-----
1329+
MIIBtDCCAVmgAwIBAgIUMdidpa3C3oXjtoG/0LvoujOIW6owCgYIKoZIzj0EAwIw
1330+
IzEhMB8GA1UEAwwYdDNjb2RlIHBvcnQgc2Nhbm5lciB0ZXN0MCAXDTI2MTAxOTEy
1331+
NTM0NloYDzIxMjYwOTI1MTI1MzQ2WjAjMSEwHwYDVQQDDBh0M2NvZGUgcG9ydCBz
1332+
Y2FubmVyIHRlc3QwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATtUyra0sW8ghP5
1333+
4f0kVE7VP/CdVgjfINH+ccMeF45Zpz1izEehFdORN5OsIWRw8OXjgaV4RQKYfn1P
1334+
fUEdc/Hko2kwZzAdBgNVHQ4EFgQUVVbZxPv2b0UfV04RxLEjGXLSn30wHwYDVR0j
1335+
BBgwFoAUVVbZxPv2b0UfV04RxLEjGXLSn30wDwYDVR0TAQH/BAUwAwEB/zAUBgNV
1336+
HREEDTALgglsb2NhbGhvc3QwCgYIKoZIzj0EAwIDSQAwRgIhAMZNLhGx5C9yws2n
1337+
/ZV6qHzzndo7xCX6qNtflHH68L/yAiEAi5MMPk7kmBSvrBe6L/rmrVhK8yv1V32H
1338+
ZWi1vTOOKL8=
1339+
-----END CERTIFICATE-----
1340+
`;
1341+
1342+
/** An HTTPS-only listener on 127.0.0.1 that answers every request with `response`. */
1343+
const selfSignedTlsServer = (response: string) =>
1344+
Effect.acquireRelease(
1345+
Effect.callback<{ readonly server: NodeTls.Server; readonly port: number }>((resume) => {
1346+
const server = NodeTls.createServer(
1347+
{ key: SELF_SIGNED_KEY, cert: SELF_SIGNED_CERT },
1348+
(socket) => {
1349+
socket.on("error", () => undefined);
1350+
socket.once("data", () => socket.end(response));
1351+
},
13311352
);
1332-
}) as typeof globalThis.fetch;
1333-
const layer = layerListenerScanner({
1334-
platform: "darwin",
1335-
run: processProbeFailure,
1336-
fetch: fetchFn,
1337-
});
1353+
server.listen(0, "127.0.0.1", () => {
1354+
const address = server.address() as NodeNet.AddressInfo;
1355+
resume(Effect.succeed({ server, port: address.port }));
1356+
});
1357+
return Effect.sync(() => {
1358+
server.close();
1359+
});
1360+
}),
1361+
({ server }) =>
1362+
Effect.callback<void>((resume) => {
1363+
server.close(() => resume(Effect.void));
1364+
}),
1365+
);
13381366

1339-
return Effect.gen(function* () {
1340-
const scanner = yield* PortScanner.PortDiscovery;
1341-
expect(yield* scanner.probeWeb(8443)).toBe("https");
1342-
expect(yield* scanner.probeWeb(8080)).toBeNull();
1343-
expect(requests.filter((url) => url.includes(":8080")).toSorted()).toEqual([
1344-
"http://127.0.0.1:8080/",
1345-
"http://[::1]:8080/",
1346-
"https://127.0.0.1:8080/",
1347-
"https://[::1]:8080/",
1348-
]);
1349-
}).pipe(Effect.provide(layer));
1350-
},
1351-
);
1367+
/** Reaches no HTTP listener, so every probe falls through to HTTPS. */
1368+
const refuseHttp = (() => Promise.reject(new TypeError("ECONNREFUSED"))) as typeof globalThis.fetch;
1369+
1370+
effectIt.effect("finds an HTTPS-only server behind an untrusted self-signed certificate", () => {
1371+
const layer = layerListenerScanner({
1372+
platform: "darwin",
1373+
run: processProbeFailure,
1374+
fetch: refuseHttp,
1375+
});
1376+
return Effect.gen(function* () {
1377+
const page = yield* selfSignedTlsServer(
1378+
"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 5\r\n\r\nhello",
1379+
);
1380+
const redirect = yield* selfSignedTlsServer(
1381+
"HTTP/1.1 302 Found\r\nLocation: /app/\r\nContent-Length: 0\r\n\r\n",
1382+
);
1383+
const scanner = yield* PortScanner.PortDiscovery;
1384+
expect(yield* scanner.probeWeb(page.port)).toBe("https");
1385+
expect(yield* scanner.probeWeb(redirect.port)).toBe("https");
1386+
}).pipe(Effect.scoped, Effect.provide(layer));
1387+
});
1388+
1389+
effectIt.effect("reports a TLS port that serves no page as non-web", () => {
1390+
const layer = layerListenerScanner({
1391+
platform: "darwin",
1392+
run: processProbeFailure,
1393+
fetch: refuseHttp,
1394+
});
1395+
return Effect.gen(function* () {
1396+
const api = yield* selfSignedTlsServer(
1397+
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\n\r\n{}",
1398+
);
1399+
const binary = yield* selfSignedTlsServer("MYSQL\r\n\r\n");
1400+
const scanner = yield* PortScanner.PortDiscovery;
1401+
expect(yield* scanner.probeWeb(api.port)).toBeNull();
1402+
expect(yield* scanner.probeWeb(binary.port)).toBeNull();
1403+
}).pipe(Effect.scoped, Effect.provide(layer));
1404+
});

‎apps/server/src/preview/PortScanner.ts‎

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as NodeSocket from "@effect/platform-node/NodeSocket";
12
import {
23
CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS,
34
PREVIEW_URL_MAX_LENGTH,
@@ -20,6 +21,7 @@ import * as Schedule from "effect/Schedule";
2021
import * as Scope from "effect/Scope";
2122
import * as Semaphore from "effect/Semaphore";
2223
import { FetchHttpClient, HttpClient } from "effect/http";
24+
import * as Socket from "effect/socket/Socket";
2325

2426
import * as ProcessRunner from "../processRunner.ts";
2527

@@ -73,8 +75,8 @@ export class PortDiscovery extends Context.Service<
7375
readonly listListeners: Effect.Effect<ReadonlyArray<ListeningSocket> | null>;
7476
/**
7577
* The protocol the port serves a web page over: HTTP on either loopback address, else HTTPS,
76-
* else null. Uncached. Only probe ports attributed to a known owner: HTTP or TLS bytes can
77-
* crash a binary protocol.
78+
* else null. Uncached. HTTPS accepts any certificate, since dev servers often self-sign. Only
79+
* probe ports attributed to a known owner: HTTP or TLS bytes can crash a binary protocol.
7880
*/
7981
readonly probeWeb: (port: number) => Effect.Effect<WebProtocol | null>;
8082
readonly terminalProcessOwners: Effect.Effect<
@@ -97,6 +99,8 @@ const WINDOWS_PROCESS_NAME_TTL_MS = Duration.toMillis(Duration.seconds(60));
9799
const WINDOWS_PROCESS_NAME_CONCURRENCY = 4;
98100
const WEB_PROBE_CONCURRENCY = 16;
99101
const NAVIGATION_REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
102+
/** Node's default cap on HTTP response headers; a head that grows past it is not a dev server's. */
103+
const WEB_PROBE_HEAD_MAX_BYTES = 16 * 1024;
100104

101105
type Listener = (servers: ReadonlyArray<DiscoveredLocalServer>) => Effect.Effect<void>;
102106

@@ -480,6 +484,76 @@ const serversEqual = (
480484
const isCommandNotFound = (error: ProcessRunner.ProcessSpawnError): boolean =>
481485
PlatformError.isPlatformError(error.cause) && error.cause.reason._tag === "NotFound";
482486

487+
/** A page a browser would render, or a redirect to one. */
488+
const isPageResponse = (response: {
489+
readonly status: number;
490+
readonly location: string | undefined;
491+
readonly contentType: string | undefined;
492+
}): boolean => {
493+
if (NAVIGATION_REDIRECT_STATUSES.has(response.status) && response.location?.trim()) return true;
494+
if (response.status < 200 || response.status >= 300) return false;
495+
if (response.status === 204 || response.status === 205) return false;
496+
const mediaType = response.contentType?.split(";", 1)[0]?.trim().toLowerCase();
497+
return mediaType === "text/html" || mediaType === "application/xhtml+xml";
498+
};
499+
500+
/** Reads a raw HTTP/1.x response head: the status line, then `name: value` header lines. */
501+
const parseResponseHead = (head: string) => {
502+
const [statusLine = "", ...lines] = head.split("\r\n");
503+
const status = /^HTTP\/1\.[01] (\d{3})(?: |$)/.exec(statusLine)?.[1];
504+
if (status === undefined) return null;
505+
const headers = new Map<string, string>();
506+
for (const line of lines) {
507+
const colon = line.indexOf(":");
508+
const name = line.slice(0, colon).trim().toLowerCase();
509+
if (colon > 0 && !headers.has(name)) headers.set(name, line.slice(colon + 1).trim());
510+
}
511+
return {
512+
status: Number(status),
513+
location: headers.get("location"),
514+
contentType: headers.get("content-type"),
515+
};
516+
};
517+
518+
/**
519+
* Whether a loopback TLS listener serves a page. Dev servers serve HTTPS with self-signed
520+
* certificates (Vite's basic-ssl, ad-hoc certs) that fetch rejects, so this one connection skips
521+
* verification instead of trusting them process-wide. Nothing is sent but a bodiless `GET /` to a
522+
* loopback address, and the answer only classifies the port.
523+
*/
524+
const probeTlsPage = Effect.fn("PortDiscovery.probeTlsPage")(
525+
function* (host: "127.0.0.1" | "::1", port: number) {
526+
const socket = yield* NodeSocket.makeTls({
527+
host,
528+
port,
529+
// The name the preview opens, so SNI-picked certificates and host allowlists answer as
530+
// they will for the browser.
531+
servername: "localhost",
532+
rejectUnauthorized: false,
533+
ALPNProtocols: ["http/1.1"],
534+
});
535+
const pull = yield* Socket.readerBytes(socket);
536+
const writer = yield* socket.writer;
537+
yield* writer.write(`GET / HTTP/1.1\r\nHost: localhost:${port}\r\nConnection: close\r\n\r\n`);
538+
const decoder = new TextDecoder();
539+
let received = "";
540+
let byteCount = 0;
541+
while (!received.includes("\r\n\r\n")) {
542+
if (byteCount > WEB_PROBE_HEAD_MAX_BYTES) return false;
543+
for (const chunk of yield* pull) {
544+
byteCount += chunk.byteLength;
545+
received += decoder.decode(chunk, { stream: true });
546+
}
547+
}
548+
const response = parseResponseHead(received.slice(0, received.indexOf("\r\n\r\n")));
549+
return response !== null && isPageResponse(response);
550+
},
551+
Effect.scoped,
552+
Effect.timeoutOption(WEB_PROBE_TIMEOUT),
553+
Effect.map(Option.getOrElse(() => false)),
554+
Effect.orElseSucceed(() => false),
555+
);
556+
483557
/** @public Service construction is part of the canonical Effect module API. */
484558
export const make = Effect.gen(function* PortDiscoveryMake() {
485559
const processRunner = yield* ProcessRunner.ProcessRunner;
@@ -603,17 +677,15 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
603677

604678
const probeWebUrl = Effect.fn("PortDiscovery.probeWebUrl")((url: string) =>
605679
httpClient.get(url).pipe(
606-
Effect.map((response) => {
607-
const location = response.headers.location?.trim();
608-
if (NAVIGATION_REDIRECT_STATUSES.has(response.status) && location) return url;
609-
if (response.status < 200 || response.status >= 300) return null;
610-
if (response.status === 204 || response.status === 205) return null;
611-
const contentType = response.headers["content-type"]
612-
?.split(";", 1)[0]
613-
?.trim()
614-
.toLowerCase();
615-
return contentType === "text/html" || contentType === "application/xhtml+xml" ? url : null;
616-
}),
680+
Effect.map((response) =>
681+
isPageResponse({
682+
status: response.status,
683+
location: response.headers.location,
684+
contentType: response.headers["content-type"],
685+
})
686+
? url
687+
: null,
688+
),
617689
Effect.scoped,
618690
Effect.timeoutOption(WEB_PROBE_TIMEOUT),
619691
Effect.map(Option.getOrNull),
@@ -910,8 +982,13 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
910982
// loopback address is asked directly.
911983
const probeLoopback = (protocol: WebProtocol, port: number) =>
912984
Effect.forEach(
913-
[`${protocol}://127.0.0.1:${port}`, `${protocol}://[::1]:${port}`],
914-
(url) => probeWebUrl(url).pipe(Effect.map((visible) => visible !== null)),
985+
["127.0.0.1", "::1"] as const,
986+
(host) =>
987+
protocol === "https"
988+
? probeTlsPage(host, port)
989+
: probeWebUrl(`http://${host === "::1" ? "[::1]" : host}:${port}`).pipe(
990+
Effect.map((visible) => visible !== null),
991+
),
915992
{ concurrency: 2 },
916993
).pipe(Effect.map((answers) => (answers.some(Boolean) ? protocol : null)));
917994

0 commit comments

Comments
 (0)