Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Route the sign-in, pairing and brand reads through serverFetch too (part of #5665)

Bugfixer 231d12a31634e
diff --git a/src/lib/brand.ts b/src/lib/brand.ts
index cd97bc264c..3e6560fec2 100644
--- a/src/lib/brand.ts
+++ b/src/lib/brand.ts
@@ -1,3 +1,4 @@
+import { serverFetch } from "./relay-server";
export interface Brand {
name: string;
tagline?: string;
@@ -107,7 +108,7 @@ function isBrandStatus(value: unknown): value is BrandStatus {
return Boolean(v) && typeof v === "object" && typeof Reflect.get(Object(v), "name") === "string";
}
-export async function bootstrapBrand(fetchImpl: typeof fetch = fetch, timeoutMs = 1500): Promise<BrandStatus> {
+export async function bootstrapBrand(fetchImpl: typeof serverFetch = serverFetch, timeoutMs = 1500): Promise<BrandStatus> {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
diff --git a/src/lib/phone-pairing.ts b/src/lib/phone-pairing.ts
index a7e9abfa6d..6a97542a89 100644
--- a/src/lib/phone-pairing.ts
+++ b/src/lib/phone-pairing.ts
@@ -6,6 +6,7 @@ import type { Action } from "@/state/store";
import { readMembership } from "./membership";
import { isOwnerOrAdmin, readSessionState, type SessionState } from "./session";
import { scrollWithin } from "./scroll-within";
+import { serverFetch } from "./relay-server";
export type PhonePairingTarget = "computer" | "cloud" | "server";
@@ -73,7 +74,7 @@ export function phoneDestinations(entry: ConnectPhoneEntry | null, cloud: CloudP
let pending: Promise<PhonePairingAccess> | null = null;
-export function loadPhonePairingAccess(fetchImpl: typeof fetch = fetch): Promise<PhonePairingAccess> {
+export function loadPhonePairingAccess(fetchImpl: typeof serverFetch = serverFetch): Promise<PhonePairingAccess> {
pending ??= readSessionState(fetchImpl).then(async (session): Promise<PhonePairingAccess> => {
if (!isOwnerOrAdmin(session)) return { session, pairingCodes: false };
const config: unknown = await fetchImpl("/api/config", { credentials: "same-origin" })
diff --git a/src/lib/relay-server-callers.test.ts b/src/lib/relay-server-callers.test.ts
index bacbbf474f..80071eb40b 100644
--- a/src/lib/relay-server-callers.test.ts
+++ b/src/lib/relay-server-callers.test.ts
@@ -15,12 +15,15 @@ function appFiles(): string[] {
.filter((file) => file !== "src/lib/relay-server.ts" && !file.endsWith("-preview.tsx"));
}
+/** A call to fetch, or fetch handed on as a value (`fetchImpl = fetch`, `run(fetch)`); not `.fetch(` or a `fetch:` key. */
+const BARE_FETCH = /(?<![\w$.])fetch\(|[=(,:]\s*fetch\b(?![\w$.(]|\s*:)/;
+
describe("app requests to its own server", () => {
it("all go through serverFetch, never a bare fetch", () => {
const bare: string[] = [];
for (const file of appFiles()) {
readFileSync(new URL(file, ROOT), "utf8").split("\n").forEach((line, index) => {
- if (/(?<![\w$.])fetch\(/.test(line.replace(/\/\/.*$/, ""))) bare.push(`${file}:${index + 1}`);
+ if (BARE_FETCH.test(line.replace(/\/\/.*$/, ""))) bare.push(`${file}:${index + 1}`);
});
}
expect(bare).toEqual([]);
diff --git a/src/lib/session.ts b/src/lib/session.ts
index f8136590d2..0621efd5f6 100644
--- a/src/lib/session.ts
+++ b/src/lib/session.ts
@@ -1,6 +1,7 @@
import { t } from "./i18n";
import { serverErrorText } from "./server-error";
import { forgetSharedSession } from "./session-reuse";
+import { serverFetch } from "./relay-server";
export interface EnvironmentDescriptor {
environmentId: string;
@@ -42,7 +43,7 @@ export function rememberedTeamSignIn(): string | null {
try { return globalThis.localStorage?.getItem(TEAM_SIGN_IN_KEY) ?? null; } catch { return null; }
}
-export async function readSessionState(fetchImpl: typeof fetch = fetch): Promise<SessionState> {
+export async function readSessionState(fetchImpl: typeof serverFetch = serverFetch): Promise<SessionState> {
let res: Response;
try {
res = await fetchImpl("/api/auth/session", { credentials: "same-origin" });
@@ -137,7 +138,7 @@ function refusalText(body: unknown, res: Response): string {
export async function pairWithCode(
input: { code: string; label: string; attemptId?: string; browser?: boolean },
- fetchImpl: typeof fetch = fetch,
+ fetchImpl: typeof serverFetch = serverFetch,
): Promise<{ ok: true } | { ok: false; error: string }> {
let res: Response;
try {
@@ -157,7 +158,7 @@ export async function pairWithCode(
return { ok: false, error: refusalText(body, res) };
}
-export async function previewBrowserSignIn(credential: string, fetchImpl: typeof fetch = fetch): Promise<{ owner: string; team?: string } | null> {
+export async function previewBrowserSignIn(credential: string, fetchImpl: typeof serverFetch = serverFetch): Promise<{ owner: string; team?: string } | null> {
try {
const res = await fetchImpl("/api/auth/pair", {
method: "POST",
@@ -173,7 +174,7 @@ export async function previewBrowserSignIn(credential: string, fetchImpl: typeof
}
}
-export function signInWithBrowserGrant(credential: string, attemptId: string, fetchImpl: typeof fetch = fetch): Promise<{ ok: true } | { ok: false; error: string }> {
+export function signInWithBrowserGrant(credential: string, attemptId: string, fetchImpl: typeof serverFetch = serverFetch): Promise<{ ok: true } | { ok: false; error: string }> {
return pairWithCode({ code: credential, label: defaultDeviceLabel(), attemptId, browser: true }, fetchImpl);
}
@@ -182,7 +183,7 @@ export function cloudOwnerOf(session: unknown): string | null {
return record.cloudHome === true && typeof record.owner === "string" && record.owner ? record.owner : null;
}
-async function postAuth(path: string, body: Record<string, unknown>, fetchImpl: typeof fetch): Promise<{ ok: true } | { ok: false; error: string }> {
+async function postAuth(path: string, body: Record<string, unknown>, fetchImpl: typeof serverFetch): Promise<{ ok: true } | { ok: false; error: string }> {
let res: Response;
try {
res = await fetchImpl(path, { method: "POST", credentials: "same-origin", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
@@ -196,11 +197,11 @@ async function postAuth(path: string, body: Record<string, unknown>, fetchImpl:
return { ok: false, error: refusalText(parsed, res) };
}
-export function startEmailSignIn(email: string, fetchImpl: typeof fetch = fetch): Promise<{ ok: true } | { ok: false; error: string }> {
+export function startEmailSignIn(email: string, fetchImpl: typeof serverFetch = serverFetch): Promise<{ ok: true } | { ok: false; error: string }> {
return postAuth("/api/auth/email/start", { email }, fetchImpl);
}
-export function verifyEmailSignIn(input: { email: string; code: string; label: string }, fetchImpl: typeof fetch = fetch): Promise<{ ok: true } | { ok: false; error: string }> {
+export function verifyEmailSignIn(input: { email: string; code: string; label: string }, fetchImpl: typeof serverFetch = serverFetch): Promise<{ ok: true } | { ok: false; error: string }> {
return postAuth("/api/auth/email/verify", { email: input.email, code: input.code, label: input.label }, fetchImpl);
}