fix: use globals to avoid loader race conditions

This commit is contained in:
Aarnav Tale 2025-02-20 10:45:52 -05:00
parent f5436f5ee3
commit f982217dd0
No known key found for this signature in database
5 changed files with 60 additions and 97 deletions

View File

@ -3,17 +3,11 @@ import { createReadableStreamFromReadable } from '@react-router/node';
import { isbot } from 'isbot'; import { isbot } from 'isbot';
import type { RenderToPipeableStreamOptions } from 'react-dom/server'; import type { RenderToPipeableStreamOptions } from 'react-dom/server';
import { renderToPipeableStream } from 'react-dom/server'; import { renderToPipeableStream } from 'react-dom/server';
import type { AppLoadContext, EntryContext } from 'react-router'; import { EntryContext, ServerRouter } from 'react-router';
import { ServerRouter } from 'react-router';
import { hs_loadConfig } from '~/utils/config/loader';
import { hp_storeContext } from '~/utils/headscale';
import { hp_loadLogger } from '~/utils/log'; import { hp_loadLogger } from '~/utils/log';
import { initSessionManager } from '~/utils/sessions.server';
import type { AppContext } from '~server/context/app'; import type { AppContext } from '~server/context/app';
export const streamTimeout = 5_000; export const streamTimeout = 5_000;
// TODO: checkOidc
export default function handleRequest( export default function handleRequest(
request: Request, request: Request,
responseStatusCode: number, responseStatusCode: number,
@ -23,15 +17,8 @@ export default function handleRequest(
) { ) {
const { context } = loadContext; const { context } = loadContext;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
initSessionManager(
context.server.cookie_secret,
context.server.cookie_secure,
);
// This is a promise but we don't need to wait for it to finish // This is a promise but we don't need to wait for it to finish
// before we start rendering the shell since it only loads once. // before we start rendering the shell since it only loads once.
hs_loadConfig(context);
hp_storeContext(context);
hp_loadLogger(context.debug); hp_loadLogger(context.debug);
let shellRendered = false; let shellRendered = false;

View File

@ -9,8 +9,6 @@ let runtimeYaml: Document | undefined = undefined;
let runtimeConfig: HeadscaleConfig | undefined = undefined; let runtimeConfig: HeadscaleConfig | undefined = undefined;
let runtimePath: string | undefined = undefined; let runtimePath: string | undefined = undefined;
let runtimeMode: 'rw' | 'ro' | 'no' = 'no'; let runtimeMode: 'rw' | 'ro' | 'no' = 'no';
let runtimeStrict = true;
const runtimeLock = mutex(); const runtimeLock = mutex();
export type ConfigModes = export type ConfigModes =
@ -42,9 +40,12 @@ export function hs_getConfig(): ConfigModes {
}; };
} }
export async function hs_loadConfig(context: HeadplaneConfig) { export async function hs_loadConfig(path?: string, strict?: boolean) {
if (runtimeConfig !== undefined) {
return;
}
runtimeLock.acquire(); runtimeLock.acquire();
const path = context.headscale.config_path;
if (!path) { if (!path) {
runtimeLock.release(); runtimeLock.release();
return; return;
@ -62,8 +63,7 @@ export async function hs_loadConfig(context: HeadplaneConfig) {
return; return;
} }
runtimeStrict = context.headscale.config_strict ?? true; const config = validateConfig(rawConfig, strict ?? true);
const config = validateConfig(rawConfig, runtimeStrict);
if (!config) { if (!config) {
runtimeMode = 'no'; runtimeMode = 'no';
} }
@ -194,3 +194,6 @@ export async function hs_patchConfig(patches: PatchConfig[]) {
await writeFile(runtimePath, runtimeYaml.toString(), 'utf8'); await writeFile(runtimePath, runtimeYaml.toString(), 'utf8');
runtimeLock.release(); runtimeLock.release();
} }
// IMPORTANT THIS IS A SIDE EFFECT ON INIT
hs_loadConfig(__hs_context.config_path, __hs_context.config_strict);

View File

@ -1,7 +1,5 @@
import log, { noContext } from '~/utils/log'; import log, { noContext } from '~/utils/log';
import { AppContext } from '~server/context/app';
type Context = AppContext['context'];
export class HeadscaleError extends Error { export class HeadscaleError extends Error {
status: number; status: number;
@ -21,21 +19,13 @@ export class FatalError extends Error {
} }
} }
let context: Context | undefined = undefined; interface HeadscaleContext {
export function hp_storeContext(ctx: Context) { url: string;
if (context) {
return;
}
context = ctx;
} }
declare const global: typeof globalThis & { __hs_context: HeadscaleContext };
export async function healthcheck() { export async function healthcheck() {
if (!context) { const prefix = __hs_context.url;
throw noContext();
}
const prefix = context.headscale.url;
log.debug('APIC', 'GET /health'); log.debug('APIC', 'GET /health');
const health = new URL('health', prefix); const health = new URL('health', prefix);
@ -50,15 +40,11 @@ export async function healthcheck() {
} }
export async function pull<T>(url: string, key: string) { export async function pull<T>(url: string, key: string) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) { if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?'); throw new Error('Missing API key, could this be a cookie setting issue?');
} }
const prefix = context.headscale.url; const prefix = __hs_context.url;
log.debug('APIC', 'GET %s', `${prefix}/api/${url}`); log.debug('APIC', 'GET %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, { const response = await fetch(`${prefix}/api/${url}`, {
@ -81,15 +67,11 @@ export async function pull<T>(url: string, key: string) {
} }
export async function post<T>(url: string, key: string, body?: unknown) { export async function post<T>(url: string, key: string, body?: unknown) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) { if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?'); throw new Error('Missing API key, could this be a cookie setting issue?');
} }
const prefix = context.headscale.url; const prefix = __hs_context.url;
log.debug('APIC', 'POST %s', `${prefix}/api/${url}`); log.debug('APIC', 'POST %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, { const response = await fetch(`${prefix}/api/${url}`, {
@ -114,15 +96,11 @@ export async function post<T>(url: string, key: string, body?: unknown) {
} }
export async function put<T>(url: string, key: string, body?: unknown) { export async function put<T>(url: string, key: string, body?: unknown) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) { if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?'); throw new Error('Missing API key, could this be a cookie setting issue?');
} }
const prefix = context.headscale.url; const prefix = __hs_context.url;
log.debug('APIC', 'PUT %s', `${prefix}/api/${url}`); log.debug('APIC', 'PUT %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, { const response = await fetch(`${prefix}/api/${url}`, {
@ -147,15 +125,11 @@ export async function put<T>(url: string, key: string, body?: unknown) {
} }
export async function del<T>(url: string, key: string) { export async function del<T>(url: string, key: string) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) { if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?'); throw new Error('Missing API key, could this be a cookie setting issue?');
} }
const prefix = context.headscale.url; const prefix = __hs_context.url;
log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`); log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, { const response = await fetch(`${prefix}/api/${url}`, {

View File

@ -1,8 +1,4 @@
import { import { Session, createCookieSessionStorage } from 'react-router';
Session,
SessionStorage,
createCookieSessionStorage,
} from 'react-router';
export type SessionData = { export type SessionData = {
hsApiKey: string; hsApiKey: string;
@ -23,42 +19,28 @@ type SessionFlashData = {
error: string; error: string;
}; };
type SessionStore = SessionStorage<SessionData, SessionFlashData>; // TODO: Domain config in cookies
const sessionStorage = createCookieSessionStorage<
// TODO: Add args to this function to allow custom domain/config SessionData,
let sessionStorage: SessionStore | null = null; SessionFlashData
export function initSessionManager(secret: string, secure: boolean) { >({
if (sessionStorage) { cookie: {
return; name: 'hp_sess',
} httpOnly: true,
maxAge: 60 * 60 * 24, // 24 hours
sessionStorage = createCookieSessionStorage<SessionData, SessionFlashData>({ path: '/',
cookie: { sameSite: 'lax',
name: 'hp_sess', secrets: [__cookie_context.cookie_secret],
httpOnly: true, secure: __cookie_context.cookie_secure,
maxAge: 60 * 60 * 24, // 24 hours },
path: '/', });
sameSite: 'lax',
secrets: [secret],
secure,
},
});
}
export function getSession(cookie: string | null) { export function getSession(cookie: string | null) {
if (!sessionStorage) {
throw new Error('Session manager not initialized');
}
return sessionStorage.getSession(cookie); return sessionStorage.getSession(cookie);
} }
export type ServerSession = Session<SessionData, SessionFlashData>; export type ServerSession = Session<SessionData, SessionFlashData>;
export async function auth(request: Request) { export async function auth(request: Request) {
if (!sessionStorage) {
return false;
}
const cookie = request.headers.get('Cookie'); const cookie = request.headers.get('Cookie');
const session = await sessionStorage.getSession(cookie); const session = await sessionStorage.getSession(cookie);
if (!session.has('hsApiKey')) { if (!session.has('hsApiKey')) {
@ -69,17 +51,9 @@ export async function auth(request: Request) {
} }
export function destroySession(session: Session) { export function destroySession(session: Session) {
if (!sessionStorage) {
throw new Error('Session manager not initialized');
}
return sessionStorage.destroySession(session); return sessionStorage.destroySession(session);
} }
export function commitSession(session: Session, opts?: { maxAge?: number }) { export function commitSession(session: Session, opts?: { maxAge?: number }) {
if (!sessionStorage) {
throw new Error('Session manager not initialized');
}
return sessionStorage.commitSession(session, opts); return sessionStorage.commitSession(session, opts);
} }

View File

@ -6,6 +6,19 @@ import log, { hpServer_loadLogger } from '~server/utils/log';
import mutex from '~server/utils/mutex'; import mutex from '~server/utils/mutex';
import { HeadplaneConfig, coalesceConfig, validateConfig } from './parser'; import { HeadplaneConfig, coalesceConfig, validateConfig } from './parser';
declare global {
let __cookie_context: {
cookie_secret: string;
cookie_secure: boolean;
};
let __hs_context: {
url: string;
config_path?: string;
config_strict?: boolean;
};
}
const envBool = type('string | undefined').pipe((v) => { const envBool = type('string | undefined').pipe((v) => {
return ['1', 'true', 'yes', 'on'].includes(v?.toLowerCase() ?? ''); return ['1', 'true', 'yes', 'on'].includes(v?.toLowerCase() ?? '');
}); });
@ -33,7 +46,6 @@ export function hp_getConfig() {
} }
const config = runtimeConfig; const config = runtimeConfig;
runtimeLock.release(); runtimeLock.release();
return config; return config;
} }
@ -106,6 +118,19 @@ export async function hp_loadConfig() {
testOidc(config.oidc); testOidc(config.oidc);
} }
// @ts-expect-error: If we remove globalThis we get a runtime error
globalThis.__cookie_context = {
cookie_secret: config.server.cookie_secret,
cookie_secure: config.server.cookie_secure,
};
// @ts-expect-error: If we remove globalThis we get a runtime error
globalThis.__hs_context = {
url: config.headscale.url,
config_path: config.headscale.config_path,
config_strict: config.headscale.config_strict,
};
runtimeConfig = config; runtimeConfig = config;
runtimeLock.release(); runtimeLock.release();
} }