Files
waggle-os/packages/server/tests/local/phase2-traversal-workspace-context.test.ts
shumeiko.it e9ed8c0765 refactor(server): read workspace state, briefings and workspaces from the memory contract (P1.5)
- Workspace state, the "Workspace Now" block, the home briefing and the
  proactive handlers read the workspace's memory through server.memory:
  decisions are memories of kind 'decision', critical ones or ones worded
  as decisions; summaries list memories most important first. They no
  longer open workspace .mind files, so the builders are async and
  ProactiveContext carries `memory` instead of `getWorkspaceMindDb`.
- The home briefing counts extracted personal facts that are neither
  confirmed nor archived as awaiting review (memory-status.ts).
- Workspace routes count memories through the contract, seed template
  starter memories with remember() in the background (origin
  'workspace-template'), export memories through list(), and deleting a
  workspace forgets its memory scope and clears its awareness items before
  the directory is removed; the delete is refused if forgetting fails.
  agentState.releaseWorkspace() replaces closeWorkspaceMind for callers.
- The command palette searches personal memory and the active workspace's
  memory (UF-033).
- chat.ts drops the MindDB-flake diagnostics; the mind cache they guarded
  no longer serves chat turns.
2026-10-24 13:19:06 +02:00

80 lines
2.9 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import os from 'node:os';
import fs from 'node:fs';
import path from 'node:path';
import { WaggleDB } from '@waggle/core';
import { createInMemoryMemoryStore } from '@waggle/memory-contract';
import { buildWorkspaceNowBlock } from '../../src/local/routes/workspace-context.js';
/**
* R6-006 path-traversal regression.
*
* `buildWorkspaceNowBlock` builds session-directory paths from an unvalidated
* `workspaceId` (dataDir/workspaces/<workspaceId>/sessions),
* enabling an existence/count probe outside the workspaces root. A guard
* (assertSafeSegment) now runs at the entry of the helper and THROWS a
* { statusCode: 400 } error before the value touches the filesystem; the
* calling route handlers propagate that to Fastify's default error handler.
*
* These are unit tests against the exported helper directly (a full route
* inject is impractical — the route needs heavy memory/wsManager decorators).
*/
describe('R6-006 workspace-context path traversal', () => {
// A throwaway data dir; the guard fires before this is ever read for the
// malicious cases, and the valid case short-circuits on a null wsManager.get.
const tmpDataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'wctx-traversal-'));
const waggleDb = new WaggleDB(':memory:');
const maliciousIds = ['../evil', '..%2f..', '../../etc', 'ws/../../secret', '..\\evil'];
for (const bad of maliciousIds) {
it(`rejects traversal workspaceId ${JSON.stringify(bad)} with a 400 error before any fs access`, async () => {
const wsManager = {
get: vi.fn(() => ({ id: bad, name: 'evil' })),
};
const memory = createInMemoryMemoryStore();
const count = vi.spyOn(memory, 'count');
let thrown: unknown;
try {
await buildWorkspaceNowBlock({
dataDir: tmpDataDir,
workspaceId: bad,
wsManager,
waggleDb,
memory,
});
} catch (e) {
thrown = e;
}
expect(thrown).toBeInstanceOf(Error);
expect((thrown as { statusCode?: number }).statusCode).toBe(400);
// Guard short-circuits BEFORE the workspace and its memory are touched.
expect(wsManager.get).not.toHaveBeenCalled();
expect(count).not.toHaveBeenCalled();
});
}
it('does NOT reject a normal valid workspaceId', async () => {
const validId = 'workspace-123_AB';
const wsManager = {
// Return null so the helper cleanly returns null after the guard passes —
// proving the guard did not reject a valid segment.
get: vi.fn(() => null),
};
const result = await buildWorkspaceNowBlock({
dataDir: tmpDataDir,
workspaceId: validId,
wsManager,
waggleDb,
memory: createInMemoryMemoryStore(),
});
// Guard passed, so execution proceeded to the (mocked) wsManager.get.
expect(wsManager.get).toHaveBeenCalledWith(validId);
expect(result).toBeNull();
});
});