Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ PLANNING_SERVICE_ORIGIN=http://127.0.0.1:4102
PLANNING_GATEWAY_CONTEXT_SECRET=replace-with-at-least-32-random-bytes
HABIT_SERVICE_ORIGIN=http://127.0.0.1:4103
HABIT_GATEWAY_CONTEXT_SECRET=replace-with-at-least-32-random-bytes
HABIT_DATA_RIGHTS_CONTEXT_SECRET=replace-with-distinct-at-least-32-random-bytes
AI_SERVICE_ORIGIN=http://127.0.0.1:4105
AI_GATEWAY_ACTIVE_KEY_ID=gateway-2026-08-a
AI_GATEWAY_ACTIVE_KEY_SECRET=replace-with-at-least-32-random-bytes
Expand Down
114 changes: 114 additions & 0 deletions apps/habit-service/src/habit-data-rights-controller-authority.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { createHmac, randomBytes } from 'node:crypto';
import { HttpException } from '@nestjs/common';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { HabitRuntime } from './habit-runtime';
import { HabitDataRightsController } from './main';

const WORKSPACE_ID = '11111111-1111-4111-8111-111111111111';
const USER_ID = '22222222-2222-4222-8222-222222222222';
const REQUEST_ID = '33333333-3333-4333-8333-333333333333';
const CONTEXT_SECRET = randomBytes(32).toString('base64url');
const REQUEST = {
contractVersion: 'life-os.data-rights-contributor.v1',
operation: 'export',
workspaceId: WORKSPACE_ID,
requestedByUserId: USER_ID,
requestId: REQUEST_ID,
} as const;

function signature(issuedAt: string): string {
return createHmac('sha256', CONTEXT_SECRET)
.update(
[
'life-os.habit-data-rights-context.v1',
REQUEST.contractVersion,
REQUEST.workspaceId,
REQUEST.requestedByUserId,
REQUEST.requestId,
REQUEST.operation,
'-',
issuedAt,
'POST',
'/v1/internal/data-rights/contributor',
].join('\n'),
'utf8',
)
.digest('base64url');
}

function controllerWith(handle: ReturnType<typeof vi.fn>): HabitDataRightsController {
return new HabitDataRightsController({
dataRightsContributor: { handle },
} as unknown as HabitRuntime);
}

async function rejectedStatus(operation: Promise<unknown>): Promise<number> {
try {
await operation;
} catch (error) {
expect(error).toBeInstanceOf(HttpException);
return (error as HttpException).getStatus();
}
throw new Error('Expected Habit data-rights transport rejection');
}

afterEach(() => {
delete process.env.HABIT_DATA_RIGHTS_CONTEXT_SECRET;
vi.restoreAllMocks();
});

describe.sequential('HabitDataRightsController authority contract', () => {
it('passes only a verified exact request to the service-owned contributor', async () => {
process.env.HABIT_DATA_RIGHTS_CONTEXT_SECRET = CONTEXT_SECRET;
const issuedAt = String(Math.floor(Date.now() / 1000));
const response = {
contractVersion: REQUEST.contractVersion,
operation: 'export',
contributor: 'habit.service',
requestId: REQUEST_ID,
schemaVersion: 'habit.data-rights.v1',
recordCount: 0,
sha256: '0'.repeat(64),
data: { habits: [], completionEvents: [] },
} as const;
const handle = vi.fn().mockResolvedValue(response);
const controller = controllerWith(handle);

await expect(
controller.contribute(issuedAt, signature(issuedAt), REQUEST),
).resolves.toEqual(response);
expect(handle).toHaveBeenCalledTimes(1);
expect(handle).toHaveBeenCalledWith(REQUEST);
});

it('fails closed before persistence when the service signature is forged', async () => {
process.env.HABIT_DATA_RIGHTS_CONTEXT_SECRET = CONTEXT_SECRET;
const issuedAt = String(Math.floor(Date.now() / 1000));
const handle = vi.fn();
const controller = controllerWith(handle);

expect(
await rejectedStatus(
controller.contribute(
issuedAt,
randomBytes(32).toString('base64url'),
REQUEST,
),
),
).toBe(401);
expect(handle).not.toHaveBeenCalled();
});

it('fails closed when the dedicated data-rights trust secret is absent', async () => {
const issuedAt = String(Math.floor(Date.now() / 1000));
const handle = vi.fn();
const controller = controllerWith(handle);

expect(
await rejectedStatus(
controller.contribute(issuedAt, signature(issuedAt), REQUEST),
),
).toBe(503);
expect(handle).not.toHaveBeenCalled();
});
});
200 changes: 200 additions & 0 deletions apps/habit-service/src/habit-data-rights-http-boundary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import { createHmac, randomBytes } from 'node:crypto';
import { HttpException } from '@nestjs/common';
import { describe, expect, it } from 'vitest';
import {
parseTrustedHabitDataRightsRequest,
type HabitDataRightsRequestBinding,
} from './habit-data-rights-http-boundary';

