|
7 | 7 | import * as NodeCrypto from "node:crypto"; |
8 | 8 | import { |
9 | 9 | AuthStandardClientScopes, |
10 | | - DESKTOP_UPDATE_RESTART_MARKER_FILE, |
11 | 10 | EnvironmentCloudEndpointUnavailableError, |
12 | 11 | type EnvironmentCloudLinkStateResult, |
13 | 12 | type EnvironmentCloudPreferencesRequest, |
@@ -53,7 +52,6 @@ import { |
53 | 52 | verifyRelayJwt, |
54 | 53 | } from "@t3tools/shared/relayJwt"; |
55 | 54 | import { isSecureRelayUrl } from "@t3tools/shared/relayUrl"; |
56 | | -import * as Clock from "effect/Clock"; |
57 | 55 | import * as Context from "effect/Context"; |
58 | 56 | import * as Crypto from "effect/Crypto"; |
59 | 57 | import * as DateTime from "effect/DateTime"; |
@@ -110,29 +108,24 @@ import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts"; |
110 | 108 | import { relayUrlConfig } from "./publicConfig.ts"; |
111 | 109 | import { filterRelayResponse, relayRequestError, shouldRetryCloudLink } from "./relayResponse.ts"; |
112 | 110 | import { |
113 | | - SERVICE_STATE_FILE, |
114 | | - SERVICE_STOP_MARKER_FILE, |
115 | | - serviceStateHasPendingUpdate, |
116 | | -} from "./serviceProtocol.ts"; |
117 | | - |
118 | | -const CLOUD_MINT_NONCE_PREFIX = "cloud-mint-nonce-"; |
119 | | -const CLOUD_MINT_JTI_PREFIX = "cloud-mint-jti-"; |
120 | | -const CLOUD_HEALTH_NONCE_PREFIX = "cloud-health-nonce-"; |
121 | | -const CLOUD_HEALTH_JTI_PREFIX = "cloud-health-jti-"; |
122 | | -/** Secret store name prefixes of cloud replay markers. The server prunes expired ones. */ |
123 | | -export const CLOUD_REPLAY_MARKER_PREFIXES = [ |
124 | | - CLOUD_MINT_NONCE_PREFIX, |
125 | | - CLOUD_MINT_JTI_PREFIX, |
126 | | - CLOUD_HEALTH_NONCE_PREFIX, |
127 | 111 | CLOUD_HEALTH_JTI_PREFIX, |
128 | | -] as const; |
129 | | -const CLOUD_PROOF_MAX_LIFETIME_SECONDS = 5 * 60; |
130 | | -const CLOUD_PROOF_CLOCK_SKEW_SECONDS = 60; |
131 | | -// The desktop app stops its backends within seconds of writing the marker. |
132 | | -const DESKTOP_UPDATE_RESTART_MARKER_TTL = Duration.minutes(1); |
133 | | -const MANAGED_ENDPOINT_PROVISION_REQUEST_TIMEOUT = Duration.minutes(2); |
134 | | -const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]); |
| 112 | + CLOUD_HEALTH_NONCE_PREFIX, |
| 113 | + CLOUD_MINT_JTI_PREFIX, |
| 114 | + CLOUD_MINT_NONCE_PREFIX, |
| 115 | + consumeCloudReplayGuards, |
| 116 | + hasBoundedCloudProofLifetime, |
| 117 | + hasExactScope, |
| 118 | + hasForwardedAuthorityHeaders, |
| 119 | + isAllowedEndpointOrigin, |
| 120 | + isSupportedLinkProviderKind, |
| 121 | + linkProofScopes, |
| 122 | + managedEndpointRuntimeConfigsMatch, |
| 123 | + parseManagedEndpointLocalOrigin, |
| 124 | + requestAbsoluteUrl, |
| 125 | +} from "./linkChecks.ts"; |
| 126 | +import { desktopUpdateRestartPending, pendingUpdateHandoffExists } from "./updateHandoff.ts"; |
135 | 127 |
|
| 128 | +const MANAGED_ENDPOINT_PROVISION_REQUEST_TIMEOUT = Duration.minutes(2); |
136 | 129 | const failEnvironmentCloudInternalError = |
137 | 130 | (message: string) => |
138 | 131 | (cause: unknown): Effect.Effect<never, EnvironmentHttpInternalServerError> => |
@@ -164,37 +157,6 @@ function stringToBytes(value: string): Uint8Array { |
164 | 157 | return new TextEncoder().encode(value); |
165 | 158 | } |
166 | 159 |
|
167 | | -export function consumeCloudReplayGuards(input: { |
168 | | - readonly secrets: ServerSecretStore.ServerSecretStore["Service"]; |
169 | | - readonly names: ReadonlyArray<string>; |
170 | | - readonly value: Uint8Array; |
171 | | -}) { |
172 | | - return Effect.forEach( |
173 | | - input.names, |
174 | | - (name) => |
175 | | - input.secrets.create(name, input.value).pipe( |
176 | | - Effect.as(true), |
177 | | - Effect.catchIf(ServerSecretStore.isSecretStoreError, (error) => |
178 | | - ServerSecretStore.isSecretAlreadyExistsError(error) |
179 | | - ? Effect.succeed(false) |
180 | | - : Effect.fail(error), |
181 | | - ), |
182 | | - ), |
183 | | - { concurrency: input.names.length }, |
184 | | - ).pipe(Effect.map((created) => created.every(Boolean))); |
185 | | -} |
186 | | - |
187 | | -function normalizePemForSignedPayload(value: string): string { |
188 | | - return value.trim(); |
189 | | -} |
190 | | - |
191 | | -function normalizeHostname(hostname: string): string { |
192 | | - return hostname |
193 | | - .trim() |
194 | | - .toLowerCase() |
195 | | - .replace(/^\[(.*)\]$/, "$1"); |
196 | | -} |
197 | | - |
198 | 160 | function validateCloudMintPublicKey( |
199 | 161 | publicKey: string, |
200 | 162 | ): Effect.Effect<void, EnvironmentHttpBadRequestError> { |
@@ -251,190 +213,13 @@ function validateRelayConfigPayload( |
251 | 213 | return Effect.void; |
252 | 214 | } |
253 | 215 |
|
254 | | -function isLoopbackHostname(hostname: string): boolean { |
255 | | - return LOOPBACK_HOSTNAMES.has(normalizeHostname(hostname)); |
256 | | -} |
257 | | - |
258 | | -function firstForwardedHeaderValue(value: string | undefined): string | undefined { |
259 | | - const first = value?.split(",")[0]?.trim(); |
260 | | - return first && first.length > 0 ? first : undefined; |
261 | | -} |
262 | | - |
263 | | -function requestAbsoluteUrl(request: HttpServerRequest.HttpServerRequest): string | null { |
264 | | - try { |
265 | | - return new URL(request.originalUrl).href; |
266 | | - } catch { |
267 | | - const host = firstForwardedHeaderValue(request.headers.host) ?? "127.0.0.1"; |
268 | | - try { |
269 | | - return new URL(request.originalUrl, `http://${host}`).href; |
270 | | - } catch { |
271 | | - return null; |
272 | | - } |
273 | | - } |
274 | | -} |
275 | | - |
276 | | -function hasForwardedAuthorityHeaders(request: HttpServerRequest.HttpServerRequest): boolean { |
277 | | - return ( |
278 | | - firstForwardedHeaderValue(request.headers["x-forwarded-host"]) !== undefined || |
279 | | - firstForwardedHeaderValue(request.headers["x-forwarded-proto"]) !== undefined |
280 | | - ); |
281 | | -} |
282 | | - |
283 | | -function endpointRequestPort(url: URL): number { |
284 | | - return Number(url.port || (url.protocol === "https:" ? 443 : 80)); |
285 | | -} |
286 | | - |
287 | | -export function parseManagedEndpointLocalOrigin(localOrigin: string) { |
288 | | - const url = new URL(localOrigin); |
289 | | - if ( |
290 | | - localOrigin !== localOrigin.trim() || |
291 | | - (url.protocol !== "http:" && url.protocol !== "https:") || |
292 | | - url.username !== "" || |
293 | | - url.password !== "" || |
294 | | - url.pathname !== "/" || |
295 | | - url.search !== "" || |
296 | | - url.hash !== "" || |
297 | | - localOrigin.includes("?") || |
298 | | - localOrigin.includes("#") |
299 | | - ) { |
300 | | - throw new Error("Invalid local origin"); |
301 | | - } |
302 | | - const wsUrl = new URL(url.origin); |
303 | | - wsUrl.protocol = url.protocol === "https:" ? "wss:" : "ws:"; |
304 | | - return { |
305 | | - httpBaseUrl: url.origin, |
306 | | - wsBaseUrl: wsUrl.origin, |
307 | | - origin: { |
308 | | - localHttpHost: url.hostname, |
309 | | - localHttpPort: endpointRequestPort(url), |
310 | | - } satisfies RelayManagedEndpointOrigin, |
311 | | - }; |
312 | | -} |
313 | | - |
314 | | -function isAllowedEndpointOrigin(input: { |
315 | | - readonly origin: RelayManagedEndpointOrigin; |
316 | | - readonly requestUrl: string; |
317 | | -}): boolean { |
318 | | - if (!isLoopbackHostname(input.origin.localHttpHost)) { |
319 | | - return false; |
320 | | - } |
321 | | - |
322 | | - const url = new URL(input.requestUrl); |
323 | | - if (!isLoopbackHostname(url.hostname)) { |
324 | | - return false; |
325 | | - } |
326 | | - |
327 | | - return input.origin.localHttpPort === endpointRequestPort(url); |
328 | | -} |
329 | | - |
330 | | -// A managed (Cloudflare tunnel) endpoint is provisioned by the relay and must |
331 | | -// point at a loopback origin. A manual endpoint is reached out of band (e.g. |
332 | | -// Tailscale) or not advertised at all for publish-only links, so it is not |
333 | | -// tied to the managed-tunnel scope. |
334 | | -export function isSupportedLinkProviderKind(request: RelayLinkProofRequest): boolean { |
335 | | - return ( |
336 | | - request.endpoint.providerKind === "cloudflare_tunnel" || |
337 | | - request.endpoint.providerKind === "manual" |
338 | | - ); |
339 | | -} |
340 | | - |
341 | | -export function linkProofScopes( |
342 | | - request: RelayLinkProofRequest, |
343 | | -): RelayEnvironmentLinkProofPayload["scopes"] { |
344 | | - return request.endpoint.providerKind === "cloudflare_tunnel" |
345 | | - ? ["agent_activity_notifications", "managed_tunnels"] |
346 | | - : ["agent_activity_notifications"]; |
347 | | -} |
348 | | - |
349 | | -function hasExactScope(input: { |
350 | | - readonly scopes: ReadonlyArray<string>; |
351 | | - readonly expected: string; |
352 | | -}): boolean { |
353 | | - return input.scopes.length === 1 && input.scopes[0] === input.expected; |
354 | | -} |
355 | | - |
356 | | -function hasBoundedCloudProofLifetime(input: { |
357 | | - readonly iat: number; |
358 | | - readonly exp: number; |
359 | | - readonly nowSeconds: number; |
360 | | -}): boolean { |
361 | | - return ( |
362 | | - input.exp > input.iat && |
363 | | - input.exp - input.iat <= CLOUD_PROOF_MAX_LIFETIME_SECONDS && |
364 | | - input.iat <= input.nowSeconds + CLOUD_PROOF_CLOCK_SKEW_SECONDS |
365 | | - ); |
366 | | -} |
367 | | - |
368 | | -function managedEndpointRuntimeConfigsMatch( |
369 | | - left: RelayManagedEndpointRuntimeConfig, |
370 | | - right: RelayManagedEndpointRuntimeConfig, |
371 | | -): boolean { |
372 | | - return ( |
373 | | - left.providerKind === right.providerKind && |
374 | | - left.connectorToken === right.connectorToken && |
375 | | - left.tunnelId === right.tunnelId && |
376 | | - left.tunnelName === right.tunnelName |
377 | | - ); |
| 216 | +function normalizePemForSignedPayload(value: string): string { |
| 217 | + return value.trim(); |
378 | 218 | } |
379 | 219 |
|
380 | 220 | const decodeCloudHealthProof = Schema.decodeUnknownEffect(RelayCloudEnvironmentHealthProofPayload); |
381 | 221 | const decodeCloudMintProof = Schema.decodeUnknownEffect(RelayCloudMintCredentialProofPayload); |
382 | 222 |
|
383 | | -// The launcher owns this durable state, so read it directly both when a trial |
384 | | -// decides whether it owns pre-activation cleanup and while a server tears down. |
385 | | -export const pendingServiceUpdateExists = Effect.gen(function* () { |
386 | | - const config = yield* ServerConfig.ServerConfig; |
387 | | - const fs = yield* FileSystem.FileSystem; |
388 | | - const path = yield* Path.Path; |
389 | | - const runtimeDir = path.join(config.baseDir, "runtime"); |
390 | | - const stateText = yield* fs |
391 | | - .readFileString(path.join(runtimeDir, SERVICE_STATE_FILE)) |
392 | | - .pipe(Effect.option); |
393 | | - return Option.isSome(stateText) && serviceStateHasPendingUpdate(stateText.value); |
394 | | -}); |
395 | | - |
396 | | -// A pending update alone is not proof a replacement server is coming: an |
397 | | -// explicit launcher stop (`t3 service uninstall`, `systemctl stop`, |
398 | | -// `launchctl bootout`) during |
399 | | -// the pending window also tears this server down. The launcher marks that case |
400 | | -// just before it signals the child, so pending + no marker is the handoff. |
401 | | -const pendingUpdateHandoffExists = Effect.gen(function* () { |
402 | | - if (!(yield* pendingServiceUpdateExists)) { |
403 | | - return false; |
404 | | - } |
405 | | - const config = yield* ServerConfig.ServerConfig; |
406 | | - const fs = yield* FileSystem.FileSystem; |
407 | | - const path = yield* Path.Path; |
408 | | - const runtimeDir = path.join(config.baseDir, "runtime"); |
409 | | - const stopping = yield* fs |
410 | | - .exists(path.join(runtimeDir, SERVICE_STOP_MARKER_FILE)) |
411 | | - .pipe(Effect.orElseSucceed(() => false)); |
412 | | - return !stopping; |
413 | | -}); |
414 | | - |
415 | | -// The desktop app writes its marker right before it stops this server to |
416 | | -// install an update, whether a remote client or the local app started it. |
417 | | -// Reading consumes it, so shutdown checks it first. Only a fresh marker counts, |
418 | | -// so a marker the server never read (a hard kill) cannot keep the tunnel on a |
419 | | -// later quit. |
420 | | -const desktopUpdateRestartPending = Effect.gen(function* () { |
421 | | - const config = yield* ServerConfig.ServerConfig; |
422 | | - const fs = yield* FileSystem.FileSystem; |
423 | | - const path = yield* Path.Path; |
424 | | - const markerPath = path.join(config.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE); |
425 | | - const marker = yield* fs.stat(markerPath).pipe(Effect.option); |
426 | | - if (Option.isNone(marker)) { |
427 | | - return false; |
428 | | - } |
429 | | - yield* fs.remove(markerPath).pipe(Effect.ignore); |
430 | | - const now = yield* Clock.currentTimeMillis; |
431 | | - return Option.match(marker.value.mtime, { |
432 | | - onNone: () => false, |
433 | | - onSome: (writtenAt) => |
434 | | - now - writtenAt.getTime() < Duration.toMillis(DESKTOP_UPDATE_RESTART_MARKER_TTL), |
435 | | - }); |
436 | | -}); |
437 | | - |
438 | 223 | type ManagedTunnelRecoveryProofInput = { |
439 | 224 | readonly environmentId: RelayManagedEndpointRecoveryProofPayload["environmentId"]; |
440 | 225 | readonly cloudUserId: string; |
|
0 commit comments