Files
sencho/backend/src/__tests__/effective-anatomy.test.ts
T
Anso ba09e6f69e fix: base Git multi-file Compose deploy env and dossier on the effective config (#1391)
* fix: resolve the root .env at deploy and render time for Git context-dir stacks

A Git multi-file source with a context dir set --project-directory to that
dir, so Docker Compose looked for .env there and missed the root .env Sencho
writes. Validation already passed the root .env with --env-file, so a stack
could validate with one effective config but deploy or render another.

Add authoredComposeEnvFileArgs, which appends --env-file <stackDir>/.env when
the applied deploy spec has a context dir and a root .env exists, and wire it
into the deploy/update, image-scan, render, and container-listing compose
invocations so they all resolve env from the same file the validator used. A
non-ENOENT access error surfaces instead of silently dropping the flag.

* fix: base multi-file Git dossier and doc-drift on the effective Compose model

The Stack Dossier and its documentation-drift check parsed only the stored root
compose file. For a multi-file Git source, services, ports, networks, or volumes
that an override file adds were invisible, so the dossier showed incomplete facts
and doc-drift could falsely warn that a documented port is unpublished when an
override actually publishes it.

Add a secret-safe GET /stacks/:name/effective-anatomy that renders the merged
effective model and extracts only structural facts (services, ports, volumes,
networks, restart), never env, label, or command values. StackAnatomyPanel
fetches it for multi-file Git stacks and feeds those facts into the dossier and
doc-drift, falling back to the root-only parse for single-file or non-git stacks
and whenever the render is unavailable.

* fix: add an inline path-injection barrier to the Git env-file resolver

CodeQL js/path-injection flagged the fs.access in authoredComposeEnvFileArgs
because the env path derives from the route-supplied stack name and the only
containment check lived in the callers, not at the sink. Resolve the stack dir
against the compose base and assert containment with startsWith inline, then
derive the .env path from the validated dir, mirroring the existing inline guards
in renderConfig and validateCompose. Valid stack names are unaffected; a name
that escapes the base now yields no --env-file.

* test: stabilize the dossier doc-drift e2e against the dossier-load race

The first assertion filled the access_urls field as soon as the Dossier panel
was visible, but the panel's GET /stacks/:name/dossier resolves by overwriting
the fields from the server (empty access_urls) and only then flips the doc-drift
gate on. When the GET landed after the fill, it clobbered the typed value and the
warning never rendered, so the test failed intermittently under CI timing. Wait
for that GET to land before typing, mirroring the spec's openStack helper.
2026-06-18 18:25:58 -04:00

117 lines
4.3 KiB
TypeScript

/**
* Unit tests for parseEffectiveAnatomy: the secret-safe structural extractor that
* maps `docker compose config --format json` (the fully-merged effective model)
* to the same anatomy facts the frontend derives from a single compose file, so a
* multi-file Git source's dossier and doc-drift reflect every override file.
*
* The extractor must read ONLY structural fields (service keys, ports, volumes,
* restart, network keys) and never an environment, label, or command value.
*/
import { describe, it, expect } from 'vitest';
import { parseEffectiveAnatomy } from '../services/effectiveAnatomy';
describe('parseEffectiveAnatomy', () => {
it('returns an empty model for null / non-object input', () => {
const empty = { services: [], ports: {}, volumes: {}, restart: null, networks: [] };
expect(parseEffectiveAnatomy(null)).toEqual(empty);
expect(parseEffectiveAnatomy('nope')).toEqual(empty);
expect(parseEffectiveAnatomy(42)).toEqual(empty);
});
it('extracts services, published ports, and volumes from the long-form render', () => {
const rendered = {
name: 'demo',
services: {
web: {
image: 'nginx',
restart: 'always',
ports: [
{ mode: 'ingress', host_ip: '0.0.0.0', target: 80, published: '8080', protocol: 'tcp' },
],
volumes: [
{ type: 'bind', source: '/srv/web', target: '/usr/share/nginx/html' },
{ type: 'volume', source: 'webdata', target: '/var/cache' },
],
networks: { default: null },
},
},
networks: { default: { name: 'demo_default' } },
};
const anatomy = parseEffectiveAnatomy(rendered);
expect(anatomy.services).toEqual(['web']);
expect(anatomy.ports).toEqual({
web: [{ host: '8080', container: '80', proto: 'tcp', published: true }],
});
expect(anatomy.volumes).toEqual({
web: [
{ host: '/srv/web', container: '/usr/share/nginx/html' },
{ host: 'webdata', container: '/var/cache' },
],
});
expect(anatomy.restart).toBe('always');
expect(anatomy.networks).toEqual(['default']);
});
it('merges ports that only an override file publishes (the blocker case)', () => {
// The root file declared `app` with no ports; an override published 9000.
// The rendered model is the merge, so the published port must appear here.
const rendered = {
services: {
app: {
ports: [{ target: 9000, published: '9000', protocol: 'tcp' }],
},
},
};
const anatomy = parseEffectiveAnatomy(rendered);
expect(anatomy.ports.app).toEqual([{ host: '9000', container: '9000', proto: 'tcp', published: true }]);
});
it('marks a container-only port as unpublished and preserves UDP', () => {
const rendered = {
services: {
svc: {
ports: [
{ target: 53, published: '53', protocol: 'udp' },
{ target: 9090, published: '', protocol: 'tcp' },
],
},
},
};
const anatomy = parseEffectiveAnatomy(rendered);
expect(anatomy.ports.svc).toEqual([
{ host: '53', container: '53', proto: 'udp', published: true },
{ host: '', container: '9090', proto: 'tcp', published: false },
]);
});
it('collects network keys from services and the top level, deduped', () => {
const rendered = {
services: {
a: { networks: { frontend: null, backend: null } },
b: { networks: ['backend'] },
},
networks: { frontend: {}, backend: {}, default: {} },
};
const anatomy = parseEffectiveAnatomy(rendered);
expect(anatomy.networks).toEqual(['frontend', 'backend', 'default']);
});
it('never surfaces environment, label, or command values', () => {
const rendered = {
services: {
web: {
environment: { DB_PASSWORD: 'super-secret', API_KEY: 'leak-me' },
labels: { 'traefik.http.routers.web.rule': 'Host(`secret.example.com`)' },
command: ['--token', 'do-not-leak'],
ports: [{ target: 80, published: '80', protocol: 'tcp' }],
},
},
};
const serialized = JSON.stringify(parseEffectiveAnatomy(rendered));
expect(serialized).not.toContain('super-secret');
expect(serialized).not.toContain('leak-me');
expect(serialized).not.toContain('do-not-leak');
expect(serialized).not.toContain('secret.example.com');
});
});