const WORKSPACE_ID = '11111111-1111-4111-8111-111111111111';
const USER_ID = '22222222-2222-4222-8222-222222222222';
const REQUEST_ID = '33333333-3333-4333-8333-333333333333';
const IDEMPOTENCY_KEY = '44444444-4444-4444-8444-444444444444';
const CONTEXT_SECRET = randomBytes(32).toString('base64url');
const NOW_SECONDS = 1_785_806_400;
const BINDING = {
method: 'POST',
path: '/v1/internal/data-rights/contributor',
} as const;

const EXPORT_REQUEST = {
contractVersion: 'life-os.data-rights-contributor.v1',
operation: 'export',
workspaceId: WORKSPACE_ID,
requestedByUserId: USER_ID,
requestId: REQUEST_ID,
} as const;

function sign(
request: Readonly<Record<string, unknown>>,
binding: HabitDataRightsRequestBinding = BINDING,
issuedAt = String(NOW_SECONDS),
): string {
const operation = String(request.operation);
const idempotencyKey =
operation === 'erase' ? String(request.idempotencyKey) : '-';
return createHmac('sha256', CONTEXT_SECRET)
.update(
[
'life-os.habit-data-rights-context.v1',
request.contractVersion,
request.workspaceId,
request.requestedByUserId,
request.requestId,
operation,
idempotencyKey,
issuedAt,
binding.method,
binding.path,
].join('\n'),
'utf8',
)
.digest('base64url');
}

function expectHttpStatus(operation: () => unknown, status: number): void {
let thrown: unknown;
try {
operation();
} catch (error) {
thrown = error;
}
if (thrown === undefined) {
throw new Error(`Expected HTTP ${status} rejection`);
}
expect(thrown).toBeInstanceOf(HttpException);
expect((thrown as HttpException).getStatus()).toBe(status);
}

describe('Habit data-rights trusted HTTP boundary', () => {
it('accepts an exact short-lived service-authenticated export request', () => {
const issuedAt = String(NOW_SECONDS);
expect(
parseTrustedHabitDataRightsRequest(
EXPORT_REQUEST,
{ issuedAt, signature: sign(EXPORT_REQUEST, BINDING, issuedAt) },
CONTEXT_SECRET,
BINDING,
NOW_SECONDS,
),
).toEqual(EXPORT_REQUEST);
});

it('rejects replay of an export signature onto an erase request', () => {
const eraseRequest = {
...EXPORT_REQUEST,
operation: 'erase',
idempotencyKey: IDEMPOTENCY_KEY,
} as const;
const issuedAt = String(NOW_SECONDS);

expectHttpStatus(
() =>
parseTrustedHabitDataRightsRequest(
eraseRequest,
{ issuedAt, signature: sign(EXPORT_REQUEST, BINDING, issuedAt) },
CONTEXT_SECRET,
BINDING,
NOW_SECONDS,
),
401,
);
});

it('binds destructive authority to the exact idempotency key', () => {
const eraseRequest = {
...EXPORT_REQUEST,
operation: 'erase',
idempotencyKey: IDEMPOTENCY_KEY,
} as const;
const tamperedRequest = {
...eraseRequest,
idempotencyKey: '55555555-5555-4555-8555-555555555555',
} as const;
const issuedAt = String(NOW_SECONDS);

expectHttpStatus(
() =>
parseTrustedHabitDataRightsRequest(
tamperedRequest,
{ issuedAt, signature: sign(eraseRequest, BINDING, issuedAt) },
CONTEXT_SECRET,
BINDING,
NOW_SECONDS,
),
401,
);
});

it('rejects a valid signature replayed to another method or path', () => {
const issuedAt = String(NOW_SECONDS);
const signature = sign(EXPORT_REQUEST, BINDING, issuedAt);

for (const binding of [
{ method: 'GET', path: BINDING.path },
{ method: 'POST', path: '/v1/internal/data-rights/other' },
]) {
expectHttpStatus(
() =>
parseTrustedHabitDataRightsRequest(
EXPORT_REQUEST,
{ issuedAt, signature },
CONTEXT_SECRET,
binding,
NOW_SECONDS,
),
401,
);
}
});

it('rejects stale authority, malformed UUIDs, extra fields, and missing secrets', () => {
const issuedAt = String(NOW_SECONDS);
const signature = sign(EXPORT_REQUEST, BINDING, issuedAt);

expectHttpStatus(
() =>
parseTrustedHabitDataRightsRequest(
EXPORT_REQUEST,
{ issuedAt: String(NOW_SECONDS - 61), signature },
CONTEXT_SECRET,
BINDING,
NOW_SECONDS,
),
401,
);
expectHttpStatus(
() =>
parseTrustedHabitDataRightsRequest(
{ ...EXPORT_REQUEST, workspaceId: 'workspace-one' },
{ issuedAt, signature },
CONTEXT_SECRET,
BINDING,
NOW_SECONDS,
),
400,
);
expectHttpStatus(
() =>
parseTrustedHabitDataRightsRequest(
{ ...EXPORT_REQUEST, unexpected: true },
{ issuedAt, signature },
CONTEXT_SECRET,
BINDING,
NOW_SECONDS,
),
400,
);
expectHttpStatus(
() =>
parseTrustedHabitDataRightsRequest(
EXPORT_REQUEST,
{ issuedAt, signature },
undefined,
BINDING,
NOW_SECONDS,
),
503,
);
});
});
Loading
Loading