refactor(server): move instance-URL resolution to app-config

This commit is contained in:
jubnl
2026-07-28 20:21:09 +02:00
parent 292f1b181c
commit cafff003a3
29 changed files with 202 additions and 73 deletions
+12
View File
@@ -9,6 +9,7 @@ the pre-init Express middleware.
env.schema.ts Zod catalog of the whole env surface; fail-fast at boot
derive.ts pure (raw env) → typed namespace functions, exact per-site coercions
env.ts readEnv() live accessor + validateEnvAtBoot()
app-url.ts getAppUrl()/getMcpSafeUrl() instance base-URL resolution
boot-validate.ts side-effect import used by index.ts (validates before other modules load)
parsers.ts shared coercion helpers (boolTrueLoose, numberOr, csvList, …)
```
@@ -43,6 +44,17 @@ tokens, `RuntimeEnvService`) and consumes the SAME derive functions.
rate limits, …) keep freeze-at-import timing, merely sourcing the value from
`readEnv()` at module top. Request-time reads stay request-time.
## app-url.ts — instance base-URL resolution (moved 2026-07-28)
`getAppUrl()` (APP_URL → first ALLOWED_ORIGINS entry → `http://localhost:PORT`,
each candidate URL-validated, ALL trailing slashes stripped) and
`getMcpSafeUrl()` (same, sanitized to HTTPS/localhost/127.0.0.1 for the MCP
SDK's issuer check) moved here **verbatim** from `services/notifications.ts`
(audit findings `auth-2`/`notifications-1`/`admin-3`/`mcp-1`). They follow
invariant 1: `readEnv()` per call, live, never cached. The invalid-URL silent
fallthrough and the strip-ALL-slashes quirk (see `parsers.ts`
`stripTrailingSlashes`) are parity-pinned — do not "fix" them here.
## Classification: boot-stable vs runtime-toggled
**Boot-stable** (frozen at app/module creation; snapshot `registerAs`/`ConfigType`
+47
View File
@@ -0,0 +1,47 @@
import { readEnv } from './env';
/**
* Instance base-URL resolution: APP_URL → first ALLOWED_ORIGINS entry →
* http://localhost:{PORT}. Moved verbatim from services/notifications.ts
* (2026-07-28, findings auth-2/notifications-1/admin-3/mcp-1) — the resolution
* chain, trailing-slash strip and invalid-URL fallthrough are parity-pinned.
* Live reads: readEnv() per call, never cached.
*/
export function getAppUrl(): string {
const appUrl = readEnv().app.appUrl;
if (appUrl) {
try {
const _ = new URL(appUrl);
return appUrl.replace(/\/+$/, '');
} catch (_ignored) {}
}
const origins = readEnv().http.allowedOriginsRaw;
if (origins) {
const first = origins.split(',')[0]?.trim();
if (first) {
try {
const _ = new URL(first);
return first.replace(/\/+$/, '');
} catch (_ignored) {}
}
}
const port = readEnv().app.port;
return `http://localhost:${port}`;
}
/** Returns a URL guaranteed to satisfy the MCP SDK's issuer requirements (HTTPS or localhost).
* Falls back to http://localhost:{PORT} when APP_URL/ALLOWED_ORIGINS use a non-HTTPS, non-localhost scheme
* that would cause checkIssuerUrl to throw "Issuer URL must be HTTPS". */
export function getMcpSafeUrl(): string {
const candidate = getAppUrl();
try {
const u = new URL(candidate);
if (u.protocol === 'https:' || u.hostname === 'localhost' || u.hostname === '127.0.0.1') {
return candidate;
}
} catch {
// candidate was somehow invalid — fall through to localhost
}
const port = readEnv().app.port;
return `http://localhost:${port}`;
}
+1
View File
@@ -1,4 +1,5 @@
export { readEnv, validateEnvAtBoot } from './env';
export { getAppUrl, getMcpSafeUrl } from './app-url';
export type { AppEnv, RawEnv } from './env';
export {
deriveAll,
+1 -1
View File
@@ -61,7 +61,7 @@ export function csvListFiltered(raw: string | undefined): string[] | null {
: null;
}
/** Strip ALL trailing slashes (`/\/+$/`) — notifications.getAppUrl / TRANSIT_API_URL variant. */
/** Strip ALL trailing slashes (`/\/+$/`) — app-url.getAppUrl / TRANSIT_API_URL variant. */
export function stripTrailingSlashes(value: string): string {
return value.replace(/\/+$/, '');
}
+1 -2
View File
@@ -24,8 +24,7 @@ const tmpDir = path.join(__dirname, '../data/tmp');
});
import * as scheduler from './scheduler';
import { readEnv } from './app-config';
import { getAppUrl, getMcpSafeUrl } from './services/notifications';
import { getAppUrl, getMcpSafeUrl, readEnv } from './app-config';
const PORT = readEnv().app.port;
const HOST = readEnv().app.host;
+1 -1
View File
@@ -12,7 +12,7 @@ import { registerTools } from './tools';
import { McpSession, sessions, revokeUserSessions, revokeUserSessionsForClient, evictOldestSessionForUser } from './sessionManager';
import { readEnv } from '../app-config';
import { writeAudit, getClientIp } from '../nest/audit/audit.bridge';
import { getMcpSafeUrl } from '../services/notifications';
import { getMcpSafeUrl } from '../app-config';
export { revokeUserSessions, revokeUserSessionsForClient };
+1 -1
View File
@@ -16,7 +16,7 @@ import {
getUserByAccessToken,
} from '../services/oauthService';
import { ALL_SCOPES } from './scopes';
import { getMcpSafeUrl } from '../services/notifications';
import { getMcpSafeUrl } from '../app-config';
import { writeAudit } from '../nest/audit/audit.bridge';
// ---------------------------------------------------------------------------
+2 -1
View File
@@ -2,7 +2,8 @@ import { Injectable } from '@nestjs/common';
import type { Request, Response } from 'express';
import * as auth from '../../services/authService';
import { setAuthCookie, clearAuthCookie } from '../../services/cookie';
import { sendPasswordResetEmail, getAppUrl } from '../../services/notifications';
import { sendPasswordResetEmail } from '../../services/notifications';
import { getAppUrl } from '../../app-config';
import type { User } from '../../types';
/**
+1 -1
View File
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import * as oauth from '../../services/oauthService';
import { isAddonEnabled } from '../../services/adminService';
import { ADDON_IDS } from '../../addons';
import { getMcpSafeUrl } from '../../services/notifications';
import { getMcpSafeUrl } from '../../app-config';
/**
* Thin Nest wrapper around the existing OAuth 2.1 service. The grant handling,
+1 -1
View File
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import type { Request, Response } from 'express';
import * as oidc from '../../services/oidcService';
import { getAppUrl } from '../../services/notifications';
import { getAppUrl } from '../../app-config';
import { resolveAuthToggles } from '../../services/authService';
import { setAuthCookie } from '../../services/cookie';
+3 -3
View File
@@ -14,7 +14,7 @@ import { mcpAuthMetadataRouter } from '@modelcontextprotocol/sdk/server/auth/rou
import { authorizationHandler } from '@modelcontextprotocol/sdk/server/auth/handlers/authorize';
import { clientRegistrationHandler } from '@modelcontextprotocol/sdk/server/auth/handlers/register';
import type { OAuthMetadata } from '@modelcontextprotocol/sdk/shared/auth';
import { getMcpSafeUrl } from '../../services/notifications';
import { getMcpSafeUrl } from '../../app-config';
// Platform / transport routes extracted verbatim from createApp() (app.ts) so they can be
// mounted on either the legacy Express app or the NestJS Express instance (strangler A6/A8).
@@ -126,8 +126,8 @@ export function applyPlatformTransport(app: express.Application): void {
next();
};
// SDK metadata router — built lazily on first request so getAppUrl() (which queries the DB)
// is not called at createApp() time, before test tables have been created.
// SDK metadata router — built lazily on first request so the issuer URL is
// resolved from the live env, not frozen at createApp() time.
// mcpAuthMetadataRouter serves:
// /.well-known/oauth-authorization-server — RFC 8414 AS metadata
// /.well-known/oauth-protected-resource/mcp — RFC 9728 path-based PRM (fixes issue #959 bug 1)
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import crypto from 'node:crypto';
import { DatabaseService } from '../database/database.service';
import { encrypt_api_key, decrypt_api_key } from '../../services/apiKeyCrypto';
import { getAppUrl } from '../../services/notifications';
import { getAppUrl } from '../../app-config';
import { isPrivateIp } from './install/safe-fetch';
import { safeFetchLlm } from '../../utils/ssrfGuard';
+1 -1
View File
@@ -2,7 +2,7 @@ import { readEnv } from '../app-config';
import { db } from '../db/database';
import { safeFetchFollow, SsrfBlockedError } from '../utils/ssrfGuard';
import { decrypt_api_key } from './apiKeyCrypto';
import { getAppUrl } from './notifications';
import { getAppUrl } from '../app-config';
// ── Photo cache (disk-backed) ────────────────────────────────────────────────
import * as placePhotoCache from './placePhotoCache';
+2 -1
View File
@@ -9,7 +9,8 @@ import {
ADMIN_SCOPED_EVENTS,
type NotifEventType,
} from './notificationPreferencesService';
import { getEventText, getUserLanguage, getAppUrl } from './notifications';
import { getEventText, getUserLanguage } from './notifications';
import { getAppUrl } from '../app-config';
import { listChannels, type ChannelMessage, type ExternalChannel } from './notifications/channelRegistry';
import {
resolveRecipients,
+1 -41
View File
@@ -1,4 +1,4 @@
import { readEnv } from '../app-config';
import { getAppUrl, readEnv } from '../app-config';
import { db } from '../db/database';
import { checkSsrf, createPinnedDispatcher } from '../utils/ssrfGuard';
import { decrypt_api_key } from './apiKeyCrypto';
@@ -68,46 +68,6 @@ function getSmtpConfig(): SmtpConfig | null {
};
}
// Exported for use by notificationService
export function getAppUrl(): string {
const appUrl = readEnv().app.appUrl;
if (appUrl) {
try {
const _ = new URL(appUrl);
return appUrl.replace(/\/+$/, '');
} catch (_ignored) {}
}
const origins = readEnv().http.allowedOriginsRaw;
if (origins) {
const first = origins.split(',')[0]?.trim();
if (first) {
try {
const _ = new URL(first);
return first.replace(/\/+$/, '');
} catch (_ignored) {}
}
}
const port = readEnv().app.port;
return `http://localhost:${port}`;
}
/** Returns a URL guaranteed to satisfy the MCP SDK's issuer requirements (HTTPS or localhost).
* Falls back to http://localhost:{PORT} when APP_URL/ALLOWED_ORIGINS use a non-HTTPS, non-localhost scheme
* that would cause checkIssuerUrl to throw "Issuer URL must be HTTPS". */
export function getMcpSafeUrl(): string {
const candidate = getAppUrl();
try {
const u = new URL(candidate);
if (u.protocol === 'https:' || u.hostname === 'localhost' || u.hostname === '127.0.0.1') {
return candidate;
}
} catch {
// candidate was somehow invalid — fall through to localhost
}
const port = readEnv().app.port;
return `http://localhost:${port}`;
}
/** Is SMTP configured at the instance level? (Independent of any one user's address.) */
export function isSmtpConfigured(): boolean {
return !!(readEnv().smtp.host || getAppSetting('smtp_host'));
+1 -1
View File
@@ -6,7 +6,7 @@ import { ADDON_IDS } from '../addons';
import { User } from '../types';
import { writeAudit, logWarn } from '../nest/audit/audit.bridge';
import { revokeUserSessionsForClient } from '../mcp/sessionManager';
import { getMcpSafeUrl } from './notifications';
import { getMcpSafeUrl } from '../app-config';
// ---------------------------------------------------------------------------
// Constants
+1 -1
View File
@@ -1,6 +1,6 @@
import { readEnv } from '../app-config';
import { buildUserAgent } from './mapsService';
import { getAppUrl } from './notifications';
import { getAppUrl } from '../app-config';
/**
* Public transit routing (#1065) backed by Transitous (api.transitous.org), the
+1 -1
View File
@@ -1,6 +1,6 @@
import { readEnv } from '../app-config';
import { db } from '../db/database';
import { getAppUrl } from './notifications';
import { getAppUrl } from '../app-config';
/**
* Resolves the WebAuthn Relying Party ID + allowed origins for this deployment.