ai_co_discuss 0.4.31 → 0.4.32
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/.agents/skills/discuss-participant/SKILL.md +5 -5
- package/.agents/skills/discuss-participant/references/writing.md +2 -2
- package/.agents/skills/discuss-session/SKILL.md +1 -1
- package/backend/entry_guard.py +111 -0
- package/backend/server.py +69 -6
- package/gateway/gateway.mjs +379 -26
- package/package.json +1 -1
- package/runner/account_env.mjs +2 -1
- package/runner/account_space.mjs +2 -0
- package/runner/adapters/antigravity_sidecar.mjs +3 -2
- package/runner/adapters/api.mjs +2 -1
- package/runner/adapters/api_images.mjs +2 -1
- package/runner/adapters/claude.mjs +9 -3
- package/runner/adapters/index.mjs +210 -209
- package/runner/admin_panel.mjs +267 -13
- package/runner/cli_login.mjs +9 -9
- package/runner/cli_login_agent.mjs +5 -3
- package/runner/discuss.py +105 -7
- package/runner/discussion_launch.mjs +3 -2
- package/runner/discussion_local.mjs +2 -1
- package/runner/discussion_runner.mjs +2 -1
- package/runner/discussion_service.mjs +9 -2
- package/runner/entry_client.mjs +159 -0
- package/runner/free_runner.mjs +56 -6
- package/runner/os_users.mjs +143 -14
- package/runner/relay_channel.mjs +4 -3
- package/runner/weixin.mjs +7 -6
- package/web/admin.css +1 -1
- package/web/admin.js +3 -3
- package/web/app.js +12 -6
- package/web/modern.css +1 -1
- package/web/modern.js +42 -42
|
@@ -36,7 +36,7 @@ description: "AI CoDiscuss 的通用参与技能,所有参与者(包括主
|
|
|
36
36
|
|
|
37
37
|
从实际任务或用户指定值核实站点、房间和身份,不从当前网页或最近聊天猜测。任务标明 `access_env: "DISCUSS_ACCESS_FILE"` 时凭据已由进程绑定,`--access-file FILE` 可省略;否则用任务交付的 `access_file`。任务凭据已绑定站点、房间、身份与本次执行,命令省略房间、`--as`、`--execution` 和 `--submission-id`,由 CLI 从凭据和内容补齐,例如 `message post --body-file 草稿.md`;下文命令里的 `ROOM`、`--as`、执行编号只在没有绑定凭据(如手动接入未带凭据)时需要填写。项目与 cwd 有值时用于本次获准资料;CLI 路径只定位工具,不把 CLI 仓库当作讨论项目或扩大文件权限。
|
|
38
38
|
|
|
39
|
-
每次 shell 调用都是一次模型往返,手动会话里还可能要用户逐条确认,所以互不依赖的只读命令放进同一条 shell
|
|
39
|
+
每次 shell 调用都是一次模型往返,手动会话里还可能要用户逐条确认,所以互不依赖的只读命令放进同一条 shell 命令依次执行。正文想好后,先用当前软件的文件写入工具把正文写成一个草稿文件(需要摘要时写在文件开头:`---`、`summary: 摘要`、`---` 三行,之后是正文),再用一条只含 `codiscuss` 的命令以 `--body-file` 提交;失败重试仍用这份草稿。不要用 heredoc、`echo` 重定向或命令行字面量传长文本,也不要在命令里调用函数生成编号(如 `[guid]::NewGuid()`、`$(uuidgen)`):自动运行时这类组合命令会被权限检查拒绝,文本中的引号(含中文弯引号)还会截断参数。其他写入命令需要编号时直接写一个新的 UUID 字面值。不同的网站写入各自单独执行并核对结果。
|
|
40
40
|
|
|
41
41
|
称呼和点名成员一律用显示名:`room view` 中 `members[].label`(如 `Gemini`)就是它在房间里的名字,正文 `@`、`--to`、`--next-review`、`--expert` 都写它。`name` 是成员 ID,与 `descriptions` 同属成员的完整信息,需要核对时再看,不写进正文;旧消息、`author` 等字段里出现的 ID 指的是同一位成员。普通成员 `@` 其他成员只是引用,不会唤醒对方;只有确实需要主持人处理(超出你的权限或职责)时才 `@` 主持人,程序会在房间空闲后唤醒它。
|
|
42
42
|
|
|
@@ -62,8 +62,8 @@ description: "AI CoDiscuss 的通用参与技能,所有参与者(包括主
|
|
|
62
62
|
|
|
63
63
|
## 保存与核验
|
|
64
64
|
|
|
65
|
-
正文用 UTF-8 `--body-file
|
|
65
|
+
正文用 UTF-8 `--body-file`,摘要写在同一草稿的文件头(见上文“命令入口与身份”),也可另用 `--summary` / `--summary-file`。需要附件时先 `attachment upload ROOM --file FILE --submission-id 上传编号`,再用 `message post --attachment ID` 关联(最多 10 个,单个 20 MiB,须在执行资格结束前上传)。
|
|
66
66
|
|
|
67
|
-
**生图**:任务里有 `image` 字段才能画图或改图;没有时如实说明本房间未开生图,不要找别的办法。按张计费,想清楚再调用,不为试探反复生成。把请求写进 UTF-8 JSON 草稿:`{"action":"generate","prompt":"画面、风格、文字","size":"1536x1024","n":1}`;改图用 `"action":"edit"` 与 `"images":[{"attachment_id":"…"}]` 或 `{"file_path":"项目内路径"}`;其他参数见 `codiscuss launch --help` 的 image 一条。运行 `image.command`(把 `REQUEST.json` 换成草稿路径),返回的附件 ID 用 `message post --attachment` 附上。能看图时先 `attachment download` 核对效果再交付;看不到图(如 API 成员)就写清画了什么和参数,不描述没看到的细节。API 成员用 `generate_image` / `edit_image` 工具,不走命令。
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
**生图**:任务里有 `image` 字段才能画图或改图;没有时如实说明本房间未开生图,不要找别的办法。按张计费,想清楚再调用,不为试探反复生成。把请求写进 UTF-8 JSON 草稿:`{"action":"generate","prompt":"画面、风格、文字","size":"1536x1024","n":1}`;改图用 `"action":"edit"` 与 `"images":[{"attachment_id":"…"}]` 或 `{"file_path":"项目内路径"}`;其他参数见 `codiscuss launch --help` 的 image 一条。运行 `image.command`(把 `REQUEST.json` 换成草稿路径),返回的附件 ID 用 `message post --attachment` 附上。能看图时先 `attachment download` 核对效果再交付;看不到图(如 API 成员)就写清画了什么和参数,不描述没看到的细节。API 成员用 `generate_image` / `edit_image` 工具,不走命令。
|
|
68
|
+
|
|
69
|
+
成功须核对服务端返回的回执:消息编号、作者和执行或请求关联(`message post` 不回显正文,只给 `text_chars` 字数);回执已足够时不再读一遍,缺少所需证据才按编号补读。答案落库后即可结束,不用旧资格继续写入。只以本人身份提交,不替他人发言。执行结束不等于整个房间的工作结束。
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
每次业务写入都带对应的 `--access-file`:手动成员使用本人的持久凭据,自动讨论使用本次执行文件。房主文件仅用于用户身份和房主管理。CLI 建房须显式指定私有房主文件,先保存建房编号;回执中的 `member_access_files` 给出各本地成员文件路径。讨论名单或主持职责变更仅房主可做,新增或改变能力的成员须提供独立的新凭据;保持未变成员原凭据,不能由成员自行扩权。
|
|
12
12
|
|
|
13
|
-
本地提交用 `
|
|
13
|
+
本地提交用 `--body` / `--body-file` 保存完整正文,摘要可写在正文文件开头(第一行 `---`,第二行 `summary: 摘要`,第三行 `---`,之后是正文;CLI 去掉这三行),也可用 `--summary` / `--summary-file`,二者不能同用;摘要上限 1000 个 Unicode 字符。摘要归提交者本人。摘要与正文一起使用同一提交编号;缺少摘要的旧记录不自动生成摘要。
|
|
14
14
|
|
|
15
15
|
一次写入先确定目标、身份、正文与关联关系,再选择对应命令:
|
|
16
16
|
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
|
|
30
30
|
长正文优先 UTF-8 文件,`--body-file -` 可读 stdin。当前消息正文上限为 32000 个 Unicode 字符,详细讨论状态上限为 4000 字;以目标站点协议为准。完整回答一次提交并完成安排,不能截断或用第一段冒充完整回答。超过上限时保留完整原文并说明限制,先明确完整材料的留档方式。
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
`message post` 成功只返回回执:消息编号、作者、职责、执行或请求关联、提交编号、摘要、正文字数(`text_chars`)与附件数,不回显正文;核对这些即可,需要看正文再 `message view`。其他写入成功后核对服务端返回的持久记录;响应已给出证据时不重复全文查询,缺少的再按编号补读。`duplicate=true` 表示服务确认已有同一提交,不是新回答。讨论暂停或收束后不再提交新回答/安排,保留原文。自动执行失效后不能借此继续写入;不能改消息职责绕过阶段限制。
|
|
33
33
|
|
|
34
34
|
保存摘要后核对服务端返回的正文和截止编号,响应不足时用 `state view` 补读。`--until` 表示实际已读材料范围,不能证明下一安排、外部通知或清理已完成;这些事项在摘要正文中保留各自真实状态。保存结果不明时先比较现存摘要与待保存内容,不直接用旧草稿覆盖更新后的进度。
|
|
35
35
|
|
|
@@ -43,6 +43,6 @@ codiscuss message post ROOM --as 本人 --kind chat --execution 执行编号 --b
|
|
|
43
43
|
codiscuss message post ROOM --as 本人 --kind chat --request 请求编号 --body-file draft.md --access-file FILE
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
需要摘要时写在正文草稿开头(`---`、`summary: 摘要`、`---` 三行),或用 `--summary` / `--summary-file`。成功提交后当前请求即完成;不要再补一条普通 chat 或状态回执。
|
|
47
47
|
|
|
48
48
|
任务执行失败时,用 `execution fail ROOM 执行编号 --reason 原因 --access-file FILE` 失败本次执行并保留草稿。该操作只处理当前请求,不自动暂停整场讨论;主持人会收到专家请求失败并判断后续安排。不要改用其他成员、请求编号或身份绕过拒绝。
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""Entry protection for a hosted instance: only callers holding the instance's key reach it.
|
|
2
|
+
|
|
3
|
+
A hosted instance listens on loopback, where every account's processes can connect and any of them may take the port
|
|
4
|
+
of an instance that is down. So, when protection is on, nothing but public assets is served unless the caller proves
|
|
5
|
+
on the same connection that it holds the instance's key, and the caller only sends its request after the instance has
|
|
6
|
+
proved the same to it:
|
|
7
|
+
|
|
8
|
+
1. The caller sends ``GET /_instance/hello`` with ``X-CoDiscuss-Nonce: n1``.
|
|
9
|
+
2. The instance answers 204 with ``X-CoDiscuss-Proof: HMAC(K, "instance|" + n1)`` and its own ``X-CoDiscuss-Nonce: n2``,
|
|
10
|
+
keeping the connection open.
|
|
11
|
+
3. The caller checks the proof, then sends one request on the same connection with
|
|
12
|
+
``X-CoDiscuss-Entry: HMAC(K, "entry|" + n2)``. The instance accepts it only on that connection and only for the key it
|
|
13
|
+
used in step 2, and closes the connection after answering (a stream keeps it until the stream ends).
|
|
14
|
+
|
|
15
|
+
The key never travels. Protection is on when the environment names a key file (``DISCUSS_ENTRY_KEY_FILE``); a key file
|
|
16
|
+
that is then missing or unreadable refuses every protected request instead of falling back to no protection.
|
|
17
|
+
"""
|
|
18
|
+
import hashlib
|
|
19
|
+
import hmac
|
|
20
|
+
import json
|
|
21
|
+
import os
|
|
22
|
+
import re
|
|
23
|
+
import secrets
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
KEY_FILE_VARIABLE = "DISCUSS_ENTRY_KEY_FILE"
|
|
27
|
+
HELLO_PATH = "/_instance/hello"
|
|
28
|
+
NONCE_HEADER = "X-CoDiscuss-Nonce"
|
|
29
|
+
PROOF_HEADER = "X-CoDiscuss-Proof"
|
|
30
|
+
ENTRY_HEADER = "X-CoDiscuss-Entry"
|
|
31
|
+
NONCE_PATTERN = re.compile(r"^[0-9a-f]{32}$")
|
|
32
|
+
KEY_PATTERN = re.compile(r"^[0-9a-f]{64}$")
|
|
33
|
+
# How long a connection that said hello may wait before its one request arrives.
|
|
34
|
+
HANDSHAKE_SECONDS = 10
|
|
35
|
+
# Paths any caller may read: the health check and the page's static files carry no data.
|
|
36
|
+
PUBLIC_PATHS = ("/api/health", "/robots.txt")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def proof(key, purpose, nonce):
|
|
40
|
+
"""HMAC-SHA256 of ``purpose|nonce`` under the hexadecimal key, as hexadecimal."""
|
|
41
|
+
return hmac.new(bytes.fromhex(key), f"{purpose}|{nonce}".encode("ascii"), hashlib.sha256).hexdigest()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class EntryPolicy:
|
|
45
|
+
"""Whether protection is on, and the instance's current key, read from its file every time it is needed.
|
|
46
|
+
|
|
47
|
+
The file is a few bytes, so it is not cached: a cache keyed on the modification time would keep honouring an old or
|
|
48
|
+
broken key after a change that left the time as it was.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def __init__(self, key_file=None):
|
|
52
|
+
configured = key_file if key_file is not None else os.getenv(KEY_FILE_VARIABLE)
|
|
53
|
+
self.path = Path(configured) if configured else None
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
def enabled(self):
|
|
57
|
+
"""Protection is on once a key file is named, whether or not it can be read."""
|
|
58
|
+
return self.path is not None
|
|
59
|
+
|
|
60
|
+
def key(self):
|
|
61
|
+
"""The current key, or None when the named file is missing or malformed (everything protected is refused)."""
|
|
62
|
+
if not self.path:
|
|
63
|
+
return None
|
|
64
|
+
try:
|
|
65
|
+
value = json.loads(self.path.read_text(encoding="utf-8")).get("key")
|
|
66
|
+
except (OSError, ValueError, AttributeError):
|
|
67
|
+
return None
|
|
68
|
+
return value if isinstance(value, str) and KEY_PATTERN.match(value) else None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def answer_hello(handler, policy):
|
|
72
|
+
"""Prove this instance to a caller and open the connection's single authenticated request.
|
|
73
|
+
|
|
74
|
+
Returns True when the hello was answered; a malformed hello or a missing key closes the connection unanswered.
|
|
75
|
+
"""
|
|
76
|
+
nonce = handler.headers.get(NONCE_HEADER, "")
|
|
77
|
+
key = policy.key()
|
|
78
|
+
handler.close_connection = True
|
|
79
|
+
# One hello per connection: a second one ends it instead of keeping the connection open indefinitely.
|
|
80
|
+
if not key or not NONCE_PATTERN.match(nonce) or getattr(handler, "entry_challenge", None) is not None:
|
|
81
|
+
return False
|
|
82
|
+
challenge = secrets.token_hex(16)
|
|
83
|
+
handler.entry_challenge = (challenge, key)
|
|
84
|
+
# Only this answer keeps the connection: HTTP/1.1 with a zero-length body, and a deadline for the request to follow.
|
|
85
|
+
handler.protocol_version = "HTTP/1.1"
|
|
86
|
+
handler.close_connection = False
|
|
87
|
+
handler.connection.settimeout(HANDSHAKE_SECONDS)
|
|
88
|
+
handler.send_response(204)
|
|
89
|
+
handler.send_header(PROOF_HEADER, proof(key, "instance", nonce))
|
|
90
|
+
handler.send_header(NONCE_HEADER, challenge)
|
|
91
|
+
handler.send_header("Content-Length", "0")
|
|
92
|
+
handler.send_header("Cache-Control", "no-store")
|
|
93
|
+
handler.end_headers()
|
|
94
|
+
return True
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def request_proven(handler, policy):
|
|
98
|
+
"""The key this request proved, or None.
|
|
99
|
+
|
|
100
|
+
The proof must answer the challenge issued on this request's own connection, under the key that challenge was issued
|
|
101
|
+
with, and that key must still be current. The challenge is used up either way, and the connection closes after this
|
|
102
|
+
request. A stream opened by the request is bound to the returned key, not to whatever is current when it starts.
|
|
103
|
+
"""
|
|
104
|
+
challenge = getattr(handler, "entry_challenge", None)
|
|
105
|
+
handler.entry_challenge = None
|
|
106
|
+
if challenge is None:
|
|
107
|
+
return None
|
|
108
|
+
handler.close_connection = True
|
|
109
|
+
nonce, key = challenge
|
|
110
|
+
given = handler.headers.get(ENTRY_HEADER, "")
|
|
111
|
+
return key if key == policy.key() and hmac.compare_digest(given, proof(key, "entry", nonce)) else None
|
package/backend/server.py
CHANGED
|
@@ -23,6 +23,7 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
|
23
23
|
from pathlib import Path
|
|
24
24
|
from urllib.parse import parse_qs, quote, urlsplit
|
|
25
25
|
|
|
26
|
+
from .entry_guard import HELLO_PATH, PUBLIC_PATHS, EntryPolicy, answer_hello, request_proven
|
|
26
27
|
from .admin import (AdminError, admin_status, change_password, list_sessions, login_admin, logout_admin, register_admin,
|
|
27
28
|
revoke_sessions, session_valid, update_profile)
|
|
28
29
|
from .remote_launch import (COMMAND_WAIT_MAX, claim_machine_commands, finish_machine_command, list_machines, machine_command_status,
|
|
@@ -1235,6 +1236,16 @@ def check_message_references(connection, room_id, message):
|
|
|
1235
1236
|
raise ValueError("回复的消息不在当前房间中")
|
|
1236
1237
|
|
|
1237
1238
|
|
|
1239
|
+
CAPTURE_REFUSED_FIELDS = ("to", "handoff", "continue", "sequential", "loop", "parallel", "gates", "detach", "need_rounds",
|
|
1240
|
+
"next_review", "review_scope", "proactive", "request_ids", "reply_to", "attachment_ids")
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
def check_captured_reply(payload):
|
|
1244
|
+
"""A reply the local service captured after the software ended unsubmitted: it answers its execution and nothing more."""
|
|
1245
|
+
if type(payload.get("execution_id")) is not int or any(payload.get(field) for field in CAPTURE_REFUSED_FIELDS):
|
|
1246
|
+
raise ValueError("自动收录的回答只能关联本次执行 execution_id,不能派活、续跑、引用或带附件")
|
|
1247
|
+
|
|
1248
|
+
|
|
1238
1249
|
def append_free_message(connection, room, payload, actor):
|
|
1239
1250
|
"""Append a free-room message: the owner's creates requests, a member's completes its execution."""
|
|
1240
1251
|
if payload.get("kind") == "moderation":
|
|
@@ -1251,8 +1262,11 @@ def append_free_message(connection, room, payload, actor):
|
|
|
1251
1262
|
if actor == "用户" and (author != "用户" or payload.get("submitted_by", "用户") != "用户"):
|
|
1252
1263
|
raise PermissionError("房间控制凭据只能以用户身份发言")
|
|
1253
1264
|
submitted_by = author
|
|
1265
|
+
source = payload.get("source") if payload.get("source") in ("api", "auto") else "cli"
|
|
1254
1266
|
expected = {"author": author, "text": text, "reply_to": reply_to, "kind": "chat", "submitted_by": submitted_by,
|
|
1255
|
-
"source": "user" if author == "用户" else
|
|
1267
|
+
"source": "user" if author == "用户" else source}
|
|
1268
|
+
if expected["source"] == "auto":
|
|
1269
|
+
check_captured_reply(payload)
|
|
1256
1270
|
expected.update(message_summary(payload, expected))
|
|
1257
1271
|
identifiers = attachment_ids(payload)
|
|
1258
1272
|
previous = connection.execute(f"{MESSAGE_QUERY} WHERE room_id=? AND submission_id=?", (room_id, submission_id)).fetchone()
|
|
@@ -1283,7 +1297,9 @@ def append_free_message(connection, room, payload, actor):
|
|
|
1283
1297
|
(cursor.lastrowid, expected["summary"], expected["summary_by"]))
|
|
1284
1298
|
for position, identifier in enumerate(identifiers):
|
|
1285
1299
|
connection.execute("UPDATE attachments SET message_id=?,position=? WHERE id=?", (cursor.lastrowid, position, identifier))
|
|
1286
|
-
|
|
1300
|
+
# A captured reply keeps its text, but its mentions wake nobody: only a submitted answer may ask for a host.
|
|
1301
|
+
if expected["source"] != "auto":
|
|
1302
|
+
save_suggestions(connection, room_id, cursor.lastrowid, actor, payload)
|
|
1287
1303
|
save_review_plan(connection, room_id, cursor.lastrowid, review_plan)
|
|
1288
1304
|
if actor != "用户" and payload.get("proactive") is True:
|
|
1289
1305
|
# Spoken on its own: it completes no request; only the host's may dispatch.
|
|
@@ -1733,6 +1749,8 @@ class DiscussionServer(ThreadingHTTPServer):
|
|
|
1733
1749
|
self.admin_setup_key = os.getenv("DISCUSS_ADMIN_SETUP_KEY") or secrets.token_urlsafe(18)
|
|
1734
1750
|
service_home = os.getenv("DISCUSS_SERVICE_HOME")
|
|
1735
1751
|
self.connections_file = Path(service_home) / "site-connections.json" if service_home else None
|
|
1752
|
+
# A hosted instance serves only callers that prove they hold its key (backend/entry_guard.py).
|
|
1753
|
+
self.entry_policy = EntryPolicy()
|
|
1736
1754
|
super().__init__(address, DiscussionHandler)
|
|
1737
1755
|
|
|
1738
1756
|
def server_bind(self):
|
|
@@ -1870,9 +1888,35 @@ class DiscussionHandler(BaseHTTPRequestHandler):
|
|
|
1870
1888
|
if self.deferred_response:
|
|
1871
1889
|
self.send_payload(*self.deferred_response)
|
|
1872
1890
|
|
|
1891
|
+
def entry_refused(self, method, parsed):
|
|
1892
|
+
"""Under entry protection: answer a hello, or refuse a request that is neither public nor proven on its connection."""
|
|
1893
|
+
policy = self.server.entry_policy
|
|
1894
|
+
if not policy.enabled:
|
|
1895
|
+
return False
|
|
1896
|
+
if method == "GET" and parsed.path == HELLO_PATH:
|
|
1897
|
+
answer_hello(self, policy)
|
|
1898
|
+
return True
|
|
1899
|
+
self.entry_key = request_proven(self, policy)
|
|
1900
|
+
if self.entry_key:
|
|
1901
|
+
return False
|
|
1902
|
+
if method == "GET" and (parsed.path in ASSETS or parsed.path in PUBLIC_PATHS):
|
|
1903
|
+
return False
|
|
1904
|
+
self.close_connection = True
|
|
1905
|
+
# Sent directly, not through respond(): a refused caller leaves nothing in any room's event log.
|
|
1906
|
+
self.send_payload(403, "application/json", json.dumps({"error": "这个工作台只接受经入口的访问"}, ensure_ascii=False).encode("utf-8"))
|
|
1907
|
+
return True
|
|
1908
|
+
|
|
1909
|
+
def end_headers(self):
|
|
1910
|
+
"""Say so when an HTTP/1.1 connection (one opened by an entry hello) closes after this response."""
|
|
1911
|
+
if self.request_version == "HTTP/1.1" and self.protocol_version == "HTTP/1.1" and self.close_connection:
|
|
1912
|
+
self.send_header("Connection", "close")
|
|
1913
|
+
super().end_headers()
|
|
1914
|
+
|
|
1873
1915
|
def dispatch_routes(self, method, parsed, params, parts, text_mode):
|
|
1874
1916
|
"""Route one request and map validation, permission and lookup errors to their status codes."""
|
|
1875
1917
|
try:
|
|
1918
|
+
if self.entry_refused(method, parsed):
|
|
1919
|
+
return None
|
|
1876
1920
|
self.bridged = self.relay_bridge(parts)
|
|
1877
1921
|
if parts[:3] == ["api", "local", "media"] and len(parts) == 4 and method == "GET":
|
|
1878
1922
|
return self.serve_media_ticket(parts[3])
|
|
@@ -1919,6 +1963,9 @@ class DiscussionHandler(BaseHTTPRequestHandler):
|
|
|
1919
1963
|
self.close_connection = True
|
|
1920
1964
|
self.connection.settimeout(5)
|
|
1921
1965
|
hub = self.server.hub
|
|
1966
|
+
# Under entry protection a stream ends when the key changes or goes, even if nothing else happens meanwhile.
|
|
1967
|
+
policy = self.server.entry_policy
|
|
1968
|
+
opened_key = getattr(self, "entry_key", None) if policy.enabled else None
|
|
1922
1969
|
with hub.updates:
|
|
1923
1970
|
version = hub.update_version
|
|
1924
1971
|
self.send_response(200)
|
|
@@ -1939,6 +1986,8 @@ class DiscussionHandler(BaseHTTPRequestHandler):
|
|
|
1939
1986
|
changed = hub.update_version != version
|
|
1940
1987
|
rooms = hub.changed_rooms(version) if changed else None
|
|
1941
1988
|
version = hub.update_version
|
|
1989
|
+
if policy.enabled and policy.key() != opened_key:
|
|
1990
|
+
return
|
|
1942
1991
|
self.wfile.write(update_frame(rooms) if changed else b": keepalive\n\n")
|
|
1943
1992
|
self.wfile.flush()
|
|
1944
1993
|
except OSError:
|
|
@@ -2158,13 +2207,28 @@ class DiscussionHandler(BaseHTTPRequestHandler):
|
|
|
2158
2207
|
return value
|
|
2159
2208
|
return self.headers.get("X-Discussion-Admin", "")
|
|
2160
2209
|
|
|
2210
|
+
def entry_member_request(self):
|
|
2211
|
+
"""Under entry protection, whether the request names a room credential: it then acts only as that credential.
|
|
2212
|
+
|
|
2213
|
+
Passing the entry proves access to this instance, not a role in a room: a member's credential must not be lifted
|
|
2214
|
+
to the owner's because the request also looks like the loopback page.
|
|
2215
|
+
"""
|
|
2216
|
+
return self.server.entry_policy.enabled and bool(self.headers.get("X-Discussion-Token"))
|
|
2217
|
+
|
|
2218
|
+
def require_entry_owner(self, connection, room_id, token):
|
|
2219
|
+
"""Under entry protection, room management needs the owner's credential or the credential-less page itself."""
|
|
2220
|
+
if self.entry_member_request():
|
|
2221
|
+
require_owner(connection, room_id, token)
|
|
2222
|
+
elif self.server.entry_policy.enabled and not self.local_visitor():
|
|
2223
|
+
raise PermissionError("需要本房间的控制凭据")
|
|
2224
|
+
|
|
2161
2225
|
def local_visitor(self):
|
|
2162
2226
|
"""Whether this request may act as the loopback-only site's implicit administrator.
|
|
2163
2227
|
|
|
2164
2228
|
The Host must name this loopback port, which defeats DNS rebinding, and a browser request must come from the site
|
|
2165
2229
|
itself rather than another page. Programs on this computer can still send these headers; this guards browsers only.
|
|
2166
2230
|
"""
|
|
2167
|
-
if not self.server.local_site:
|
|
2231
|
+
if not self.server.local_site or self.entry_member_request():
|
|
2168
2232
|
return False
|
|
2169
2233
|
host = self.headers.get("Host", "")
|
|
2170
2234
|
if host not in (f"127.0.0.1:{self.server.server_port}", f"localhost:{self.server.server_port}"):
|
|
@@ -2485,6 +2549,8 @@ class DiscussionHandler(BaseHTTPRequestHandler):
|
|
|
2485
2549
|
pass
|
|
2486
2550
|
elif method == "POST":
|
|
2487
2551
|
free_actor = authorize_free_write(connection, room_id, token, parts[3] if len(parts) > 3 else "", self.read_payload())
|
|
2552
|
+
if (parts[3:] == ["backup"] and method == "GET") or (len(parts) == 4 and parts[3] in DIRECTORY_OPERATIONS and method == "POST"):
|
|
2553
|
+
self.require_entry_owner(connection, room_id, token)
|
|
2488
2554
|
if parts[3:] == ["backup"] and method == "GET":
|
|
2489
2555
|
if session_read_boundary(connection, room_id, token) is not None:
|
|
2490
2556
|
raise PermissionError("独立观点阶段需房主凭据导出")
|
|
@@ -2852,9 +2918,6 @@ def main():
|
|
|
2852
2918
|
except KeyboardInterrupt:
|
|
2853
2919
|
logging.info("Server stopped")
|
|
2854
2920
|
finally:
|
|
2855
|
-
if remote:
|
|
2856
|
-
remote.shutdown()
|
|
2857
|
-
remote.server_close()
|
|
2858
2921
|
server.server_close()
|
|
2859
2922
|
|
|
2860
2923
|
|