mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
bfb3bcd63c
Some installs are run by somebody other than the person who administers them. The operator owns the credentials, the configuration and the upgrade schedule, and the admin of the instance is a user of the result rather than the one who set it up. Nothing in the app knows that today, so it keeps offering settings whose values are not the admin's to change, and the honest answer to half of them would be a 403 the UI never anticipated. TREK_MANAGED says it once. It is a single boolean and the surfaces that care read it themselves, so adding one never comes back to the config layer. Runtime-toggled, in the same class as DEMO_MODE, rather than a registerAs token: roughly sixty tests mutate env mid-lifetime, and a value frozen per built app breaks all of them. `=== true` on the way in, so an unset, empty or misspelled value means off — a managed install sets it deliberately, and a typo must not quietly take settings away from an admin who never asked for it. The guard keys on a decorator, not on a path. MfaPolicyGuard documents where the two literal path lists it replaced ended up: they stopped being maintained, and every endpoint added afterwards silently fell on the wrong side. A marker on the handler cannot drift away from the handler. It carries a mandatory reason, which is for the next reader of the source rather than for the client, so the marks stay auditable. It registers third, after the auth and MFA guards, so a stranger still gets the 401 those two would have given them instead of a 403 that would confirm the route exists. And it checks the mode before it touches metadata, because it runs on every request of every install and almost all of them are self-hosted. One 403 body, shared by REST and MCP. DEMO_MODE grew two texts for the same condition and neither can be changed now without hunting for the other. The flag reaches the client through app-config so the UI can stop offering what the server would refuse anyway, but it is deliberately not persisted the way demo_mode is: two installs can share a browser profile, and a stale "this one is managed" would take settings away on an install that never set it. No route carries the marker yet. The per-domain marks follow. Unset, which is every self-hosted install, nothing about this changes anything.
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { readEnv, type AppEnv } from '../../app-config';
|
|
|
|
/**
|
|
* DI-friendly access to the RUNTIME-TOGGLED env values (DEMO_MODE, NODE_ENV,
|
|
* APP_VERSION, OIDC_*, …). Every method delegates to readEnv() at call time —
|
|
* never snapshot a namespace into a field or the constructor, or the ~60 tests
|
|
* that mutate process.env mid-lifetime break.
|
|
*
|
|
* Boot-stable values are NOT accessed through this service; inject their
|
|
* registerAs token instead (see tokens.ts).
|
|
*/
|
|
@Injectable()
|
|
export class RuntimeEnvService {
|
|
/** The full live-derived env — prefer the convenience getters where one exists. */
|
|
env(): AppEnv {
|
|
return readEnv();
|
|
}
|
|
|
|
isDemoMode(): boolean {
|
|
return readEnv().demo.enabled;
|
|
}
|
|
|
|
/**
|
|
* True when somebody other than this instance's admin owns its configuration.
|
|
* Runtime-toggled like DEMO_MODE, so never cache the result in a field.
|
|
*/
|
|
isManaged(): boolean {
|
|
return readEnv().managed.enabled;
|
|
}
|
|
|
|
/** True only under exactly NODE_ENV='test' (case-sensitive, like the :memory: DB pick). */
|
|
isTest(): boolean {
|
|
return readEnv().app.isTest;
|
|
}
|
|
|
|
nodeEnv(): string | undefined {
|
|
return readEnv().app.nodeEnv;
|
|
}
|
|
|
|
isProduction(): boolean {
|
|
return readEnv().app.isProduction;
|
|
}
|
|
|
|
isDevelopment(): boolean {
|
|
return readEnv().app.isDevelopment;
|
|
}
|
|
|
|
/** Raw APP_VERSION — call sites keep their own fallback ('0.0.0' vs package.json vs semver). */
|
|
appVersion(): string | undefined {
|
|
return readEnv().app.appVersion;
|
|
}
|
|
|
|
/** Raw APP_URL — call sites keep their own trailing-slash handling. */
|
|
appUrl(): string | undefined {
|
|
return readEnv().app.appUrl;
|
|
}
|
|
}
|