@openclaw/crabline 0.1.9 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -22
- package/assets/crabline-banner.svg +20 -0
- package/dist/src/bin/crabline.js.map +1 -1
- package/dist/src/cli/program.d.ts +25 -1
- package/dist/src/cli/program.js +579 -82
- package/dist/src/cli/program.js.map +1 -1
- package/dist/src/config/load.js +59 -6
- package/dist/src/config/load.js.map +1 -1
- package/dist/src/config/schema.d.ts +116 -92
- package/dist/src/config/schema.js +179 -73
- package/dist/src/config/schema.js.map +1 -1
- package/dist/src/core/errors.js +9 -2
- package/dist/src/core/errors.js.map +1 -1
- package/dist/src/core/exit-codes.js.map +1 -1
- package/dist/src/core/matcher.js +7 -6
- package/dist/src/core/matcher.js.map +1 -1
- package/dist/src/core/message-template.js.map +1 -1
- package/dist/src/core/nonces.d.ts +1 -0
- package/dist/src/core/nonces.js +4 -2
- package/dist/src/core/nonces.js.map +1 -1
- package/dist/src/core/reporters.d.ts +1 -0
- package/dist/src/core/reporters.js +42 -4
- package/dist/src/core/reporters.js.map +1 -1
- package/dist/src/core/run.d.ts +3 -0
- package/dist/src/core/run.js +415 -107
- package/dist/src/core/run.js.map +1 -1
- package/dist/src/core/safe-regex.d.ts +3 -0
- package/dist/src/core/safe-regex.js +21 -0
- package/dist/src/core/safe-regex.js.map +1 -0
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/openclaw/artifact-generation.d.ts +43 -0
- package/dist/src/openclaw/artifact-generation.js +363 -0
- package/dist/src/openclaw/artifact-generation.js.map +1 -0
- package/dist/src/openclaw/bridges/matrix.js +97 -12
- package/dist/src/openclaw/bridges/matrix.js.map +1 -1
- package/dist/src/openclaw/bridges/mattermost.js +32 -10
- package/dist/src/openclaw/bridges/mattermost.js.map +1 -1
- package/dist/src/openclaw/bridges/signal.js +55 -18
- package/dist/src/openclaw/bridges/signal.js.map +1 -1
- package/dist/src/openclaw/bridges/slack.js +85 -7
- package/dist/src/openclaw/bridges/slack.js.map +1 -1
- package/dist/src/openclaw/bridges/telegram.js +124 -30
- package/dist/src/openclaw/bridges/telegram.js.map +1 -1
- package/dist/src/openclaw/bridges/whatsapp.js +78 -17
- package/dist/src/openclaw/bridges/whatsapp.js.map +1 -1
- package/dist/src/openclaw/bridges/zalo.js +14 -5
- package/dist/src/openclaw/bridges/zalo.js.map +1 -1
- package/dist/src/openclaw/outbound-contract.d.ts +5 -0
- package/dist/src/openclaw/outbound-contract.js +40 -0
- package/dist/src/openclaw/outbound-contract.js.map +1 -0
- package/dist/src/openclaw/private-file.d.ts +28 -0
- package/dist/src/openclaw/private-file.js +465 -0
- package/dist/src/openclaw/private-file.js.map +1 -0
- package/dist/src/openclaw/shared.d.ts +21 -2
- package/dist/src/openclaw/shared.js +115 -18
- package/dist/src/openclaw/shared.js.map +1 -1
- package/dist/src/openclaw/smoke-lock.d.ts +63 -0
- package/dist/src/openclaw/smoke-lock.js +1004 -0
- package/dist/src/openclaw/smoke-lock.js.map +1 -0
- package/dist/src/openclaw.d.ts +16 -8
- package/dist/src/openclaw.js +334 -52
- package/dist/src/openclaw.js.map +1 -1
- package/dist/src/providers/builtin/discord.d.ts +10 -1
- package/dist/src/providers/builtin/discord.js +144 -18
- package/dist/src/providers/builtin/discord.js.map +1 -1
- package/dist/src/providers/builtin/external-webhook-auth.d.ts +11 -0
- package/dist/src/providers/builtin/external-webhook-auth.js +10 -0
- package/dist/src/providers/builtin/external-webhook-auth.js.map +1 -0
- package/dist/src/providers/builtin/feishu.d.ts +19 -2
- package/dist/src/providers/builtin/feishu.js +307 -25
- package/dist/src/providers/builtin/feishu.js.map +1 -1
- package/dist/src/providers/builtin/googlechat.d.ts +20 -1
- package/dist/src/providers/builtin/googlechat.js +178 -19
- package/dist/src/providers/builtin/googlechat.js.map +1 -1
- package/dist/src/providers/builtin/imessage.d.ts +4 -0
- package/dist/src/providers/builtin/imessage.js +26 -11
- package/dist/src/providers/builtin/imessage.js.map +1 -1
- package/dist/src/providers/builtin/loopback.js +92 -30
- package/dist/src/providers/builtin/loopback.js.map +1 -1
- package/dist/src/providers/builtin/matrix.d.ts +11 -1
- package/dist/src/providers/builtin/matrix.js +53 -29
- package/dist/src/providers/builtin/matrix.js.map +1 -1
- package/dist/src/providers/builtin/mattermost.d.ts +10 -0
- package/dist/src/providers/builtin/mattermost.js +38 -15
- package/dist/src/providers/builtin/mattermost.js.map +1 -1
- package/dist/src/providers/builtin/msteams.d.ts +17 -1
- package/dist/src/providers/builtin/msteams.js +113 -14
- package/dist/src/providers/builtin/msteams.js.map +1 -1
- package/dist/src/providers/builtin/native-local-mock.d.ts +2 -9
- package/dist/src/providers/builtin/native-local-mock.js +12 -35
- package/dist/src/providers/builtin/native-local-mock.js.map +1 -1
- package/dist/src/providers/builtin/script.d.ts +11 -15
- package/dist/src/providers/builtin/script.js +566 -104
- package/dist/src/providers/builtin/script.js.map +1 -1
- package/dist/src/providers/builtin/slack.d.ts +6 -0
- package/dist/src/providers/builtin/slack.js +99 -40
- package/dist/src/providers/builtin/slack.js.map +1 -1
- package/dist/src/providers/builtin/telegram.d.ts +6 -0
- package/dist/src/providers/builtin/telegram.js +64 -43
- package/dist/src/providers/builtin/telegram.js.map +1 -1
- package/dist/src/providers/builtin/whatsapp.d.ts +28 -8
- package/dist/src/providers/builtin/whatsapp.js +567 -30
- package/dist/src/providers/builtin/whatsapp.js.map +1 -1
- package/dist/src/providers/builtin/zalo.js +30 -19
- package/dist/src/providers/builtin/zalo.js.map +1 -1
- package/dist/src/providers/catalog.d.ts +3 -3
- package/dist/src/providers/catalog.js +3 -3
- package/dist/src/providers/catalog.js.map +1 -1
- package/dist/src/providers/local-mock.d.ts +11 -2
- package/dist/src/providers/local-mock.js +322 -96
- package/dist/src/providers/local-mock.js.map +1 -1
- package/dist/src/providers/native-ids.d.ts +1 -0
- package/dist/src/providers/native-ids.js +6 -1
- package/dist/src/providers/native-ids.js.map +1 -1
- package/dist/src/providers/recorder.d.ts +30 -2
- package/dist/src/providers/recorder.js +549 -70
- package/dist/src/providers/recorder.js.map +1 -1
- package/dist/src/providers/registry.d.ts +25 -1
- package/dist/src/providers/registry.js +280 -40
- package/dist/src/providers/registry.js.map +1 -1
- package/dist/src/providers/signed-jwt.d.ts +29 -0
- package/dist/src/providers/signed-jwt.js +202 -0
- package/dist/src/providers/signed-jwt.js.map +1 -0
- package/dist/src/providers/slack-ids.js.map +1 -1
- package/dist/src/providers/target-normalizers.d.ts +26 -0
- package/dist/src/providers/target-normalizers.js +276 -0
- package/dist/src/providers/target-normalizers.js.map +1 -0
- package/dist/src/providers/types.d.ts +5 -0
- package/dist/src/providers/types.js.map +1 -1
- package/dist/src/providers/webhook-server.d.ts +2 -0
- package/dist/src/providers/webhook-server.js +72 -25
- package/dist/src/providers/webhook-server.js.map +1 -1
- package/dist/src/servers/http.d.ts +23 -3
- package/dist/src/servers/http.js +213 -20
- package/dist/src/servers/http.js.map +1 -1
- package/dist/src/servers/index.js +20 -21
- package/dist/src/servers/index.js.map +1 -1
- package/dist/src/servers/matrix.js +465 -51
- package/dist/src/servers/matrix.js.map +1 -1
- package/dist/src/servers/mattermost.d.ts +5 -0
- package/dist/src/servers/mattermost.js +306 -58
- package/dist/src/servers/mattermost.js.map +1 -1
- package/dist/src/servers/pending-events.d.ts +2 -0
- package/dist/src/servers/pending-events.js +11 -0
- package/dist/src/servers/pending-events.js.map +1 -0
- package/dist/src/servers/recorder.d.ts +5 -0
- package/dist/src/servers/recorder.js +262 -3
- package/dist/src/servers/recorder.js.map +1 -1
- package/dist/src/servers/signal.d.ts +2 -0
- package/dist/src/servers/signal.js +444 -56
- package/dist/src/servers/signal.js.map +1 -1
- package/dist/src/servers/slack.d.ts +5 -0
- package/dist/src/servers/slack.js +515 -49
- package/dist/src/servers/slack.js.map +1 -1
- package/dist/src/servers/telegram.d.ts +111 -0
- package/dist/src/servers/telegram.js +830 -158
- package/dist/src/servers/telegram.js.map +1 -1
- package/dist/src/servers/webhook-target.d.ts +40 -0
- package/dist/src/servers/webhook-target.js +349 -0
- package/dist/src/servers/webhook-target.js.map +1 -0
- package/dist/src/servers/websocket.d.ts +2 -0
- package/dist/src/servers/websocket.js +29 -0
- package/dist/src/servers/websocket.js.map +1 -0
- package/dist/src/servers/whatsapp-baileys-websocket.d.ts +65 -1
- package/dist/src/servers/whatsapp-baileys-websocket.js +723 -99
- package/dist/src/servers/whatsapp-baileys-websocket.js.map +1 -1
- package/dist/src/servers/whatsapp-jid.d.ts +5 -0
- package/dist/src/servers/whatsapp-jid.js +32 -0
- package/dist/src/servers/whatsapp-jid.js.map +1 -0
- package/dist/src/servers/whatsapp-wire/binary-node.d.ts +5 -0
- package/dist/src/servers/whatsapp-wire/binary-node.js +87 -20
- package/dist/src/servers/whatsapp-wire/binary-node.js.map +1 -1
- package/dist/src/servers/whatsapp-wire/crypto.d.ts +6 -1
- package/dist/src/servers/whatsapp-wire/crypto.js +99 -45
- package/dist/src/servers/whatsapp-wire/crypto.js.map +1 -1
- package/dist/src/servers/whatsapp-wire/handshake.js +39 -4
- package/dist/src/servers/whatsapp-wire/handshake.js.map +1 -1
- package/dist/src/servers/whatsapp-wire/signal.js.map +1 -1
- package/dist/src/servers/whatsapp.d.ts +12 -7
- package/dist/src/servers/whatsapp.js +269 -107
- package/dist/src/servers/whatsapp.js.map +1 -1
- package/dist/src/servers/zalo.d.ts +14 -0
- package/dist/src/servers/zalo.js +521 -88
- package/dist/src/servers/zalo.js.map +1 -1
- package/docs/channel-setup.md +197 -19
- package/fixtures/examples/crabline.example.yaml +10 -0
- package/package.json +20 -15
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import fs, {} from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
const execFileAsync = promisify(execFile);
|
|
7
|
+
const WINDOWS_OWNER_ONLY_ACL_SCRIPT = String.raw `
|
|
8
|
+
$ErrorActionPreference = "Stop"
|
|
9
|
+
$filePath = $env:CRABLINE_PRIVATE_FILE_PATH
|
|
10
|
+
if ([string]::IsNullOrWhiteSpace($filePath)) {
|
|
11
|
+
throw "CRABLINE_PRIVATE_FILE_PATH is required."
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
|
15
|
+
$sid = $identity.User
|
|
16
|
+
if ($null -eq $sid) {
|
|
17
|
+
throw "Could not resolve the current Windows user SID."
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
$acl = Get-Acl -LiteralPath $filePath
|
|
21
|
+
$acl.SetOwner($sid)
|
|
22
|
+
$acl.SetAccessRuleProtection($true, $false)
|
|
23
|
+
$existingRules = @($acl.GetAccessRules(
|
|
24
|
+
$true,
|
|
25
|
+
$false,
|
|
26
|
+
[System.Security.Principal.SecurityIdentifier]
|
|
27
|
+
))
|
|
28
|
+
foreach ($existingRule in $existingRules) {
|
|
29
|
+
[void]$acl.RemoveAccessRuleSpecific($existingRule)
|
|
30
|
+
}
|
|
31
|
+
$rule = [System.Security.AccessControl.FileSystemAccessRule]::new(
|
|
32
|
+
$sid,
|
|
33
|
+
[System.Security.AccessControl.FileSystemRights]::FullControl,
|
|
34
|
+
[System.Security.AccessControl.AccessControlType]::Allow
|
|
35
|
+
)
|
|
36
|
+
$acl.SetAccessRule($rule)
|
|
37
|
+
Set-Acl -LiteralPath $filePath -AclObject $acl
|
|
38
|
+
|
|
39
|
+
$actual = Get-Acl -LiteralPath $filePath
|
|
40
|
+
$ownerSid = $actual.GetOwner([System.Security.Principal.SecurityIdentifier])
|
|
41
|
+
$rules = @($actual.GetAccessRules(
|
|
42
|
+
$true,
|
|
43
|
+
$true,
|
|
44
|
+
[System.Security.Principal.SecurityIdentifier]
|
|
45
|
+
))
|
|
46
|
+
if (-not $actual.AreAccessRulesProtected) {
|
|
47
|
+
throw "Private file DACL still inherits permissions."
|
|
48
|
+
}
|
|
49
|
+
if ($ownerSid.Value -ne $sid.Value) {
|
|
50
|
+
throw "Private file owner SID does not match the current user."
|
|
51
|
+
}
|
|
52
|
+
if ($rules.Count -ne 1) {
|
|
53
|
+
throw "Private file DACL must contain exactly one access rule."
|
|
54
|
+
}
|
|
55
|
+
$actualRule = $rules[0]
|
|
56
|
+
if (
|
|
57
|
+
$actualRule.IsInherited -or
|
|
58
|
+
$actualRule.IdentityReference.Value -ne $sid.Value -or
|
|
59
|
+
$actualRule.AccessControlType -ne [System.Security.AccessControl.AccessControlType]::Allow -or
|
|
60
|
+
(($actualRule.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::FullControl) -ne [System.Security.AccessControl.FileSystemRights]::FullControl)
|
|
61
|
+
) {
|
|
62
|
+
throw "Private file DACL is not owner-only full control."
|
|
63
|
+
}
|
|
64
|
+
`;
|
|
65
|
+
const WINDOWS_OWNER_ONLY_DIRECTORY_ACL_SCRIPT = String.raw `
|
|
66
|
+
$ErrorActionPreference = "Stop"
|
|
67
|
+
$directoryPath = $env:CRABLINE_PRIVATE_DIRECTORY_PATH
|
|
68
|
+
if ([string]::IsNullOrWhiteSpace($directoryPath)) {
|
|
69
|
+
throw "CRABLINE_PRIVATE_DIRECTORY_PATH is required."
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
|
73
|
+
$sid = $identity.User
|
|
74
|
+
if ($null -eq $sid) {
|
|
75
|
+
throw "Could not resolve the current Windows user SID."
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
$acl = Get-Acl -LiteralPath $directoryPath
|
|
79
|
+
$acl.SetOwner($sid)
|
|
80
|
+
$acl.SetAccessRuleProtection($true, $false)
|
|
81
|
+
$existingRules = @($acl.GetAccessRules(
|
|
82
|
+
$true,
|
|
83
|
+
$false,
|
|
84
|
+
[System.Security.Principal.SecurityIdentifier]
|
|
85
|
+
))
|
|
86
|
+
foreach ($existingRule in $existingRules) {
|
|
87
|
+
[void]$acl.RemoveAccessRuleSpecific($existingRule)
|
|
88
|
+
}
|
|
89
|
+
$inheritance = (
|
|
90
|
+
[System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor
|
|
91
|
+
[System.Security.AccessControl.InheritanceFlags]::ObjectInherit
|
|
92
|
+
)
|
|
93
|
+
$rule = [System.Security.AccessControl.FileSystemAccessRule]::new(
|
|
94
|
+
$sid,
|
|
95
|
+
[System.Security.AccessControl.FileSystemRights]::FullControl,
|
|
96
|
+
$inheritance,
|
|
97
|
+
[System.Security.AccessControl.PropagationFlags]::None,
|
|
98
|
+
[System.Security.AccessControl.AccessControlType]::Allow
|
|
99
|
+
)
|
|
100
|
+
$acl.SetAccessRule($rule)
|
|
101
|
+
Set-Acl -LiteralPath $directoryPath -AclObject $acl
|
|
102
|
+
|
|
103
|
+
$actual = Get-Acl -LiteralPath $directoryPath
|
|
104
|
+
$ownerSid = $actual.GetOwner([System.Security.Principal.SecurityIdentifier])
|
|
105
|
+
$rules = @($actual.GetAccessRules(
|
|
106
|
+
$true,
|
|
107
|
+
$true,
|
|
108
|
+
[System.Security.Principal.SecurityIdentifier]
|
|
109
|
+
))
|
|
110
|
+
if (-not $actual.AreAccessRulesProtected) {
|
|
111
|
+
throw "Private directory DACL still inherits permissions."
|
|
112
|
+
}
|
|
113
|
+
if ($ownerSid.Value -ne $sid.Value) {
|
|
114
|
+
throw "Private directory owner SID does not match the current user."
|
|
115
|
+
}
|
|
116
|
+
if ($rules.Count -ne 1) {
|
|
117
|
+
throw "Private directory DACL must contain exactly one access rule."
|
|
118
|
+
}
|
|
119
|
+
$actualRule = $rules[0]
|
|
120
|
+
$requiredInheritance = (
|
|
121
|
+
[System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor
|
|
122
|
+
[System.Security.AccessControl.InheritanceFlags]::ObjectInherit
|
|
123
|
+
)
|
|
124
|
+
if (
|
|
125
|
+
$actualRule.IsInherited -or
|
|
126
|
+
$actualRule.IdentityReference.Value -ne $sid.Value -or
|
|
127
|
+
$actualRule.AccessControlType -ne [System.Security.AccessControl.AccessControlType]::Allow -or
|
|
128
|
+
(($actualRule.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::FullControl) -ne [System.Security.AccessControl.FileSystemRights]::FullControl) -or
|
|
129
|
+
(($actualRule.InheritanceFlags -band $requiredInheritance) -ne $requiredInheritance)
|
|
130
|
+
) {
|
|
131
|
+
throw "Private directory DACL is not owner-only inheritable full control."
|
|
132
|
+
}
|
|
133
|
+
`;
|
|
134
|
+
const runWindowsAclCommand = async (command, args, options) => {
|
|
135
|
+
await execFileAsync(command, args, options);
|
|
136
|
+
};
|
|
137
|
+
export function resolveWindowsPowerShellPath(systemRoot) {
|
|
138
|
+
const normalizedRoot = systemRoot?.trim() ? path.win32.normalize(systemRoot.trim()) : undefined;
|
|
139
|
+
if (!normalizedRoot || !/^[A-Za-z]:\\/.test(normalizedRoot)) {
|
|
140
|
+
throw new Error("SystemRoot must be an absolute local Windows path.");
|
|
141
|
+
}
|
|
142
|
+
return path.win32.join(normalizedRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
143
|
+
}
|
|
144
|
+
export async function applyOwnerOnlyWindowsAcl(filePath, run = runWindowsAclCommand, systemRoot = process.env.SystemRoot) {
|
|
145
|
+
try {
|
|
146
|
+
const powershellPath = resolveWindowsPowerShellPath(systemRoot);
|
|
147
|
+
await run(powershellPath, ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", WINDOWS_OWNER_ONLY_ACL_SCRIPT], {
|
|
148
|
+
env: {
|
|
149
|
+
...process.env,
|
|
150
|
+
CRABLINE_PRIVATE_FILE_PATH: path.resolve(filePath),
|
|
151
|
+
},
|
|
152
|
+
windowsHide: true,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
throw new Error("Could not apply and verify an owner-only Windows ACL; powershell.exe with Set-Acl is required.", { cause: error });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
export async function applyOwnerOnlyWindowsDirectoryAcl(directoryPath, run = runWindowsAclCommand, systemRoot = process.env.SystemRoot) {
|
|
160
|
+
try {
|
|
161
|
+
const powershellPath = resolveWindowsPowerShellPath(systemRoot);
|
|
162
|
+
await run(powershellPath, [
|
|
163
|
+
"-NoLogo",
|
|
164
|
+
"-NoProfile",
|
|
165
|
+
"-NonInteractive",
|
|
166
|
+
"-Command",
|
|
167
|
+
WINDOWS_OWNER_ONLY_DIRECTORY_ACL_SCRIPT,
|
|
168
|
+
], {
|
|
169
|
+
env: {
|
|
170
|
+
...process.env,
|
|
171
|
+
CRABLINE_PRIVATE_DIRECTORY_PATH: path.resolve(directoryPath),
|
|
172
|
+
},
|
|
173
|
+
windowsHide: true,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
throw new Error("Could not apply and verify an owner-only Windows directory ACL; powershell.exe with Set-Acl is required.", { cause: error });
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
async function readHandleIdentity(handle) {
|
|
181
|
+
const stats = await handle.stat({ bigint: true });
|
|
182
|
+
if (stats.ino <= 0n) {
|
|
183
|
+
throw new Error("The filesystem did not provide a stable private file identity.");
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
device: stats.dev,
|
|
187
|
+
inode: stats.ino,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
async function assertPathIdentity(filePath, expected) {
|
|
191
|
+
try {
|
|
192
|
+
const stats = await fs.lstat(filePath, { bigint: true });
|
|
193
|
+
if (stats.isFile() &&
|
|
194
|
+
stats.nlink === 1n &&
|
|
195
|
+
stats.dev === expected.device &&
|
|
196
|
+
stats.ino === expected.inode) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
throw new Error("Private file path identity changed during publication.", { cause: error });
|
|
202
|
+
}
|
|
203
|
+
throw new Error("Private file path identity changed during publication.");
|
|
204
|
+
}
|
|
205
|
+
async function readDirectoryHandleIdentity(handle) {
|
|
206
|
+
const stats = await handle.stat({ bigint: true });
|
|
207
|
+
if (!stats.isDirectory() || stats.ino <= 0n) {
|
|
208
|
+
throw new Error("The filesystem did not provide a stable private directory identity.");
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
device: stats.dev,
|
|
212
|
+
inode: stats.ino,
|
|
213
|
+
userId: stats.uid,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
async function assertDirectoryPathIdentity(directoryPath, expected) {
|
|
217
|
+
try {
|
|
218
|
+
const stats = await fs.lstat(directoryPath, { bigint: true });
|
|
219
|
+
if (stats.isDirectory() && stats.dev === expected.device && stats.ino === expected.inode) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
throw new Error("Private directory path identity changed during publication.", {
|
|
225
|
+
cause: error,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
throw new Error("Private directory path identity changed during publication.");
|
|
229
|
+
}
|
|
230
|
+
export async function captureDirectoryIdentity(directoryPath) {
|
|
231
|
+
const handle = await fs.open(directoryPath, "r");
|
|
232
|
+
try {
|
|
233
|
+
const identity = await readDirectoryHandleIdentity(handle);
|
|
234
|
+
const secured = {
|
|
235
|
+
async assertIdentityAt(currentPath = directoryPath) {
|
|
236
|
+
await assertDirectoryPathIdentity(currentPath, identity);
|
|
237
|
+
},
|
|
238
|
+
directoryPath,
|
|
239
|
+
};
|
|
240
|
+
await secured.assertIdentityAt();
|
|
241
|
+
return secured;
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
await handle.close();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
export async function syncParentDirectory(filePath, platform = process.platform) {
|
|
248
|
+
if (platform === "win32") {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const handle = await fs.open(path.dirname(filePath), "r");
|
|
252
|
+
try {
|
|
253
|
+
await handle.sync();
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
await handle.close();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
async function syncParentAtAccessibleBoundary(filePath, syncParent, platform) {
|
|
260
|
+
try {
|
|
261
|
+
await syncParent(filePath, platform);
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
const code = error.code;
|
|
266
|
+
if (code === "EACCES" || code === "EPERM") {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
throw error;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async function syncPathAncestry(filePath, syncParent, platform, firstCreatedDirectory) {
|
|
273
|
+
const resolvedFilePath = path.resolve(filePath);
|
|
274
|
+
let currentPath = resolvedFilePath;
|
|
275
|
+
const syncThroughPath = firstCreatedDirectory === undefined ? undefined : path.resolve(firstCreatedDirectory);
|
|
276
|
+
for (;;) {
|
|
277
|
+
const mandatory = syncThroughPath !== undefined || currentPath === resolvedFilePath;
|
|
278
|
+
if (mandatory) {
|
|
279
|
+
await syncParent(currentPath, platform);
|
|
280
|
+
}
|
|
281
|
+
else if (!(await syncParentAtAccessibleBoundary(currentPath, syncParent, platform))) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (currentPath === syncThroughPath) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
const parentPath = path.dirname(currentPath);
|
|
288
|
+
if (path.dirname(parentPath) === parentPath) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
currentPath = parentPath;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
export async function removeSecuredPrivateDirectory(secured, currentPath = secured.directoryPath, quarantineBaseName = path.basename(currentPath)) {
|
|
295
|
+
if (path.basename(quarantineBaseName) !== quarantineBaseName || quarantineBaseName.length === 0) {
|
|
296
|
+
throw new Error("Private directory quarantine basename is malformed.");
|
|
297
|
+
}
|
|
298
|
+
const quarantinePath = path.join(path.dirname(currentPath), `.${quarantineBaseName}.${process.pid}.${randomUUID()}.remove`);
|
|
299
|
+
await secured.assertIdentityAt(currentPath);
|
|
300
|
+
await fs.rename(currentPath, quarantinePath);
|
|
301
|
+
await secured.assertIdentityAt(quarantinePath);
|
|
302
|
+
await syncParentDirectory(quarantinePath);
|
|
303
|
+
await fs.rm(quarantinePath, { force: true, recursive: true });
|
|
304
|
+
await syncParentDirectory(quarantinePath);
|
|
305
|
+
}
|
|
306
|
+
export async function securePrivateDirectory(directoryPath, options = {}) {
|
|
307
|
+
let created = false;
|
|
308
|
+
try {
|
|
309
|
+
await fs.mkdir(directoryPath, { mode: 0o700 });
|
|
310
|
+
created = true;
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
if (error.code !== "EEXIST") {
|
|
314
|
+
throw error;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const handle = await fs.open(directoryPath, "r");
|
|
318
|
+
let handleOpen = true;
|
|
319
|
+
const closeHandle = async () => {
|
|
320
|
+
if (!handleOpen) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
handleOpen = false;
|
|
324
|
+
await handle.close();
|
|
325
|
+
};
|
|
326
|
+
let identity;
|
|
327
|
+
try {
|
|
328
|
+
identity = await readDirectoryHandleIdentity(handle);
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
try {
|
|
332
|
+
await closeHandle();
|
|
333
|
+
}
|
|
334
|
+
catch (closeError) {
|
|
335
|
+
const aggregateError = new AggregateError([error, closeError], "Private directory identity verification failed and its handle could not be closed.");
|
|
336
|
+
aggregateError.cause = error;
|
|
337
|
+
throw aggregateError;
|
|
338
|
+
}
|
|
339
|
+
throw error;
|
|
340
|
+
}
|
|
341
|
+
const secured = {
|
|
342
|
+
async assertIdentityAt(currentPath = directoryPath) {
|
|
343
|
+
await assertDirectoryPathIdentity(currentPath, identity);
|
|
344
|
+
},
|
|
345
|
+
directoryPath,
|
|
346
|
+
};
|
|
347
|
+
try {
|
|
348
|
+
await secured.assertIdentityAt();
|
|
349
|
+
if ((options.platform ?? process.platform) === "win32") {
|
|
350
|
+
await (options.secureWindowsDirectory ?? applyOwnerOnlyWindowsDirectoryAcl)(directoryPath);
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
const currentUserId = options.currentUserId ?? process.geteuid?.();
|
|
354
|
+
if (currentUserId === undefined ||
|
|
355
|
+
!Number.isSafeInteger(currentUserId) ||
|
|
356
|
+
currentUserId < 0 ||
|
|
357
|
+
identity.userId !== BigInt(currentUserId)) {
|
|
358
|
+
throw new Error("Private directory must be owned by the current POSIX user.");
|
|
359
|
+
}
|
|
360
|
+
await handle.chmod(0o700);
|
|
361
|
+
}
|
|
362
|
+
await secured.assertIdentityAt();
|
|
363
|
+
const syncParent = options.syncParent ?? syncParentDirectory;
|
|
364
|
+
if (created) {
|
|
365
|
+
await syncParent(directoryPath, options.platform);
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
await syncParentAtAccessibleBoundary(directoryPath, syncParent, options.platform);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
catch (error) {
|
|
372
|
+
let primaryError = error;
|
|
373
|
+
try {
|
|
374
|
+
await closeHandle();
|
|
375
|
+
}
|
|
376
|
+
catch (closeError) {
|
|
377
|
+
const aggregateError = new AggregateError([error, closeError], "Private directory securing failed and its verification handle could not be closed.");
|
|
378
|
+
aggregateError.cause = error;
|
|
379
|
+
primaryError = aggregateError;
|
|
380
|
+
}
|
|
381
|
+
if (created) {
|
|
382
|
+
try {
|
|
383
|
+
await removeSecuredPrivateDirectory(secured);
|
|
384
|
+
}
|
|
385
|
+
catch (cleanupError) {
|
|
386
|
+
const aggregateError = new AggregateError([primaryError, cleanupError], `Private directory securing failed and rollback cleanup also failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
387
|
+
aggregateError.cause = primaryError;
|
|
388
|
+
throw aggregateError;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
throw primaryError;
|
|
392
|
+
}
|
|
393
|
+
finally {
|
|
394
|
+
await closeHandle();
|
|
395
|
+
}
|
|
396
|
+
return secured;
|
|
397
|
+
}
|
|
398
|
+
export async function publishPrivateFileAtomically(filePath, contents, options = {}) {
|
|
399
|
+
const temporaryPath = path.join(path.dirname(filePath), `.${path.basename(filePath)}.${process.pid}.${randomUUID()}.tmp`);
|
|
400
|
+
const parentDirectory = path.resolve(path.dirname(filePath));
|
|
401
|
+
const firstCreatedDirectory = await fs.mkdir(parentDirectory, { recursive: true });
|
|
402
|
+
let handle;
|
|
403
|
+
let publicationFailed = false;
|
|
404
|
+
let primaryError;
|
|
405
|
+
try {
|
|
406
|
+
handle = await fs.open(temporaryPath, "wx+", 0o600);
|
|
407
|
+
const identity = await readHandleIdentity(handle);
|
|
408
|
+
if ((options.platform ?? process.platform) === "win32") {
|
|
409
|
+
await (options.secureWindowsFile ?? applyOwnerOnlyWindowsAcl)(temporaryPath);
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
await handle.chmod(0o600);
|
|
413
|
+
}
|
|
414
|
+
await assertPathIdentity(temporaryPath, identity);
|
|
415
|
+
await handle.writeFile(contents, "utf8");
|
|
416
|
+
await handle.sync();
|
|
417
|
+
await assertPathIdentity(temporaryPath, identity);
|
|
418
|
+
await options.beforeRename?.(temporaryPath);
|
|
419
|
+
await assertPathIdentity(temporaryPath, identity);
|
|
420
|
+
await fs.rename(temporaryPath, filePath);
|
|
421
|
+
await syncPathAncestry(filePath, options.syncParent ?? syncParentDirectory, options.platform, firstCreatedDirectory);
|
|
422
|
+
await options.afterRename?.(filePath);
|
|
423
|
+
await assertPathIdentity(filePath, identity);
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
publicationFailed = true;
|
|
427
|
+
primaryError = error;
|
|
428
|
+
}
|
|
429
|
+
const cleanupErrors = [];
|
|
430
|
+
try {
|
|
431
|
+
await handle?.close();
|
|
432
|
+
}
|
|
433
|
+
catch (error) {
|
|
434
|
+
cleanupErrors.push(error);
|
|
435
|
+
}
|
|
436
|
+
try {
|
|
437
|
+
await (options.removeTemporaryFile ??
|
|
438
|
+
((candidatePath) => fs.rm(candidatePath, { force: true })))(temporaryPath);
|
|
439
|
+
}
|
|
440
|
+
catch (error) {
|
|
441
|
+
cleanupErrors.push(error);
|
|
442
|
+
}
|
|
443
|
+
if (publicationFailed) {
|
|
444
|
+
if (cleanupErrors.length > 0) {
|
|
445
|
+
const primaryMessage = primaryError instanceof Error ? primaryError.message : String(primaryError);
|
|
446
|
+
const aggregateError = new AggregateError([primaryError, ...cleanupErrors], `${primaryMessage} Private temporary file cleanup also failed.`);
|
|
447
|
+
aggregateError.cause = primaryError;
|
|
448
|
+
const primaryCode = typeof primaryError === "object" && primaryError !== null
|
|
449
|
+
? primaryError.code
|
|
450
|
+
: undefined;
|
|
451
|
+
if (primaryCode) {
|
|
452
|
+
Object.assign(aggregateError, { code: primaryCode });
|
|
453
|
+
}
|
|
454
|
+
throw aggregateError;
|
|
455
|
+
}
|
|
456
|
+
throw primaryError;
|
|
457
|
+
}
|
|
458
|
+
if (cleanupErrors.length === 1) {
|
|
459
|
+
throw cleanupErrors[0];
|
|
460
|
+
}
|
|
461
|
+
if (cleanupErrors.length > 1) {
|
|
462
|
+
throw new AggregateError(cleanupErrors, "Private temporary file publication cleanup failed.");
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
//# sourceMappingURL=private-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"private-file.js","sourceRoot":"","sources":["../../../src/openclaw/private-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,EAAE,EAAmB,MAAM,kB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D,MAAM,YAAY,CAAC;IACrB,CAAC;IACD,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/B,MAAM,aAAa,CAAC,CAAC,CAAC,CAAC;IACzB,CAAC;IACD,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,cAAc,CAAC,aAAa,EAAE,oDAAoD,CAAC,CAAC;IAChG,CAAC;AACH,CAAC","sourcesContent":["import { execFile } from \"node:child_process\";\nimport { randomUUID } from \"node:crypto\";\nimport fs, { type FileHandle } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { promisify } from \"node:util\";\n\nconst execFileAsync = promisify(execFile);\n\nconst WINDOWS_OWNER_ONLY_ACL_SCRIPT = String.raw`\n$ErrorActionPreference = \"Stop\"\n$filePath = $env:CRABLINE_PRIVATE_FILE_PATH\nif ([string]::IsNullOrWhiteSpace($filePath)) {\n throw \"CRABLINE_PRIVATE_FILE_PATH is required.\"\n}\n\n$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()\n$sid = $identity.User\nif ($null -eq $sid) {\n throw \"Could not resolve the current Windows user SID.\"\n}\n\n$acl = Get-Acl -LiteralPath $filePath\n$acl.SetOwner($sid)\n$acl.SetAccessRuleProtection($true, $false)\n$existingRules = @($acl.GetAccessRules(\n $true,\n $false,\n [System.Security.Principal.SecurityIdentifier]\n))\nforeach ($existingRule in $existingRules) {\n [void]$acl.RemoveAccessRuleSpecific($existingRule)\n}\n$rule = [System.Security.AccessControl.FileSystemAccessRule]::new(\n $sid,\n [System.Security.AccessControl.FileSystemRights]::FullControl,\n [System.Security.AccessControl.AccessControlType]::Allow\n)\n$acl.SetAccessRule($rule)\nSet-Acl -LiteralPath $filePath -AclObject $acl\n\n$actual = Get-Acl -LiteralPath $filePath\n$ownerSid = $actual.GetOwner([System.Security.Principal.SecurityIdentifier])\n$rules = @($actual.GetAccessRules(\n $true,\n $true,\n [System.Security.Principal.SecurityIdentifier]\n))\nif (-not $actual.AreAccessRulesProtected) {\n throw \"Private file DACL still inherits permissions.\"\n}\nif ($ownerSid.Value -ne $sid.Value) {\n throw \"Private file owner SID does not match the current user.\"\n}\nif ($rules.Count -ne 1) {\n throw \"Private file DACL must contain exactly one access rule.\"\n}\n$actualRule = $rules[0]\nif (\n $actualRule.IsInherited -or\n $actualRule.IdentityReference.Value -ne $sid.Value -or\n $actualRule.AccessControlType -ne [System.Security.AccessControl.AccessControlType]::Allow -or\n (($actualRule.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::FullControl) -ne [System.Security.AccessControl.FileSystemRights]::FullControl)\n) {\n throw \"Private file DACL is not owner-only full control.\"\n}\n`;\n\nconst WINDOWS_OWNER_ONLY_DIRECTORY_ACL_SCRIPT = String.raw`\n$ErrorActionPreference = \"Stop\"\n$directoryPath = $env:CRABLINE_PRIVATE_DIRECTORY_PATH\nif ([string]::IsNullOrWhiteSpace($directoryPath)) {\n throw \"CRABLINE_PRIVATE_DIRECTORY_PATH is required.\"\n}\n\n$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()\n$sid = $identity.User\nif ($null -eq $sid) {\n throw \"Could not resolve the current Windows user SID.\"\n}\n\n$acl = Get-Acl -LiteralPath $directoryPath\n$acl.SetOwner($sid)\n$acl.SetAccessRuleProtection($true, $false)\n$existingRules = @($acl.GetAccessRules(\n $true,\n $false,\n [System.Security.Principal.SecurityIdentifier]\n))\nforeach ($existingRule in $existingRules) {\n [void]$acl.RemoveAccessRuleSpecific($existingRule)\n}\n$inheritance = (\n [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor\n [System.Security.AccessControl.InheritanceFlags]::ObjectInherit\n)\n$rule = [System.Security.AccessControl.FileSystemAccessRule]::new(\n $sid,\n [System.Security.AccessControl.FileSystemRights]::FullControl,\n $inheritance,\n [System.Security.AccessControl.PropagationFlags]::None,\n [System.Security.AccessControl.AccessControlType]::Allow\n)\n$acl.SetAccessRule($rule)\nSet-Acl -LiteralPath $directoryPath -AclObject $acl\n\n$actual = Get-Acl -LiteralPath $directoryPath\n$ownerSid = $actual.GetOwner([System.Security.Principal.SecurityIdentifier])\n$rules = @($actual.GetAccessRules(\n $true,\n $true,\n [System.Security.Principal.SecurityIdentifier]\n))\nif (-not $actual.AreAccessRulesProtected) {\n throw \"Private directory DACL still inherits permissions.\"\n}\nif ($ownerSid.Value -ne $sid.Value) {\n throw \"Private directory owner SID does not match the current user.\"\n}\nif ($rules.Count -ne 1) {\n throw \"Private directory DACL must contain exactly one access rule.\"\n}\n$actualRule = $rules[0]\n$requiredInheritance = (\n [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor\n [System.Security.AccessControl.InheritanceFlags]::ObjectInherit\n)\nif (\n $actualRule.IsInherited -or\n $actualRule.IdentityReference.Value -ne $sid.Value -or\n $actualRule.AccessControlType -ne [System.Security.AccessControl.AccessControlType]::Allow -or\n (($actualRule.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::FullControl) -ne [System.Security.AccessControl.FileSystemRights]::FullControl) -or\n (($actualRule.InheritanceFlags -band $requiredInheritance) -ne $requiredInheritance)\n) {\n throw \"Private directory DACL is not owner-only inheritable full control.\"\n}\n`;\n\nexport type WindowsAclRunner = (\n command: string,\n args: string[],\n options: {\n env: NodeJS.ProcessEnv;\n windowsHide: boolean;\n },\n) => Promise<void>;\n\nconst runWindowsAclCommand: WindowsAclRunner = async (command, args, options) => {\n await execFileAsync(command, args, options);\n};\n\nexport function resolveWindowsPowerShellPath(systemRoot: string | null | undefined): string {\n const normalizedRoot = systemRoot?.trim() ? path.win32.normalize(systemRoot.trim()) : undefined;\n if (!normalizedRoot || !/^[A-Za-z]:\\\\/.test(normalizedRoot)) {\n throw new Error(\"SystemRoot must be an absolute local Windows path.\");\n }\n return path.win32.join(normalizedRoot, \"System32\", \"WindowsPowerShell\", \"v1.0\", \"powershell.exe\");\n}\n\nexport async function applyOwnerOnlyWindowsAcl(\n filePath: string,\n run: WindowsAclRunner = runWindowsAclCommand,\n systemRoot: string | null | undefined = process.env.SystemRoot,\n): Promise<void> {\n try {\n const powershellPath = resolveWindowsPowerShellPath(systemRoot);\n await run(\n powershellPath,\n [\"-NoLogo\", \"-NoProfile\", \"-NonInteractive\", \"-Command\", WINDOWS_OWNER_ONLY_ACL_SCRIPT],\n {\n env: {\n ...process.env,\n CRABLINE_PRIVATE_FILE_PATH: path.resolve(filePath),\n },\n windowsHide: true,\n },\n );\n } catch (error) {\n throw new Error(\n \"Could not apply and verify an owner-only Windows ACL; powershell.exe with Set-Acl is required.\",\n { cause: error },\n );\n }\n}\n\nexport async function applyOwnerOnlyWindowsDirectoryAcl(\n directoryPath: string,\n run: WindowsAclRunner = runWindowsAclCommand,\n systemRoot: string | null | undefined = process.env.SystemRoot,\n): Promise<void> {\n try {\n const powershellPath = resolveWindowsPowerShellPath(systemRoot);\n await run(\n powershellPath,\n [\n \"-NoLogo\",\n \"-NoProfile\",\n \"-NonInteractive\",\n \"-Command\",\n WINDOWS_OWNER_ONLY_DIRECTORY_ACL_SCRIPT,\n ],\n {\n env: {\n ...process.env,\n CRABLINE_PRIVATE_DIRECTORY_PATH: path.resolve(directoryPath),\n },\n windowsHide: true,\n },\n );\n } catch (error) {\n throw new Error(\n \"Could not apply and verify an owner-only Windows directory ACL; powershell.exe with Set-Acl is required.\",\n { cause: error },\n );\n }\n}\n\ntype FileIdentity = {\n device: bigint;\n inode: bigint;\n};\n\nasync function readHandleIdentity(handle: FileHandle): Promise<FileIdentity> {\n const stats = await handle.stat({ bigint: true });\n if (stats.ino <= 0n) {\n throw new Error(\"The filesystem did not provide a stable private file identity.\");\n }\n return {\n device: stats.dev,\n inode: stats.ino,\n };\n}\n\nasync function assertPathIdentity(filePath: string, expected: FileIdentity): Promise<void> {\n try {\n const stats = await fs.lstat(filePath, { bigint: true });\n if (\n stats.isFile() &&\n stats.nlink === 1n &&\n stats.dev === expected.device &&\n stats.ino === expected.inode\n ) {\n return;\n }\n } catch (error) {\n throw new Error(\"Private file path identity changed during publication.\", { cause: error });\n }\n throw new Error(\"Private file path identity changed during publication.\");\n}\n\ntype DirectoryIdentity = {\n device: bigint;\n inode: bigint;\n userId: bigint;\n};\n\nasync function readDirectoryHandleIdentity(handle: FileHandle): Promise<DirectoryIdentity> {\n const stats = await handle.stat({ bigint: true });\n if (!stats.isDirectory() || stats.ino <= 0n) {\n throw new Error(\"The filesystem did not provide a stable private directory identity.\");\n }\n return {\n device: stats.dev,\n inode: stats.ino,\n userId: stats.uid,\n };\n}\n\nasync function assertDirectoryPathIdentity(\n directoryPath: string,\n expected: DirectoryIdentity,\n): Promise<void> {\n try {\n const stats = await fs.lstat(directoryPath, { bigint: true });\n if (stats.isDirectory() && stats.dev === expected.device && stats.ino === expected.inode) {\n return;\n }\n } catch (error) {\n throw new Error(\"Private directory path identity changed during publication.\", {\n cause: error,\n });\n }\n throw new Error(\"Private directory path identity changed during publication.\");\n}\n\nexport type SecuredPrivateDirectory = {\n assertIdentityAt(directoryPath?: string): Promise<void>;\n directoryPath: string;\n};\n\nexport async function captureDirectoryIdentity(\n directoryPath: string,\n): Promise<SecuredPrivateDirectory> {\n const handle = await fs.open(directoryPath, \"r\");\n try {\n const identity = await readDirectoryHandleIdentity(handle);\n const secured: SecuredPrivateDirectory = {\n async assertIdentityAt(currentPath = directoryPath) {\n await assertDirectoryPathIdentity(currentPath, identity);\n },\n directoryPath,\n };\n await secured.assertIdentityAt();\n return secured;\n } finally {\n await handle.close();\n }\n}\n\nexport async function syncParentDirectory(\n filePath: string,\n platform: NodeJS.Platform = process.platform,\n): Promise<void> {\n if (platform === \"win32\") {\n return;\n }\n const handle = await fs.open(path.dirname(filePath), \"r\");\n try {\n await handle.sync();\n } finally {\n await handle.close();\n }\n}\n\nasync function syncParentAtAccessibleBoundary(\n filePath: string,\n syncParent: (filePath: string, platform?: NodeJS.Platform) => Promise<void>,\n platform?: NodeJS.Platform,\n): Promise<boolean> {\n try {\n await syncParent(filePath, platform);\n return true;\n } catch (error) {\n const code = (error as NodeJS.ErrnoException).code;\n if (code === \"EACCES\" || code === \"EPERM\") {\n return false;\n }\n throw error;\n }\n}\n\nasync function syncPathAncestry(\n filePath: string,\n syncParent: (filePath: string, platform?: NodeJS.Platform) => Promise<void>,\n platform?: NodeJS.Platform,\n firstCreatedDirectory?: string,\n): Promise<void> {\n const resolvedFilePath = path.resolve(filePath);\n let currentPath = resolvedFilePath;\n const syncThroughPath =\n firstCreatedDirectory === undefined ? undefined : path.resolve(firstCreatedDirectory);\n for (;;) {\n const mandatory = syncThroughPath !== undefined || currentPath === resolvedFilePath;\n if (mandatory) {\n await syncParent(currentPath, platform);\n } else if (!(await syncParentAtAccessibleBoundary(currentPath, syncParent, platform))) {\n return;\n }\n if (currentPath === syncThroughPath) {\n return;\n }\n const parentPath = path.dirname(currentPath);\n if (path.dirname(parentPath) === parentPath) {\n return;\n }\n currentPath = parentPath;\n }\n}\n\nexport async function removeSecuredPrivateDirectory(\n secured: SecuredPrivateDirectory,\n currentPath = secured.directoryPath,\n quarantineBaseName = path.basename(currentPath),\n): Promise<void> {\n if (path.basename(quarantineBaseName) !== quarantineBaseName || quarantineBaseName.length === 0) {\n throw new Error(\"Private directory quarantine basename is malformed.\");\n }\n const quarantinePath = path.join(\n path.dirname(currentPath),\n `.${quarantineBaseName}.${process.pid}.${randomUUID()}.remove`,\n );\n await secured.assertIdentityAt(currentPath);\n await fs.rename(currentPath, quarantinePath);\n await secured.assertIdentityAt(quarantinePath);\n await syncParentDirectory(quarantinePath);\n await fs.rm(quarantinePath, { force: true, recursive: true });\n await syncParentDirectory(quarantinePath);\n}\n\nexport async function securePrivateDirectory(\n directoryPath: string,\n options: {\n currentUserId?: number;\n platform?: NodeJS.Platform;\n secureWindowsDirectory?: (directoryPath: string) => Promise<void>;\n syncParent?: (filePath: string, platform?: NodeJS.Platform) => Promise<void>;\n } = {},\n): Promise<SecuredPrivateDirectory> {\n let created = false;\n try {\n await fs.mkdir(directoryPath, { mode: 0o700 });\n created = true;\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code !== \"EEXIST\") {\n throw error;\n }\n }\n\n const handle = await fs.open(directoryPath, \"r\");\n let handleOpen = true;\n const closeHandle = async () => {\n if (!handleOpen) {\n return;\n }\n handleOpen = false;\n await handle.close();\n };\n let identity: DirectoryIdentity;\n try {\n identity = await readDirectoryHandleIdentity(handle);\n } catch (error) {\n try {\n await closeHandle();\n } catch (closeError) {\n const aggregateError = new AggregateError(\n [error, closeError],\n \"Private directory identity verification failed and its handle could not be closed.\",\n );\n aggregateError.cause = error;\n throw aggregateError;\n }\n throw error;\n }\n const secured: SecuredPrivateDirectory = {\n async assertIdentityAt(currentPath = directoryPath) {\n await assertDirectoryPathIdentity(currentPath, identity);\n },\n directoryPath,\n };\n try {\n await secured.assertIdentityAt();\n if ((options.platform ?? process.platform) === \"win32\") {\n await (options.secureWindowsDirectory ?? applyOwnerOnlyWindowsDirectoryAcl)(directoryPath);\n } else {\n const currentUserId = options.currentUserId ?? process.geteuid?.();\n if (\n currentUserId === undefined ||\n !Number.isSafeInteger(currentUserId) ||\n currentUserId < 0 ||\n identity.userId !== BigInt(currentUserId)\n ) {\n throw new Error(\"Private directory must be owned by the current POSIX user.\");\n }\n await handle.chmod(0o700);\n }\n await secured.assertIdentityAt();\n const syncParent = options.syncParent ?? syncParentDirectory;\n if (created) {\n await syncParent(directoryPath, options.platform);\n } else {\n await syncParentAtAccessibleBoundary(directoryPath, syncParent, options.platform);\n }\n } catch (error) {\n let primaryError = error;\n try {\n await closeHandle();\n } catch (closeError) {\n const aggregateError = new AggregateError(\n [error, closeError],\n \"Private directory securing failed and its verification handle could not be closed.\",\n );\n aggregateError.cause = error;\n primaryError = aggregateError;\n }\n if (created) {\n try {\n await removeSecuredPrivateDirectory(secured);\n } catch (cleanupError) {\n const aggregateError = new AggregateError(\n [primaryError, cleanupError],\n `Private directory securing failed and rollback cleanup also failed: ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n aggregateError.cause = primaryError;\n throw aggregateError;\n }\n }\n throw primaryError;\n } finally {\n await closeHandle();\n }\n\n return secured;\n}\n\nexport async function publishPrivateFileAtomically(\n filePath: string,\n contents: string,\n options: {\n afterRename?: (filePath: string) => Promise<void>;\n beforeRename?: (temporaryPath: string) => Promise<void>;\n platform?: NodeJS.Platform;\n removeTemporaryFile?: (temporaryPath: string) => Promise<void>;\n secureWindowsFile?: (temporaryPath: string) => Promise<void>;\n syncParent?: (filePath: string, platform?: NodeJS.Platform) => Promise<void>;\n } = {},\n): Promise<void> {\n const temporaryPath = path.join(\n path.dirname(filePath),\n `.${path.basename(filePath)}.${process.pid}.${randomUUID()}.tmp`,\n );\n const parentDirectory = path.resolve(path.dirname(filePath));\n const firstCreatedDirectory = await fs.mkdir(parentDirectory, { recursive: true });\n let handle: FileHandle | undefined;\n let publicationFailed = false;\n let primaryError: unknown;\n try {\n handle = await fs.open(temporaryPath, \"wx+\", 0o600);\n const identity = await readHandleIdentity(handle);\n if ((options.platform ?? process.platform) === \"win32\") {\n await (options.secureWindowsFile ?? applyOwnerOnlyWindowsAcl)(temporaryPath);\n } else {\n await handle.chmod(0o600);\n }\n\n await assertPathIdentity(temporaryPath, identity);\n await handle.writeFile(contents, \"utf8\");\n await handle.sync();\n await assertPathIdentity(temporaryPath, identity);\n await options.beforeRename?.(temporaryPath);\n await assertPathIdentity(temporaryPath, identity);\n await fs.rename(temporaryPath, filePath);\n await syncPathAncestry(\n filePath,\n options.syncParent ?? syncParentDirectory,\n options.platform,\n firstCreatedDirectory,\n );\n await options.afterRename?.(filePath);\n await assertPathIdentity(filePath, identity);\n } catch (error) {\n publicationFailed = true;\n primaryError = error;\n }\n\n const cleanupErrors: unknown[] = [];\n try {\n await handle?.close();\n } catch (error) {\n cleanupErrors.push(error);\n }\n try {\n await (\n options.removeTemporaryFile ??\n ((candidatePath: string) => fs.rm(candidatePath, { force: true }))\n )(temporaryPath);\n } catch (error) {\n cleanupErrors.push(error);\n }\n\n if (publicationFailed) {\n if (cleanupErrors.length > 0) {\n const primaryMessage =\n primaryError instanceof Error ? primaryError.message : String(primaryError);\n const aggregateError = new AggregateError(\n [primaryError, ...cleanupErrors],\n `${primaryMessage} Private temporary file cleanup also failed.`,\n );\n aggregateError.cause = primaryError;\n const primaryCode =\n typeof primaryError === \"object\" && primaryError !== null\n ? (primaryError as NodeJS.ErrnoException).code\n : undefined;\n if (primaryCode) {\n Object.assign(aggregateError, { code: primaryCode });\n }\n throw aggregateError;\n }\n throw primaryError;\n }\n if (cleanupErrors.length === 1) {\n throw cleanupErrors[0];\n }\n if (cleanupErrors.length > 1) {\n throw new AggregateError(cleanupErrors, \"Private temporary file publication cleanup failed.\");\n }\n}\n"]}
|
|
@@ -2,19 +2,34 @@ import type { CrablineServerChannel, CrablineServerManifest } from "../servers/i
|
|
|
2
2
|
import type { ServerEventObserver } from "../servers/recorder.js";
|
|
3
3
|
export declare const DEFAULT_ACCOUNT_ID = "default";
|
|
4
4
|
export declare const OPENCLAW_CRABLINE_CHANNEL_CAPABILITY_MATRIX_PATH = "crabline-fake-provider-capabilities.json";
|
|
5
|
+
export declare const OPENCLAW_CRABLINE_PROVIDER_READINESS_PATH = "crabline-fake-provider-smoke.json";
|
|
6
|
+
/** @deprecated Use OPENCLAW_CRABLINE_PROVIDER_READINESS_PATH. */
|
|
5
7
|
export declare const OPENCLAW_CRABLINE_CHANNEL_SMOKE_PATH = "crabline-fake-provider-smoke.json";
|
|
6
8
|
export declare const OPENCLAW_CRABLINE_MANIFEST_PATH = "crabline-fake-provider-server.json";
|
|
9
|
+
export declare const OPENCLAW_CRABLINE_ARTIFACT_STORE_DIRECTORY = ".crabline-smoke-artifacts";
|
|
10
|
+
export declare const OPENCLAW_CRABLINE_ARTIFACT_POINTER_PATH = ".crabline-smoke-artifacts/current.json";
|
|
7
11
|
export declare const OPENCLAW_CRABLINE_DEFAULT_CHANNEL = "telegram";
|
|
8
12
|
export type OpenClawCrablineChannelDriverSelection = {
|
|
9
13
|
channel: CrablineServerChannel;
|
|
10
14
|
channelDriver: "crabline";
|
|
11
15
|
capabilityMatrixPath: typeof OPENCLAW_CRABLINE_CHANNEL_CAPABILITY_MATRIX_PATH;
|
|
16
|
+
providerReadinessArtifactPath?: typeof OPENCLAW_CRABLINE_PROVIDER_READINESS_PATH;
|
|
12
17
|
smokeArtifactPath: typeof OPENCLAW_CRABLINE_CHANNEL_SMOKE_PATH;
|
|
13
18
|
};
|
|
19
|
+
/** @deprecated Use OpenClawCrablineProviderReadinessResult. */
|
|
14
20
|
export type OpenClawCrablineChannelDriverSmokeResult = {
|
|
21
|
+
artifactPointerPath: string;
|
|
15
22
|
capabilityReport: unknown;
|
|
23
|
+
capabilityMatrixPath: string;
|
|
24
|
+
generation: string;
|
|
16
25
|
manifestPath: string;
|
|
17
26
|
smoke: unknown;
|
|
27
|
+
smokeArtifactPath: string;
|
|
28
|
+
warnings?: string[];
|
|
29
|
+
};
|
|
30
|
+
export type OpenClawCrablineProviderReadinessResult = OpenClawCrablineChannelDriverSmokeResult & {
|
|
31
|
+
providerReadiness: unknown;
|
|
32
|
+
providerReadinessArtifactPath: string;
|
|
18
33
|
};
|
|
19
34
|
export type OpenClawCrablineConversation = {
|
|
20
35
|
id: string;
|
|
@@ -36,7 +51,7 @@ export type OpenClawCrablineAgentDelivery = {
|
|
|
36
51
|
export type OpenClawCrablineInboundInput = {
|
|
37
52
|
conversation: {
|
|
38
53
|
id: string;
|
|
39
|
-
kind:
|
|
54
|
+
kind: "direct" | "group";
|
|
40
55
|
};
|
|
41
56
|
senderId: string;
|
|
42
57
|
senderName?: string | undefined;
|
|
@@ -86,6 +101,7 @@ export type StartedOpenClawCrablineAdapter = OpenClawCrablineGatewayBinding & {
|
|
|
86
101
|
export type ParsedQaTarget = {
|
|
87
102
|
kind: "direct" | "group";
|
|
88
103
|
id: string;
|
|
104
|
+
native: boolean;
|
|
89
105
|
threadId?: string;
|
|
90
106
|
};
|
|
91
107
|
export type OpenClawCrablineProviderAdapter = {
|
|
@@ -96,7 +112,7 @@ export type OpenClawCrablineProviderAdapter = {
|
|
|
96
112
|
event: unknown;
|
|
97
113
|
targetByProviderTarget: ReadonlyMap<string, string>;
|
|
98
114
|
}): OpenClawCrablineOutboundMessage | null;
|
|
99
|
-
probe(): Promise<unknown>;
|
|
115
|
+
probe(signal?: AbortSignal): Promise<unknown>;
|
|
100
116
|
};
|
|
101
117
|
export type OpenClawCrablineProviderBridge<TManifest extends CrablineServerManifest = CrablineServerManifest> = {
|
|
102
118
|
createAdapter(manifest: TManifest): OpenClawCrablineProviderAdapter;
|
|
@@ -116,10 +132,13 @@ export declare function createOpenClawCrablineProviderBridge<TProvider extends C
|
|
|
116
132
|
}): OpenClawCrablineProviderBridge<Extract<CrablineServerManifest, {
|
|
117
133
|
provider: TProvider;
|
|
118
134
|
}>>;
|
|
135
|
+
export declare function runOpenClawCrablineProviderProbe<T>(provider: CrablineServerManifest["provider"], probe: (signal: AbortSignal) => Promise<T>): Promise<T>;
|
|
119
136
|
export declare function readString(value: unknown): string | undefined;
|
|
137
|
+
export declare function readNonBlankString(value: unknown): string | undefined;
|
|
120
138
|
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
121
139
|
export declare function readInteger(value: unknown): number | undefined;
|
|
122
140
|
export declare function parseQaTarget(target: string): ParsedQaTarget;
|
|
141
|
+
export declare function canonicalConversationIdForInbound(input: OpenClawCrablineInboundInput): string;
|
|
123
142
|
export declare function qaTargetForInbound(input: OpenClawCrablineInboundInput): string;
|
|
124
143
|
export declare function createAdminInboundRequest(manifest: CrablineServerManifest): {
|
|
125
144
|
providerHeaders: {
|