Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

linkChecks.ts

142 lines4.1 KB

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");
19​
20function isLoopbackHostname(hostname: string): boolean {
21 return LOOPBACK_HOSTNAMES.has(normalizeHostname(hostname));
23​
24function firstForwardedHeaderValue(value: string | undefined): string | undefined {
25 const first = value?.split(",")[0]?.trim();
26 return first && first.length > 0 ? first : undefined;
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 }
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 );
50​
51function endpointRequestPort(url: URL): number {
52 return Number(url.port || (url.protocol === "https:" ? 443 : 80));
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 };
81​
82export function isAllowedEndpointOrigin(input: {
83 readonly origin: RelayManagedEndpointOrigin;
84 readonly requestUrl: string;
85}): boolean {
86 if (!isLoopbackHostname(input.origin.localHttpHost)) {
87 return false;
88 }
89​
90 const url = new URL(input.requestUrl);
91 if (!isLoopbackHostname(url.hostname)) {
92 return false;
93 }
94​
95 return input.origin.localHttpPort === endpointRequestPort(url);
97​
98export function isSupportedLinkProviderKind(request: RelayLinkProofRequest): boolean {
99 return (
100 request.endpoint.providerKind === "cloudflare_tunnel" ||
101 request.endpoint.providerKind === "manual"
102 );
104​
105export function linkProofScopes(
106 request: RelayLinkProofRequest,
107): RelayEnvironmentLinkProofPayload["scopes"] {
108 return request.endpoint.providerKind === "cloudflare_tunnel"
109 ? ["agent_activity_notifications", "managed_tunnels"]
110 : ["agent_activity_notifications"];
112​
113export function hasExactScope(input: {
114 readonly scopes: ReadonlyArray<string>;
115 readonly expected: string;
116}): boolean {
117 return input.scopes.length === 1 && input.scopes[0] === input.expected;
119​
120export function hasBoundedCloudProofLifetime(input: {
121 readonly iat: number;
122 readonly exp: number;
123 readonly nowSeconds: number;
124}): boolean {
125 return (
126 input.exp > input.iat &&
127 input.exp - input.iat <= CLOUD_PROOF_MAX_LIFETIME_SECONDS &&
128 input.iat <= input.nowSeconds + CLOUD_PROOF_CLOCK_SKEW_SECONDS
129 );
131​
132export function managedEndpointRuntimeConfigsMatch(
133 left: RelayManagedEndpointRuntimeConfig,
134 right: RelayManagedEndpointRuntimeConfig,
135): boolean {
136 return (
137 left.providerKind === right.providerKind &&
138 left.connectorToken === right.connectorToken &&
139 left.tunnelId === right.tunnelId &&
140 left.tunnelName === right.tunnelName
141 );