wishforge-ide/packages/runtime/test/application-access.test.ts
eust-w 20bc7cfeb7 refactor: remove the private application gateway and its proof chain
Environments defaulted to "private" visibility, which required a
separate per-environment preview gateway, capability tokens, signed
anonymous/authorized probe proofs and API-side proof verification;
without that stack every deployment was blocked. Access control for a
deployed application is now the target environment's responsibility
(docs/requirements.md FR-G6).

- Delete apps/preview-gateway, the API preview controllers/domain
  (previews, private-access-verifier) and runtime private-access.
- Environments keep an optional application URL; production releases
  still require one plus a fresh access check reported by the Runner.
  The URL may never share an origin with the console, API or IDE.
- Release bindings, runner task payloads, deployment evidence and
  rollback compatibility no longer carry visibility or private-access
  policy digests. PATCH /v1/environments/:id/access becomes
  /v1/environments/:id/application-url.
- Web: the delivery launch route is gone; environments configure an
  application URL only (the always-rejected "Runner ID" field is
  removed); releases and run detail link the live application URL.
- Remove the preview-gateway image target and test:preview script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 13:48:49 +08:00

120 lines
5.3 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { normalizeApplicationUrl, matchesApplicationAccess } from '@wishforge/contracts';
import { ComposeDeploymentAdapter, KubernetesDeploymentAdapter, type ReleaseManifest } from '../src/index.js';
import { verifyHttp, validateRelease, deliveryConfiguration } from '../src/deploy.js';
const release: ReleaseManifest = {
schemaVersion: 1,
id: 'release',
tenantId: 'tenant',
environmentId: 'env',
sourceCommit: 'a'.repeat(40),
image: `registry.example/web@sha256:${'b'.repeat(64)}`,
containerPort: 3000,
healthPath: '/health',
smokeChecks: [{ path: '/catalog', expectedStatus: 200, bodyIncludes: 'record' }],
expectedPreviousReleaseId: null,
applicationUrl: 'https://app.example.test/catalog',
deliveryRequired: true,
};
describe('declared application URL and actual stable access acceptance', () => {
it('rejects inline credentials, query, fragment, insecure and internal service addresses', () => {
for (const value of [
'http://app.example.test',
'https://u:p@app.example.test',
'https://app.example.test/?token=x',
'https://app.example.test/#x',
'https://app.example.test/?',
'https://10.0.0.2',
'https://172.31.1.2',
'https://service.ns.svc.cluster.local',
'https://service',
'https://[::1]',
'https://app.example.test/\\evil',
])
expect(() => normalizeApplicationUrl(value)).toThrow();
expect(normalizeApplicationUrl('https://app.example.test')).toBe('https://app.example.test/');
expect(() => normalizeApplicationUrl('http://127.0.0.1:9999')).toThrow();
expect(normalizeApplicationUrl('http://127.0.0.1:9999', true)).toBe('http://127.0.0.1:9999/');
});
it('requires URL and access configuration before deployment, with no command or network side effects', async () => {
const execute = vi.fn(),
fetcher = vi.fn();
for (const adapter of [
new ComposeDeploymentAdapter({ healthUrl: 'https://app.example.test' }, execute, fetcher),
new KubernetesDeploymentAdapter({ healthUrl: 'https://app.example.test' }, execute, fetcher),
]) {
expect((await adapter.deploy({ ...release, applicationUrl: undefined }, { execute: true })).status).toBe(
'unconfigured',
);
expect((await adapter.reconcile({ ...release, applicationUrl: 'https://other.example.test/' })).status).toBe(
'unconfigured',
);
}
expect(execute).not.toHaveBeenCalled();
expect(fetcher).not.toHaveBeenCalled();
});
it('records the exact external URL only after stable health, business response and anonymous application probe pass', async () => {
const calls: string[] = [];
const fetcher: typeof fetch = async url => {
calls.push(String(url));
return new Response('record');
};
const result = await verifyHttp(release, 'https://app.example.test/', fetcher, { stable: true });
expect(result).toMatchObject({
passed: true,
deliveryUrl: release.applicationUrl,
accessEvidence: {
applicationUrl: release.applicationUrl,
stableOrigin: 'https://app.example.test',
checks: ['application-url', 'public-access'],
},
});
expect(calls).toEqual([
'https://app.example.test/health',
'https://app.example.test/catalog',
'https://app.example.test/catalog',
]);
});
it('rejects changed URL, wrong stable origin and stale or future access evidence', () => {
const evidence = {
applicationUrl: release.applicationUrl,
stableOrigin: 'https://app.example.test',
checkedAt: new Date().toISOString(),
checks: ['application-url', 'public-access'],
};
expect(matchesApplicationAccess(release, evidence)).toBe(true);
for (const change of [
{ applicationUrl: 'https://other.example.test/' },
{ stableOrigin: 'https://other.example.test' },
{ checkedAt: new Date(Date.now() - 600000).toISOString() },
{ checkedAt: new Date(Date.now() + 600000).toISOString() },
])
expect(matchesApplicationAccess(release, { ...evidence, ...change })).toBe(false);
});
it('candidate-only checks never create delivery evidence, and denied final application URL cannot be delivered', async () => {
expect(await verifyHttp(release, 'http://candidate.ns.svc/', async () => new Response('record'))).toMatchObject({
passed: true,
});
const candidate = await verifyHttp(release, 'http://candidate.ns.svc/', async () => new Response('record'));
expect(candidate).not.toHaveProperty('deliveryUrl');
let count = 0;
const final = await verifyHttp(
release,
'https://app.example.test/',
async () => (++count < 3 ? new Response('record') : new Response('', { status: 401 })),
{ stable: true },
);
expect(final.passed).toBe(false);
expect(final).not.toHaveProperty('deliveryUrl');
});
it('does not accept stable origin drift as public access', async () => {
const fetcher = vi.fn();
expect((await verifyHttp(release, 'https://other.example.test/', fetcher, { stable: true })).passed).toBe(false);
expect(fetcher).not.toHaveBeenCalled();
expect(
deliveryConfiguration({ ...release, applicationUrl: undefined, deliveryRequired: false }, undefined),
).toBeUndefined();
expect(() => validateRelease({ ...release, applicationUrl: 'https://app.example.test' })).toThrow();
});
});