openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
70 lines (69 loc) • 3.06 kB
JavaScript
import { l as isRecord } from "../record-coerce-CVPuDqcc.mjs";
import { v as readSecureFile } from "../fs-safe-BbWvmdVn.mjs";
import { a as resolveConfiguredGitHubHost } from "../github-host-CjR25cOO.mjs";
import path from "node:path";
import fs from "node:fs/promises";
import { inspectPathPermissions } from "@openclaw/fs-safe/permissions";
import { parseDocument } from "yaml";
//#region src/agents/github-exec-credential.ts
const GITHUB_EXEC_CREDENTIAL_UNAVAILABLE = "GitHub Identity credential is unavailable or insecure. Reconnect or change GitHub Identity, then retry.";
async function privateProfileStat(e) {
const t = await fs.lstat(e);
if (!t.isDirectory() || t.isSymbolicLink()) throw new Error(GITHUB_EXEC_CREDENTIAL_UNAVAILABLE);
if (process.platform === "win32") {
const t = await inspectPathPermissions(e);
if (!t.ok || t.source !== "windows-acl" || t.ownerTrusted !== true || t.groupReadable || t.worldReadable || t.groupWritable || t.worldWritable) throw new Error(GITHUB_EXEC_CREDENTIAL_UNAVAILABLE);
} else if ((t.mode & 63) !== 0 || t.uid !== process.getuid?.()) throw new Error(GITHUB_EXEC_CREDENTIAL_UNAVAILABLE);
return t;
}
/** Called only inside the local launcher, never by the Gateway or its supervision pipeline. */
async function readGitHubExecToken(e, t) {
try {
const n = resolveConfiguredGitHubHost({ gateway: { github: { host: t } } });
const r = await privateProfileStat(e);
const i = await fs.realpath(e);
const a = path.join(e, "hosts.yml");
const o = await fs.lstat(a);
if (!o.isFile() || o.isSymbolicLink() || o.nlink !== 1) throw new Error(GITHUB_EXEC_CREDENTIAL_UNAVAILABLE);
const s = await readSecureFile({
filePath: a,
trust: { trustedDirs: [i] },
io: {
maxBytes: 65536,
timeoutMs: 5e3
}
});
try {
const t = await privateProfileStat(e);
if (t.dev !== r.dev || t.ino !== r.ino || path.dirname(s.realPath) !== i || s.stat.nlink !== 1) throw new Error(GITHUB_EXEC_CREDENTIAL_UNAVAILABLE);
const a = parseDocument(s.buffer.toString("utf8"), { prettyErrors: false });
if (a.errors.length || a.warnings.length) throw new Error(GITHUB_EXEC_CREDENTIAL_UNAVAILABLE);
const o = a.toJS({ maxAliasCount: 0 });
const c = isRecord(o) ? o[n] : void 0;
const l = isRecord(c) && typeof c.oauth_token === "string" ? c.oauth_token.trim() : "";
if (!l || /[\r\n\0]/u.test(l)) throw new Error(GITHUB_EXEC_CREDENTIAL_UNAVAILABLE);
return l;
} finally {
s.buffer.fill(0);
}
} catch {
throw new Error(GITHUB_EXEC_CREDENTIAL_UNAVAILABLE);
}
}
//#endregion
//#region src/agents/github-exec-launcher.ts
async function resolveCredential() {
const e = await readGitHubExecToken(process.argv[2] ?? "", process.argv[3]);
process.stdout.write(e);
}
function credentialUnavailable() {
process.exitCode = 1;
process.stderr.write(`${GITHUB_EXEC_CREDENTIAL_UNAVAILABLE}\n`);
}
process.stdout.on("error", credentialUnavailable);
process.stderr.on("error", () => {
process.exitCode = 1;
});
resolveCredential().catch(credentialUnavailable);
//#endregion
export {};