@chroxy/server 0.10.0
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/CONFIG.md +695 -0
- package/README.md +105 -0
- package/hooks/permission-hook.sh +285 -0
- package/package.json +69 -0
- package/scripts/fix-node-pty-helper.js +44 -0
- package/sidecar/Dockerfile +40 -0
- package/sidecar/PROTOCOL.md +691 -0
- package/sidecar/agent.js +1290 -0
- package/sidecar/package-lock.json +43 -0
- package/sidecar/package.json +17 -0
- package/src/activity-registry.js +491 -0
- package/src/anthropic-compatible-config.js +534 -0
- package/src/anthropic-compatible-session.js +579 -0
- package/src/anthropic-openai-shim.js +109 -0
- package/src/anthropic-openai-translate.js +377 -0
- package/src/auth-probes.js +296 -0
- package/src/background-shell-tracker.js +320 -0
- package/src/background-shells.js +125 -0
- package/src/base-session.js +1518 -0
- package/src/billing-budget.js +191 -0
- package/src/billing-canary-monitor.js +203 -0
- package/src/billing-class.js +149 -0
- package/src/bind-host.js +82 -0
- package/src/built-in-tools/bash-exec.js +167 -0
- package/src/built-in-tools/file-ops.js +198 -0
- package/src/built-in-tools/tool-transforms.js +179 -0
- package/src/builtin-commands.js +107 -0
- package/src/byok-compose-state-shared.js +31 -0
- package/src/byok-compose-state.js +234 -0
- package/src/byok-credentials.js +127 -0
- package/src/byok-event-translator.js +136 -0
- package/src/byok-mcp-client.js +404 -0
- package/src/byok-mcp-config.js +159 -0
- package/src/byok-mcp-fleet.js +214 -0
- package/src/byok-mcp-trust.js +175 -0
- package/src/byok-session.js +2019 -0
- package/src/byok-subagent-profiles.js +101 -0
- package/src/byok-tool-executor.js +813 -0
- package/src/byok-tools.js +271 -0
- package/src/channels/PACKAGING.md +181 -0
- package/src/channels/README.md +99 -0
- package/src/channels/chroxy-channel-server.js +248 -0
- package/src/checkpoint-manager.js +515 -0
- package/src/child-stream-guard.js +38 -0
- package/src/claude-channel-session.js +185 -0
- package/src/claude-model-catalog.js +266 -0
- package/src/claude-stream-parser.js +106 -0
- package/src/claude-tui/form-driver.js +874 -0
- package/src/claude-tui/pty-driver.js +520 -0
- package/src/claude-tui/tested-cli-version.js +22 -0
- package/src/claude-tui-attachments.js +342 -0
- package/src/claude-tui-session.js +4029 -0
- package/src/cli/config-cmd.js +34 -0
- package/src/cli/credentials-cmd.js +59 -0
- package/src/cli/deploy-cmd.js +167 -0
- package/src/cli/doctor-cmd.js +52 -0
- package/src/cli/identity-cmd.js +82 -0
- package/src/cli/init-cmd.js +209 -0
- package/src/cli/pages-cmd.js +167 -0
- package/src/cli/pair-code-cmd.js +108 -0
- package/src/cli/pair-discord-cmd.js +100 -0
- package/src/cli/providers-cmd.js +168 -0
- package/src/cli/server-cmd.js +94 -0
- package/src/cli/service-cmd.js +237 -0
- package/src/cli/session-cmd.js +143 -0
- package/src/cli/shared.js +194 -0
- package/src/cli/shell-cmd.js +130 -0
- package/src/cli/status-cmd.js +204 -0
- package/src/cli/tunnel-cmd.js +101 -0
- package/src/cli/update-cmd.js +45 -0
- package/src/cli/worktree-gc-cmd.js +195 -0
- package/src/cli-session.js +1820 -0
- package/src/cli.js +60 -0
- package/src/codex-app-server-client.js +156 -0
- package/src/codex-app-server-session.js +572 -0
- package/src/codex-session.js +604 -0
- package/src/config.js +1757 -0
- package/src/connection-info.js +42 -0
- package/src/connection-locality.js +55 -0
- package/src/content-blocks.js +42 -0
- package/src/control-room/byok-pool.js +130 -0
- package/src/control-room/constants.js +26 -0
- package/src/control-room/containers.js +258 -0
- package/src/control-room/emulators.js +279 -0
- package/src/control-room/handler-factory.js +142 -0
- package/src/control-room/host-prune.js +387 -0
- package/src/control-room/integrations.js +929 -0
- package/src/control-room/repo-runtime-config.js +239 -0
- package/src/control-room/repo-set.js +162 -0
- package/src/control-room/runners.js +479 -0
- package/src/control-room/simulators.js +223 -0
- package/src/control-room/skills-inventory.js +303 -0
- package/src/control-room/survey.js +685 -0
- package/src/control-room/wsl.js +182 -0
- package/src/conversation-scanner.js +270 -0
- package/src/conversation-scope.js +52 -0
- package/src/conversation-search.js +216 -0
- package/src/cost-budget-manager.js +190 -0
- package/src/credential-cipher.js +179 -0
- package/src/credential-store.js +786 -0
- package/src/credential-test.js +145 -0
- package/src/credentials-file.js +58 -0
- package/src/dashboard-next/dist/assets/index-C2ipeGlq.js +111 -0
- package/src/dashboard-next/dist/assets/index-DM-_dISG.css +1 -0
- package/src/dashboard-next/dist/index.html +13 -0
- package/src/deepseek-credentials.js +58 -0
- package/src/deepseek-session.js +192 -0
- package/src/detect-thinking-keyword.js +67 -0
- package/src/dev-preview.js +227 -0
- package/src/devcontainer-config.js +269 -0
- package/src/device-preferences.js +267 -0
- package/src/diagnostics.js +187 -0
- package/src/diff-parser.js +157 -0
- package/src/discord-credentials.js +188 -0
- package/src/discord-pair-delivery.js +113 -0
- package/src/docker-byok-pool-stats.js +221 -0
- package/src/docker-byok-pool.js +841 -0
- package/src/docker-byok-session.js +2172 -0
- package/src/docker-image-allowlist.js +99 -0
- package/src/docker-sdk-session.js +396 -0
- package/src/docker-session.js +382 -0
- package/src/doctor-billing.js +179 -0
- package/src/doctor.js +667 -0
- package/src/duration.js +77 -0
- package/src/environment-manager.js +753 -0
- package/src/environment-statuses.js +20 -0
- package/src/environments/backends/docker.js +931 -0
- package/src/environments/backends/k8s.js +3030 -0
- package/src/environments/backends/rancher.js +326 -0
- package/src/environments/backends/types.js +332 -0
- package/src/event-ingest.js +546 -0
- package/src/event-normalizer.js +1083 -0
- package/src/external-session-registry.js +0 -0
- package/src/file-store.js +52 -0
- package/src/gemini-session.js +498 -0
- package/src/get-public-ip.js +52 -0
- package/src/git.js +15 -0
- package/src/github-webhook.js +256 -0
- package/src/handler-utils.js +663 -0
- package/src/handlers/checkpoint-handlers.js +202 -0
- package/src/handlers/control-room-handlers.js +1945 -0
- package/src/handlers/conversation-handlers.js +201 -0
- package/src/handlers/credential-handlers.js +247 -0
- package/src/handlers/evaluator-handlers.js +94 -0
- package/src/handlers/feature-handlers.js +332 -0
- package/src/handlers/file-handlers.js +142 -0
- package/src/handlers/ide-handlers.js +238 -0
- package/src/handlers/input-handlers.js +1048 -0
- package/src/handlers/pairing-handlers.js +94 -0
- package/src/handlers/repo-handlers.js +295 -0
- package/src/handlers/session-handlers.js +660 -0
- package/src/handlers/settings-handlers.js +818 -0
- package/src/handlers/skills-handlers.js +682 -0
- package/src/handlers/summarize-handlers.js +172 -0
- package/src/handlers/token-handlers.js +57 -0
- package/src/http-oversize.js +51 -0
- package/src/http-routes.js +1158 -0
- package/src/ide/search.js +268 -0
- package/src/ide/symbols.js +423 -0
- package/src/ip-utils.js +85 -0
- package/src/json-state-file.js +169 -0
- package/src/jsonl-reader.js +187 -0
- package/src/jsonl-subprocess-session.js +441 -0
- package/src/keychain.js +458 -0
- package/src/lan-ip.js +38 -0
- package/src/logger.js +388 -0
- package/src/mailbox-route.js +252 -0
- package/src/mcp-tools.js +46 -0
- package/src/message-transform.js +102 -0
- package/src/metrics.js +67 -0
- package/src/model-discovery.js +288 -0
- package/src/models.js +1426 -0
- package/src/notification-prefs.js +544 -0
- package/src/notifications/discord-billing-sink.js +323 -0
- package/src/notifications/discord-webhook-client.js +187 -0
- package/src/notifications/discord-webhook-sink.js +768 -0
- package/src/notifications/expo-push-sink.js +414 -0
- package/src/notifications/ready-body.js +177 -0
- package/src/notifications/sink-registry.js +98 -0
- package/src/notifications/sink.js +82 -0
- package/src/ollama-session.js +221 -0
- package/src/ollama-tags.js +238 -0
- package/src/openai-compatible-session.js +118 -0
- package/src/pages-store.js +255 -0
- package/src/pairing.js +836 -0
- package/src/path-hash-trust-ledger.js +311 -0
- package/src/per-session-settings.js +366 -0
- package/src/permission-audit.js +120 -0
- package/src/permission-hook.js +257 -0
- package/src/permission-manager.js +829 -0
- package/src/permission-resolver.js +144 -0
- package/src/platform.js +139 -0
- package/src/prompt-evaluator.js +386 -0
- package/src/providers.js +436 -0
- package/src/push.js +565 -0
- package/src/rate-limiter.js +433 -0
- package/src/redaction.js +206 -0
- package/src/sdk-session.js +1773 -0
- package/src/semver.js +92 -0
- package/src/server-cli/push-notification-handler.js +240 -0
- package/src/server-cli/server-orchestrator.js +168 -0
- package/src/server-cli/startup-display.js +146 -0
- package/src/server-cli/tunnel-lifecycle-handler.js +219 -0
- package/src/server-cli-child.js +207 -0
- package/src/server-cli.js +1223 -0
- package/src/server-identity.js +383 -0
- package/src/service.js +835 -0
- package/src/session-context.js +49 -0
- package/src/session-lock.js +60 -0
- package/src/session-manager.js +3257 -0
- package/src/session-message-history.js +511 -0
- package/src/session-preset-trust.js +78 -0
- package/src/session-preset.js +458 -0
- package/src/session-state-persistence.js +307 -0
- package/src/session-timeout-manager.js +166 -0
- package/src/session-token-store.js +83 -0
- package/src/shell-approval-info.js +45 -0
- package/src/shell-approval-store.js +163 -0
- package/src/shell-audit.js +63 -0
- package/src/skills-budget.js +181 -0
- package/src/skills-content-validator.js +106 -0
- package/src/skills-frontmatter.js +422 -0
- package/src/skills-loader.js +1893 -0
- package/src/skills-manager.js +256 -0
- package/src/skills-trust.js +532 -0
- package/src/skills-usage.js +335 -0
- package/src/snapshots-store.js +345 -0
- package/src/ssrf-guard.js +130 -0
- package/src/subagent-counter.js +149 -0
- package/src/summarize-session.js +268 -0
- package/src/supervisor.js +929 -0
- package/src/test-client.js +131 -0
- package/src/token-compare.js +28 -0
- package/src/token-manager.js +220 -0
- package/src/tool-result.js +87 -0
- package/src/transcript-tasks.js +334 -0
- package/src/tunnel/base.js +294 -0
- package/src/tunnel/cloudflare.js +281 -0
- package/src/tunnel/index.js +54 -0
- package/src/tunnel-check.js +84 -0
- package/src/turn-tracker.js +0 -0
- package/src/user-shell-registry.js +218 -0
- package/src/user-shell-session.js +411 -0
- package/src/utils/check-dependencies.js +70 -0
- package/src/utils/concurrency.js +24 -0
- package/src/utils/context-window-learn.js +148 -0
- package/src/utils/error-message.js +25 -0
- package/src/utils/format-bytes.js +35 -0
- package/src/utils/preflight.js +143 -0
- package/src/utils/resolve-binary.js +91 -0
- package/src/utils/respawn-rate-limiter.js +59 -0
- package/src/utils/sleep.js +58 -0
- package/src/utils/spawn-env.js +193 -0
- package/src/utils/validation-patterns.js +23 -0
- package/src/utils/win-spawn.js +98 -0
- package/src/version-check.js +16 -0
- package/src/web-task-manager.js +383 -0
- package/src/worktree-gc.js +418 -0
- package/src/worktree-reaper.js +168 -0
- package/src/ws-auth.js +645 -0
- package/src/ws-broadcaster.js +385 -0
- package/src/ws-client-manager.js +476 -0
- package/src/ws-client-sender.js +115 -0
- package/src/ws-file-ops/browser.js +594 -0
- package/src/ws-file-ops/common.js +163 -0
- package/src/ws-file-ops/git.js +298 -0
- package/src/ws-file-ops/index.js +52 -0
- package/src/ws-file-ops/reader.js +595 -0
- package/src/ws-forwarding.js +543 -0
- package/src/ws-handler-context.js +196 -0
- package/src/ws-history.js +1251 -0
- package/src/ws-message-handlers.js +165 -0
- package/src/ws-permissions.js +676 -0
- package/src/ws-schemas.js +119 -0
- package/src/ws-server.js +2866 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
import { readFile, writeFile as fsWriteFile, stat, mkdir, realpath, open } from 'fs/promises'
|
|
2
|
+
import { constants as fsConstants } from 'fs'
|
|
3
|
+
import { resolve, normalize, extname } from 'path'
|
|
4
|
+
import { execFile as execFileCb } from 'child_process'
|
|
5
|
+
import { promisify } from 'util'
|
|
6
|
+
import { parseDiff } from '../diff-parser.js'
|
|
7
|
+
import { GIT } from '../git.js'
|
|
8
|
+
|
|
9
|
+
const execFileAsync = promisify(execFileCb)
|
|
10
|
+
|
|
11
|
+
/** Image extensions to MIME type mapping (module-level to avoid per-call allocation) */
|
|
12
|
+
const IMAGE_MIME = {
|
|
13
|
+
png: 'image/png',
|
|
14
|
+
jpg: 'image/jpeg',
|
|
15
|
+
jpeg: 'image/jpeg',
|
|
16
|
+
gif: 'image/gif',
|
|
17
|
+
webp: 'image/webp',
|
|
18
|
+
ico: 'image/x-icon',
|
|
19
|
+
bmp: 'image/bmp',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* File reading, writing, and diff operations.
|
|
24
|
+
*
|
|
25
|
+
* @param {Function} sendFn - (ws, message) => void
|
|
26
|
+
* @param {Function} resolveSessionCwd - shared CWD resolver
|
|
27
|
+
* @param {Function} validatePathWithinCwd - shared path validator
|
|
28
|
+
* @returns {Object} reader operation methods
|
|
29
|
+
*/
|
|
30
|
+
export function createReaderOps(sendFn, resolveSessionCwd, validatePathWithinCwd) {
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Read file content at a given path within the session CWD.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} [requestId] - #6502: optional request nonce echoed back on
|
|
36
|
+
* every `file_content` reply so the dashboard can correlate replies to the
|
|
37
|
+
* latest in-flight request. Omitted from the payload when not provided.
|
|
38
|
+
*/
|
|
39
|
+
async function readFileContent(ws, requestedPath, sessionCwd, requestId) {
|
|
40
|
+
// Attach the request nonce (when present) to every file_content reply so a
|
|
41
|
+
// superseded read can be dropped client-side without echoed-path matching.
|
|
42
|
+
const send = requestId === undefined
|
|
43
|
+
? (payload) => sendFn(ws, payload)
|
|
44
|
+
: (payload) => sendFn(ws, { ...payload, requestId })
|
|
45
|
+
if (!sessionCwd) {
|
|
46
|
+
send({
|
|
47
|
+
type: 'file_content',
|
|
48
|
+
path: null,
|
|
49
|
+
content: null,
|
|
50
|
+
language: null,
|
|
51
|
+
size: null,
|
|
52
|
+
truncated: false,
|
|
53
|
+
error: 'File reading is not available in this mode',
|
|
54
|
+
})
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!requestedPath || typeof requestedPath !== 'string' || !requestedPath.trim()) {
|
|
59
|
+
send({
|
|
60
|
+
type: 'file_content',
|
|
61
|
+
path: null,
|
|
62
|
+
content: null,
|
|
63
|
+
language: null,
|
|
64
|
+
size: null,
|
|
65
|
+
truncated: false,
|
|
66
|
+
error: 'No file path provided',
|
|
67
|
+
})
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let absPath = null
|
|
72
|
+
try {
|
|
73
|
+
absPath = normalize(resolve(sessionCwd, requestedPath.trim()))
|
|
74
|
+
|
|
75
|
+
// Resolve symlinks before validation to prevent TOCTOU attacks.
|
|
76
|
+
// realpath() is called here so both the validation and the subsequent read
|
|
77
|
+
// use the same canonical path — a symlink swapped between calls cannot
|
|
78
|
+
// redirect the read outside the workspace.
|
|
79
|
+
let resolvedAbsPath
|
|
80
|
+
try {
|
|
81
|
+
resolvedAbsPath = await realpath(absPath)
|
|
82
|
+
} catch (err) {
|
|
83
|
+
if (err.code === 'ENOENT') {
|
|
84
|
+
// Before surfacing "File not found", enforce workspace boundary.
|
|
85
|
+
// A nonexistent path outside the root must return Access denied to
|
|
86
|
+
// avoid leaking filesystem existence information as an oracle.
|
|
87
|
+
const cwdReal = await resolveSessionCwd(sessionCwd)
|
|
88
|
+
const lexicallyWithinCwd = absPath.startsWith(cwdReal + '/') || absPath === cwdReal
|
|
89
|
+
if (!lexicallyWithinCwd) {
|
|
90
|
+
send({
|
|
91
|
+
type: 'file_content',
|
|
92
|
+
path: absPath,
|
|
93
|
+
content: null,
|
|
94
|
+
language: null,
|
|
95
|
+
size: null,
|
|
96
|
+
truncated: false,
|
|
97
|
+
error: 'Access denied: file reading is restricted to the project directory',
|
|
98
|
+
})
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
send({
|
|
102
|
+
type: 'file_content',
|
|
103
|
+
path: absPath,
|
|
104
|
+
content: null,
|
|
105
|
+
language: null,
|
|
106
|
+
size: null,
|
|
107
|
+
truncated: false,
|
|
108
|
+
error: 'File not found',
|
|
109
|
+
})
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
throw err
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const { valid } = await validatePathWithinCwd(resolvedAbsPath, sessionCwd)
|
|
116
|
+
if (!valid) {
|
|
117
|
+
send({
|
|
118
|
+
type: 'file_content',
|
|
119
|
+
path: absPath,
|
|
120
|
+
content: null,
|
|
121
|
+
language: null,
|
|
122
|
+
size: null,
|
|
123
|
+
truncated: false,
|
|
124
|
+
error: 'Access denied: file reading is restricted to the project directory',
|
|
125
|
+
})
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const fileStat = await stat(resolvedAbsPath)
|
|
130
|
+
if (fileStat.isDirectory()) {
|
|
131
|
+
send({
|
|
132
|
+
type: 'file_content',
|
|
133
|
+
path: absPath,
|
|
134
|
+
content: null,
|
|
135
|
+
language: null,
|
|
136
|
+
size: null,
|
|
137
|
+
truncated: false,
|
|
138
|
+
error: 'Cannot read a directory',
|
|
139
|
+
})
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (fileStat.size > 512 * 1024) {
|
|
144
|
+
send({
|
|
145
|
+
type: 'file_content',
|
|
146
|
+
path: absPath,
|
|
147
|
+
content: null,
|
|
148
|
+
language: null,
|
|
149
|
+
size: fileStat.size,
|
|
150
|
+
truncated: false,
|
|
151
|
+
error: 'File too large (max 512KB)',
|
|
152
|
+
})
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Open with O_NOFOLLOW to close the post-validation TOCTOU window:
|
|
157
|
+
// if the file at resolvedAbsPath was replaced with a symlink between
|
|
158
|
+
// validatePathWithinCwd() and this open(), the kernel rejects it (ELOOP).
|
|
159
|
+
let buf
|
|
160
|
+
{
|
|
161
|
+
let fh
|
|
162
|
+
try {
|
|
163
|
+
fh = await open(resolvedAbsPath, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW)
|
|
164
|
+
buf = await fh.readFile()
|
|
165
|
+
} catch (openErr) {
|
|
166
|
+
if (openErr.code === 'ELOOP') {
|
|
167
|
+
// Symlink appeared at the canonical path after validation — reject
|
|
168
|
+
send({
|
|
169
|
+
type: 'file_content',
|
|
170
|
+
path: absPath,
|
|
171
|
+
content: null,
|
|
172
|
+
language: null,
|
|
173
|
+
size: null,
|
|
174
|
+
truncated: false,
|
|
175
|
+
error: 'Access denied: file reading is restricted to the project directory',
|
|
176
|
+
})
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
throw openErr
|
|
180
|
+
} finally {
|
|
181
|
+
await fh?.close()
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const ext = extname(absPath).slice(1).toLowerCase()
|
|
185
|
+
|
|
186
|
+
// Image files: send as base64 data URL for preview
|
|
187
|
+
// SVG excluded — it's an active document format (scripts/external refs); render as text instead
|
|
188
|
+
if (IMAGE_MIME[ext]) {
|
|
189
|
+
const dataUrl = `data:${IMAGE_MIME[ext]};base64,${buf.toString('base64')}`
|
|
190
|
+
send({
|
|
191
|
+
type: 'file_content',
|
|
192
|
+
path: absPath,
|
|
193
|
+
content: dataUrl,
|
|
194
|
+
language: 'image',
|
|
195
|
+
size: fileStat.size,
|
|
196
|
+
truncated: false,
|
|
197
|
+
error: null,
|
|
198
|
+
})
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Binary detection: check first 8KB for null bytes
|
|
203
|
+
const checkLen = Math.min(buf.length, 8192)
|
|
204
|
+
for (let i = 0; i < checkLen; i++) {
|
|
205
|
+
if (buf[i] === 0) {
|
|
206
|
+
send({
|
|
207
|
+
type: 'file_content',
|
|
208
|
+
path: absPath,
|
|
209
|
+
content: null,
|
|
210
|
+
language: null,
|
|
211
|
+
size: fileStat.size,
|
|
212
|
+
truncated: false,
|
|
213
|
+
error: 'Binary file cannot be displayed',
|
|
214
|
+
})
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
let content = buf.toString('utf-8')
|
|
220
|
+
let truncated = false
|
|
221
|
+
if (content.length > 100 * 1024) {
|
|
222
|
+
content = content.slice(0, 100 * 1024)
|
|
223
|
+
truncated = true
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
send({
|
|
227
|
+
type: 'file_content',
|
|
228
|
+
path: absPath,
|
|
229
|
+
content,
|
|
230
|
+
language: ext || null,
|
|
231
|
+
size: fileStat.size,
|
|
232
|
+
truncated,
|
|
233
|
+
error: null,
|
|
234
|
+
})
|
|
235
|
+
} catch (err) {
|
|
236
|
+
let errorMessage
|
|
237
|
+
if (err.code === 'ENOENT') errorMessage = 'File not found'
|
|
238
|
+
else if (err.code === 'EACCES') errorMessage = 'Permission denied'
|
|
239
|
+
else errorMessage = err.message || 'Unknown error'
|
|
240
|
+
|
|
241
|
+
send({
|
|
242
|
+
type: 'file_content',
|
|
243
|
+
path: absPath || requestedPath || null,
|
|
244
|
+
content: null,
|
|
245
|
+
language: null,
|
|
246
|
+
size: null,
|
|
247
|
+
truncated: false,
|
|
248
|
+
error: errorMessage,
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Write file content at a given path within the session CWD */
|
|
254
|
+
async function writeFileContent(ws, requestedPath, content, sessionCwd) {
|
|
255
|
+
if (!sessionCwd) {
|
|
256
|
+
sendFn(ws, {
|
|
257
|
+
type: 'write_file_result',
|
|
258
|
+
path: null,
|
|
259
|
+
error: 'File writing is not available in this mode',
|
|
260
|
+
})
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (!requestedPath || typeof requestedPath !== 'string' || !requestedPath.trim()) {
|
|
265
|
+
sendFn(ws, {
|
|
266
|
+
type: 'write_file_result',
|
|
267
|
+
path: null,
|
|
268
|
+
error: 'No file path provided',
|
|
269
|
+
})
|
|
270
|
+
return
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// 5MB size limit
|
|
274
|
+
const MAX_SIZE = 5 * 1024 * 1024
|
|
275
|
+
if (typeof content === 'string' && content.length > MAX_SIZE) {
|
|
276
|
+
sendFn(ws, {
|
|
277
|
+
type: 'write_file_result',
|
|
278
|
+
path: requestedPath,
|
|
279
|
+
error: 'Content too large (max 5MB)',
|
|
280
|
+
})
|
|
281
|
+
return
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let absPath = null
|
|
285
|
+
try {
|
|
286
|
+
absPath = normalize(resolve(sessionCwd, requestedPath.trim()))
|
|
287
|
+
|
|
288
|
+
const cwdReal = await resolveSessionCwd(sessionCwd)
|
|
289
|
+
|
|
290
|
+
// Resolve absPath through realpath of the session CWD to handle symlinks
|
|
291
|
+
// (e.g. macOS /var → /private/var)
|
|
292
|
+
const absInCwd = normalize(resolve(cwdReal, requestedPath.trim()))
|
|
293
|
+
|
|
294
|
+
// Determine whether the target file already exists so we can choose
|
|
295
|
+
// between O_NOFOLLOW (existing) and parent-validated creation (new).
|
|
296
|
+
let resolvedTarget
|
|
297
|
+
let fileExists = false
|
|
298
|
+
try {
|
|
299
|
+
resolvedTarget = await realpath(absInCwd)
|
|
300
|
+
fileExists = true
|
|
301
|
+
} catch (err) {
|
|
302
|
+
if (err.code === 'ENOENT') {
|
|
303
|
+
// File doesn't exist yet — validate parent directory instead.
|
|
304
|
+
// Use the lexical path for the new-file case.
|
|
305
|
+
resolvedTarget = absInCwd
|
|
306
|
+
} else {
|
|
307
|
+
throw err
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (fileExists) {
|
|
312
|
+
// Existing file: validate the resolved (symlink-followed) path
|
|
313
|
+
const { valid: writeValid } = await validatePathWithinCwd(resolvedTarget, sessionCwd)
|
|
314
|
+
if (!writeValid) {
|
|
315
|
+
sendFn(ws, {
|
|
316
|
+
type: 'write_file_result',
|
|
317
|
+
path: requestedPath,
|
|
318
|
+
error: 'Access denied: file writing is restricted to the project directory',
|
|
319
|
+
})
|
|
320
|
+
return
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
// New file: validate the lexical path is within CWD
|
|
324
|
+
const { valid: writeValid } = await validatePathWithinCwd(absInCwd, sessionCwd)
|
|
325
|
+
if (!writeValid) {
|
|
326
|
+
sendFn(ws, {
|
|
327
|
+
type: 'write_file_result',
|
|
328
|
+
path: requestedPath,
|
|
329
|
+
error: 'Access denied: file writing is restricted to the project directory',
|
|
330
|
+
})
|
|
331
|
+
return
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
absPath = fileExists ? resolvedTarget : absInCwd
|
|
335
|
+
|
|
336
|
+
// Create parent directories if needed
|
|
337
|
+
await mkdir(resolve(absPath, '..'), { recursive: true })
|
|
338
|
+
|
|
339
|
+
// Write the file using O_NOFOLLOW to close the post-validation TOCTOU
|
|
340
|
+
// window: if a symlink is swapped in at absPath between validation and
|
|
341
|
+
// this open(), the kernel rejects it with ELOOP.
|
|
342
|
+
const data = Buffer.from(content || '', 'utf-8')
|
|
343
|
+
{
|
|
344
|
+
let fh
|
|
345
|
+
try {
|
|
346
|
+
const flags = fileExists
|
|
347
|
+
? fsConstants.O_WRONLY | fsConstants.O_NOFOLLOW | fsConstants.O_TRUNC
|
|
348
|
+
: fsConstants.O_WRONLY | fsConstants.O_NOFOLLOW | fsConstants.O_CREAT | fsConstants.O_EXCL
|
|
349
|
+
fh = await open(absPath, flags, 0o666)
|
|
350
|
+
await fh.writeFile(data)
|
|
351
|
+
} catch (openErr) {
|
|
352
|
+
if (openErr.code === 'ELOOP') {
|
|
353
|
+
// Symlink appeared at the target path after validation — reject
|
|
354
|
+
sendFn(ws, {
|
|
355
|
+
type: 'write_file_result',
|
|
356
|
+
path: requestedPath,
|
|
357
|
+
error: 'Access denied: file writing is restricted to the project directory',
|
|
358
|
+
})
|
|
359
|
+
return
|
|
360
|
+
}
|
|
361
|
+
if (openErr.code === 'EEXIST') {
|
|
362
|
+
// Race: file was created between our existence check and O_EXCL open.
|
|
363
|
+
// Retry once using the existing-file path (O_TRUNC without O_EXCL).
|
|
364
|
+
try {
|
|
365
|
+
fh = await open(absPath, fsConstants.O_WRONLY | fsConstants.O_NOFOLLOW | fsConstants.O_TRUNC, 0o666)
|
|
366
|
+
await fh.writeFile(data)
|
|
367
|
+
} catch (retryErr) {
|
|
368
|
+
if (retryErr.code === 'ELOOP') {
|
|
369
|
+
sendFn(ws, {
|
|
370
|
+
type: 'write_file_result',
|
|
371
|
+
path: requestedPath,
|
|
372
|
+
error: 'Access denied: file writing is restricted to the project directory',
|
|
373
|
+
})
|
|
374
|
+
return
|
|
375
|
+
}
|
|
376
|
+
throw retryErr
|
|
377
|
+
}
|
|
378
|
+
} else {
|
|
379
|
+
throw openErr
|
|
380
|
+
}
|
|
381
|
+
} finally {
|
|
382
|
+
await fh?.close()
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
sendFn(ws, {
|
|
387
|
+
type: 'write_file_result',
|
|
388
|
+
path: absPath,
|
|
389
|
+
error: null,
|
|
390
|
+
})
|
|
391
|
+
} catch (err) {
|
|
392
|
+
let errorMessage
|
|
393
|
+
if (err.code === 'EACCES') errorMessage = 'Permission denied'
|
|
394
|
+
else errorMessage = err.message || 'Unknown error'
|
|
395
|
+
|
|
396
|
+
sendFn(ws, {
|
|
397
|
+
type: 'write_file_result',
|
|
398
|
+
path: absPath || requestedPath || null,
|
|
399
|
+
error: errorMessage,
|
|
400
|
+
})
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Get git diff for uncommitted changes in the session CWD */
|
|
405
|
+
async function getDiff(ws, base, sessionCwd) {
|
|
406
|
+
if (!sessionCwd) {
|
|
407
|
+
sendFn(ws, {
|
|
408
|
+
type: 'diff_result',
|
|
409
|
+
files: [],
|
|
410
|
+
error: 'Diff is not available in this mode',
|
|
411
|
+
})
|
|
412
|
+
return
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
try {
|
|
416
|
+
const cwdReal = await resolveSessionCwd(sessionCwd)
|
|
417
|
+
|
|
418
|
+
// Check if the directory is a git repository before running git commands
|
|
419
|
+
try {
|
|
420
|
+
await execFileAsync(GIT, ['rev-parse', '--git-dir'], {
|
|
421
|
+
cwd: cwdReal,
|
|
422
|
+
timeout: 5000,
|
|
423
|
+
})
|
|
424
|
+
} catch (revParseErr) {
|
|
425
|
+
const stderr = (revParseErr.stderr || revParseErr.message || '').toLowerCase()
|
|
426
|
+
const isNotGitRepo = stderr.includes('not a git repository') ||
|
|
427
|
+
revParseErr.code === 128
|
|
428
|
+
sendFn(ws, {
|
|
429
|
+
type: 'diff_result',
|
|
430
|
+
files: [],
|
|
431
|
+
error: isNotGitRepo
|
|
432
|
+
? 'Not a git repository'
|
|
433
|
+
: `Git error: ${revParseErr.message || 'unknown failure'}`,
|
|
434
|
+
})
|
|
435
|
+
return
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const rawBase = (typeof base === 'string' && base.trim()) ? base.trim() : 'HEAD'
|
|
439
|
+
// Validate ref name to prevent git flag injection
|
|
440
|
+
const diffBase = /^[a-zA-Z0-9._\-\/~^@{}:]+$/.test(rawBase) ? rawBase : 'HEAD'
|
|
441
|
+
|
|
442
|
+
let diffOutput = ''
|
|
443
|
+
try {
|
|
444
|
+
const { stdout } = await execFileAsync(GIT, ['diff', diffBase], {
|
|
445
|
+
cwd: cwdReal,
|
|
446
|
+
maxBuffer: 2 * 1024 * 1024,
|
|
447
|
+
timeout: 10000,
|
|
448
|
+
})
|
|
449
|
+
diffOutput = stdout
|
|
450
|
+
} catch (err) {
|
|
451
|
+
if (err.message && err.message.includes('unknown revision')) {
|
|
452
|
+
try {
|
|
453
|
+
const { stdout } = await execFileAsync(GIT, ['diff'], {
|
|
454
|
+
cwd: cwdReal,
|
|
455
|
+
maxBuffer: 2 * 1024 * 1024,
|
|
456
|
+
timeout: 10000,
|
|
457
|
+
})
|
|
458
|
+
diffOutput = stdout
|
|
459
|
+
} catch (innerErr) {
|
|
460
|
+
sendFn(ws, {
|
|
461
|
+
type: 'diff_result',
|
|
462
|
+
files: [],
|
|
463
|
+
error: innerErr.message || 'Failed to run git diff',
|
|
464
|
+
})
|
|
465
|
+
return
|
|
466
|
+
}
|
|
467
|
+
} else {
|
|
468
|
+
sendFn(ws, {
|
|
469
|
+
type: 'diff_result',
|
|
470
|
+
files: [],
|
|
471
|
+
error: err.message || 'Failed to run git diff',
|
|
472
|
+
})
|
|
473
|
+
return
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Also get staged changes if diffBase is HEAD
|
|
478
|
+
if (diffBase === 'HEAD') {
|
|
479
|
+
try {
|
|
480
|
+
const { stdout: stagedOutput } = await execFileAsync(GIT, ['diff', '--cached', 'HEAD'], {
|
|
481
|
+
cwd: cwdReal,
|
|
482
|
+
maxBuffer: 2 * 1024 * 1024,
|
|
483
|
+
timeout: 10000,
|
|
484
|
+
})
|
|
485
|
+
if (stagedOutput) {
|
|
486
|
+
diffOutput = (diffOutput ? diffOutput + '\n' : '') + stagedOutput
|
|
487
|
+
}
|
|
488
|
+
} catch {
|
|
489
|
+
// Ignore errors for staged diff
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const files = diffOutput.trim() ? parseDiff(diffOutput) : []
|
|
494
|
+
|
|
495
|
+
// Deduplicate files that appear in both unstaged and staged diffs
|
|
496
|
+
const seen = new Map()
|
|
497
|
+
for (const file of files) {
|
|
498
|
+
if (seen.has(file.path)) {
|
|
499
|
+
const existing = seen.get(file.path)
|
|
500
|
+
existing.hunks.push(...file.hunks)
|
|
501
|
+
existing.additions += file.additions
|
|
502
|
+
existing.deletions += file.deletions
|
|
503
|
+
} else {
|
|
504
|
+
seen.set(file.path, file)
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// Discover untracked files (new files not yet staged)
|
|
509
|
+
try {
|
|
510
|
+
const { stdout: untrackedOutput } = await execFileAsync(
|
|
511
|
+
GIT, ['ls-files', '--others', '--exclude-standard'],
|
|
512
|
+
{ cwd: cwdReal, maxBuffer: 512 * 1024, timeout: 5000 }
|
|
513
|
+
)
|
|
514
|
+
if (untrackedOutput.trim()) {
|
|
515
|
+
const untrackedPaths = untrackedOutput.trim().split('\n')
|
|
516
|
+
.filter(p => p && !seen.has(p))
|
|
517
|
+
.sort()
|
|
518
|
+
.slice(0, 10)
|
|
519
|
+
|
|
520
|
+
const MAX_UNTRACKED_SIZE = 50 * 1024
|
|
521
|
+
for (const filePath of untrackedPaths) {
|
|
522
|
+
try {
|
|
523
|
+
const absPath = resolve(cwdReal, filePath)
|
|
524
|
+
const validation = await validatePathWithinCwd(absPath, sessionCwd)
|
|
525
|
+
if (!validation.valid) continue
|
|
526
|
+
const fileStat = await stat(validation.realPath)
|
|
527
|
+
if (!fileStat.isFile()) continue
|
|
528
|
+
|
|
529
|
+
let lines, additions
|
|
530
|
+
if (fileStat.size > MAX_UNTRACKED_SIZE) {
|
|
531
|
+
lines = [{ type: 'context', content: `File too large to preview (${(fileStat.size / 1024).toFixed(1)} KB)` }]
|
|
532
|
+
additions = 0
|
|
533
|
+
} else {
|
|
534
|
+
const buf = await readFile(validation.realPath)
|
|
535
|
+
const checkLen = Math.min(buf.length, 8192)
|
|
536
|
+
let isBinary = false
|
|
537
|
+
for (let i = 0; i < checkLen; i++) {
|
|
538
|
+
if (buf[i] === 0) {
|
|
539
|
+
isBinary = true
|
|
540
|
+
break
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
if (isBinary) {
|
|
544
|
+
lines = [{ type: 'context', content: 'Binary file — not shown' }]
|
|
545
|
+
additions = 0
|
|
546
|
+
} else {
|
|
547
|
+
const content = buf.toString('utf-8')
|
|
548
|
+
const contentLines = content.split('\n')
|
|
549
|
+
if (contentLines.length > 0 && contentLines[contentLines.length - 1] === '') {
|
|
550
|
+
contentLines.pop()
|
|
551
|
+
}
|
|
552
|
+
lines = contentLines.map(l => ({ type: 'addition', content: l }))
|
|
553
|
+
additions = lines.length
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
seen.set(filePath, {
|
|
558
|
+
path: filePath,
|
|
559
|
+
status: 'untracked',
|
|
560
|
+
additions,
|
|
561
|
+
deletions: 0,
|
|
562
|
+
hunks: [{
|
|
563
|
+
header: 'New untracked file',
|
|
564
|
+
lines,
|
|
565
|
+
}],
|
|
566
|
+
})
|
|
567
|
+
} catch {
|
|
568
|
+
// Skip files that can't be read
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
} catch {
|
|
573
|
+
// Ignore ls-files errors
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
sendFn(ws, {
|
|
577
|
+
type: 'diff_result',
|
|
578
|
+
files: Array.from(seen.values()),
|
|
579
|
+
error: null,
|
|
580
|
+
})
|
|
581
|
+
} catch (err) {
|
|
582
|
+
sendFn(ws, {
|
|
583
|
+
type: 'diff_result',
|
|
584
|
+
files: [],
|
|
585
|
+
error: err.message || 'Unknown error',
|
|
586
|
+
})
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
return {
|
|
591
|
+
readFile: readFileContent,
|
|
592
|
+
writeFile: writeFileContent,
|
|
593
|
+
getDiff,
|
|
594
|
+
}
|
|
595
|
+
}
|