mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
51dc0e6a82
A GL map fetches its tiles and its style, so a template on a host of the operator's own (a self-hosted tile server) was refused by connect-src while Leaflet drew it as images. The new variable lists such hosts; each entry becomes an https source in connect-src and the same list reaches the client as map_extra_connect_hosts in the app config, so the renderer choice and the policy can never disagree. The variable is owned by readEnv() like the rest of the maps namespace and read once when the middleware is applied. The boot refuses an entry outside the shared grammar; a path that skips the boot check drops it, so a bad value never widens the policy. Unset, the CSP and the app config are as before apart from an empty list. Documented in .env.example, docker-compose.yml, the Helm values and ConfigMap and the wiki. Making room for the app config field shrinks auth.service.ts by one line, and its size baseline follows.
60 lines
2.7 KiB
TypeScript
60 lines
2.7 KiB
TypeScript
/**
|
|
* MAP_EXTRA_CONNECT_HOSTS end to end, on the whole app: the operator's own map
|
|
* hosts reach the Content-Security-Policy the server sends and the app config
|
|
* the client reads, as one list, so a GL map is only asked to draw what the
|
|
* browser lets it fetch. Without the variable both stay as they were.
|
|
*/
|
|
import { buildApp } from '../../src/bootstrap';
|
|
import type { INestApplication } from '@nestjs/common';
|
|
import { mapExtraConnectHostsSchema } from '@trek/shared';
|
|
|
|
import request from 'supertest';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
let app: INestApplication | undefined;
|
|
|
|
afterEach(async () => {
|
|
await app?.close();
|
|
app = undefined;
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
/** The connect-src sources and the app config of an app built with `hosts` as MAP_EXTRA_CONNECT_HOSTS. */
|
|
async function bootWith(hosts: string) {
|
|
vi.stubEnv('MAP_EXTRA_CONNECT_HOSTS', hosts);
|
|
app = await buildApp();
|
|
const res = await request(app.getHttpServer()).get('/api/auth/app-config');
|
|
const connectSrc =
|
|
String(res.headers['content-security-policy'] ?? '')
|
|
.split(';')
|
|
.map((directive) => directive.trim())
|
|
.find((directive) => directive.startsWith('connect-src'))
|
|
?.split(/\s+/)
|
|
.slice(1) ?? [];
|
|
return { status: res.status, config: res.body as Record<string, unknown>, connectSrc };
|
|
}
|
|
|
|
describe('the operator map hosts', () => {
|
|
it('MAPHOST-E2E-001: unset, the app config lists none and the policy names only the shipped hosts', async () => {
|
|
const { status, config, connectSrc } = await bootWith('');
|
|
expect(status).toBe(200);
|
|
expect(config.map_extra_connect_hosts).toEqual([]);
|
|
expect(connectSrc).toContain('https://tiles.openfreemap.org');
|
|
expect(connectSrc.some((source) => source.includes('example'))).toBe(false);
|
|
});
|
|
|
|
it('MAPHOST-E2E-002: set, both carry the same hosts, the config in the shared contract', async () => {
|
|
const { config, connectSrc } = await bootWith('Tiles.Example.com, *.maps.example.org:8443');
|
|
const hosts = mapExtraConnectHostsSchema.parse(config.map_extra_connect_hosts);
|
|
expect(hosts).toEqual(['tiles.example.com', '*.maps.example.org:8443']);
|
|
for (const host of hosts) expect(connectSrc).toContain(`https://${host}`);
|
|
expect(connectSrc).toContain('https://tiles.openfreemap.org');
|
|
});
|
|
|
|
it('MAPHOST-E2E-003: an entry the boot would refuse reaches neither the policy nor the client', async () => {
|
|
const { config, connectSrc } = await bootWith('tiles.example.com,https://evil.example.com,*.com');
|
|
expect(config.map_extra_connect_hosts).toEqual(['tiles.example.com']);
|
|
expect(connectSrc.filter((source) => source.includes('evil') || source === 'https://*.com')).toEqual([]);
|
|
});
|
|
});
|