@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,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub webhook receiver for the Control Room repo-events feed (#5966, epic #5422).
|
|
3
|
+
*
|
|
4
|
+
* Accepts GitHub webhook deliveries (`POST /api/github/webhook`), verifies the
|
|
5
|
+
* `X-Hub-Signature-256` HMAC against the configured secret (constant-time, over
|
|
6
|
+
* the RAW body before any parse), normalizes the events the Control Room surfaces
|
|
7
|
+
* (push / pull_request / issues / ping) into a compact repo-event, and pushes it
|
|
8
|
+
* onto a bounded in-memory store the Control Room pane drains. (The WS broadcast +
|
|
9
|
+
* dashboard pane that read this store are separate #5966 sub-tasks.)
|
|
10
|
+
*
|
|
11
|
+
* Security: the HMAC is the ONLY auth — an unsigned / mis-signed delivery is
|
|
12
|
+
* rejected 401 with no detail, constant-time, BEFORE the payload is parsed or
|
|
13
|
+
* trusted. With no secret configured the endpoint is inert (503), so it can ship
|
|
14
|
+
* dark and light up only once the operator sets a secret + points GitHub at it.
|
|
15
|
+
*/
|
|
16
|
+
import { createHmac, timingSafeEqual } from 'node:crypto'
|
|
17
|
+
import { RateLimiter, getRateLimitKey } from './rate-limiter.js'
|
|
18
|
+
import { sendOversizeResponse } from './http-oversize.js'
|
|
19
|
+
import { createLogger } from './logger.js'
|
|
20
|
+
|
|
21
|
+
const log = createLogger('github-webhook')
|
|
22
|
+
|
|
23
|
+
/** Max accepted delivery body. GitHub caps payloads at 25 MiB; most are tiny. */
|
|
24
|
+
export const MAX_WEBHOOK_BYTES = 2 * 1024 * 1024 // 2 MiB
|
|
25
|
+
/** Bounded repo-event ring capacity. */
|
|
26
|
+
export const REPO_EVENT_STORE_CAP = 200
|
|
27
|
+
|
|
28
|
+
const WEBHOOK_WINDOW_MS = 60_000
|
|
29
|
+
const WEBHOOK_MAX_PER_WINDOW = 120
|
|
30
|
+
const WEBHOOK_BURST = 30
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Verify a GitHub `X-Hub-Signature-256` header against the raw request body.
|
|
34
|
+
* Constant-time for equal-length inputs; returns false on ANY malformed input
|
|
35
|
+
* (missing secret, wrong prefix, length mismatch) and never throws.
|
|
36
|
+
*
|
|
37
|
+
* @param {Buffer|string} rawBody - the EXACT bytes GitHub signed
|
|
38
|
+
* @param {unknown} signatureHeader - the `X-Hub-Signature-256` header value
|
|
39
|
+
* @param {string} secret - the configured webhook secret
|
|
40
|
+
* @returns {boolean}
|
|
41
|
+
*/
|
|
42
|
+
export function verifyGithubSignature(rawBody, signatureHeader, secret) {
|
|
43
|
+
if (typeof secret !== 'string' || secret.length === 0) return false
|
|
44
|
+
if (typeof signatureHeader !== 'string' || !signatureHeader.startsWith('sha256=')) return false
|
|
45
|
+
const body = Buffer.isBuffer(rawBody) ? rawBody : Buffer.from(String(rawBody ?? ''), 'utf8')
|
|
46
|
+
const expected = `sha256=${createHmac('sha256', secret).update(body).digest('hex')}`
|
|
47
|
+
const got = Buffer.from(signatureHeader)
|
|
48
|
+
const want = Buffer.from(expected)
|
|
49
|
+
// timingSafeEqual throws on length mismatch — guard so a wrong-length signature
|
|
50
|
+
// is a plain `false`, not an exception path (and no length-based timing oracle).
|
|
51
|
+
if (got.length !== want.length) return false
|
|
52
|
+
return timingSafeEqual(got, want)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Normalize a GitHub webhook payload into a compact repo-event. Returns null for
|
|
57
|
+
* event types the Control Room does not surface (the caller then accepts-and-skips).
|
|
58
|
+
*
|
|
59
|
+
* @param {string} eventType - the `X-GitHub-Event` header value
|
|
60
|
+
* @param {object} payload - the parsed webhook body
|
|
61
|
+
* @param {{ now?: () => Date }} [deps]
|
|
62
|
+
* @returns {object|null}
|
|
63
|
+
*/
|
|
64
|
+
export function normalizeGithubEvent(eventType, payload, { now = () => new Date() } = {}) {
|
|
65
|
+
if (!payload || typeof payload !== 'object') return null
|
|
66
|
+
const repo = payload.repository?.full_name || payload.repository?.name || null
|
|
67
|
+
const actor = payload.sender?.login || null
|
|
68
|
+
const base = { kind: eventType, repo, actor, at: now().toISOString() }
|
|
69
|
+
|
|
70
|
+
switch (eventType) {
|
|
71
|
+
case 'push': {
|
|
72
|
+
const ref = typeof payload.ref === 'string' ? payload.ref : null
|
|
73
|
+
const branch = ref ? ref.replace(/^refs\/heads\//, '') : null
|
|
74
|
+
const count = Array.isArray(payload.commits) ? payload.commits.length : 0
|
|
75
|
+
const head = typeof payload.head_commit?.message === 'string' ? payload.head_commit.message.split('\n')[0] : null
|
|
76
|
+
return {
|
|
77
|
+
...base,
|
|
78
|
+
branch,
|
|
79
|
+
title: head,
|
|
80
|
+
url: payload.head_commit?.url || payload.compare || null,
|
|
81
|
+
summary: `pushed ${count} commit${count === 1 ? '' : 's'} to ${branch ?? ref ?? '(unknown ref)'}`,
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
case 'pull_request': {
|
|
85
|
+
const pr = payload.pull_request || {}
|
|
86
|
+
return {
|
|
87
|
+
...base,
|
|
88
|
+
action: payload.action || null,
|
|
89
|
+
number: typeof pr.number === 'number' ? pr.number : null,
|
|
90
|
+
title: pr.title || null,
|
|
91
|
+
url: pr.html_url || null,
|
|
92
|
+
summary: `${payload.action || 'updated'} PR #${pr.number ?? '?'}`,
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
case 'issues': {
|
|
96
|
+
const issue = payload.issue || {}
|
|
97
|
+
return {
|
|
98
|
+
...base,
|
|
99
|
+
action: payload.action || null,
|
|
100
|
+
number: typeof issue.number === 'number' ? issue.number : null,
|
|
101
|
+
title: issue.title || null,
|
|
102
|
+
url: issue.html_url || null,
|
|
103
|
+
summary: `${payload.action || 'updated'} issue #${issue.number ?? '?'}`,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
case 'ping':
|
|
107
|
+
return { ...base, title: payload.zen || null, url: null, summary: 'webhook configured (ping)' }
|
|
108
|
+
default:
|
|
109
|
+
return null // unsurfaced event type
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Bounded FIFO store of normalized repo-events. The Control Room pane drains it. */
|
|
114
|
+
export class RepoEventStore {
|
|
115
|
+
constructor({ cap = REPO_EVENT_STORE_CAP } = {}) {
|
|
116
|
+
this._cap = cap > 0 ? cap : REPO_EVENT_STORE_CAP
|
|
117
|
+
this._events = []
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
push(event) {
|
|
121
|
+
this._events.push(event)
|
|
122
|
+
const overflow = this._events.length - this._cap
|
|
123
|
+
if (overflow > 0) this._events.splice(0, overflow)
|
|
124
|
+
return event
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Most-recent-last; `limit` returns the tail. */
|
|
128
|
+
list({ limit } = {}) {
|
|
129
|
+
return typeof limit === 'number' && limit >= 0 ? this._events.slice(-limit) : this._events.slice()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
get size() {
|
|
133
|
+
return this._events.length
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function sendJson(res, status, body, extraHeaders = {}) {
|
|
138
|
+
const payload = JSON.stringify(body)
|
|
139
|
+
res.writeHead(status, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(payload), ...extraHeaders })
|
|
140
|
+
res.end(payload)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Resolve the webhook secret. Prefers an explicit `server._githubWebhookSecret`
|
|
145
|
+
* (set from config at startup), falling back to `$GITHUB_WEBHOOK_SECRET`.
|
|
146
|
+
*/
|
|
147
|
+
function resolveWebhookSecret(server) {
|
|
148
|
+
if (typeof server?._githubWebhookSecret === 'string' && server._githubWebhookSecret.length > 0) {
|
|
149
|
+
return server._githubWebhookSecret
|
|
150
|
+
}
|
|
151
|
+
const env = process.env.GITHUB_WEBHOOK_SECRET
|
|
152
|
+
return typeof env === 'string' && env.length > 0 ? env : ''
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Handle `POST /api/github/webhook`. Gate order:
|
|
157
|
+
* 1. per-IP rate limit (pre-auth; 429 + Retry-After)
|
|
158
|
+
* 2. secret configured? (503 if not — inert until set)
|
|
159
|
+
* 3. read RAW body (capped; 413)
|
|
160
|
+
* 4. HMAC-verify the X-Hub-Signature-256 over the raw body (401, constant-time)
|
|
161
|
+
* 5. parse JSON (400) + normalize the X-GitHub-Event (202 accept-and-skip if unsurfaced)
|
|
162
|
+
* 6. push onto the bounded RepoEventStore (lazily created on the server) → 202
|
|
163
|
+
*/
|
|
164
|
+
export function handleGithubWebhook(server, req, res) {
|
|
165
|
+
if (!server._githubWebhookRateLimiter) {
|
|
166
|
+
server._githubWebhookRateLimiter = new RateLimiter({
|
|
167
|
+
windowMs: WEBHOOK_WINDOW_MS,
|
|
168
|
+
maxMessages: WEBHOOK_MAX_PER_WINDOW,
|
|
169
|
+
burst: WEBHOOK_BURST,
|
|
170
|
+
name: 'github-webhook',
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
const clientIp = getRateLimitKey(req.socket?.remoteAddress || '', req)
|
|
174
|
+
const limit = server._githubWebhookRateLimiter.check(clientIp)
|
|
175
|
+
if (!limit.allowed) {
|
|
176
|
+
sendJson(res, 429, { error: 'rate limited' }, { 'Retry-After': Math.ceil(limit.retryAfterMs / 1000) })
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const secret = resolveWebhookSecret(server)
|
|
181
|
+
if (!secret) {
|
|
182
|
+
sendJson(res, 503, { error: 'github webhook receiver not configured' })
|
|
183
|
+
return
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const chunks = []
|
|
187
|
+
let bytes = 0
|
|
188
|
+
let oversized = false
|
|
189
|
+
req.on('data', (chunk) => {
|
|
190
|
+
if (oversized) return
|
|
191
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)
|
|
192
|
+
bytes += buf.length
|
|
193
|
+
if (bytes > MAX_WEBHOOK_BYTES) {
|
|
194
|
+
oversized = true
|
|
195
|
+
// #5433: respond + STOP consuming from inside the 'data' handler. The
|
|
196
|
+
// helper removes the data listener, pauses the stream (TCP backpressure),
|
|
197
|
+
// and sends the 413 with Connection: close — never drain an unbounded body.
|
|
198
|
+
// Cap checked BEFORE push: the violating chunk is never buffered.
|
|
199
|
+
sendOversizeResponse(req, res, { error: 'payload too large' })
|
|
200
|
+
return
|
|
201
|
+
}
|
|
202
|
+
chunks.push(buf)
|
|
203
|
+
})
|
|
204
|
+
req.on('error', () => {
|
|
205
|
+
// Body-stream error (e.g. client reset mid-send). Guard the response — the
|
|
206
|
+
// socket may already be gone.
|
|
207
|
+
try {
|
|
208
|
+
sendJson(res, 400, { error: 'read error' })
|
|
209
|
+
} catch {
|
|
210
|
+
/* socket already torn down */
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
req.on('end', () => {
|
|
214
|
+
// #5313 pattern: this callback runs on a later tick, OUTSIDE the HTTP
|
|
215
|
+
// dispatch try/catch — wrap everything so a throw here (e.g. writeHead on a
|
|
216
|
+
// torn-down socket) can't escape to uncaughtException and crash the daemon.
|
|
217
|
+
try {
|
|
218
|
+
if (oversized) return // 413 already sent from the 'data' handler
|
|
219
|
+
const raw = Buffer.concat(chunks)
|
|
220
|
+
const sig = req.headers['x-hub-signature-256']
|
|
221
|
+
if (!verifyGithubSignature(raw, sig, secret)) {
|
|
222
|
+
log.warn('Rejected GitHub webhook: invalid or missing X-Hub-Signature-256')
|
|
223
|
+
sendJson(res, 401, { error: 'invalid signature' })
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
let payload
|
|
227
|
+
try {
|
|
228
|
+
payload = JSON.parse(raw.toString('utf8'))
|
|
229
|
+
} catch {
|
|
230
|
+
sendJson(res, 400, { error: 'invalid JSON' })
|
|
231
|
+
return
|
|
232
|
+
}
|
|
233
|
+
const eventType = String(req.headers['x-github-event'] || '')
|
|
234
|
+
const event = normalizeGithubEvent(eventType, payload)
|
|
235
|
+
if (!event) {
|
|
236
|
+
// Authentic delivery, but an event type the Control Room doesn't surface.
|
|
237
|
+
sendJson(res, 202, { accepted: true, surfaced: false })
|
|
238
|
+
return
|
|
239
|
+
}
|
|
240
|
+
if (!server._repoEventStore) server._repoEventStore = new RepoEventStore()
|
|
241
|
+
server._repoEventStore.push(event)
|
|
242
|
+
// #6536: push the normalized event to connected host-level Control Room
|
|
243
|
+
// panes so the repo-events feed updates live (no Refresh). Guarded — a
|
|
244
|
+
// bare/stubbed server (tests) may not wire the WS broadcast.
|
|
245
|
+
if (typeof server._broadcastRepoEvent === 'function') server._broadcastRepoEvent(event)
|
|
246
|
+
sendJson(res, 202, { accepted: true, surfaced: true, kind: event.kind })
|
|
247
|
+
} catch (err) {
|
|
248
|
+
log.error(`github webhook handler error: ${err?.stack || err}`)
|
|
249
|
+
try {
|
|
250
|
+
sendJson(res, 500, { error: 'internal error' })
|
|
251
|
+
} catch {
|
|
252
|
+
/* socket already torn down */
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
})
|
|
256
|
+
}
|