Files
sencho/backend/src/__tests__/hardened-entitlement.test.ts
T
Anso 381ed2a91f feat: add Admiral Hardened Build channel and business assurance surfaces (#1629)
* feat: add Admiral Hardened Build channel and business assurance surfaces

Introduce Studio Saelix entitlement-backed Hardened Build switching, a
single-flight image operation coordinator, Recovery Vault naming, Admiral
Account settings, and typed Fleet update failures while preserving Community
custom-repo and targetless pull-current updates.

* fix: harden image-op paths and clear CI CodeQL/pilot flake

Validate operation IDs before filesystem use, use hostname checks in Fleet
fetch mocks, sanitize registry probe logs, and swallow expected TCP teardown
errors in the pilot reverse-route post-handshake test.

* fix: sanitize image-op docker config write and probe logs

Allowlist-copy registry host keys and base64 auth before writing the
temp DOCKER_CONFIG, and log registry probe failures with a fixed message
so CodeQL no longer flags network-to-file and log-injection mediums.

* fix: address Admiral Hardened Build audit blockers

Expose imageChannel so hardened Fleet peers still POST for typed rejection, claim community updates before 202, terminalize helper failures, gate Hardened on paid, and align support/docs/e2e wording.

* fix: terminalize image ops on helper survival and aborted claims

* fix: prevent recreating persist from overwriting helper-exit failure

* test: assert helper-exit failure lands before recreating persist

* fix: keep current pointer when acknowledging a stale image operation
2026-07-14 10:47:54 -04:00

135 lines
4.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
const { mockGetSystemState, mockPost, mockGetTier } = vi.hoisted(() => ({
mockGetSystemState: vi.fn(),
mockPost: vi.fn(),
mockGetTier: vi.fn((): 'paid' | 'community' => 'paid'),
}));
vi.mock('../services/DatabaseService', () => ({
DatabaseService: {
getInstance: () => ({
getSystemState: mockGetSystemState,
}),
},
}));
vi.mock('../services/LicenseService', () => ({
LicenseService: {
getInstance: () => ({
getTier: mockGetTier,
}),
},
}));
vi.mock('axios', () => ({
default: {
post: mockPost,
isAxiosError: vi.fn(),
},
}));
import { validateAllowedImageRefAgainstRequirement } from '../helpers/allowedImageRef';
import { HardenedEntitlementService } from '../services/HardenedEntitlementService';
const registryRequirement = {
registry_host: 'ghcr.io',
package_scope: 'studio-saelix/sencho-hardened',
credential_instructions: 'Create a pull token.',
supports_pull_token: true,
};
const entitlementFixture = {
hardened_build_access: true,
channel: 'hardened',
allowed_image_ref: 'ghcr.io/studio-saelix/sencho-hardened@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
pin_recommendation: 'Use the supplied digest.',
registry_requirement: registryRequirement,
checked_at: '2026-07-13T12:00:00.000Z',
};
beforeEach(() => {
vi.clearAllMocks();
HardenedEntitlementService.getInstance().invalidateCache();
mockGetTier.mockReturnValue('paid');
mockGetSystemState.mockImplementation((key: string) => ({
license_key: 'license-for-test',
instance_id: 'instance-for-test',
})[key] ?? '');
delete process.env.SENCHO_ASSURANCE_ENTITLEMENT_STUB;
});
describe('allowed Hardened image references', () => {
it('rejects a bare digest', () => {
expect(validateAllowedImageRefAgainstRequirement(
'sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
registryRequirement,
)).toBe(false);
});
it('rejects an image outside the entitled package scope', () => {
expect(validateAllowedImageRefAgainstRequirement(
'ghcr.io/studio-saelix/other@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
registryRequirement,
)).toBe(false);
});
});
describe('HardenedEntitlementService', () => {
it('rejects entitlement checks when the tier is not paid', async () => {
mockGetTier.mockReturnValue('community');
process.env.SENCHO_ASSURANCE_ENTITLEMENT_STUB = 'entitled';
await expect(HardenedEntitlementService.getInstance().getEntitlement('status'))
.resolves.toEqual({ success: false, code: 'unauthorized' });
});
it('ignores the entitlement stub in production', async () => {
const previous = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
process.env.SENCHO_ASSURANCE_ENTITLEMENT_STUB = 'entitled';
mockPost.mockResolvedValue({ status: 200, data: entitlementFixture });
try {
await expect(HardenedEntitlementService.getInstance().getEntitlement('status'))
.resolves.toMatchObject({ success: true, entitlement: entitlementFixture });
expect(mockPost).toHaveBeenCalledTimes(1);
} finally {
process.env.NODE_ENV = previous;
}
});
it('returns the entitled stub response', async () => {
process.env.SENCHO_ASSURANCE_ENTITLEMENT_STUB = 'entitled';
await expect(HardenedEntitlementService.getInstance().getEntitlement('status'))
.resolves.toMatchObject({
success: true,
entitlement: {
...entitlementFixture,
checked_at: expect.any(String),
},
});
});
it('returns the unauthorized stub response', async () => {
process.env.SENCHO_ASSURANCE_ENTITLEMENT_STUB = 'unauthorized';
await expect(HardenedEntitlementService.getInstance().getEntitlement('status'))
.resolves.toEqual({ success: false, code: 'unauthorized' });
});
it('invalidates a cached status entitlement', async () => {
mockPost.mockResolvedValue({ status: 200, data: entitlementFixture });
const service = HardenedEntitlementService.getInstance();
await service.getEntitlement('status');
await service.getEntitlement('status');
expect(mockPost).toHaveBeenCalledTimes(1);
service.invalidateCache();
await service.getEntitlement('status');
expect(mockPost).toHaveBeenCalledTimes(2);
});
});