@mostra/runtime 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -73,8 +73,14 @@ export function headerPairs(incoming: IncomingMessage): [string, string][] {
73
73
  return pairs;
74
74
  }
75
75
 
76
+ /** The header the worker-mode service worker adds to a navigation it sends on (D70). */
77
+ export const NAVIGATION_HEADER = "x-mostra-navigation";
78
+
76
79
  /** Whether a request is a navigation that should get an HTML page rather than JSON. */
77
80
  export function wantsDocument(headers: Headers): boolean {
81
+ // A worker-mode frame's navigation, which its service worker sends again as a plain request
82
+ // (D70): the browser marks that one `Sec-Fetch-Dest: empty`.
83
+ if (headers.get(NAVIGATION_HEADER) === "1") return true;
78
84
  const dest = headers.get("sec-fetch-dest");
79
85
  if (dest !== null) return dest === "document" || dest === "iframe" || dest === "frame";
80
86
  return (headers.get("accept") ?? "").includes("text/html");
@@ -152,14 +158,18 @@ export function cookieValues(header: string | null, name: string): string[] {
152
158
  return values;
153
159
  }
154
160
 
155
- /** The `Set-Cookie` value of a session cookie (D98 item 1); never `Secure` over loopback HTTP. */
161
+ /**
162
+ * The `Set-Cookie` value of a session cookie (D98 item 1): never `Secure` over loopback HTTP,
163
+ * always over a tunnel's HTTPS address (D158), where it must never travel in clear.
164
+ */
156
165
  export function sessionCookie(
157
166
  name: string,
158
167
  value: string,
159
168
  path: string,
160
169
  maxAgeSeconds: number,
170
+ secure = false,
161
171
  ): string {
162
- return `${name}=${value}; Path=${path}; Max-Age=${maxAgeSeconds}; HttpOnly; SameSite=Strict`;
172
+ return `${name}=${value}; Path=${path}; Max-Age=${maxAgeSeconds}; HttpOnly; SameSite=Strict${secure ? "; Secure" : ""}`;
163
173
  }
164
174
 
165
175
  /**
@@ -99,3 +99,78 @@ export async function resolveIdentity(input: IdentityInput): Promise<IdentityOut
99
99
  }
100
100
  return { kind: "default" };
101
101
  }
102
+
103
+ /**
104
+ * Decodes RFC 2047 encoded words (`=?utf-8?q?Ferris_B=C3=BCller?=`, and `b` for base64), as
105
+ * Tailscale Serve writes a non-ASCII name; text outside encoded words is kept, and whitespace
106
+ * between two encoded words is dropped (RFC 2047 §6.2). Anything malformed is kept as written.
107
+ */
108
+ export function decodeEncodedWords(value: string): string {
109
+ const word = /=\?([^?\s]+)\?([qQbB])\?([^?\s]*)\?=/g;
110
+ const decodeOne = (charset: string, encoding: string, text: string): string | undefined => {
111
+ let bytes: Uint8Array;
112
+ if (encoding.toLowerCase() === "b") {
113
+ bytes = Buffer.from(text, "base64");
114
+ } else {
115
+ const out: number[] = [];
116
+ for (let index = 0; index < text.length; index++) {
117
+ const char = text[index] as string;
118
+ if (char === "_") out.push(0x20);
119
+ else if (char === "=" && /^[0-9A-Fa-f]{2}$/.test(text.slice(index + 1, index + 3))) {
120
+ out.push(Number.parseInt(text.slice(index + 1, index + 3), 16));
121
+ index += 2;
122
+ } else out.push(char.charCodeAt(0) & 0xff);
123
+ }
124
+ bytes = Uint8Array.from(out);
125
+ }
126
+ try {
127
+ return new TextDecoder(charset, { fatal: true }).decode(bytes);
128
+ } catch {
129
+ return undefined;
130
+ }
131
+ };
132
+ let result = "";
133
+ let last = 0;
134
+ let previousWasWord = false;
135
+ for (const match of value.matchAll(word)) {
136
+ const between = value.slice(last, match.index);
137
+ const decoded = decodeOne(match[1] as string, match[2] as string, match[3] as string);
138
+ if (decoded === undefined) {
139
+ result += between + match[0];
140
+ previousWasWord = false;
141
+ } else {
142
+ result += previousWasWord && /^\s*$/.test(between) ? "" : between;
143
+ result += decoded;
144
+ previousWasWord = true;
145
+ }
146
+ last = match.index + match[0].length;
147
+ }
148
+ return result + value.slice(last);
149
+ }
150
+
151
+ /** Whether a connection's peer is this machine: the only way a tunnel tool reaches a face. */
152
+ export function isLoopbackPeer(remote: string | undefined): boolean {
153
+ if (remote === undefined) return false;
154
+ const address = remote.startsWith("::ffff:") ? remote.slice(7) : remote;
155
+ return address === "::1" || /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(address);
156
+ }
157
+
158
+ /**
159
+ * The tailnet person Tailscale Serve names in a request (D158 item 2): `Tailscale-User-Login` and
160
+ * `Tailscale-User-Name`, which Serve sets on every tailnet request after removing any a client
161
+ * sent. They are believed only from a loopback peer (faces bind loopback, so only `tailscaled` and
162
+ * this machine's own programs reach them) and never beside `Tailscale-Funnel-Request`, which
163
+ * marks a public request. A tagged device sends no login, so nobody is named.
164
+ */
165
+ export function tailnetPerson(
166
+ headers: Headers,
167
+ remote: string | undefined,
168
+ ): ViewerSpec | undefined {
169
+ if (!isLoopbackPeer(remote) || headers.has("tailscale-funnel-request")) return undefined;
170
+ const login = headers.get("tailscale-user-login");
171
+ if (login === null) return undefined;
172
+ const ref = decodeEncodedWords(login).trim();
173
+ if (ref === "" || ref.length > 512) return undefined;
174
+ const name = decodeEncodedWords(headers.get("tailscale-user-name") ?? "").trim();
175
+ return { ref, ...(name !== "" && name.length <= 256 ? { name } : {}) };
176
+ }