mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23:04 +00:00
fix(settings): make the self-hosted routing engine an instance setting
The field was rendered in every non-managed user's own Map settings and written to their personal settings row. The CSP is built from something else: bootstrap reads the ADMIN default and puts that origin into `connect-src`. So filling the field in was the one path the product offered, and it could not work — the route calculator happily built its URLs against the typed origin and the browser refused every one of them with a bare TypeError, not a status, so no fallback and no retry could see it. Day-plan routes, transport legs, the mobile day route, Studio's road route and the whole road-trip rail stopped routing, app-wide, with a generic "route could not be calculated". Nothing in the UI could repair it except emptying the field again, and there was no admin field to set the value that would have made the origin allowed. The repo has decided this shape once already: `llm_base_url` is admin-only because it names where the server sends its own requests and only whoever runs the instance can judge what is reachable. This is the same class for a different reason, so it joins the same guard, which is now named for what it actually covers. Clearing stays open to everyone, so a row written before the rule cleans itself up rather than blocking the save. The field moves to the admin's default-user settings, next to the tile URL and the Carto key, where a value takes effect.
This commit is contained in:
@@ -2,7 +2,7 @@ import { Body, Controller, Get, HttpCode, HttpException, Post, Put, Req, UseGuar
|
||||
import type { Request } from 'express';
|
||||
import { MASKED_SETTING_VALUE } from '@trek/shared';
|
||||
import type { User } from '../../types';
|
||||
import { SettingsService, isAdminOnlyLlmSetting } from './settings.service';
|
||||
import { SettingsService, isAdminOnlyEndpointSetting } from './settings.service';
|
||||
import { SettingUpsertDto, SettingsBulkDto } from './settings.dto';
|
||||
import { AdminDefaultUserSettingsDto } from '../admin/admin.dto';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
@@ -46,9 +46,9 @@ export class SettingsController {
|
||||
* behind AdminGuard. A personal row would be a second, invisible place for
|
||||
* the same value.
|
||||
*/
|
||||
private assertMayWriteLlmEndpoint(settings: Record<string, unknown>) {
|
||||
private assertMayWriteInstanceEndpoint(settings: Record<string, unknown>) {
|
||||
for (const [key, value] of Object.entries(settings)) {
|
||||
if (isAdminOnlyLlmSetting(key, value)) {
|
||||
if (isAdminOnlyEndpointSetting(key, value)) {
|
||||
throw new HttpException({ error: 'Admin access required' }, 403);
|
||||
}
|
||||
}
|
||||
@@ -61,8 +61,8 @@ export class SettingsController {
|
||||
|
||||
@Put()
|
||||
upsert(@CurrentUser() user: User, @Body() body: SettingUpsertDto) {
|
||||
this.assertMayWriteLlmEndpoint({ [body.key]: body.value });
|
||||
// assertMayWriteLlmEndpoint only covers llm_base_url and provider 'local'.
|
||||
this.assertMayWriteInstanceEndpoint({ [body.key]: body.value });
|
||||
// assertMayWriteInstanceEndpoint only covers llm_base_url and provider 'local'.
|
||||
// llm_api_key and llm_model are writable by every user, and on a managed
|
||||
// install both cost the operator money, so the key list decides here.
|
||||
if (isManagedLockedKey(body.key) && this.env.isManaged()) {
|
||||
@@ -79,7 +79,7 @@ export class SettingsController {
|
||||
@Post('bulk')
|
||||
@HttpCode(200) // Express answers bulk with res.json (200), not the POST-default 201.
|
||||
bulk(@CurrentUser() user: User, @Body() body: SettingsBulkDto) {
|
||||
this.assertMayWriteLlmEndpoint(body.settings);
|
||||
this.assertMayWriteInstanceEndpoint(body.settings);
|
||||
const { allowed, blocked } = splitManagedKeys(body.settings, this.env.isManaged());
|
||||
try {
|
||||
const updated = this.settings.bulkUpsertSettings(user.id, allowed);
|
||||
|
||||
@@ -83,8 +83,9 @@ const KEY_LIST = DISPLAY_PREFERENCE_KEYS.join(', ');
|
||||
* write carries the demo gate the other write tools carry.
|
||||
*
|
||||
* The controller's own two refusals stay out of reach by construction rather
|
||||
* than by being repeated: assertMayWriteLlmEndpoint guards llm_base_url and
|
||||
* llm_provider, and isManagedLockedKey guards the operator-owned keys, and not
|
||||
* than by being repeated: assertMayWriteInstanceEndpoint guards llm_base_url,
|
||||
* llm_provider and routing_base_url, and isManagedLockedKey guards the
|
||||
* operator-owned keys, and not
|
||||
* one name from either list is on the allow-list above. tools-settings.test.ts
|
||||
* asserts that intersection is empty instead of trusting the reading.
|
||||
*/
|
||||
|
||||
@@ -79,18 +79,30 @@ const VALID_VALUES: Partial<Record<DefaultableKey, unknown[]>> = {
|
||||
const BOOLEAN_KEYS = new Set<DefaultableKey>(['blur_booking_codes', 'mapbox_3d_enabled', 'mapbox_quality_mode', 'llm_multimodal']);
|
||||
|
||||
/**
|
||||
* #1772: per-user LLM settings a non-admin must not write. Both of them pick
|
||||
* the address this server sends its own LLM requests to (provider 'local' means
|
||||
* nothing but "an endpoint I name"), and the SSRF guard in front of those
|
||||
* requests allows loopback/LAN on purpose so a self-hosted Ollama works. Only
|
||||
* whoever runs the instance can judge what is reachable from it.
|
||||
* Per-user settings that name an address, which a non-admin must not write.
|
||||
*
|
||||
* Clearing stays open to everyone: both Settings sections always send
|
||||
* `llm_base_url: ''` while the field is hidden, so a row written before this
|
||||
* rule cleans itself up on the next save instead of blocking it.
|
||||
* #1772 for the LLM pair: both pick where this server sends its own LLM
|
||||
* requests (provider 'local' means nothing but "an endpoint I name"), and the
|
||||
* SSRF guard in front of those requests allows loopback and LAN on purpose so a
|
||||
* self-hosted Ollama works. Only whoever runs the instance can judge what is
|
||||
* reachable from it.
|
||||
*
|
||||
* `routing_base_url` (#1797) is the same class for a different reason: it names
|
||||
* an origin the BROWSER connects to, so it has to appear in the CSP
|
||||
* `connect-src` the server emits at boot — and that list is built from the admin
|
||||
* default, which is the only place a value can take effect. A row on a personal
|
||||
* settings record is read by the route calculator and blocked by the browser, so
|
||||
* the whole app stops routing with no catchable error. Refusing it here means
|
||||
* the caller finds out instead of silently breaking their own map.
|
||||
*
|
||||
* Clearing stays open to everyone: the Settings sections send an empty string
|
||||
* while the field is hidden, so a row written before this rule cleans itself up
|
||||
* on the next save instead of blocking it.
|
||||
*/
|
||||
export function isAdminOnlyLlmSetting(key: string, value: unknown): boolean {
|
||||
if (key === 'llm_base_url') return typeof value === 'string' && value.trim() !== '';
|
||||
export function isAdminOnlyEndpointSetting(key: string, value: unknown): boolean {
|
||||
if (key === 'llm_base_url' || key === 'routing_base_url') {
|
||||
return typeof value === 'string' && value.trim() !== '';
|
||||
}
|
||||
if (key === 'llm_provider') return value === 'local';
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user