Files
sencho/backend/src/__tests__/security-pre-deploy-scan-advisory-route.test.ts
T
Anso 7ce045accb feat: pre-deploy scan visibility and pinned scanner version (#1378)
* feat: pre-deploy scan visibility and pinned scanner version

Pin managed Trivy installs and add an opt-in pre-deploy scan advisory so a
manual deploy can surface each image's latest scan before it runs.

- Managed Trivy now installs a pinned, known-good version by default for
  reproducible installs. Auto-update still tracks the latest release, and an
  explicit update always pulls the latest.
- Add an opt-in pre-deploy scan advisory: when enabled, deploying a stack from
  the editor first shows each image's latest cached scan severity for review.
  It is visibility only and never blocks; deploy enforcement is unchanged.
- Backend: pre_deploy_scan_advisory setting, PUT
  /security/pre-deploy-scan-advisory, a cache-only GET
  /security/stacks/:name/pre-deploy-summary, and a node-scoped
  getLatestVulnScanByDigestForNode lookup.
- Frontend: advisory toggle on the Security page scanner setup, and a
  PreDeployScanDialog wired into the editor deploy flow that fails open when the
  summary is unavailable.
- Docs: scanner configuration, version pinning, and the advisory.

* fix: harden pre-deploy advisory guard, toggle visibility, and installer busy state

Addresses review findings on the pre-deploy advisory.

- Block a second editor deploy during the async advisory window with a
  synchronous pending ref, cleared on cancel and in the deploy's finally, so a
  double-click can no longer start two deploys.
- Keep the pre-deploy advisory toggle visible to admins whenever the setting is
  on, so it can still be turned off after the scanner becomes unavailable.
- Resolve the managed Trivy version inside the install lock so the busy state and
  serialization cover the latest-version fetch and the managed-install check.
2026-06-16 00:42:33 -04:00

97 lines
3.8 KiB
TypeScript

/**
* PUT /api/security/pre-deploy-scan-advisory.
*
* Flips the per-instance `pre_deploy_scan_advisory` setting that drives the
* manual-deploy scan advisory. Visibility only, so it is admin-gated but carries
* no tier gate: reachable by any admin on Community as well as Admiral. The
* value is reflected back on GET /trivy-status.
*/
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
import request from 'supertest';
import bcrypt from 'bcrypt';
import { setupTestDb, cleanupTestDb, loginAsTestAdmin } from './helpers/setupTestDb';
let tmpDir: string;
let app: import('express').Express;
let DatabaseService: typeof import('../services/DatabaseService').DatabaseService;
let adminCookie: string;
let viewerCookie: string;
beforeAll(async () => {
tmpDir = await setupTestDb();
({ DatabaseService } = await import('../services/DatabaseService'));
const { LicenseService } = await import('../services/LicenseService');
vi.spyOn(LicenseService.getInstance(), 'getTier').mockReturnValue('paid');
({ app } = await import('../index'));
adminCookie = await loginAsTestAdmin(app);
const viewerHash = await bcrypt.hash('viewerpass4', 1);
DatabaseService.getInstance().addUser({ username: 'advisory-viewer', password_hash: viewerHash, role: 'viewer' });
const viewerRes = await request(app).post('/api/auth/login').send({ username: 'advisory-viewer', password: 'viewerpass4' });
const cookies = viewerRes.headers['set-cookie'] as string | string[];
viewerCookie = Array.isArray(cookies) ? cookies[0] : cookies;
});
afterAll(() => cleanupTestDb(tmpDir));
describe('PUT /api/security/pre-deploy-scan-advisory', () => {
it('rejects unauthenticated requests with 401', async () => {
const res = await request(app).put('/api/security/pre-deploy-scan-advisory').send({ enabled: true });
expect(res.status).toBe(401);
});
it('rejects an authenticated viewer with 403', async () => {
const res = await request(app)
.put('/api/security/pre-deploy-scan-advisory')
.set('Cookie', viewerCookie)
.send({ enabled: true });
expect(res.status).toBe(403);
});
it('rejects a non-boolean enabled with 400', async () => {
const res = await request(app)
.put('/api/security/pre-deploy-scan-advisory')
.set('Cookie', adminCookie)
.send({ enabled: 'yes' });
expect(res.status).toBe(400);
});
it('accepts a Community admin (no tier gate) and persists the setting', async () => {
const { LicenseService } = await import('../services/LicenseService');
const spy = vi.spyOn(LicenseService.getInstance(), 'getTier').mockReturnValue('community');
try {
const res = await request(app)
.put('/api/security/pre-deploy-scan-advisory')
.set('Cookie', adminCookie)
.send({ enabled: true });
expect(res.status).toBe(200);
expect(res.body.preDeployScanAdvisory).toBe(true);
expect(DatabaseService.getInstance().getGlobalSettings().pre_deploy_scan_advisory).toBe('1');
} finally {
spy.mockReturnValue('paid');
}
});
it('reflects the persisted value on GET /trivy-status', async () => {
await request(app)
.put('/api/security/pre-deploy-scan-advisory')
.set('Cookie', adminCookie)
.send({ enabled: true });
const res = await request(app).get('/api/security/trivy-status').set('Cookie', adminCookie);
expect(res.status).toBe(200);
expect(res.body.preDeployScanAdvisory).toBe(true);
});
it('disables the setting', async () => {
const res = await request(app)
.put('/api/security/pre-deploy-scan-advisory')
.set('Cookie', adminCookie)
.send({ enabled: false });
expect(res.status).toBe(200);
expect(res.body.preDeployScanAdvisory).toBe(false);
expect(DatabaseService.getInstance().getGlobalSettings().pre_deploy_scan_advisory).toBe('0');
});
});