Open History to see the last commit
1import type {
2 RelayEnvironmentLinkProofPayload,
3 RelayLinkProofRequest,
4 RelayManagedEndpointOrigin,
5 RelayManagedEndpointRuntimeConfig,
6} from "@t3tools/contracts/relay";
7import type { HttpServerRequest } from "effect/http";
8
9const CLOUD_PROOF_MAX_LIFETIME_SECONDS = 5 * 60;
10const CLOUD_PROOF_CLOCK_SKEW_SECONDS = 60;
11const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
12
13function normalizeHostname(hostname: string): string {
14 return hostname
15 .trim()
16 .toLowerCase()
17 .replace(/^\[(.*)\]$/, "$1");
18}
19
20function isLoopbackHostname(hostname: string): boolean {
21 return LOOPBACK_HOSTNAMES.has(normalizeHostname(hostname));
22}
23
24function firstForwardedHeaderValue(value: string | undefined): string | undefined {
25 const first = value?.split(",")[0]?.trim();
26 return first && first.length > 0 ? first : undefined;
27}
28
29export function requestAbsoluteUrl(request: HttpServerRequest.HttpServerRequest): string | null {
30 try {
31 return new URL(request.originalUrl).href;
32 } catch {
33 const host = firstForwardedHeaderValue(request.headers.host) ?? "127.0.0.1";
34 try {
35 return new URL(request.originalUrl, `http://${host}`).href;
36 } catch {
37 return null;
38 }
39 }
40}
41
42export function hasForwardedAuthorityHeaders(
43 request: HttpServerRequest.HttpServerRequest,
44): boolean {
45 return (
46 firstForwardedHeaderValue(request.headers["x-forwarded-host"]) !== undefined ||
47 firstForwardedHeaderValue(request.headers["x-forwarded-proto"]) !== undefined
48 );
49}
50
51function endpointRequestPort(url: URL): number {
52 return Number(url.port || (url.protocol === "https:" ? 443 : 80));
53}
54
55export function parseManagedEndpointLocalOrigin(localOrigin: string) {
56 const url = new URL(localOrigin);
57 if (
58 localOrigin !== localOrigin.trim() ||
59 (url.protocol !== "http:" && url.protocol !== "https:") ||
60 url.username !== "" ||
61 url.password !== "" ||
62 url.pathname !== "/" ||
63 url.search !== "" ||
64 url.hash !== "" ||
65 localOrigin.includes("?") ||
66 localOrigin.includes("#")
67 ) {
68 throw new Error("Invalid local origin");
69 }
70 const wsUrl = new URL(url.origin);
71 wsUrl.protocol = url.protocol === "https:" ? "wss:" : "ws:";
72 return {
73 httpBaseUrl: url.origin,
74 wsBaseUrl: wsUrl.origin,
75 origin: {
76 localHttpHost: url.hostname,
77 localHttpPort: endpointRequestPort(url),
78 } satisfies RelayManagedEndpointOrigin,
79 };
80}
81
82export function isAllowedEndpointOrigin(input: {
83 readonly origin: RelayManagedEndpointOrigin;
84 readonly requestUrl: string;
85 readonly allowNonLoopbackRequestHost?: boolean;
86}): boolean {
87 if (!isLoopbackHostname(input.origin.localHttpHost)) {
88 return false;
89 }
90
91 const url = new URL(input.requestUrl);
92 if (!input.allowNonLoopbackRequestHost && !isLoopbackHostname(url.hostname)) {
93 return false;
94 }
95
96 return input.origin.localHttpPort === endpointRequestPort(url);
97}
98
99export function isSupportedLinkProviderKind(request: RelayLinkProofRequest): boolean {
100 return (
101 request.endpoint.providerKind === "cloudflare_tunnel" ||
102 request.endpoint.providerKind === "manual"
103 );
104}
105
106export function linkProofScopes(
107 request: RelayLinkProofRequest,
108): RelayEnvironmentLinkProofPayload["scopes"] {
109 return request.endpoint.providerKind === "cloudflare_tunnel"
110 ? ["agent_activity_notifications", "managed_tunnels"]
111 : ["agent_activity_notifications"];
112}
113
114export function hasExactScope(input: {
115 readonly scopes: ReadonlyArray<string>;
116 readonly expected: string;
117}): boolean {
118 return input.scopes.length === 1 && input.scopes[0] === input.expected;
119}
120
121export function hasBoundedCloudProofLifetime(input: {
122 readonly iat: number;
123 readonly exp: number;
124 readonly nowSeconds: number;
125}): boolean {
126 return (
127 input.exp > input.iat &&
128 input.exp - input.iat <= CLOUD_PROOF_MAX_LIFETIME_SECONDS &&
129 input.iat <= input.nowSeconds + CLOUD_PROOF_CLOCK_SKEW_SECONDS
130 );
131}
132
133export function managedEndpointRuntimeConfigsMatch(
134 left: RelayManagedEndpointRuntimeConfig,
135 right: RelayManagedEndpointRuntimeConfig,
136): boolean {
137 return (
138 left.providerKind === right.providerKind &&
139 left.connectorToken === right.connectorToken &&
140 left.tunnelId === right.tunnelId &&
141 left.tunnelName === right.tunnelName
142 );
143}