@cortex-agent/server 2026.7.30 → 2026.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/defaults/CORTEX.md +1 -0
- package/defaults/config/CORTEX.md +2 -1
- package/defaults/config/hooks/13-status-md-guard.json +11 -0
- package/defaults/config/hooks/CORTEX.md +1 -0
- package/defaults/config/mcp-config-empty.json +3 -0
- package/defaults/config/thread-templates/agents/benchmark-coder.json +22 -0
- package/defaults/config/thread-templates/agents/benchmark-reviewer.json +22 -0
- package/defaults/config/thread-templates/agents/coder-reviewer.json +4 -9
- package/defaults/config/thread-templates/agents/coder.json +4 -14
- package/defaults/config/thread-templates/templates/benchmark-coder-review.json +37 -0
- package/defaults/config/thread-templates/templates/coder-review.json +3 -27
- package/defaults/context/projects/CORTEX.md +1 -1
- package/defaults/data/schedules.json +12 -0
- package/defaults/hooks/CORTEX.md +1 -0
- package/defaults/hooks/new-session-hook.mjs +2 -2
- package/defaults/hooks/status-md-guard.mjs +193 -0
- package/defaults/plugins/cortex-coder/.claude-plugin/plugin.json +1 -1
- package/defaults/plugins/cortex-coder/skills/develop/SKILL.md +28 -2
- package/defaults/plugins/cortex-common/.claude-plugin/plugin.json +1 -1
- package/defaults/plugins/cortex-common/skills/compound/SKILL.md +1 -1
- package/defaults/plugins/cortex-common/skills/compound-simple/SKILL.md +4 -2
- package/defaults/plugins/cortex-common/skills/postmortem/SKILL.md +1 -1
- package/defaults/plugins/cortex-stage-gate/.claude-plugin/plugin.json +1 -1
- package/defaults/plugins/cortex-stage-gate/skills/director-method/SKILL.md +1 -1
- package/defaults/plugins/cortex-stage-gate/skills/need-approval/SKILL.md +1 -0
- package/defaults/plugins/cortex-stage-gate/skills/reorient/SKILL.md +2 -2
- package/defaults/plugins/cortex-stage-gate/skills/task/SKILL.md +11 -8
- package/defaults/plugins/cortex-system/skills/feedback/SKILL.md +1 -1
- package/defaults/plugins/cortex-system/skills/project-init/SKILL.md +13 -16
- package/defaults/prompts/directives/coder-reviewer.md +24 -20
- package/defaults/prompts/directives/coder.md +4 -5
- package/defaults/prompts/directives/director.md +4 -10
- package/defaults/prompts/directives/manager.md +9 -10
- package/defaults/prompts/systemPrompts/worker.md +2 -2
- package/defaults/rules/cortex-md.md +31 -0
- package/defaults/rules/experiment-format.md +8 -1
- package/defaults/rules/issues-md.md +10 -9
- package/defaults/rules/meta-conventions.md +5 -4
- package/defaults/rules/mission-md.md +2 -1
- package/defaults/rules/status-md.md +39 -21
- package/dist/agent-adapter/bg-wait.d.ts +10 -12
- package/dist/agent-adapter/bg-wait.js +209 -129
- package/dist/agent-adapter/claude/adapter-tui.d.ts +3 -1
- package/dist/agent-adapter/claude/adapter-tui.js +8 -5
- package/dist/agent-adapter/claude/adapter.d.ts +43 -14
- package/dist/agent-adapter/claude/adapter.js +276 -161
- package/dist/agent-adapter/claude/defaults.d.ts +2 -0
- package/dist/agent-adapter/claude/defaults.js +5 -5
- package/dist/agent-adapter/claude/event-parser.d.ts +3 -8
- package/dist/agent-adapter/claude/event-parser.js +28 -31
- package/dist/agent-adapter/claude/hooks-builder.js +4 -3
- package/dist/agent-adapter/claude/jsonl-tail.js +54 -35
- package/dist/agent-adapter/claude/spawn-args.d.ts +15 -12
- package/dist/agent-adapter/claude/spawn-args.js +44 -38
- package/dist/agent-adapter/claude/tmux-control.d.ts +4 -17
- package/dist/agent-adapter/claude/tmux-control.js +39 -32
- package/dist/agent-adapter/event-tee.d.ts +19 -0
- package/dist/agent-adapter/event-tee.js +145 -0
- package/dist/agent-adapter/index.d.ts +1 -0
- package/dist/agent-adapter/index.js +7 -3
- package/dist/agent-adapter/normalize/event-types.d.ts +17 -9
- package/dist/agent-adapter/normalize/event-types.js +4 -4
- package/dist/agent-adapter/pi/adapter.d.ts +16 -4
- package/dist/agent-adapter/pi/adapter.js +69 -36
- package/dist/agent-adapter/pi/discovery.d.ts +19 -3
- package/dist/agent-adapter/pi/discovery.js +81 -43
- package/dist/agent-adapter/pi/event-parser.js +4 -2
- package/dist/agent-adapter/pi/spawn-args.d.ts +2 -4
- package/dist/agent-adapter/pi/spawn-args.js +6 -5
- package/dist/agent-adapter/types.d.ts +43 -2
- package/dist/agent-adapter/types.js +8 -4
- package/dist/core/atomic-write.d.ts +6 -1
- package/dist/core/atomic-write.js +183 -7
- package/dist/core/cli-utils.d.ts +15 -2
- package/dist/core/cli-utils.js +46 -41
- package/dist/core/config-generator.d.ts +4 -0
- package/dist/core/config-generator.js +34 -21
- package/dist/core/json-repository.d.ts +4 -0
- package/dist/core/json-repository.js +6 -3
- package/dist/core/locales/en.d.ts +94 -0
- package/dist/core/locales/slices/commands.d.ts +76 -0
- package/dist/core/locales/slices/commands.js +155 -1
- package/dist/core/locales/slices/status.d.ts +18 -0
- package/dist/core/locales/slices/status.js +43 -2
- package/dist/core/log.d.ts +5 -0
- package/dist/core/log.js +25 -23
- package/dist/core/pi-session-filename.d.ts +2 -0
- package/dist/core/pi-session-filename.js +18 -0
- package/dist/core/resume-reminder.d.ts +5 -0
- package/dist/core/resume-reminder.js +18 -0
- package/dist/core/runtime-env.d.ts +1 -0
- package/dist/core/runtime-env.js +15 -0
- package/dist/core/settings-migration.d.ts +5 -0
- package/dist/core/settings-migration.js +128 -0
- package/dist/core/settings-spec.d.ts +174 -0
- package/dist/core/settings-spec.js +150 -0
- package/dist/core/settings.d.ts +10 -0
- package/dist/core/settings.js +235 -0
- package/dist/core/task-parser.d.ts +11 -1
- package/dist/core/task-parser.js +34 -3
- package/dist/core/types/agent-types.d.ts +19 -0
- package/dist/core/types/agent-types.js +1 -1
- package/dist/core/types/thread-types.d.ts +48 -3
- package/dist/core/types/thread-types.js +2 -2
- package/dist/domain/agent-run/access-probe-cli.d.ts +7 -0
- package/dist/domain/agent-run/access-probe-cli.js +139 -0
- package/dist/domain/agent-run/access-probe-policy.d.ts +61 -0
- package/dist/domain/agent-run/access-probe-policy.js +614 -0
- package/dist/domain/agent-run/access-probe.d.ts +21 -0
- package/dist/domain/agent-run/access-probe.js +194 -0
- package/dist/domain/agent-run/agent-run-cli.d.ts +19 -0
- package/dist/domain/agent-run/agent-run-cli.js +171 -0
- package/dist/domain/agent-run/atif.d.ts +54 -0
- package/dist/domain/agent-run/atif.js +211 -0
- package/dist/domain/agent-run/benchmark-local-thread-orchestrator.d.ts +32 -0
- package/dist/domain/agent-run/benchmark-local-thread-orchestrator.js +616 -0
- package/dist/domain/agent-run/benchmark-thread-identity.d.ts +34 -0
- package/dist/domain/agent-run/benchmark-thread-identity.js +106 -0
- package/dist/domain/agent-run/identity.d.ts +62 -0
- package/dist/domain/agent-run/identity.js +118 -0
- package/dist/domain/agent-run/journal.d.ts +51 -0
- package/dist/domain/agent-run/journal.js +259 -0
- package/dist/domain/agent-run/manifest-contract.d.ts +44 -0
- package/dist/domain/agent-run/manifest-contract.js +134 -0
- package/dist/domain/agent-run/manifest.d.ts +51 -0
- package/dist/domain/agent-run/manifest.js +801 -0
- package/dist/domain/agent-run/pinned-node-process.d.ts +35 -0
- package/dist/domain/agent-run/pinned-node-process.js +206 -0
- package/dist/domain/agent-run/role-surface.d.ts +4 -0
- package/dist/domain/agent-run/role-surface.js +99 -0
- package/dist/domain/agent-run/run-config.d.ts +22 -0
- package/dist/domain/agent-run/run-config.js +135 -0
- package/dist/domain/agent-run/runner.d.ts +7 -0
- package/dist/domain/agent-run/runner.js +518 -0
- package/dist/domain/agent-run/supervisor.d.ts +78 -0
- package/dist/domain/agent-run/supervisor.js +425 -0
- package/dist/domain/agent-run/trajectory-merge-cli.d.ts +11 -0
- package/dist/domain/agent-run/trajectory-merge-cli.js +111 -0
- package/dist/domain/agent-run/trajectory-merge.d.ts +39 -0
- package/dist/domain/agent-run/trajectory-merge.js +505 -0
- package/dist/domain/agents/config.d.ts +10 -0
- package/dist/domain/agents/config.js +149 -44
- package/dist/domain/agents/facade.d.ts +42 -7
- package/dist/domain/agents/facade.js +174 -170
- package/dist/domain/agents/provider-run-lifecycle.d.ts +18 -0
- package/dist/domain/agents/provider-run-lifecycle.js +60 -0
- package/dist/domain/auth/auth-events.d.ts +18 -0
- package/dist/domain/auth/auth-events.js +38 -0
- package/dist/domain/auth/auth-format.d.ts +2 -0
- package/dist/domain/auth/auth-format.js +58 -0
- package/dist/domain/auth/auth-status.d.ts +61 -0
- package/dist/domain/auth/auth-status.js +304 -0
- package/dist/domain/auth/auth-watch.d.ts +16 -0
- package/dist/domain/auth/auth-watch.js +125 -0
- package/dist/domain/auth/cc-login.d.ts +7 -0
- package/dist/domain/auth/cc-login.js +16 -0
- package/dist/domain/auth/cc-subscription.d.ts +38 -0
- package/dist/domain/auth/cc-subscription.js +204 -0
- package/dist/domain/auth/index.d.ts +12 -0
- package/dist/domain/auth/index.js +16 -0
- package/dist/domain/auth/login-flow.d.ts +120 -0
- package/dist/domain/auth/login-flow.js +253 -0
- package/dist/domain/auth/login-service.d.ts +32 -0
- package/dist/domain/auth/login-service.js +97 -0
- package/dist/domain/auth/logout.d.ts +32 -0
- package/dist/domain/auth/logout.js +123 -0
- package/dist/domain/auth/pi-login.d.ts +36 -0
- package/dist/domain/auth/pi-login.js +72 -0
- package/dist/domain/auth/pi-oauth.d.ts +36 -0
- package/dist/domain/auth/pi-oauth.js +84 -0
- package/dist/domain/auth/pi-runtime.d.ts +72 -0
- package/dist/domain/auth/pi-runtime.js +86 -0
- package/dist/domain/mcp/benchmark-thread-server.d.ts +1 -0
- package/dist/domain/mcp/benchmark-thread-server.js +26 -0
- package/dist/domain/mcp/tools/benchmark-thread-run.d.ts +23 -0
- package/dist/domain/mcp/tools/benchmark-thread-run.js +146 -0
- package/dist/domain/memory/user-context.js +6 -8
- package/dist/domain/monitor/disk-monitor.js +47 -16
- package/dist/domain/projects/project-store.js +23 -6
- package/dist/domain/remote/client-manager.js +12 -6
- package/dist/domain/scheduling/jobs/auth-expiry-scan.d.ts +14 -0
- package/dist/domain/scheduling/jobs/auth-expiry-scan.js +86 -0
- package/dist/domain/scheduling/jobs/task-dispatch.js +36 -40
- package/dist/domain/scheduling/runner.d.ts +2 -0
- package/dist/domain/scheduling/runner.js +7 -4
- package/dist/domain/sessions/session-backup.d.ts +14 -20
- package/dist/domain/sessions/session-backup.js +57 -68
- package/dist/domain/system/doctor.d.ts +4 -0
- package/dist/domain/system/doctor.js +113 -28
- package/dist/domain/system/server-update-check.js +7 -5
- package/dist/domain/system/system-notice.d.ts +3 -1
- package/dist/domain/system/system-notice.js +6 -11
- package/dist/domain/tasks/archiver.d.ts +1 -0
- package/dist/domain/tasks/archiver.js +13 -7
- package/dist/domain/tasks/claim-recovery.d.ts +3 -3
- package/dist/domain/tasks/claim-recovery.js +12 -8
- package/dist/domain/tasks/dispatch-utils.d.ts +15 -14
- package/dist/domain/tasks/dispatch-utils.js +22 -18
- package/dist/domain/tasks/dispatcher.d.ts +1 -0
- package/dist/domain/tasks/dispatcher.js +9 -5
- package/dist/domain/tasks/mutator.d.ts +21 -11
- package/dist/domain/tasks/mutator.js +30 -24
- package/dist/domain/tasks/pending-tracker.d.ts +5 -2
- package/dist/domain/tasks/pending-tracker.js +11 -5
- package/dist/domain/tasks/system/cortex-run.d.ts +2 -1
- package/dist/domain/tasks/system/cortex-run.js +24 -4
- package/dist/domain/tasks/system/task-cli.d.ts +1 -0
- package/dist/domain/tasks/system/task-cli.js +72 -22
- package/dist/domain/tasks/system/task-completion.d.ts +19 -2
- package/dist/domain/tasks/system/task-completion.js +31 -17
- package/dist/domain/tasks/system/task-file-input.d.ts +10 -0
- package/dist/domain/tasks/system/task-file-input.js +57 -0
- package/dist/domain/tasks/system/task-id-utils.d.ts +9 -9
- package/dist/domain/tasks/system/task-id-utils.js +13 -4
- package/dist/domain/tasks/system/task-lifecycle-edit.d.ts +6 -2
- package/dist/domain/tasks/system/task-lifecycle-edit.js +200 -6
- package/dist/domain/tasks/system/task-lock.js +19 -5
- package/dist/domain/tasks/system/task-mutations.d.ts +22 -12
- package/dist/domain/tasks/system/task-mutations.js +28 -4
- package/dist/domain/tasks/system/task-process.js +10 -8
- package/dist/domain/tasks/system/task-state.d.ts +65 -11
- package/dist/domain/tasks/system/task-state.js +86 -13
- package/dist/domain/threads/local-runtime-deps.d.ts +32 -0
- package/dist/domain/threads/local-runtime-deps.js +91 -0
- package/dist/domain/threads/local-runtime-scope.d.ts +6 -0
- package/dist/domain/threads/local-runtime-scope.js +12 -0
- package/dist/domain/threads/pending-user-inputs.d.ts +3 -0
- package/dist/domain/threads/pending-user-inputs.js +37 -0
- package/dist/domain/threads/prompt-builder.d.ts +12 -1
- package/dist/domain/threads/prompt-builder.js +73 -26
- package/dist/domain/threads/runner.d.ts +15 -3
- package/dist/domain/threads/runner.js +261 -105
- package/dist/domain/threads/state-machine.d.ts +1 -1
- package/dist/domain/threads/state-machine.js +17 -8
- package/dist/domain/ui-service/app-router.d.ts +86 -6
- package/dist/domain/ui-service/app-router.js +171 -133
- package/dist/domain/ui-service/input-schemas.d.ts +132 -10
- package/dist/domain/ui-service/input-schemas.js +59 -5
- package/dist/domain/ui-service/mutate/approvals.d.ts +2 -1
- package/dist/domain/ui-service/mutate/approvals.js +2 -1
- package/dist/domain/ui-service/mutate/auth.d.ts +6 -0
- package/dist/domain/ui-service/mutate/auth.js +83 -0
- package/dist/domain/ui-service/mutate/config.js +11 -10
- package/dist/domain/ui-service/query/approvals.js +3 -0
- package/dist/domain/ui-service/query/auth.d.ts +6 -0
- package/dist/domain/ui-service/query/auth.js +12 -0
- package/dist/domain/ui-service/query/config.d.ts +5 -5
- package/dist/domain/ui-service/query/config.js +27 -17
- package/dist/domain/ui-service/query/tasks.d.ts +1 -1
- package/dist/domain/ui-service/query/tasks.js +69 -30
- package/dist/domain/ui-service/query/threads.js +3 -3
- package/dist/domain/ui-service/types.d.ts +67 -8
- package/dist/domain/ui-service/types.js +2 -2
- package/dist/domain/ui-service/ui-service.js +16 -2
- package/dist/entry/admin-channel-hot-reload.d.ts +4 -0
- package/dist/entry/admin-channel-hot-reload.js +22 -0
- package/dist/entry/app.js +38 -38
- package/dist/entry/auth-cli.d.ts +10 -0
- package/dist/entry/auth-cli.js +52 -0
- package/dist/entry/cli-help.d.ts +5 -0
- package/dist/entry/cli-help.js +130 -0
- package/dist/entry/cli.d.ts +5 -5
- package/dist/entry/cli.js +174 -248
- package/dist/entry/daemon.d.ts +11 -0
- package/dist/entry/daemon.js +204 -103
- package/dist/entry/draft-attachments.d.ts +8 -0
- package/dist/entry/draft-attachments.js +83 -0
- package/dist/entry/feishu-login.d.ts +2 -6
- package/dist/entry/feishu-login.js +16 -21
- package/dist/entry/init.js +6 -4
- package/dist/entry/start-ui-http.d.ts +2 -8
- package/dist/entry/start-ui-http.js +7 -34
- package/dist/events/event-logger.js +11 -13
- package/dist/events/event-types.d.ts +19 -1
- package/dist/events/event-types.js +3 -3
- package/dist/events/index.d.ts +1 -1
- package/dist/events/index.js +3 -3
- package/dist/hooks/new-session-hook.mjs +2 -2
- package/dist/hooks/status-md-guard.mjs +193 -0
- package/dist/orchestration/agent-file-send.js +44 -14
- package/dist/orchestration/agent-runner.d.ts +7 -3
- package/dist/orchestration/agent-runner.js +65 -41
- package/dist/orchestration/conversation-runner.d.ts +5 -1
- package/dist/orchestration/conversation-runner.js +16 -7
- package/dist/orchestration/lifecycle.d.ts +23 -1
- package/dist/orchestration/lifecycle.js +103 -40
- package/dist/orchestration/mid-turn-inject.d.ts +2 -0
- package/dist/orchestration/mid-turn-inject.js +29 -13
- package/dist/orchestration/resume-dispatcher.d.ts +3 -5
- package/dist/orchestration/resume-dispatcher.js +9 -17
- package/dist/orchestration/routing/commands/index.d.ts +4 -1
- package/dist/orchestration/routing/commands/index.js +83 -61
- package/dist/orchestration/routing/commands/login-notice.d.ts +21 -0
- package/dist/orchestration/routing/commands/login-notice.js +97 -0
- package/dist/orchestration/routing/commands/login.d.ts +12 -0
- package/dist/orchestration/routing/commands/login.js +555 -0
- package/dist/orchestration/routing/commands/status.js +45 -59
- package/dist/orchestration/routing/edit-handler.d.ts +5 -0
- package/dist/orchestration/routing/edit-handler.js +127 -73
- package/dist/orchestration/routing/webhook.js +5 -4
- package/dist/orchestration/session-rewind.d.ts +5 -1
- package/dist/orchestration/session-rewind.js +106 -62
- package/dist/orchestration/session-send.d.ts +2 -0
- package/dist/orchestration/session-send.js +5 -6
- package/dist/orchestration/status-helpers.d.ts +1 -2
- package/dist/orchestration/status-helpers.js +7 -10
- package/dist/orchestration/thread-callback.d.ts +19 -11
- package/dist/orchestration/thread-callback.js +132 -61
- package/dist/orchestration/thread-executor.js +37 -27
- package/dist/orchestration/turn-mutation-lock.d.ts +3 -0
- package/dist/orchestration/turn-mutation-lock.js +36 -0
- package/dist/orchestration/turn-notify.d.ts +2 -5
- package/dist/orchestration/turn-notify.js +9 -21
- package/dist/platform/adapter.d.ts +1 -0
- package/dist/platform/adapter.js +2 -2
- package/dist/platform/adapters/composite-adapter.d.ts +2 -0
- package/dist/platform/adapters/composite-adapter.js +13 -3
- package/dist/platform/adapters/feishu.d.ts +2 -4
- package/dist/platform/adapters/feishu.js +38 -59
- package/dist/platform/adapters/index.d.ts +1 -1
- package/dist/platform/adapters/index.js +13 -12
- package/dist/platform/adapters/slack.d.ts +1 -1
- package/dist/platform/adapters/slack.js +12 -35
- package/dist/platform/index.d.ts +2 -1
- package/dist/platform/index.js +3 -3
- package/dist/platform/testing.d.ts +1 -0
- package/dist/platform/testing.js +13 -4
- package/dist/platform/tool-trace.js +6 -11
- package/dist/platform/ui-http/ui-http-server.d.ts +1 -1
- package/dist/platform/ui-http/ui-http-server.js +6 -18
- package/dist/store/execution-repo.d.ts +17 -1
- package/dist/store/execution-repo.js +55 -4
- package/dist/store/prompt-migration-replacements.d.ts +4 -0
- package/dist/store/prompt-migration-replacements.js +149 -0
- package/dist/store/thread-repo.d.ts +15 -4
- package/dist/store/thread-repo.js +61 -24
- package/dist/store/version-migrations.js +36 -1
- package/native/cortex-supervisor/dist/build-manifest.json +22 -0
- package/native/cortex-supervisor/dist/cortex-supervisor +0 -0
- package/package.json +5 -2
- package/web/dist/assets/index-DrNx9R6c.js +139 -0
- package/web/dist/assets/index-zpf3OSKC.css +1 -0
- package/web/dist/index.html +2 -2
- package/dist/domain/scheduling/jobs/failed-dispatch-reconciliation.d.ts +0 -1
- package/dist/domain/scheduling/jobs/failed-dispatch-reconciliation.js +0 -78
- package/web/dist/assets/index-B19jLgji.js +0 -138
- package/web/dist/assets/index-BuFyPyRW.css +0 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type AccessMode = 'read' | 'write' | 'network' | 'unknown';
|
|
2
|
+
export interface AccessViolation {
|
|
3
|
+
syscall: string;
|
|
4
|
+
path: string;
|
|
5
|
+
reason: string;
|
|
6
|
+
access: AccessMode;
|
|
7
|
+
pid: number;
|
|
8
|
+
traceFile: string;
|
|
9
|
+
line: number;
|
|
10
|
+
raw: string;
|
|
11
|
+
}
|
|
12
|
+
export interface AccessProbePolicy {
|
|
13
|
+
workspace: string;
|
|
14
|
+
cortexHome: string;
|
|
15
|
+
logsDir: string;
|
|
16
|
+
installRoot: string;
|
|
17
|
+
hostHome: string;
|
|
18
|
+
hostCortexHome: string;
|
|
19
|
+
nodeExecutable: string;
|
|
20
|
+
nodeModuleRoots?: string[];
|
|
21
|
+
tracedPids?: ReadonlySet<number>;
|
|
22
|
+
}
|
|
23
|
+
export interface AccessProbeCounts {
|
|
24
|
+
traceLines: number;
|
|
25
|
+
fileCalls: number;
|
|
26
|
+
networkCalls: number;
|
|
27
|
+
allowed: number;
|
|
28
|
+
}
|
|
29
|
+
export interface TraceClassification {
|
|
30
|
+
violations: AccessViolation[];
|
|
31
|
+
counts: AccessProbeCounts;
|
|
32
|
+
}
|
|
33
|
+
export interface AccessTraceOptions {
|
|
34
|
+
policy: AccessProbePolicy;
|
|
35
|
+
initialCwd: string;
|
|
36
|
+
pid: number;
|
|
37
|
+
traceFile: string;
|
|
38
|
+
}
|
|
39
|
+
export declare class AccessTraceClassifier {
|
|
40
|
+
private readonly options;
|
|
41
|
+
private readonly counts;
|
|
42
|
+
private readonly pending;
|
|
43
|
+
private readonly processes;
|
|
44
|
+
private readonly symlinks;
|
|
45
|
+
private readonly violations;
|
|
46
|
+
private rootPid;
|
|
47
|
+
constructor(options: AccessTraceOptions);
|
|
48
|
+
private eventOptions;
|
|
49
|
+
private processState;
|
|
50
|
+
private inheritProcess;
|
|
51
|
+
private updateSymlinks;
|
|
52
|
+
private updateProcessState;
|
|
53
|
+
private classifyCall;
|
|
54
|
+
private classifyBody;
|
|
55
|
+
private startPending;
|
|
56
|
+
private finishPending;
|
|
57
|
+
consume(raw: string): void;
|
|
58
|
+
finish(): TraceClassification;
|
|
59
|
+
}
|
|
60
|
+
export declare function classifyTraceLines(lines: string[], options: AccessTraceOptions): TraceClassification;
|
|
61
|
+
export declare function classifyTraceStream(input: NodeJS.ReadableStream, options: AccessTraceOptions): Promise<TraceClassification>;
|
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
// input: strace stream, initial cwd, and C8 policy roots
|
|
2
|
+
// output: bounded counts and fail-closed access violations
|
|
3
|
+
// pos: Stateful classifier for benchmark access traces
|
|
4
|
+
// >>> If I am updated, update my header and folder CORTEX.md <<<
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { createInterface } from 'node:readline';
|
|
8
|
+
const DIRECT_PATH = [{ pathIndex: 0 }];
|
|
9
|
+
const AT_PATH = [{ pathIndex: 1, dirIndex: 0 }];
|
|
10
|
+
const FILE_ARGUMENTS = {
|
|
11
|
+
access: DIRECT_PATH,
|
|
12
|
+
chdir: DIRECT_PATH,
|
|
13
|
+
chmod: DIRECT_PATH,
|
|
14
|
+
chown: DIRECT_PATH,
|
|
15
|
+
creat: DIRECT_PATH,
|
|
16
|
+
execve: DIRECT_PATH,
|
|
17
|
+
lchown: DIRECT_PATH,
|
|
18
|
+
link: [{ pathIndex: 0 }, { pathIndex: 1 }],
|
|
19
|
+
lstat: DIRECT_PATH,
|
|
20
|
+
mkdir: DIRECT_PATH,
|
|
21
|
+
mknod: DIRECT_PATH,
|
|
22
|
+
mount: [{ pathIndex: 0 }, { pathIndex: 1 }],
|
|
23
|
+
open: DIRECT_PATH,
|
|
24
|
+
readlink: DIRECT_PATH,
|
|
25
|
+
rename: [{ pathIndex: 0 }, { pathIndex: 1 }],
|
|
26
|
+
rmdir: DIRECT_PATH,
|
|
27
|
+
stat: DIRECT_PATH,
|
|
28
|
+
statfs: DIRECT_PATH,
|
|
29
|
+
symlink: [{ pathIndex: 1 }],
|
|
30
|
+
truncate: DIRECT_PATH,
|
|
31
|
+
umount: DIRECT_PATH,
|
|
32
|
+
umount2: DIRECT_PATH,
|
|
33
|
+
unlink: DIRECT_PATH,
|
|
34
|
+
utime: DIRECT_PATH,
|
|
35
|
+
utimes: DIRECT_PATH,
|
|
36
|
+
execveat: AT_PATH,
|
|
37
|
+
faccessat: AT_PATH,
|
|
38
|
+
faccessat2: AT_PATH,
|
|
39
|
+
getcwd: DIRECT_PATH,
|
|
40
|
+
inotify_add_watch: [{ pathIndex: 1 }],
|
|
41
|
+
fchmodat: AT_PATH,
|
|
42
|
+
fchownat: AT_PATH,
|
|
43
|
+
mkdirat: AT_PATH,
|
|
44
|
+
mknodat: AT_PATH,
|
|
45
|
+
name_to_handle_at: AT_PATH,
|
|
46
|
+
newfstatat: AT_PATH,
|
|
47
|
+
openat: AT_PATH,
|
|
48
|
+
openat2: AT_PATH,
|
|
49
|
+
readlinkat: AT_PATH,
|
|
50
|
+
statx: AT_PATH,
|
|
51
|
+
unlinkat: AT_PATH,
|
|
52
|
+
utimensat: AT_PATH,
|
|
53
|
+
linkat: [{ pathIndex: 1, dirIndex: 0 }, { pathIndex: 3, dirIndex: 2 }],
|
|
54
|
+
renameat: [{ pathIndex: 1, dirIndex: 0 }, { pathIndex: 3, dirIndex: 2 }],
|
|
55
|
+
renameat2: [{ pathIndex: 1, dirIndex: 0 }, { pathIndex: 3, dirIndex: 2 }],
|
|
56
|
+
symlinkat: [{ pathIndex: 2, dirIndex: 1 }],
|
|
57
|
+
};
|
|
58
|
+
const WRITE_SYSCALLS = new Set([
|
|
59
|
+
'chmod', 'chown', 'creat', 'fchmodat', 'fchownat', 'lchown', 'link', 'linkat', 'mkdir',
|
|
60
|
+
'mkdirat', 'mknod', 'mknodat', 'mount', 'rename', 'renameat', 'renameat2', 'rmdir', 'symlink',
|
|
61
|
+
'symlinkat', 'truncate', 'umount', 'umount2', 'unlink', 'unlinkat', 'utime', 'utimensat', 'utimes',
|
|
62
|
+
]);
|
|
63
|
+
const PROCESS_METADATA = new Set([
|
|
64
|
+
'clone', 'clone3', 'exit', 'exit_group', 'fork', 'kill', 'setpgid', 'tgkill', 'vfork', 'wait4',
|
|
65
|
+
'waitid',
|
|
66
|
+
]);
|
|
67
|
+
const NETWORK_DENIALS = {
|
|
68
|
+
bind: 'network_bind_denied',
|
|
69
|
+
listen: 'network_listen_denied',
|
|
70
|
+
connect: 'network_connect_denied',
|
|
71
|
+
};
|
|
72
|
+
const NETWORK_SYSCALLS = new Set([
|
|
73
|
+
'accept', 'accept4', 'bind', 'connect', 'getpeername', 'getsockname', 'getsockopt', 'listen',
|
|
74
|
+
'recvfrom', 'recvmmsg', 'recvmsg', 'sendmmsg', 'sendmsg', 'sendto', 'setsockopt', 'shutdown',
|
|
75
|
+
'socket', 'socketpair',
|
|
76
|
+
]);
|
|
77
|
+
const SYSTEM_ROOTS = [
|
|
78
|
+
'/lib', '/lib64', '/usr/lib', '/usr/lib64', '/usr/share/zoneinfo', '/sys/fs/cgroup',
|
|
79
|
+
'/sys/devices/system/cpu', '/sys/kernel/mm',
|
|
80
|
+
];
|
|
81
|
+
const SYSTEM_FILES = new Set([
|
|
82
|
+
'/dev/null', '/dev/random', '/dev/urandom', '/etc/gai.conf', '/etc/host.conf', '/etc/hosts',
|
|
83
|
+
'/etc/ld.so.cache', '/etc/ld.so.preload', '/etc/localtime', '/etc/netsvc.conf',
|
|
84
|
+
'/etc/nsswitch.conf', '/etc/resolv.conf', '/etc/services', '/etc/ssl/openssl.cnf',
|
|
85
|
+
'/etc/svc.conf', '/proc/cpuinfo', '/proc/filesystems', '/proc/meminfo', '/proc/stat',
|
|
86
|
+
'/proc/sys/vm/overcommit_memory', '/proc/version_signature',
|
|
87
|
+
]);
|
|
88
|
+
function splitArguments(value) {
|
|
89
|
+
const result = [];
|
|
90
|
+
let start = 0;
|
|
91
|
+
let quote = false;
|
|
92
|
+
let escaped = false;
|
|
93
|
+
let depth = 0;
|
|
94
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
95
|
+
const char = value[index];
|
|
96
|
+
if (escaped)
|
|
97
|
+
escaped = false;
|
|
98
|
+
else if (char === '\\' && quote)
|
|
99
|
+
escaped = true;
|
|
100
|
+
else if (char === '"')
|
|
101
|
+
quote = !quote;
|
|
102
|
+
else if (!quote && '{[('.includes(char))
|
|
103
|
+
depth += 1;
|
|
104
|
+
else if (!quote && '}])'.includes(char))
|
|
105
|
+
depth -= 1;
|
|
106
|
+
else if (!quote && depth === 0 && char === ',') {
|
|
107
|
+
result.push(value.slice(start, index).trim());
|
|
108
|
+
start = index + 1;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
result.push(value.slice(start).trim());
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
function parsePrefix(raw, fallbackPid) {
|
|
115
|
+
const bracketed = /^\[pid\s+(\d+)\]\s+(.+)$/.exec(raw);
|
|
116
|
+
if (bracketed)
|
|
117
|
+
return { pid: Number(bracketed[1]), body: bracketed[2] };
|
|
118
|
+
const combined = /^(\d+)\s+(\d+(?:\.\d+)?\s+.+)$/.exec(raw);
|
|
119
|
+
if (combined)
|
|
120
|
+
return { pid: Number(combined[1]), body: combined[2] };
|
|
121
|
+
return { pid: fallbackPid, body: raw };
|
|
122
|
+
}
|
|
123
|
+
function parseTraceCall(body) {
|
|
124
|
+
const callBody = body.replace(/^\d+(?:\.\d+)?\s+/, '');
|
|
125
|
+
const match = /^([A-Za-z0-9_]+)\((.*)\)\s+=\s+(.+)$/.exec(callBody);
|
|
126
|
+
if (!match)
|
|
127
|
+
return null;
|
|
128
|
+
return { syscall: match[1], args: splitArguments(match[2]), result: match[3] };
|
|
129
|
+
}
|
|
130
|
+
function decodeQuoted(value) {
|
|
131
|
+
if (typeof value !== 'string')
|
|
132
|
+
return null;
|
|
133
|
+
const match = /^"((?:[^"\\]|\\.)*)"/.exec(value.trim());
|
|
134
|
+
if (!match)
|
|
135
|
+
return null;
|
|
136
|
+
const jsonEscaped = match[1].replace(/\\([0-7]{1,3})/g, (_whole, octal) => (`\\u${Number.parseInt(octal, 8).toString(16).padStart(4, '0')}`));
|
|
137
|
+
try {
|
|
138
|
+
return JSON.parse(`"${jsonEscaped}"`);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function annotatedPath(value) {
|
|
145
|
+
if (typeof value !== 'string')
|
|
146
|
+
return null;
|
|
147
|
+
return /<(\/[^>]*)>/.exec(value)?.[1] ?? null;
|
|
148
|
+
}
|
|
149
|
+
function appendRaw(base, segments) {
|
|
150
|
+
if (segments.length === 0)
|
|
151
|
+
return base;
|
|
152
|
+
const separator = base.endsWith(path.sep) ? '' : path.sep;
|
|
153
|
+
return `${base}${separator}${segments.join(path.sep)}`;
|
|
154
|
+
}
|
|
155
|
+
function dirBase(argument, state) {
|
|
156
|
+
if (argument === undefined || String(argument).startsWith('AT_FDCWD')) {
|
|
157
|
+
return annotatedPath(argument) ?? state.cwd;
|
|
158
|
+
}
|
|
159
|
+
const annotated = annotatedPath(argument);
|
|
160
|
+
if (annotated)
|
|
161
|
+
return annotated;
|
|
162
|
+
const fd = Number.parseInt(String(argument), 10);
|
|
163
|
+
return Number.isSafeInteger(fd) ? state.fds.get(fd) ?? null : null;
|
|
164
|
+
}
|
|
165
|
+
function resolveTracedPath(argument, dirArgument, state) {
|
|
166
|
+
const value = decodeQuoted(argument);
|
|
167
|
+
if (value === null)
|
|
168
|
+
return null;
|
|
169
|
+
if (path.isAbsolute(value))
|
|
170
|
+
return value;
|
|
171
|
+
const base = dirBase(dirArgument, state);
|
|
172
|
+
if (!base)
|
|
173
|
+
return null;
|
|
174
|
+
return value.length === 0 ? base : appendRaw(base, [value]);
|
|
175
|
+
}
|
|
176
|
+
function resolveKnownSymlinks(value, symlinks, followFinal, depth = 0) {
|
|
177
|
+
const absolute = path.isAbsolute(value) ? value : appendRaw(process.cwd(), [value]);
|
|
178
|
+
if (depth >= 40)
|
|
179
|
+
return absolute;
|
|
180
|
+
const parsed = path.parse(absolute);
|
|
181
|
+
const segments = absolute.slice(parsed.root.length).split(path.sep).filter(Boolean);
|
|
182
|
+
let cursor = parsed.root;
|
|
183
|
+
for (let index = 0; index < segments.length; index += 1) {
|
|
184
|
+
if (segments[index] === '..') {
|
|
185
|
+
cursor = path.dirname(cursor);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (segments[index] === '.')
|
|
189
|
+
continue;
|
|
190
|
+
const next = path.join(cursor, segments[index]);
|
|
191
|
+
let target = symlinks.get(next);
|
|
192
|
+
if (target === undefined) {
|
|
193
|
+
try {
|
|
194
|
+
if (fs.lstatSync(next).isSymbolicLink())
|
|
195
|
+
target = fs.readlinkSync(next);
|
|
196
|
+
}
|
|
197
|
+
catch { }
|
|
198
|
+
}
|
|
199
|
+
if (target !== undefined && (followFinal || index < segments.length - 1)) {
|
|
200
|
+
const resolved = path.isAbsolute(target) ? target : appendRaw(path.dirname(next), [target]);
|
|
201
|
+
return resolveKnownSymlinks(appendRaw(resolved, segments.slice(index + 1)), symlinks, followFinal, depth + 1);
|
|
202
|
+
}
|
|
203
|
+
cursor = next;
|
|
204
|
+
}
|
|
205
|
+
return cursor;
|
|
206
|
+
}
|
|
207
|
+
function canonicalFsPath(value, followFinal, depth = 0) {
|
|
208
|
+
if (depth >= 40)
|
|
209
|
+
return value;
|
|
210
|
+
const parsed = path.parse(value);
|
|
211
|
+
const segments = value.slice(parsed.root.length).split(path.sep).filter(Boolean);
|
|
212
|
+
let cursor = parsed.root;
|
|
213
|
+
for (let index = 0; index < segments.length; index += 1) {
|
|
214
|
+
const next = path.join(cursor, segments[index]);
|
|
215
|
+
let stat;
|
|
216
|
+
try {
|
|
217
|
+
stat = fs.lstatSync(next);
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
return appendRaw(next, segments.slice(index + 1));
|
|
221
|
+
}
|
|
222
|
+
if (stat.isSymbolicLink() && (followFinal || index < segments.length - 1)) {
|
|
223
|
+
let target;
|
|
224
|
+
try {
|
|
225
|
+
target = fs.readlinkSync(next);
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
return appendRaw(next, segments.slice(index + 1));
|
|
229
|
+
}
|
|
230
|
+
const resolved = path.isAbsolute(target) ? target : appendRaw(path.dirname(next), [target]);
|
|
231
|
+
return canonicalFsPath(appendRaw(resolved, segments.slice(index + 1)), followFinal, depth + 1);
|
|
232
|
+
}
|
|
233
|
+
cursor = next;
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
return followFinal ? fs.realpathSync(cursor) : cursor;
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
return value;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function canonicalPath(value, symlinks = new Map(), followFinal = true) {
|
|
243
|
+
return canonicalFsPath(resolveKnownSymlinks(value, symlinks, followFinal), followFinal);
|
|
244
|
+
}
|
|
245
|
+
function isWithin(candidate, root) {
|
|
246
|
+
const relative = path.relative(path.resolve(root), path.resolve(candidate));
|
|
247
|
+
return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..'
|
|
248
|
+
&& !path.isAbsolute(relative));
|
|
249
|
+
}
|
|
250
|
+
function isHostDotfile(candidate, policy) {
|
|
251
|
+
if (!isWithin(candidate, policy.hostHome))
|
|
252
|
+
return false;
|
|
253
|
+
const first = path.relative(policy.hostHome, candidate).split(path.sep)[0];
|
|
254
|
+
return first.startsWith('.');
|
|
255
|
+
}
|
|
256
|
+
const SAFE_PROC_FILES = new Set(['auxv', 'cgroup', 'exe', 'maps', 'stat', 'statm', 'status']);
|
|
257
|
+
function isProcessRuntimePath(candidate, pid, tracedPids) {
|
|
258
|
+
const match = /^\/proc\/(self|thread-self|\d+)\/([^/]+)$/.exec(candidate);
|
|
259
|
+
if (!match || !SAFE_PROC_FILES.has(match[2]))
|
|
260
|
+
return false;
|
|
261
|
+
if (match[1] === 'self' || match[1] === 'thread-self')
|
|
262
|
+
return true;
|
|
263
|
+
const targetPid = Number(match[1]);
|
|
264
|
+
return targetPid === pid || tracedPids?.has(targetPid) === true;
|
|
265
|
+
}
|
|
266
|
+
function readOnlyRoots(policy) {
|
|
267
|
+
return [policy.installRoot, ...(policy.nodeModuleRoots ?? []), ...SYSTEM_ROOTS];
|
|
268
|
+
}
|
|
269
|
+
function isNodeExecutable(candidate, policy) {
|
|
270
|
+
return candidate === canonicalPath(policy.nodeExecutable);
|
|
271
|
+
}
|
|
272
|
+
function isAllowedRootAncestor(candidate, policy) {
|
|
273
|
+
const roots = [
|
|
274
|
+
policy.installRoot,
|
|
275
|
+
...(policy.nodeModuleRoots ?? []),
|
|
276
|
+
...SYSTEM_ROOTS,
|
|
277
|
+
...SYSTEM_FILES,
|
|
278
|
+
];
|
|
279
|
+
return roots.some(root => isWithin(root, candidate));
|
|
280
|
+
}
|
|
281
|
+
function pathDecision(reason, offender) {
|
|
282
|
+
return { reason, offender };
|
|
283
|
+
}
|
|
284
|
+
function classifyPath(candidate, access, policy, pid, symlinks, followFinal) {
|
|
285
|
+
const traced = resolveKnownSymlinks(candidate, symlinks, followFinal);
|
|
286
|
+
const canonical = canonicalFsPath(traced, followFinal);
|
|
287
|
+
if (isWithin(candidate, policy.hostCortexHome))
|
|
288
|
+
return pathDecision('host_cortex_path', candidate);
|
|
289
|
+
if (isWithin(canonical, policy.hostCortexHome))
|
|
290
|
+
return pathDecision('host_cortex_path', traced === candidate ? candidate : canonical);
|
|
291
|
+
if (isNodeExecutable(canonical, policy) && access === 'read')
|
|
292
|
+
return pathDecision(null, candidate);
|
|
293
|
+
if (isHostDotfile(candidate, policy))
|
|
294
|
+
return pathDecision('host_home_dotfile', candidate);
|
|
295
|
+
if (isHostDotfile(canonical, policy))
|
|
296
|
+
return pathDecision('host_home_dotfile', traced === candidate ? candidate : canonical);
|
|
297
|
+
const writable = [policy.workspace, policy.cortexHome, policy.logsDir];
|
|
298
|
+
if (writable.some(root => isWithin(canonical, root)))
|
|
299
|
+
return pathDecision(null, candidate);
|
|
300
|
+
if (isAllowedRootAncestor(canonical, policy) && access === 'read') {
|
|
301
|
+
return pathDecision(null, candidate);
|
|
302
|
+
}
|
|
303
|
+
if ((SYSTEM_FILES.has(candidate)
|
|
304
|
+
|| isProcessRuntimePath(candidate, pid, policy.tracedPids)) && access === 'read') {
|
|
305
|
+
return pathDecision(null, candidate);
|
|
306
|
+
}
|
|
307
|
+
const readOnly = readOnlyRoots(policy).some(root => isWithin(canonical, root));
|
|
308
|
+
if (readOnly && access === 'read')
|
|
309
|
+
return pathDecision(null, candidate);
|
|
310
|
+
return pathDecision(readOnly ? 'read_only_root_write' : 'unclassified_path', candidate);
|
|
311
|
+
}
|
|
312
|
+
function accessMode(call) {
|
|
313
|
+
if (WRITE_SYSCALLS.has(call.syscall))
|
|
314
|
+
return 'write';
|
|
315
|
+
if (call.syscall === 'access' || call.syscall.startsWith('faccessat')) {
|
|
316
|
+
return call.args.some(argument => argument.includes('W_OK')) ? 'write' : 'read';
|
|
317
|
+
}
|
|
318
|
+
if (call.syscall === 'open' || call.syscall === 'openat' || call.syscall === 'openat2') {
|
|
319
|
+
const flags = call.args.join(',');
|
|
320
|
+
return /O_(?:WRONLY|RDWR|CREAT|TRUNC|APPEND|TMPFILE)/.test(flags) ? 'write' : 'read';
|
|
321
|
+
}
|
|
322
|
+
return 'read';
|
|
323
|
+
}
|
|
324
|
+
function endpointFromSockaddr(args) {
|
|
325
|
+
const text = args.join(',');
|
|
326
|
+
const unix = /sun_path=(?:@)?"([^"]+)"/.exec(text);
|
|
327
|
+
if (unix)
|
|
328
|
+
return unix[1].startsWith('/') ? unix[1] : `unix:@${unix[1]}`;
|
|
329
|
+
const address = /(?:inet_addr\(|inet_pton\([^,]+,\s*)"([^"]+)"/.exec(text)?.[1];
|
|
330
|
+
const port = /sin6?_port=htons\((\d+)\)/.exec(text)?.[1];
|
|
331
|
+
return address && port ? `${address}:${port}` : null;
|
|
332
|
+
}
|
|
333
|
+
function endpointFromFd(argument) {
|
|
334
|
+
const match = /<(?:TCP|TCPv6):\[([^\]]+)\]>/.exec(argument);
|
|
335
|
+
return match?.[1] ?? null;
|
|
336
|
+
}
|
|
337
|
+
function violation(syscall, offender, reason, access, options, line, raw) {
|
|
338
|
+
return {
|
|
339
|
+
syscall,
|
|
340
|
+
path: offender,
|
|
341
|
+
reason,
|
|
342
|
+
access,
|
|
343
|
+
pid: options.pid,
|
|
344
|
+
traceFile: options.traceFile,
|
|
345
|
+
line,
|
|
346
|
+
raw,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
function classifyNetwork(call, options, line, raw) {
|
|
350
|
+
if (call.syscall === 'socket' || call.syscall === 'socketpair') {
|
|
351
|
+
const family = /^AF_[A-Z0-9_]+/.exec(call.args[0] ?? '')?.[0] ?? 'unknown';
|
|
352
|
+
const reason = family === 'AF_INET' || family === 'AF_INET6'
|
|
353
|
+
? 'network_socket_denied' : 'unclassified_network_socket';
|
|
354
|
+
return violation(call.syscall, family, reason, 'network', options, line, raw);
|
|
355
|
+
}
|
|
356
|
+
const reason = NETWORK_DENIALS[call.syscall];
|
|
357
|
+
if (reason) {
|
|
358
|
+
const endpoint = call.syscall === 'listen'
|
|
359
|
+
? endpointFromFd(call.args[0])
|
|
360
|
+
: endpointFromSockaddr(call.args);
|
|
361
|
+
return violation(call.syscall, endpoint ?? `fd:${call.args[0] ?? 'unknown'}`, reason, 'network', options, line, raw);
|
|
362
|
+
}
|
|
363
|
+
const unixMetadata = call.syscall === 'getsockname' || call.syscall === 'getsockopt';
|
|
364
|
+
if (unixMetadata && /^[12]<UNIX-STREAM:/.test(call.args[0] ?? ''))
|
|
365
|
+
return null;
|
|
366
|
+
return violation(call.syscall, raw, 'unclassified_network_syscall', 'unknown', options, line, raw);
|
|
367
|
+
}
|
|
368
|
+
function unknownViolation(call, options, line, raw) {
|
|
369
|
+
const syscall = call?.syscall ?? 'trace';
|
|
370
|
+
const offender = call?.args.map(decodeQuoted).find(value => value !== null) ?? raw;
|
|
371
|
+
const reason = call ? 'unclassified_syscall' : 'trace_parse_failed';
|
|
372
|
+
return violation(syscall, offender, reason, 'unknown', options, line, raw);
|
|
373
|
+
}
|
|
374
|
+
const FD_RETURNING_FILE_CALLS = new Set(['creat', 'open', 'openat', 'openat2']);
|
|
375
|
+
function returnedFdPath(call) {
|
|
376
|
+
if (!FD_RETURNING_FILE_CALLS.has(call.syscall))
|
|
377
|
+
return null;
|
|
378
|
+
const match = /^\d+<(.+)>/.exec(call.result);
|
|
379
|
+
if (!match?.[1].startsWith('/'))
|
|
380
|
+
return null;
|
|
381
|
+
return match[1].replace(/<(?:char|block) [^>]+>$/, '');
|
|
382
|
+
}
|
|
383
|
+
const NOFOLLOW_FINAL = new Set([
|
|
384
|
+
'lchown', 'link', 'linkat', 'lstat', 'readlink', 'readlinkat', 'rename', 'renameat',
|
|
385
|
+
'renameat2', 'rmdir', 'symlink', 'symlinkat', 'unlink', 'unlinkat',
|
|
386
|
+
]);
|
|
387
|
+
function followsFinalSymlink(call) {
|
|
388
|
+
if (NOFOLLOW_FINAL.has(call.syscall))
|
|
389
|
+
return false;
|
|
390
|
+
if (call.syscall === 'newfstatat' && call.args.some(arg => arg.includes('AT_SYMLINK_NOFOLLOW'))) {
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
if (call.syscall.startsWith('open') && call.args.some(arg => arg.includes('O_NOFOLLOW'))) {
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
function pathViolation(call, candidate, observed, access, options, line, raw, symlinks) {
|
|
399
|
+
if (!candidate)
|
|
400
|
+
return [violation(call.syscall, raw, 'unclassifiable_path', 'unknown', options, line, raw)];
|
|
401
|
+
const followFinal = followsFinalSymlink(call);
|
|
402
|
+
const attempted = classifyPath(candidate, access, options.policy, options.pid, symlinks, followFinal);
|
|
403
|
+
if (attempted.reason)
|
|
404
|
+
return [violation(call.syscall, attempted.offender, attempted.reason, access, options, line, raw)];
|
|
405
|
+
if (!observed || canonicalPath(observed) === canonicalPath(candidate, symlinks, followFinal))
|
|
406
|
+
return [];
|
|
407
|
+
const actual = classifyPath(observed, access, options.policy, options.pid, symlinks, true);
|
|
408
|
+
return actual.reason ? [violation(call.syscall, actual.offender, actual.reason, access, options, line, raw)] : [];
|
|
409
|
+
}
|
|
410
|
+
function classifyFile(call, state, options, line, raw, symlinks) {
|
|
411
|
+
const specs = FILE_ARGUMENTS[call.syscall];
|
|
412
|
+
if (!specs)
|
|
413
|
+
return { violations: [unknownViolation(call, options, line, raw)], operands: [null] };
|
|
414
|
+
const access = accessMode(call);
|
|
415
|
+
const operands = specs.map(spec => resolveTracedPath(call.args[spec.pathIndex], spec.dirIndex === undefined ? undefined : call.args[spec.dirIndex], state));
|
|
416
|
+
const observed = returnedFdPath(call);
|
|
417
|
+
const violations = operands.flatMap((candidate, index) => pathViolation(call, candidate, index === 0 ? observed : null, access, options, line, raw, symlinks));
|
|
418
|
+
return { violations, operands };
|
|
419
|
+
}
|
|
420
|
+
function isNetworkCall(call) {
|
|
421
|
+
return NETWORK_SYSCALLS.has(call.syscall);
|
|
422
|
+
}
|
|
423
|
+
function isSignalMetadata(body) {
|
|
424
|
+
return /^\d+(?:\.\d+)?\s+--- SIG[A-Z0-9]+ /.test(body);
|
|
425
|
+
}
|
|
426
|
+
function emptyCounts() {
|
|
427
|
+
return { traceLines: 0, fileCalls: 0, networkCalls: 0, allowed: 0 };
|
|
428
|
+
}
|
|
429
|
+
function succeeded(call) {
|
|
430
|
+
return !call.result.startsWith('-1') && call.result !== '?';
|
|
431
|
+
}
|
|
432
|
+
function returnedNumber(call) {
|
|
433
|
+
const value = Number.parseInt(call.result, 10);
|
|
434
|
+
return Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
435
|
+
}
|
|
436
|
+
function cloneState(state) {
|
|
437
|
+
return { cwd: state.cwd, fds: new Map(state.fds) };
|
|
438
|
+
}
|
|
439
|
+
function moveSymlinks(symlinks, source, destination) {
|
|
440
|
+
for (const [entry, target] of [...symlinks]) {
|
|
441
|
+
if (!isWithin(entry, source))
|
|
442
|
+
continue;
|
|
443
|
+
symlinks.delete(entry);
|
|
444
|
+
const suffix = path.relative(source, entry);
|
|
445
|
+
symlinks.set(suffix ? path.join(destination, suffix) : destination, target);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function unfinishedSyscall(body) {
|
|
449
|
+
if (!body.endsWith('<unfinished ...>'))
|
|
450
|
+
return null;
|
|
451
|
+
const callBody = body.replace(/^\d+(?:\.\d+)?\s+/, '');
|
|
452
|
+
return /^([A-Za-z0-9_]+)\(/.exec(callBody)?.[1] ?? null;
|
|
453
|
+
}
|
|
454
|
+
function resumedCall(body) {
|
|
455
|
+
const callBody = body.replace(/^\d+(?:\.\d+)?\s+/, '');
|
|
456
|
+
const match = /^<\.\.\. ([A-Za-z0-9_]+) resumed>(.*)$/.exec(callBody);
|
|
457
|
+
return match ? { syscall: match[1], suffix: match[2] } : null;
|
|
458
|
+
}
|
|
459
|
+
export class AccessTraceClassifier {
|
|
460
|
+
options;
|
|
461
|
+
counts = emptyCounts();
|
|
462
|
+
pending = new Map();
|
|
463
|
+
processes = new Map();
|
|
464
|
+
symlinks = new Map();
|
|
465
|
+
violations = [];
|
|
466
|
+
rootPid;
|
|
467
|
+
constructor(options) {
|
|
468
|
+
this.options = options;
|
|
469
|
+
this.rootPid = options.pid > 0 ? options.pid : null;
|
|
470
|
+
if (this.rootPid !== null) {
|
|
471
|
+
this.processes.set(this.rootPid, { cwd: path.resolve(options.initialCwd), fds: new Map() });
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
eventOptions(pid) {
|
|
475
|
+
return { ...this.options, pid };
|
|
476
|
+
}
|
|
477
|
+
processState(pid) {
|
|
478
|
+
const existing = this.processes.get(pid);
|
|
479
|
+
if (existing)
|
|
480
|
+
return existing;
|
|
481
|
+
const isRoot = this.rootPid === null;
|
|
482
|
+
if (isRoot)
|
|
483
|
+
this.rootPid = pid;
|
|
484
|
+
const state = {
|
|
485
|
+
cwd: isRoot ? path.resolve(this.options.initialCwd) : null,
|
|
486
|
+
fds: new Map(),
|
|
487
|
+
};
|
|
488
|
+
this.processes.set(pid, state);
|
|
489
|
+
const traced = this.options.policy.tracedPids;
|
|
490
|
+
traced?.add?.(pid);
|
|
491
|
+
return state;
|
|
492
|
+
}
|
|
493
|
+
inheritProcess(call, pid) {
|
|
494
|
+
if (!PROCESS_METADATA.has(call.syscall))
|
|
495
|
+
return false;
|
|
496
|
+
const childPid = returnedNumber(call);
|
|
497
|
+
if (succeeded(call) && childPid !== null
|
|
498
|
+
&& ['clone', 'clone3', 'fork', 'vfork'].includes(call.syscall)) {
|
|
499
|
+
this.processes.set(childPid, cloneState(this.processState(pid)));
|
|
500
|
+
const traced = this.options.policy.tracedPids;
|
|
501
|
+
traced?.add?.(childPid);
|
|
502
|
+
}
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
updateSymlinks(call, operands) {
|
|
506
|
+
if (!succeeded(call))
|
|
507
|
+
return;
|
|
508
|
+
const entry = operands[0];
|
|
509
|
+
if ((call.syscall === 'symlink' || call.syscall === 'symlinkat') && entry) {
|
|
510
|
+
const target = decodeQuoted(call.args[0]);
|
|
511
|
+
if (target !== null)
|
|
512
|
+
this.symlinks.set(canonicalPath(entry, this.symlinks, false), target);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (['unlink', 'unlinkat'].includes(call.syscall) && entry) {
|
|
516
|
+
this.symlinks.delete(canonicalPath(entry, this.symlinks, false));
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
if (call.syscall.startsWith('rename') && entry && operands[1]) {
|
|
520
|
+
moveSymlinks(this.symlinks, canonicalPath(entry, this.symlinks, false), canonicalPath(operands[1], this.symlinks, false));
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
updateProcessState(call, state, operands) {
|
|
524
|
+
if (!succeeded(call))
|
|
525
|
+
return;
|
|
526
|
+
if (call.syscall === 'chdir' && operands[0]) {
|
|
527
|
+
state.cwd = canonicalPath(operands[0], this.symlinks);
|
|
528
|
+
}
|
|
529
|
+
const fd = FD_RETURNING_FILE_CALLS.has(call.syscall) ? returnedNumber(call) : null;
|
|
530
|
+
const fdPath = returnedFdPath(call) ?? operands[0];
|
|
531
|
+
if (fd !== null && fdPath)
|
|
532
|
+
state.fds.set(fd, canonicalPath(fdPath, this.symlinks));
|
|
533
|
+
this.updateSymlinks(call, operands);
|
|
534
|
+
}
|
|
535
|
+
classifyCall(call, pid, line, raw) {
|
|
536
|
+
const options = this.eventOptions(pid);
|
|
537
|
+
if (isNetworkCall(call)) {
|
|
538
|
+
this.counts.networkCalls += 1;
|
|
539
|
+
const denied = classifyNetwork(call, options, line, raw);
|
|
540
|
+
if (denied)
|
|
541
|
+
this.violations.push(denied);
|
|
542
|
+
else
|
|
543
|
+
this.counts.allowed += 1;
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
if (this.inheritProcess(call, pid))
|
|
547
|
+
return;
|
|
548
|
+
const state = this.processState(pid);
|
|
549
|
+
const result = classifyFile(call, state, options, line, raw, this.symlinks);
|
|
550
|
+
this.counts.fileCalls += result.operands.length;
|
|
551
|
+
this.violations.push(...result.violations);
|
|
552
|
+
this.counts.allowed += Math.max(0, result.operands.length - result.violations.length);
|
|
553
|
+
this.updateProcessState(call, state, result.operands);
|
|
554
|
+
}
|
|
555
|
+
classifyBody(body, pid, line, raw) {
|
|
556
|
+
const call = parseTraceCall(body);
|
|
557
|
+
if (!call)
|
|
558
|
+
this.violations.push(unknownViolation(null, this.eventOptions(pid), line, raw));
|
|
559
|
+
else
|
|
560
|
+
this.classifyCall(call, pid, line, raw);
|
|
561
|
+
}
|
|
562
|
+
startPending(pid, body, line, raw, syscall) {
|
|
563
|
+
const previous = this.pending.get(pid);
|
|
564
|
+
if (previous)
|
|
565
|
+
this.violations.push(unknownViolation(null, this.eventOptions(pid), previous.line, previous.raw));
|
|
566
|
+
this.pending.set(pid, {
|
|
567
|
+
body: body.slice(0, -'<unfinished ...>'.length), line, raw, syscall,
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
finishPending(pid, resumed, line, raw) {
|
|
571
|
+
const pending = this.pending.get(pid);
|
|
572
|
+
this.pending.delete(pid);
|
|
573
|
+
if (!pending || pending.syscall !== resumed.syscall) {
|
|
574
|
+
this.violations.push(unknownViolation(null, this.eventOptions(pid), line, raw));
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
this.classifyBody(`${pending.body}${resumed.suffix}`, pid, pending.line, `${pending.raw} ${raw}`);
|
|
578
|
+
}
|
|
579
|
+
consume(raw) {
|
|
580
|
+
if (raw.length === 0)
|
|
581
|
+
return;
|
|
582
|
+
this.counts.traceLines += 1;
|
|
583
|
+
const line = this.counts.traceLines;
|
|
584
|
+
const { body, pid } = parsePrefix(raw, this.options.pid);
|
|
585
|
+
if (isSignalMetadata(body))
|
|
586
|
+
return;
|
|
587
|
+
const unfinished = unfinishedSyscall(body);
|
|
588
|
+
if (unfinished)
|
|
589
|
+
return this.startPending(pid, body, line, raw, unfinished);
|
|
590
|
+
const resumed = resumedCall(body);
|
|
591
|
+
if (resumed)
|
|
592
|
+
return this.finishPending(pid, resumed, line, raw);
|
|
593
|
+
this.classifyBody(body, pid, line, raw);
|
|
594
|
+
}
|
|
595
|
+
finish() {
|
|
596
|
+
for (const [pid, pending] of this.pending) {
|
|
597
|
+
this.violations.push(unknownViolation(null, this.eventOptions(pid), pending.line, pending.raw));
|
|
598
|
+
}
|
|
599
|
+
this.pending.clear();
|
|
600
|
+
return { counts: { ...this.counts }, violations: [...this.violations] };
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
export function classifyTraceLines(lines, options) {
|
|
604
|
+
const classifier = new AccessTraceClassifier(options);
|
|
605
|
+
lines.forEach(line => classifier.consume(line));
|
|
606
|
+
return classifier.finish();
|
|
607
|
+
}
|
|
608
|
+
export async function classifyTraceStream(input, options) {
|
|
609
|
+
const classifier = new AccessTraceClassifier(options);
|
|
610
|
+
const lines = createInterface({ input, crlfDelay: Infinity });
|
|
611
|
+
for await (const line of lines)
|
|
612
|
+
classifier.consume(line);
|
|
613
|
+
return classifier.finish();
|
|
614
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type AccessProbeCounts, type AccessViolation } from './access-probe-policy.js';
|
|
2
|
+
import { type PinnedNodeLaunchOptions } from './pinned-node-process.js';
|
|
3
|
+
export type ProbeFailureReason = 'strace_unavailable' | 'ptrace_unavailable' | 'trace_missing' | 'target_failed' | 'probe_timeout' | 'containment_failed';
|
|
4
|
+
export interface NodeAccessProbeOptions extends Omit<PinnedNodeLaunchOptions, 'stdio'> {
|
|
5
|
+
installRoot: string;
|
|
6
|
+
hostHome: string;
|
|
7
|
+
hostCortexHome: string;
|
|
8
|
+
stracePath?: string;
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
supervisorBinary?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface AccessProbeVerdict {
|
|
13
|
+
ok: boolean;
|
|
14
|
+
violations: AccessViolation[];
|
|
15
|
+
counts: AccessProbeCounts;
|
|
16
|
+
failureReason?: ProbeFailureReason;
|
|
17
|
+
targetExitCode?: number | null;
|
|
18
|
+
traceFiles: string[];
|
|
19
|
+
}
|
|
20
|
+
export declare function runNodeAccessProbe(options: NodeAccessProbeOptions): Promise<AccessProbeVerdict>;
|
|
21
|
+
export declare function formatAccessProbeSummary(verdict: AccessProbeVerdict): string;
|