@allbluecn/web-app 0.28.11 → 0.28.13
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/package.json +5 -5
- package/public/dev-gallery.html +19 -0
- package/src/__tests__/bench/baselines/format-timestamp.json +44 -0
- package/src/__typeprobe__.d.ts +4 -0
- package/src/components/harness/extension-panel/panels/terminal/__tests__/terminal-instance.test.tsx +93 -4
- package/src/components/harness/extension-panel/panels/terminal/sse-transport.ts +28 -5
- package/src/components/harness/extension-panel/panels/terminal/terminal-instance.tsx +53 -10
- package/src/components/harness/extension-panel/panels/terminal/transport.ts +10 -3
- package/src/components/harness/extension-panel/panels/terminal/ws-transport.ts +13 -2
- package/src/components/settings/harness/connectors-card.tsx +5 -0
- package/src/components/story/detail/story-detail-toolbar/associated-attachments-panel.tsx +2 -1
- package/src/dev/gallery-entry.tsx +38 -0
- package/src/dev/gallery.tsx +610 -0
- package/src/lib/changelog/sdk-versions.ts +4 -4
- package/src/lib/session.ts +10 -0
- package/src/paraglide/README.md +1 -1
- package/src/paraglide/messages/_index.d.ts +9 -0
- package/src/paraglide/messages/_index.js +9 -0
- package/src/paraglide/messages/capabilities_webhook.d.ts +16 -0
- package/src/paraglide/messages/capabilities_webhook.js +29 -0
- package/src/paraglide/messages/capabilities_workflow.d.ts +16 -0
- package/src/paraglide/messages/capabilities_workflow.js +29 -0
- package/src/paraglide/messages/harness_panel_terminal_exitedrecycled1.d.ts +17 -0
- package/src/paraglide/messages/harness_panel_terminal_exitedrecycled1.js +30 -0
- package/src/paraglide/messages/harness_panel_terminal_restarted.d.ts +16 -0
- package/src/paraglide/messages/harness_panel_terminal_restarted.js +29 -0
- package/src/paraglide/messages/settings_harness_connectors_needsauth1.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_connectors_needsauth1.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphenabled1.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphenabled1.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphenabled_description1.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphenabled_description1.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds2.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds2.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds_description2.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds_description2.js +30 -0
- package/src/routeTree.gen.ts +21 -0
- package/src/routes/api/auth/desktop-callback.ts +9 -0
- package/src/routes/api/chat.ts +8 -1
- package/src/routes/api/harness/browser/proxy.ts +41 -12
- package/src/routes/api/harness/terminal/$.ts +32 -13
- package/src/routes/api/harness/terminal/__tests__/route.test.ts +41 -0
- package/src/routes/api/harness/webhook/$token.ts +23 -0
- package/src/routes/api/sync/__tests__/bootstrap.test.ts +32 -1
- package/src/routes/api/sync/bootstrap.ts +11 -0
- package/src/server/ai-gateway/adapter.ts +7 -3
- package/src/server/ai-gateway/connectivity-check.ts +3 -2
- package/src/server/ai-gateway/durability/__tests__/sse-resume-frames.test.ts +123 -0
- package/src/server/ai-gateway/embedding.ts +3 -2
- package/src/server/ai-gateway/model-fetch.ts +3 -2
- package/src/server/ai-gateway/sandbox/__tests__/provider.test.ts +12 -1
- package/src/server/ai-gateway/sandbox/policy.ts +27 -1
- package/src/server/ai-gateway/sandbox/provider.ts +10 -0
- package/src/server/ai-gateway/tools/definitions.ts +1 -1
- package/src/server/ai-gateway/tools/server.ts +4 -2
- package/src/server/harness/__tests__/agent-terminal-sandbox.test.ts +23 -4
- package/src/server/harness/__tests__/agent-terminal.server.test.ts +118 -2
- package/src/server/harness/__tests__/credentials.server.test.ts +25 -2
- package/src/server/harness/__tests__/host-schedule-worker.test.ts +43 -0
- package/src/server/harness/__tests__/runtime-without-providers.test.ts +11 -0
- package/src/server/harness/__tests__/sticky-route.test.ts +86 -0
- package/src/server/harness/__tests__/terminal-foreground.server.test.ts +80 -0
- package/src/server/harness/__tests__/terminal-manager.server.test.ts +75 -1
- package/src/server/harness/__tests__/tool-entitlements.test.ts +6 -2
- package/src/server/harness/agent-terminal.server.ts +25 -4
- package/src/server/harness/agents/__tests__/agent-control.test.ts +1 -0
- package/src/server/harness/agents/run-view.server.ts +55 -2
- package/src/server/harness/capabilities/__tests__/__snapshots__/matrix.test.ts.snap +92 -0
- package/src/server/harness/capabilities/__tests__/execution-plane-denials.test.ts +3 -1
- package/src/server/harness/capabilities/__tests__/p3-matrix.test.ts +3 -3
- package/src/server/harness/capabilities/__tests__/p5-matrix.test.ts +293 -0
- package/src/server/harness/capabilities/__tests__/validate.test.ts +8 -2
- package/src/server/harness/capabilities/execution-plane-denials.ts +9 -3
- package/src/server/harness/capabilities/manifests/agent-terminal.ts +3 -2
- package/src/server/harness/capabilities/manifests/webhook.ts +45 -0
- package/src/server/harness/capabilities/manifests/workflow.ts +40 -0
- package/src/server/harness/capabilities/registry.ts +4 -0
- package/src/server/harness/connectors/__tests__/connector-accounts.test.ts +22 -1
- package/src/server/harness/connectors/connector-accounts.server.ts +7 -0
- package/src/server/harness/credentials.server.ts +3 -1
- package/src/server/harness/define.server.ts +11 -0
- package/src/server/harness/mcp/mcp-config.server.ts +3 -1
- package/src/server/harness/outbox/outbox-dispatcher.server.ts +9 -0
- package/src/server/harness/outbox/outbox-store.server.ts +7 -0
- package/src/server/harness/persistence/__tests__/log-store.server.test.ts +4 -1
- package/src/server/harness/plugins/__tests__/tool-risk-audit.test.ts +5 -1
- package/src/server/harness/plugins/builtin/agent-terminal.plugin.ts +12 -2
- package/src/server/harness/providers/contracts.ts +16 -0
- package/src/server/harness/schedule/schedule-dispatcher.server.ts +3 -0
- package/src/server/harness/search/__tests__/indexer.test.ts +4 -1
- package/src/server/harness/search/__tests__/lifecycle.test.ts +4 -1
- package/src/server/harness/search/__tests__/search.test.ts +4 -1
- package/src/server/harness/sticky-route.server.ts +72 -0
- package/src/server/harness/terminal-foreground.server.ts +90 -0
- package/src/server/harness/terminal-manager.server.ts +78 -3
- package/src/server/harness/tool-entitlements.server.ts +5 -0
- package/src/server/harness/webhook/__tests__/body.test.ts +73 -0
- package/src/server/harness/webhook/__tests__/payload.test.ts +215 -0
- package/src/server/harness/webhook/__tests__/signature.test.ts +170 -0
- package/src/server/harness/webhook/__tests__/webhook-dispatcher.test.ts +212 -0
- package/src/server/harness/webhook/__tests__/webhook.plugin.test.ts +195 -0
- package/src/server/harness/webhook/__tests__/webhook.service.test.ts +354 -0
- package/src/server/harness/webhook/body.server.ts +56 -0
- package/src/server/harness/webhook/payload.server.ts +169 -0
- package/src/server/harness/webhook/signature.server.ts +118 -0
- package/src/server/harness/webhook/types.ts +57 -0
- package/src/server/harness/webhook/webhook-dispatcher.server.ts +495 -0
- package/src/server/harness/webhook/webhook-store.server.ts +115 -0
- package/src/server/harness/webhook/webhook.plugin.ts +273 -0
- package/src/server/harness/webhook/webhook.service.server.ts +577 -0
- package/src/server/harness/worker/__tests__/worker-http.test.ts +8 -1
- package/src/server/harness/worker/worker-health.server.ts +4 -1
- package/src/server/harness/worker/worker-runtime.server.ts +43 -1
- package/src/server/harness/workflow/__tests__/engine.test.ts +195 -0
- package/src/server/harness/workflow/__tests__/settings.test.ts +125 -0
- package/src/server/harness/workflow/__tests__/validate.test.ts +151 -0
- package/src/server/harness/workflow/engine.server.ts +155 -0
- package/src/server/harness/workflow/settings.ts +84 -0
- package/src/server/harness/workflow/store.server.ts +147 -0
- package/src/server/harness/workflow/types.ts +80 -0
- package/src/server/harness/workflow/validate.ts +130 -0
- package/src/server/harness/workflow/workflow.plugin.ts +261 -0
- package/src/server/knowledge/search-scope.ts +8 -1
- package/src/server/middlewares/security-headers.ts +1 -0
- package/src/server/serverFns/__tests__/link-preview.test.ts +2 -2
- package/src/server/serverFns/collab.ts +4 -0
- package/src/server/serverFns/design/versions.ts +6 -0
- package/src/server/serverFns/harness/__tests__/fork-thread.test.ts +14 -1
- package/src/server/serverFns/harness/fork-thread.ts +17 -0
- package/src/server/serverFns/story/link-preview.ts +3 -2
- package/src/server-entry.ts +13 -1
- package/src/start.ts +15 -1
- package/vite.shared.ts +0 -13
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
|
|
5
|
+
// 入站 webhook 的纯函数层:事件摘要提取、去重键、白名单/订阅匹配、prompt 渲染。
|
|
6
|
+
// 全为确定函数,接收管线只做 I/O 编排,便于逐条单测(计划任务 3 测试项 ③④)。
|
|
7
|
+
|
|
8
|
+
import { createHash } from 'node:crypto'
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
GITHUB_DELIVERY_HEADER,
|
|
12
|
+
GITHUB_EVENT_HEADER,
|
|
13
|
+
TIMESTAMPED_TIMESTAMP_HEADER,
|
|
14
|
+
} from './signature.server'
|
|
15
|
+
import type { WebhookEventSummary } from './types'
|
|
16
|
+
|
|
17
|
+
const MAX_TITLE_LENGTH = 200
|
|
18
|
+
|
|
19
|
+
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
20
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
21
|
+
? (value as Record<string, unknown>)
|
|
22
|
+
: null
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function asString(value: unknown): string | null {
|
|
26
|
+
return typeof value === 'string' && value.length > 0 ? value : null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function truncated(value: string | null): string | null {
|
|
30
|
+
if (value === null) return null
|
|
31
|
+
return value.length <= MAX_TITLE_LENGTH ? value : `${value.slice(0, MAX_TITLE_LENGTH)}…`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function nested(value: unknown, ...path: string[]): unknown {
|
|
35
|
+
let current: unknown = value
|
|
36
|
+
for (const key of path) {
|
|
37
|
+
const record = asRecord(current)
|
|
38
|
+
if (!record) return null
|
|
39
|
+
current = record[key]
|
|
40
|
+
}
|
|
41
|
+
return current
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 从 GitHub 事件体提取有界摘要。非 GitHub 形状时各字段退化为 null,不抛错
|
|
46
|
+
* (未知 provider 的事件仍可投递,只是上下文更少)。
|
|
47
|
+
*/
|
|
48
|
+
export function extractEventSummary(input: {
|
|
49
|
+
headers: Headers
|
|
50
|
+
payload: unknown
|
|
51
|
+
}): WebhookEventSummary {
|
|
52
|
+
const event = input.headers.get(GITHUB_EVENT_HEADER)?.trim() || 'unknown'
|
|
53
|
+
const pullRequest = nested(input.payload, 'pull_request')
|
|
54
|
+
const issue = nested(input.payload, 'issue')
|
|
55
|
+
const repository = nested(input.payload, 'repository')
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
event,
|
|
59
|
+
action: truncated(asString(nested(input.payload, 'action'))),
|
|
60
|
+
repo: truncated(asString(nested(repository, 'full_name'))),
|
|
61
|
+
owner: truncated(
|
|
62
|
+
asString(nested(repository, 'owner', 'login'))
|
|
63
|
+
?? asString(nested(repository, 'owner', 'name')),
|
|
64
|
+
),
|
|
65
|
+
ref: truncated(
|
|
66
|
+
asString(nested(input.payload, 'ref'))
|
|
67
|
+
?? asString(nested(pullRequest, 'head', 'ref')),
|
|
68
|
+
),
|
|
69
|
+
title: truncated(
|
|
70
|
+
asString(nested(pullRequest, 'title'))
|
|
71
|
+
?? asString(nested(issue, 'title'))
|
|
72
|
+
?? firstLine(asString(nested(input.payload, 'head_commit', 'message'))),
|
|
73
|
+
),
|
|
74
|
+
url: truncated(
|
|
75
|
+
asString(nested(pullRequest, 'html_url'))
|
|
76
|
+
?? asString(nested(issue, 'html_url'))
|
|
77
|
+
?? asString(nested(input.payload, 'compare'))
|
|
78
|
+
?? asString(nested(repository, 'html_url')),
|
|
79
|
+
),
|
|
80
|
+
actor: truncated(asString(nested(input.payload, 'sender', 'login'))),
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function firstLine(value: string | null): string | null {
|
|
85
|
+
if (value === null) return null
|
|
86
|
+
const line = value.split('\n', 1)[0]?.trim()
|
|
87
|
+
return line && line.length > 0 ? line : null
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 去重键:
|
|
92
|
+
* 1. GitHub 优先用投递 guid(`X-GitHub-Delivery`,上游重投时不变);
|
|
93
|
+
* 2. 带时间戳方案用 `timestamp + body 摘要`(同窗口重放命中唯一键);
|
|
94
|
+
* 3. 兜底用整段 body 摘要(同 body 重投同样去重)。
|
|
95
|
+
*/
|
|
96
|
+
export function computeEventKey(input: {
|
|
97
|
+
headers: Headers
|
|
98
|
+
body: Uint8Array
|
|
99
|
+
summary: WebhookEventSummary
|
|
100
|
+
}): string {
|
|
101
|
+
const delivery = input.headers.get(GITHUB_DELIVERY_HEADER)?.trim()
|
|
102
|
+
if (delivery) return `github:${delivery}`
|
|
103
|
+
const digest = createHash('sha256').update(input.body).digest('hex')
|
|
104
|
+
const timestamp = input.headers.get(TIMESTAMPED_TIMESTAMP_HEADER)?.trim()
|
|
105
|
+
if (timestamp && /^\d{1,12}$/.test(timestamp)) {
|
|
106
|
+
return `${input.summary.event}:${timestamp}:${digest.slice(0, 16)}`
|
|
107
|
+
}
|
|
108
|
+
return `${input.summary.event}:${digest.slice(0, 32)}`
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 事件订阅匹配:events 为 null 表示全部订阅。 */
|
|
112
|
+
export function matchesSubscribedEvents(
|
|
113
|
+
events: readonly string[] | null,
|
|
114
|
+
summary: WebhookEventSummary,
|
|
115
|
+
): boolean {
|
|
116
|
+
if (events === null || events.length === 0) return true
|
|
117
|
+
return events.includes(summary.event)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* repo/owner 白名单匹配(精确匹配,大小写不敏感——GitHub owner/repo 大小写不敏感)。
|
|
122
|
+
* 两个白名单都为 null 视为「未配置」→ 不匹配(fail-closed:规则创建时已强制至少一个)。
|
|
123
|
+
*/
|
|
124
|
+
export function matchesWhitelist(input: {
|
|
125
|
+
repoWhitelist: readonly string[] | null
|
|
126
|
+
ownerWhitelist: readonly string[] | null
|
|
127
|
+
summary: Pick<WebhookEventSummary, 'repo' | 'owner'>
|
|
128
|
+
}): boolean {
|
|
129
|
+
const repo = input.summary.repo?.toLowerCase() ?? null
|
|
130
|
+
const owner = input.summary.owner?.toLowerCase() ?? null
|
|
131
|
+
const repos = input.repoWhitelist?.map((entry) => entry.toLowerCase()) ?? null
|
|
132
|
+
const owners = input.ownerWhitelist?.map((entry) => entry.toLowerCase()) ?? null
|
|
133
|
+
|
|
134
|
+
if (repos !== null && repos.length > 0 && repo !== null && repos.includes(repo)) return true
|
|
135
|
+
if (owners !== null && owners.length > 0 && owner !== null && owners.includes(owner)) return true
|
|
136
|
+
return false
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 投递给 agent 的 prompt:固定格式头 + 规则 prompt。
|
|
141
|
+
* 头只含有界摘要字段(无 payload 全文、无签名、无密钥)。
|
|
142
|
+
*/
|
|
143
|
+
export function renderWebhookPrompt(input: {
|
|
144
|
+
name: string
|
|
145
|
+
provider: string
|
|
146
|
+
prompt: string
|
|
147
|
+
summary: WebhookEventSummary
|
|
148
|
+
}): string {
|
|
149
|
+
const lines = [`[webhook] ${input.provider} ${input.summary.event} · ${input.name}`]
|
|
150
|
+
if (input.summary.repo) lines.push(`repo: ${input.summary.repo}`)
|
|
151
|
+
if (input.summary.action) lines.push(`action: ${input.summary.action}`)
|
|
152
|
+
if (input.summary.ref) lines.push(`ref: ${input.summary.ref}`)
|
|
153
|
+
if (input.summary.actor) lines.push(`actor: ${input.summary.actor}`)
|
|
154
|
+
if (input.summary.title) lines.push(`title: ${input.summary.title}`)
|
|
155
|
+
if (input.summary.url) lines.push(`url: ${input.summary.url}`)
|
|
156
|
+
lines.push('', input.prompt)
|
|
157
|
+
return lines.join('\n')
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 解析 JSON body;非对象或解析失败返回 null(接收管线据此 400)。 */
|
|
161
|
+
export function parseWebhookPayload(body: Uint8Array): unknown | null {
|
|
162
|
+
try {
|
|
163
|
+
const text = new TextDecoder('utf-8', { fatal: false }).decode(body)
|
|
164
|
+
const parsed: unknown = JSON.parse(text)
|
|
165
|
+
return parsed
|
|
166
|
+
} catch {
|
|
167
|
+
return null
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
|
|
5
|
+
// 入站 webhook 签名校验(P5 任务 3,母规格 §6.2)。
|
|
6
|
+
//
|
|
7
|
+
// 两种方案并存,按头选择:
|
|
8
|
+
// 1. **GitHub**:`X-Hub-Signature-256: sha256=<hex>`,HMAC-SHA256(secret, rawBody)。
|
|
9
|
+
// GitHub 不带时间戳,重放由投递 guid(`X-GitHub-Delivery`)的唯一键去重承担。
|
|
10
|
+
// 2. **带时间戳**(generic):`X-AllBlue-Timestamp: <unix 秒>` +
|
|
11
|
+
// `X-AllBlue-Signature: v1=<hex>`,签名体 `${timestamp}.${rawBody}`,窗口 ±5 分钟。
|
|
12
|
+
// 时间戳参与签名:窗口内篡改时间戳无法通过验签。
|
|
13
|
+
//
|
|
14
|
+
// 一律 timingSafeEqual 比较;任何格式异常按 invalid 处理,不回显期望值。
|
|
15
|
+
|
|
16
|
+
import { createHmac, timingSafeEqual } from 'node:crypto'
|
|
17
|
+
|
|
18
|
+
import { WEBHOOK_REPLAY_WINDOW_MS } from './types'
|
|
19
|
+
|
|
20
|
+
export const GITHUB_SIGNATURE_HEADER = 'x-hub-signature-256'
|
|
21
|
+
export const GITHUB_EVENT_HEADER = 'x-github-event'
|
|
22
|
+
export const GITHUB_DELIVERY_HEADER = 'x-github-delivery'
|
|
23
|
+
export const TIMESTAMPED_SIGNATURE_HEADER = 'x-allblue-signature'
|
|
24
|
+
export const TIMESTAMPED_TIMESTAMP_HEADER = 'x-allblue-timestamp'
|
|
25
|
+
|
|
26
|
+
export type SignatureScheme = 'github' | 'timestamped'
|
|
27
|
+
|
|
28
|
+
export type SignatureCheck =
|
|
29
|
+
| { ok: true; scheme: SignatureScheme }
|
|
30
|
+
| {
|
|
31
|
+
ok: false
|
|
32
|
+
reason: 'missing_signature' | 'invalid_signature' | 'stale_timestamp' | 'bad_timestamp'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function hmacHex(secret: string, chunks: readonly Uint8Array[]): string {
|
|
36
|
+
const hmac = createHmac('sha256', secret)
|
|
37
|
+
for (const chunk of chunks) hmac.update(chunk)
|
|
38
|
+
return hmac.digest('hex')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** HMAC-SHA256(secret, body) 的小写 hex(GitHub 方案)。 */
|
|
42
|
+
export function computeHmacHex(secret: string, body: Uint8Array): string {
|
|
43
|
+
return hmacHex(secret, [body])
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 带时间戳签名:HMAC-SHA256(secret, `${timestamp}.${body}`) 的小写 hex。 */
|
|
47
|
+
export function computeTimestampedSignature(
|
|
48
|
+
secret: string,
|
|
49
|
+
timestamp: string,
|
|
50
|
+
body: Uint8Array,
|
|
51
|
+
): string {
|
|
52
|
+
return hmacHex(secret, [Buffer.from(`${timestamp}.`, 'utf8'), body])
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 去掉可选的 `sha256=` / `v1=` 前缀,返回小写 hex;格式非法返回 null。 */
|
|
56
|
+
function normalizeHexSignature(raw: string, prefixes: readonly string[]): string | null {
|
|
57
|
+
let value = raw.trim()
|
|
58
|
+
for (const prefix of prefixes) {
|
|
59
|
+
if (value.startsWith(prefix)) {
|
|
60
|
+
value = value.slice(prefix.length)
|
|
61
|
+
break
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (value.length !== 64 || !/^[0-9a-fA-F]{64}$/.test(value)) return null
|
|
65
|
+
return value.toLowerCase()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function safeEqualHex(expectedHex: string, actualHex: string): boolean {
|
|
69
|
+
const expected = Buffer.from(expectedHex, 'hex')
|
|
70
|
+
const actual = Buffer.from(actualHex, 'hex')
|
|
71
|
+
if (expected.length !== actual.length) return false
|
|
72
|
+
return timingSafeEqual(expected, actual)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 按请求头选择方案并验签。
|
|
77
|
+
*
|
|
78
|
+
* `now` 可注入,便于测试重放窗口边界。
|
|
79
|
+
*/
|
|
80
|
+
export function verifyWebhookSignature(input: {
|
|
81
|
+
secret: string
|
|
82
|
+
body: Uint8Array
|
|
83
|
+
headers: Headers
|
|
84
|
+
now?: number
|
|
85
|
+
}): SignatureCheck {
|
|
86
|
+
const githubRaw = input.headers.get(GITHUB_SIGNATURE_HEADER)
|
|
87
|
+
if (githubRaw !== null) {
|
|
88
|
+
const provided = normalizeHexSignature(githubRaw, ['sha256='])
|
|
89
|
+
if (!provided) return { ok: false, reason: 'invalid_signature' }
|
|
90
|
+
const expected = computeHmacHex(input.secret, input.body)
|
|
91
|
+
return safeEqualHex(expected, provided)
|
|
92
|
+
? { ok: true, scheme: 'github' }
|
|
93
|
+
: { ok: false, reason: 'invalid_signature' }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const timestamp = input.headers.get(TIMESTAMPED_TIMESTAMP_HEADER)
|
|
97
|
+
const signature = input.headers.get(TIMESTAMPED_SIGNATURE_HEADER)
|
|
98
|
+
if (timestamp === null && signature === null) {
|
|
99
|
+
return { ok: false, reason: 'missing_signature' }
|
|
100
|
+
}
|
|
101
|
+
if (timestamp === null || signature === null) {
|
|
102
|
+
return { ok: false, reason: 'missing_signature' }
|
|
103
|
+
}
|
|
104
|
+
if (!/^\d{1,12}$/.test(timestamp.trim())) {
|
|
105
|
+
return { ok: false, reason: 'bad_timestamp' }
|
|
106
|
+
}
|
|
107
|
+
const timestampMs = Number(timestamp.trim()) * 1000
|
|
108
|
+
const now = input.now ?? Date.now()
|
|
109
|
+
if (Math.abs(now - timestampMs) > WEBHOOK_REPLAY_WINDOW_MS) {
|
|
110
|
+
return { ok: false, reason: 'stale_timestamp' }
|
|
111
|
+
}
|
|
112
|
+
const provided = normalizeHexSignature(signature, ['v1='])
|
|
113
|
+
if (!provided) return { ok: false, reason: 'invalid_signature' }
|
|
114
|
+
const expected = computeTimestampedSignature(input.secret, timestamp.trim(), input.body)
|
|
115
|
+
return safeEqualHex(expected, provided)
|
|
116
|
+
? { ok: true, scheme: 'timestamped' }
|
|
117
|
+
: { ok: false, reason: 'invalid_signature' }
|
|
118
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
|
|
5
|
+
// 入站 webhook 共享类型与边界常量(P5 任务 3,母规格 §6.2)。
|
|
6
|
+
// 安全边界集中在此,接收管线与单测共用同一组常量,避免两处漂移。
|
|
7
|
+
|
|
8
|
+
/** raw body 上限(1 MiB):GitHub 事件体远小于此,超出即视为 DoS 探测。 */
|
|
9
|
+
export const WEBHOOK_MAX_BODY_BYTES = 1024 * 1024
|
|
10
|
+
|
|
11
|
+
/** 带时间戳签名的重放窗口(±5 分钟)。 */
|
|
12
|
+
export const WEBHOOK_REPLAY_WINDOW_MS = 5 * 60 * 1000
|
|
13
|
+
|
|
14
|
+
/** 端点令牌长度(base64url 24 字节 → 32 字符)。 */
|
|
15
|
+
export const WEBHOOK_TOKEN_BYTES = 24
|
|
16
|
+
|
|
17
|
+
/** 密钥长度(32 字节随机 → 64 hex 字符)。 */
|
|
18
|
+
export const WEBHOOK_SECRET_BYTES = 32
|
|
19
|
+
|
|
20
|
+
export type WebhookProvider = 'github' | 'generic'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 有界事件摘要(落 HarnessWebhookDelivery.summary)。
|
|
24
|
+
* 不存整段 payload:验签用 raw body,落库只需可重建的触发上下文(母规格 §8 密钥/体积卫生)。
|
|
25
|
+
*/
|
|
26
|
+
export interface WebhookEventSummary {
|
|
27
|
+
event: string
|
|
28
|
+
action: string | null
|
|
29
|
+
repo: string | null
|
|
30
|
+
owner: string | null
|
|
31
|
+
ref: string | null
|
|
32
|
+
title: string | null
|
|
33
|
+
url: string | null
|
|
34
|
+
actor: string | null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 工具与列表投影(零 secret / 零 prompt 全文)。 */
|
|
38
|
+
export interface WebhookRuleSummary {
|
|
39
|
+
id: string
|
|
40
|
+
name: string
|
|
41
|
+
provider: string
|
|
42
|
+
status: string
|
|
43
|
+
/** 端点相对路径(含不透明 token),可直接拼到站点根 */
|
|
44
|
+
path: string
|
|
45
|
+
secretHint: string
|
|
46
|
+
/**
|
|
47
|
+
* 密钥是否在本设备可用(P5 任务 5):跨端同步只带定义、不带密钥(secretCipher 为
|
|
48
|
+
* NULL)→ false。此时该端点校验前即 503,须在本设备重新录入密钥(webhook_edit)。
|
|
49
|
+
*/
|
|
50
|
+
hasSecret: boolean
|
|
51
|
+
events: string[] | null
|
|
52
|
+
repoWhitelist: string[] | null
|
|
53
|
+
ownerWhitelist: string[] | null
|
|
54
|
+
threadId: string
|
|
55
|
+
lastDeliveryAt: Date | null
|
|
56
|
+
revision: number
|
|
57
|
+
}
|