mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-22 22:52:03 +00:00
* feat(secrets): expand secret target coverage and gateway tooling * docs(secrets): align gateway and CLI secret docs * chore(protocol): regenerate swift gateway models for secrets methods * fix(config): restore talk apiKey fallback and stabilize runner test * ci(windows): reduce test worker count for shard stability * ci(windows): raise node heap for test shard stability * test(feishu): make proxy env precedence assertion windows-safe * fix(gateway): resolve auth password SecretInput refs for clients * fix(gateway): resolve remote SecretInput credentials for clients * fix(secrets): skip inactive refs in command snapshot assignments * fix(secrets): scope gateway.remote refs to effective auth surfaces * fix(secrets): ignore memory defaults when enabled agents disable search * fix(secrets): honor Google Chat serviceAccountRef inheritance * fix(secrets): address tsgo errors in command and gateway collectors * fix(secrets): avoid auth-store load in providers-only configure * fix(gateway): defer local password ref resolution by precedence * fix(secrets): gate telegram webhook secret refs by webhook mode * fix(secrets): gate slack signing secret refs to http mode * fix(secrets): skip telegram botToken refs when tokenFile is set * fix(secrets): gate discord pluralkit refs by enabled flag * fix(secrets): gate discord voice tts refs by voice enabled * test(secrets): make runtime fixture modes explicit * fix(cli): resolve local qr password secret refs * fix(cli): fail when gateway leaves command refs unresolved * fix(gateway): fail when local password SecretRef is unresolved * fix(gateway): fail when required remote SecretRefs are unresolved * fix(gateway): resolve local password refs only when password can win * fix(cli): skip local password SecretRef resolution on qr token override * test(gateway): cast SecretRef fixtures to OpenClawConfig * test(secrets): activate mode-gated targets in runtime coverage fixture * fix(cron): support SecretInput webhook tokens safely * fix(bluebubbles): support SecretInput passwords across config paths * fix(msteams): make appPassword SecretInput-safe in onboarding/token paths * fix(bluebubbles): align SecretInput schema helper typing * fix(cli): clarify secrets.resolve version-skew errors * refactor(secrets): return structured inactive paths from secrets.resolve * refactor(gateway): type onboarding secret writes as SecretInput * chore(protocol): regenerate swift models for secrets.resolve * feat(secrets): expand extension credential secretref support * fix(secrets): gate web-search refs by active provider * fix(onboarding): detect SecretRef credentials in extension status * fix(onboarding): allow keeping existing ref in secret prompt * fix(onboarding): resolve gateway password SecretRefs for probe and tui * fix(onboarding): honor secret-input-mode for local gateway auth * fix(acp): resolve gateway SecretInput credentials * fix(secrets): gate gateway.remote refs to remote surfaces * test(secrets): cover pattern matching and inactive array refs * docs(secrets): clarify secrets.resolve and remote active surfaces * fix(bluebubbles): keep existing SecretRef during onboarding * fix(tests): resolve CI type errors in new SecretRef coverage * fix(extensions): replace raw fetch with SSRF-guarded fetch * test(secrets): mark gateway remote targets active in runtime coverage * test(infra): normalize home-prefix expectation across platforms * fix(cli): only resolve local qr password refs in password mode * test(cli): cover local qr token mode with unresolved password ref * docs(cli): clarify local qr password ref resolution behavior * refactor(extensions): reuse sdk SecretInput helpers * fix(wizard): resolve onboarding env-template secrets before plaintext * fix(cli): surface secrets.resolve diagnostics in memory and qr * test(secrets): repair post-rebase runtime and fixtures * fix(gateway): skip remote password ref resolution when token wins * fix(secrets): treat tailscale remote gateway refs as active * fix(gateway): allow remote password fallback when token ref is unresolved * fix(gateway): ignore stale local password refs for none and trusted-proxy * fix(gateway): skip remote secret ref resolution on local call paths * test(cli): cover qr remote tailscale secret ref resolution * fix(secrets): align gateway password active-surface with auth inference * fix(cli): resolve inferred local gateway password refs in qr * fix(gateway): prefer resolvable remote password over token ref pre-resolution * test(gateway): cover none and trusted-proxy stale password refs * docs(secrets): sync qr and gateway active-surface behavior * fix: restore stability blockers from pre-release audit * Secrets: fix collector/runtime precedence contradictions * docs: align secrets and web credential docs * fix(rebase): resolve integration regressions after main rebase * fix(node-host): resolve gateway secret refs for auth * fix(secrets): harden secretinput runtime readers * gateway: skip inactive auth secretref resolution * cli: avoid gateway preflight for inactive secret refs * extensions: allow unresolved refs in onboarding status * tests: fix qr-cli module mock hoist ordering * Security: align audit checks with SecretInput resolution * Gateway: resolve local-mode remote fallback secret refs * Node host: avoid resolving inactive password secret refs * Secrets runtime: mark Slack appToken inactive for HTTP mode * secrets: keep inactive gateway remote refs non-blocking * cli: include agent memory secret targets in runtime resolution * docs(secrets): sync docs with active-surface and web search behavior * fix(secrets): keep telegram top-level token refs active for blank account tokens * fix(daemon): resolve gateway password secret refs for probe auth * fix(secrets): skip IRC NickServ ref resolution when NickServ is disabled * fix(secrets): align token inheritance and exec timeout defaults * docs(secrets): clarify active-surface notes in cli docs * cli: require secrets.resolve gateway capability * gateway: log auth secret surface diagnostics * secrets: remove dead provider resolver module * fix(secrets): restore gateway auth precedence and fallback resolution * fix(tests): align plugin runtime mock typings --------- Co-authored-by: Peter Steinberger <steipete@gmail.com>
460 lines
14 KiB
TypeScript
460 lines
14 KiB
TypeScript
import type { DmPolicy } from "openclaw/plugin-sdk";
|
|
import {
|
|
addWildcardAllowFrom,
|
|
formatResolvedUnresolvedNote,
|
|
formatDocsLink,
|
|
hasConfiguredSecretInput,
|
|
mergeAllowFromEntries,
|
|
promptSingleChannelSecretInput,
|
|
promptChannelAccessConfig,
|
|
type SecretInput,
|
|
type ChannelOnboardingAdapter,
|
|
type ChannelOnboardingDmPolicy,
|
|
type WizardPrompter,
|
|
} from "openclaw/plugin-sdk";
|
|
import { listMatrixDirectoryGroupsLive } from "./directory-live.js";
|
|
import { resolveMatrixAccount } from "./matrix/accounts.js";
|
|
import { ensureMatrixSdkInstalled, isMatrixSdkAvailable } from "./matrix/deps.js";
|
|
import { resolveMatrixTargets } from "./resolve-targets.js";
|
|
import type { CoreConfig } from "./types.js";
|
|
|
|
const channel = "matrix" as const;
|
|
|
|
function setMatrixDmPolicy(cfg: CoreConfig, policy: DmPolicy) {
|
|
const allowFrom =
|
|
policy === "open" ? addWildcardAllowFrom(cfg.channels?.matrix?.dm?.allowFrom) : undefined;
|
|
return {
|
|
...cfg,
|
|
channels: {
|
|
...cfg.channels,
|
|
matrix: {
|
|
...cfg.channels?.matrix,
|
|
dm: {
|
|
...cfg.channels?.matrix?.dm,
|
|
policy,
|
|
...(allowFrom ? { allowFrom } : {}),
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
async function noteMatrixAuthHelp(prompter: WizardPrompter): Promise<void> {
|
|
await prompter.note(
|
|
[
|
|
"Matrix requires a homeserver URL.",
|
|
"Use an access token (recommended) or a password (logs in and stores a token).",
|
|
"With access token: user ID is fetched automatically.",
|
|
"Env vars supported: MATRIX_HOMESERVER, MATRIX_USER_ID, MATRIX_ACCESS_TOKEN, MATRIX_PASSWORD.",
|
|
`Docs: ${formatDocsLink("/channels/matrix", "channels/matrix")}`,
|
|
].join("\n"),
|
|
"Matrix setup",
|
|
);
|
|
}
|
|
|
|
async function promptMatrixAllowFrom(params: {
|
|
cfg: CoreConfig;
|
|
prompter: WizardPrompter;
|
|
}): Promise<CoreConfig> {
|
|
const { cfg, prompter } = params;
|
|
const existingAllowFrom = cfg.channels?.matrix?.dm?.allowFrom ?? [];
|
|
const account = resolveMatrixAccount({ cfg });
|
|
const canResolve = Boolean(account.configured);
|
|
|
|
const parseInput = (raw: string) =>
|
|
raw
|
|
.split(/[\n,;]+/g)
|
|
.map((entry) => entry.trim())
|
|
.filter(Boolean);
|
|
|
|
const isFullUserId = (value: string) => value.startsWith("@") && value.includes(":");
|
|
|
|
while (true) {
|
|
const entry = await prompter.text({
|
|
message: "Matrix allowFrom (full @user:server; display name only if unique)",
|
|
placeholder: "@user:server",
|
|
initialValue: existingAllowFrom[0] ? String(existingAllowFrom[0]) : undefined,
|
|
validate: (value) => (String(value ?? "").trim() ? undefined : "Required"),
|
|
});
|
|
const parts = parseInput(String(entry));
|
|
const resolvedIds: string[] = [];
|
|
const pending: string[] = [];
|
|
const unresolved: string[] = [];
|
|
const unresolvedNotes: string[] = [];
|
|
|
|
for (const part of parts) {
|
|
if (isFullUserId(part)) {
|
|
resolvedIds.push(part);
|
|
continue;
|
|
}
|
|
if (!canResolve) {
|
|
unresolved.push(part);
|
|
continue;
|
|
}
|
|
pending.push(part);
|
|
}
|
|
|
|
if (pending.length > 0) {
|
|
const results = await resolveMatrixTargets({
|
|
cfg,
|
|
inputs: pending,
|
|
kind: "user",
|
|
}).catch(() => []);
|
|
for (const result of results) {
|
|
if (result?.resolved && result.id) {
|
|
resolvedIds.push(result.id);
|
|
continue;
|
|
}
|
|
if (result?.input) {
|
|
unresolved.push(result.input);
|
|
if (result.note) {
|
|
unresolvedNotes.push(`${result.input}: ${result.note}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (unresolved.length > 0) {
|
|
const details = unresolvedNotes.length > 0 ? unresolvedNotes : unresolved;
|
|
await prompter.note(
|
|
`Could not resolve:\n${details.join("\n")}\nUse full @user:server IDs.`,
|
|
"Matrix allowlist",
|
|
);
|
|
continue;
|
|
}
|
|
|
|
const unique = mergeAllowFromEntries(existingAllowFrom, resolvedIds);
|
|
return {
|
|
...cfg,
|
|
channels: {
|
|
...cfg.channels,
|
|
matrix: {
|
|
...cfg.channels?.matrix,
|
|
enabled: true,
|
|
dm: {
|
|
...cfg.channels?.matrix?.dm,
|
|
policy: "allowlist",
|
|
allowFrom: unique,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
function setMatrixGroupPolicy(cfg: CoreConfig, groupPolicy: "open" | "allowlist" | "disabled") {
|
|
return {
|
|
...cfg,
|
|
channels: {
|
|
...cfg.channels,
|
|
matrix: {
|
|
...cfg.channels?.matrix,
|
|
enabled: true,
|
|
groupPolicy,
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
function setMatrixGroupRooms(cfg: CoreConfig, roomKeys: string[]) {
|
|
const groups = Object.fromEntries(roomKeys.map((key) => [key, { allow: true }]));
|
|
return {
|
|
...cfg,
|
|
channels: {
|
|
...cfg.channels,
|
|
matrix: {
|
|
...cfg.channels?.matrix,
|
|
enabled: true,
|
|
groups,
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
const dmPolicy: ChannelOnboardingDmPolicy = {
|
|
label: "Matrix",
|
|
channel,
|
|
policyKey: "channels.matrix.dm.policy",
|
|
allowFromKey: "channels.matrix.dm.allowFrom",
|
|
getCurrent: (cfg) => (cfg as CoreConfig).channels?.matrix?.dm?.policy ?? "pairing",
|
|
setPolicy: (cfg, policy) => setMatrixDmPolicy(cfg as CoreConfig, policy),
|
|
promptAllowFrom: promptMatrixAllowFrom,
|
|
};
|
|
|
|
export const matrixOnboardingAdapter: ChannelOnboardingAdapter = {
|
|
channel,
|
|
getStatus: async ({ cfg }) => {
|
|
const account = resolveMatrixAccount({ cfg: cfg as CoreConfig });
|
|
const configured = account.configured;
|
|
const sdkReady = isMatrixSdkAvailable();
|
|
return {
|
|
channel,
|
|
configured,
|
|
statusLines: [
|
|
`Matrix: ${configured ? "configured" : "needs homeserver + access token or password"}`,
|
|
],
|
|
selectionHint: !sdkReady
|
|
? "install @vector-im/matrix-bot-sdk"
|
|
: configured
|
|
? "configured"
|
|
: "needs auth",
|
|
};
|
|
},
|
|
configure: async ({ cfg, runtime, prompter, forceAllowFrom }) => {
|
|
let next = cfg as CoreConfig;
|
|
await ensureMatrixSdkInstalled({
|
|
runtime,
|
|
confirm: async (message) =>
|
|
await prompter.confirm({
|
|
message,
|
|
initialValue: true,
|
|
}),
|
|
});
|
|
const existing = next.channels?.matrix ?? {};
|
|
const account = resolveMatrixAccount({ cfg: next });
|
|
if (!account.configured) {
|
|
await noteMatrixAuthHelp(prompter);
|
|
}
|
|
|
|
const envHomeserver = process.env.MATRIX_HOMESERVER?.trim();
|
|
const envUserId = process.env.MATRIX_USER_ID?.trim();
|
|
const envAccessToken = process.env.MATRIX_ACCESS_TOKEN?.trim();
|
|
const envPassword = process.env.MATRIX_PASSWORD?.trim();
|
|
const envReady = Boolean(envHomeserver && (envAccessToken || (envUserId && envPassword)));
|
|
|
|
if (
|
|
envReady &&
|
|
!existing.homeserver &&
|
|
!existing.userId &&
|
|
!existing.accessToken &&
|
|
!existing.password
|
|
) {
|
|
const useEnv = await prompter.confirm({
|
|
message: "Matrix env vars detected. Use env values?",
|
|
initialValue: true,
|
|
});
|
|
if (useEnv) {
|
|
next = {
|
|
...next,
|
|
channels: {
|
|
...next.channels,
|
|
matrix: {
|
|
...next.channels?.matrix,
|
|
enabled: true,
|
|
},
|
|
},
|
|
};
|
|
if (forceAllowFrom) {
|
|
next = await promptMatrixAllowFrom({ cfg: next, prompter });
|
|
}
|
|
return { cfg: next };
|
|
}
|
|
}
|
|
|
|
const homeserver = String(
|
|
await prompter.text({
|
|
message: "Matrix homeserver URL",
|
|
initialValue: existing.homeserver ?? envHomeserver,
|
|
validate: (value) => {
|
|
const raw = String(value ?? "").trim();
|
|
if (!raw) {
|
|
return "Required";
|
|
}
|
|
if (!/^https?:\/\//i.test(raw)) {
|
|
return "Use a full URL (https://...)";
|
|
}
|
|
return undefined;
|
|
},
|
|
}),
|
|
).trim();
|
|
|
|
let accessToken = existing.accessToken ?? "";
|
|
let password: SecretInput | undefined = existing.password;
|
|
let userId = existing.userId ?? "";
|
|
const existingPasswordConfigured = hasConfiguredSecretInput(existing.password);
|
|
const passwordConfigured = () => hasConfiguredSecretInput(password);
|
|
|
|
if (accessToken || passwordConfigured()) {
|
|
const keep = await prompter.confirm({
|
|
message: "Matrix credentials already configured. Keep them?",
|
|
initialValue: true,
|
|
});
|
|
if (!keep) {
|
|
accessToken = "";
|
|
password = undefined;
|
|
userId = "";
|
|
}
|
|
}
|
|
|
|
if (!accessToken && !passwordConfigured()) {
|
|
// Ask auth method FIRST before asking for user ID
|
|
const authMode = await prompter.select({
|
|
message: "Matrix auth method",
|
|
options: [
|
|
{ value: "token", label: "Access token (user ID fetched automatically)" },
|
|
{ value: "password", label: "Password (requires user ID)" },
|
|
],
|
|
});
|
|
|
|
if (authMode === "token") {
|
|
accessToken = String(
|
|
await prompter.text({
|
|
message: "Matrix access token",
|
|
validate: (value) => (value?.trim() ? undefined : "Required"),
|
|
}),
|
|
).trim();
|
|
// With access token, we can fetch the userId automatically - don't prompt for it
|
|
// The client.ts will use whoami() to get it
|
|
userId = "";
|
|
} else {
|
|
// Password auth requires user ID upfront
|
|
userId = String(
|
|
await prompter.text({
|
|
message: "Matrix user ID",
|
|
initialValue: existing.userId ?? envUserId,
|
|
validate: (value) => {
|
|
const raw = String(value ?? "").trim();
|
|
if (!raw) {
|
|
return "Required";
|
|
}
|
|
if (!raw.startsWith("@")) {
|
|
return "Matrix user IDs should start with @";
|
|
}
|
|
if (!raw.includes(":")) {
|
|
return "Matrix user IDs should include a server (:server)";
|
|
}
|
|
return undefined;
|
|
},
|
|
}),
|
|
).trim();
|
|
const passwordResult = await promptSingleChannelSecretInput({
|
|
cfg: next,
|
|
prompter,
|
|
providerHint: "matrix",
|
|
credentialLabel: "password",
|
|
accountConfigured: Boolean(existingPasswordConfigured),
|
|
canUseEnv: Boolean(envPassword?.trim()) && !existingPasswordConfigured,
|
|
hasConfigToken: existingPasswordConfigured,
|
|
envPrompt: "MATRIX_PASSWORD detected. Use env var?",
|
|
keepPrompt: "Matrix password already configured. Keep it?",
|
|
inputPrompt: "Matrix password",
|
|
preferredEnvVar: "MATRIX_PASSWORD",
|
|
});
|
|
if (passwordResult.action === "set") {
|
|
password = passwordResult.value;
|
|
}
|
|
if (passwordResult.action === "use-env") {
|
|
password = undefined;
|
|
}
|
|
}
|
|
}
|
|
|
|
const deviceName = String(
|
|
await prompter.text({
|
|
message: "Matrix device name (optional)",
|
|
initialValue: existing.deviceName ?? "OpenClaw Gateway",
|
|
}),
|
|
).trim();
|
|
|
|
// Ask about E2EE encryption
|
|
const enableEncryption = await prompter.confirm({
|
|
message: "Enable end-to-end encryption (E2EE)?",
|
|
initialValue: existing.encryption ?? false,
|
|
});
|
|
|
|
next = {
|
|
...next,
|
|
channels: {
|
|
...next.channels,
|
|
matrix: {
|
|
...next.channels?.matrix,
|
|
enabled: true,
|
|
homeserver,
|
|
userId: userId || undefined,
|
|
accessToken: accessToken || undefined,
|
|
password: password,
|
|
deviceName: deviceName || undefined,
|
|
encryption: enableEncryption || undefined,
|
|
},
|
|
},
|
|
};
|
|
|
|
if (forceAllowFrom) {
|
|
next = await promptMatrixAllowFrom({ cfg: next, prompter });
|
|
}
|
|
|
|
const existingGroups = next.channels?.matrix?.groups ?? next.channels?.matrix?.rooms;
|
|
const accessConfig = await promptChannelAccessConfig({
|
|
prompter,
|
|
label: "Matrix rooms",
|
|
currentPolicy: next.channels?.matrix?.groupPolicy ?? "allowlist",
|
|
currentEntries: Object.keys(existingGroups ?? {}),
|
|
placeholder: "!roomId:server, #alias:server, Project Room",
|
|
updatePrompt: Boolean(existingGroups),
|
|
});
|
|
if (accessConfig) {
|
|
if (accessConfig.policy !== "allowlist") {
|
|
next = setMatrixGroupPolicy(next, accessConfig.policy);
|
|
} else {
|
|
let roomKeys = accessConfig.entries;
|
|
if (accessConfig.entries.length > 0) {
|
|
try {
|
|
const resolvedIds: string[] = [];
|
|
const unresolved: string[] = [];
|
|
for (const entry of accessConfig.entries) {
|
|
const trimmed = entry.trim();
|
|
if (!trimmed) {
|
|
continue;
|
|
}
|
|
const cleaned = trimmed.replace(/^(room|channel):/i, "").trim();
|
|
if (cleaned.startsWith("!") && cleaned.includes(":")) {
|
|
resolvedIds.push(cleaned);
|
|
continue;
|
|
}
|
|
const matches = await listMatrixDirectoryGroupsLive({
|
|
cfg: next,
|
|
query: trimmed,
|
|
limit: 10,
|
|
});
|
|
const exact = matches.find(
|
|
(match) => (match.name ?? "").toLowerCase() === trimmed.toLowerCase(),
|
|
);
|
|
const best = exact ?? matches[0];
|
|
if (best?.id) {
|
|
resolvedIds.push(best.id);
|
|
} else {
|
|
unresolved.push(entry);
|
|
}
|
|
}
|
|
roomKeys = [...resolvedIds, ...unresolved.map((entry) => entry.trim()).filter(Boolean)];
|
|
const resolution = formatResolvedUnresolvedNote({
|
|
resolved: resolvedIds,
|
|
unresolved,
|
|
});
|
|
if (resolution) {
|
|
await prompter.note(resolution, "Matrix rooms");
|
|
}
|
|
} catch (err) {
|
|
await prompter.note(
|
|
`Room lookup failed; keeping entries as typed. ${String(err)}`,
|
|
"Matrix rooms",
|
|
);
|
|
}
|
|
}
|
|
next = setMatrixGroupPolicy(next, "allowlist");
|
|
next = setMatrixGroupRooms(next, roomKeys);
|
|
}
|
|
}
|
|
|
|
return { cfg: next };
|
|
},
|
|
dmPolicy,
|
|
disable: (cfg) => ({
|
|
...(cfg as CoreConfig),
|
|
channels: {
|
|
...(cfg as CoreConfig).channels,
|
|
matrix: { ...(cfg as CoreConfig).channels?.matrix, enabled: false },
|
|
},
|
|
}),
|
|
};
|