mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 08:07:27 +00:00
fix(security): harden plugin/hook npm installs
This commit is contained in:
@@ -31,6 +31,7 @@ Docs: https://docs.openclaw.ai
|
|||||||
- Gateway/Auth: add trusted-proxy mode hardening follow-ups by keeping `OPENCLAW_GATEWAY_*` env compatibility, auto-normalizing invalid setup combinations in interactive `gateway configure` (trusted-proxy forces `bind=lan` and disables Tailscale serve/funnel), and suppressing shared-secret/rate-limit audit findings that do not apply to trusted-proxy deployments. (#15940) Thanks @nickytonline.
|
- Gateway/Auth: add trusted-proxy mode hardening follow-ups by keeping `OPENCLAW_GATEWAY_*` env compatibility, auto-normalizing invalid setup combinations in interactive `gateway configure` (trusted-proxy forces `bind=lan` and disables Tailscale serve/funnel), and suppressing shared-secret/rate-limit audit findings that do not apply to trusted-proxy deployments. (#15940) Thanks @nickytonline.
|
||||||
- Docs/Hooks: update hooks documentation URLs to the new `/automation/hooks` location. (#16165) Thanks @nicholascyh.
|
- Docs/Hooks: update hooks documentation URLs to the new `/automation/hooks` location. (#16165) Thanks @nicholascyh.
|
||||||
- Security/Audit: warn when `gateway.tools.allow` re-enables default-denied tools over HTTP `POST /tools/invoke`, since this can increase RCE blast radius if the gateway is reachable.
|
- Security/Audit: warn when `gateway.tools.allow` re-enables default-denied tools over HTTP `POST /tools/invoke`, since this can increase RCE blast radius if the gateway is reachable.
|
||||||
|
- Security/Plugins/Hooks: harden npm-based installs by restricting specs to registry packages only, passing `--ignore-scripts` to `npm pack`, and cleaning up temp install directories.
|
||||||
- Feishu: stop persistent Typing reaction on NO_REPLY/suppressed runs by wiring reply-dispatcher cleanup to remove typing indicators. (#15464) Thanks @arosstale.
|
- Feishu: stop persistent Typing reaction on NO_REPLY/suppressed runs by wiring reply-dispatcher cleanup to remove typing indicators. (#15464) Thanks @arosstale.
|
||||||
- BlueBubbles: gracefully degrade when Private API is disabled by filtering private-only actions, skipping private-only reactions/reply effects, and avoiding private reply markers so non-private flows remain usable. (#16002) Thanks @L-U-C-K-Y.
|
- BlueBubbles: gracefully degrade when Private API is disabled by filtering private-only actions, skipping private-only reactions/reply effects, and avoiding private reply markers so non-private flows remain usable. (#16002) Thanks @L-U-C-K-Y.
|
||||||
- Outbound: add a write-ahead delivery queue with crash-recovery retries to prevent lost outbound messages after gateway restarts. (#15636) Thanks @nabbilkhan, @thewilloftheshadow.
|
- Outbound: add a write-ahead delivery queue with crash-recovery retries to prevent lost outbound messages after gateway restarts. (#15636) Thanks @nabbilkhan, @thewilloftheshadow.
|
||||||
|
|||||||
@@ -103,6 +103,8 @@ Hook packs are standard npm packages that export one or more hooks via `openclaw
|
|||||||
openclaw hooks install <path-or-spec>
|
openclaw hooks install <path-or-spec>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Npm specs are registry-only (package name + optional version/tag). Git/URL/file specs are rejected.
|
||||||
|
|
||||||
Example `package.json`:
|
Example `package.json`:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
@@ -118,6 +120,10 @@ Example `package.json`:
|
|||||||
Each entry points to a hook directory containing `HOOK.md` and `handler.ts` (or `index.ts`).
|
Each entry points to a hook directory containing `HOOK.md` and `handler.ts` (or `index.ts`).
|
||||||
Hook packs can ship dependencies; they will be installed under `~/.openclaw/hooks/<id>`.
|
Hook packs can ship dependencies; they will be installed under `~/.openclaw/hooks/<id>`.
|
||||||
|
|
||||||
|
Security note: `openclaw hooks install` installs dependencies with `npm install --ignore-scripts`
|
||||||
|
(no lifecycle scripts). Keep hook pack dependency trees "pure JS/TS" and avoid packages that rely
|
||||||
|
on `postinstall` builds.
|
||||||
|
|
||||||
## Hook Structure
|
## Hook Structure
|
||||||
|
|
||||||
### HOOK.md Format
|
### HOOK.md Format
|
||||||
|
|||||||
@@ -192,6 +192,9 @@ openclaw hooks install <path-or-spec>
|
|||||||
|
|
||||||
Install a hook pack from a local folder/archive or npm.
|
Install a hook pack from a local folder/archive or npm.
|
||||||
|
|
||||||
|
Npm specs are **registry-only** (package name + optional version/tag). Git/URL/file
|
||||||
|
specs are rejected. Dependency installs run with `--ignore-scripts` for safety.
|
||||||
|
|
||||||
**What it does:**
|
**What it does:**
|
||||||
|
|
||||||
- Copies the hook pack into `~/.openclaw/hooks/<id>`
|
- Copies the hook pack into `~/.openclaw/hooks/<id>`
|
||||||
|
|||||||
@@ -44,6 +44,9 @@ openclaw plugins install <path-or-spec>
|
|||||||
|
|
||||||
Security note: treat plugin installs like running code. Prefer pinned versions.
|
Security note: treat plugin installs like running code. Prefer pinned versions.
|
||||||
|
|
||||||
|
Npm specs are **registry-only** (package name + optional version/tag). Git/URL/file
|
||||||
|
specs are rejected. Dependency installs run with `--ignore-scripts` for safety.
|
||||||
|
|
||||||
Supported archives: `.zip`, `.tgz`, `.tar.gz`, `.tar`.
|
Supported archives: `.zip`, `.tgz`, `.tar.gz`, `.tar`.
|
||||||
|
|
||||||
Use `--link` to avoid copying a local directory (adds to `plugins.load.paths`):
|
Use `--link` to avoid copying a local directory (adds to `plugins.load.paths`):
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ openclaw plugins list
|
|||||||
openclaw plugins install @openclaw/voice-call
|
openclaw plugins install @openclaw/voice-call
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Npm specs are **registry-only** (package name + optional version/tag). Git/URL/file
|
||||||
|
specs are rejected.
|
||||||
|
|
||||||
3. Restart the Gateway, then configure under `plugins.entries.<id>.config`.
|
3. Restart the Gateway, then configure under `plugins.entries.<id>.config`.
|
||||||
|
|
||||||
See [Voice Call](/plugins/voice-call) for a concrete example plugin.
|
See [Voice Call](/plugins/voice-call) for a concrete example plugin.
|
||||||
@@ -138,6 +141,10 @@ becomes `name/<fileBase>`.
|
|||||||
If your plugin imports npm deps, install them in that directory so
|
If your plugin imports npm deps, install them in that directory so
|
||||||
`node_modules` is available (`npm install` / `pnpm install`).
|
`node_modules` is available (`npm install` / `pnpm install`).
|
||||||
|
|
||||||
|
Security note: `openclaw plugins install` installs plugin dependencies with
|
||||||
|
`npm install --ignore-scripts` (no lifecycle scripts). Keep plugin dependency
|
||||||
|
trees "pure JS/TS" and avoid packages that require `postinstall` builds.
|
||||||
|
|
||||||
### Channel catalog metadata
|
### Channel catalog metadata
|
||||||
|
|
||||||
Channel plugins can advertise onboarding metadata via `openclaw.channel` and
|
Channel plugins can advertise onboarding metadata via `openclaw.channel` and
|
||||||
@@ -424,7 +431,7 @@ Notes:
|
|||||||
|
|
||||||
### Write a new messaging channel (step‑by‑step)
|
### Write a new messaging channel (step‑by‑step)
|
||||||
|
|
||||||
Use this when you want a **new chat surface** (a “messaging channel”), not a model provider.
|
Use this when you want a **new chat surface** (a "messaging channel"), not a model provider.
|
||||||
Model provider docs live under `/providers/*`.
|
Model provider docs live under `/providers/*`.
|
||||||
|
|
||||||
1. Pick an id + config shape
|
1. Pick an id + config shape
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import fs from "node:fs";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import * as tar from "tar";
|
import * as tar from "tar";
|
||||||
import { afterAll, describe, expect, it, vi } from "vitest";
|
import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
const fixtureRoot = path.join(os.tmpdir(), `openclaw-hook-install-${randomUUID()}`);
|
const fixtureRoot = path.join(os.tmpdir(), `openclaw-hook-install-${randomUUID()}`);
|
||||||
let tempDirIndex = 0;
|
let tempDirIndex = 0;
|
||||||
@@ -13,6 +13,28 @@ vi.mock("../process/exec.js", () => ({
|
|||||||
runCommandWithTimeout: vi.fn(),
|
runCommandWithTimeout: vi.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
async function packToArchive({
|
||||||
|
pkgDir,
|
||||||
|
outDir,
|
||||||
|
outName,
|
||||||
|
}: {
|
||||||
|
pkgDir: string;
|
||||||
|
outDir: string;
|
||||||
|
outName: string;
|
||||||
|
}) {
|
||||||
|
const dest = path.join(outDir, outName);
|
||||||
|
fs.rmSync(dest, { force: true });
|
||||||
|
await tar.c(
|
||||||
|
{
|
||||||
|
gzip: true,
|
||||||
|
file: dest,
|
||||||
|
cwd: path.dirname(pkgDir),
|
||||||
|
},
|
||||||
|
[path.basename(pkgDir)],
|
||||||
|
);
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
function makeTempDir() {
|
function makeTempDir() {
|
||||||
const dir = path.join(fixtureRoot, `case-${tempDirIndex++}`);
|
const dir = path.join(fixtureRoot, `case-${tempDirIndex++}`);
|
||||||
fs.mkdirSync(dir, { recursive: true });
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
@@ -20,7 +42,8 @@ function makeTempDir() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { runCommandWithTimeout } = await import("../process/exec.js");
|
const { runCommandWithTimeout } = await import("../process/exec.js");
|
||||||
const { installHooksFromArchive, installHooksFromPath } = await import("./install.js");
|
const { installHooksFromArchive, installHooksFromNpmSpec, installHooksFromPath } =
|
||||||
|
await import("./install.js");
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
try {
|
try {
|
||||||
@@ -30,6 +53,10 @@ afterAll(() => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
describe("installHooksFromArchive", () => {
|
describe("installHooksFromArchive", () => {
|
||||||
it("installs hook packs from zip archives", async () => {
|
it("installs hook packs from zip archives", async () => {
|
||||||
const stateDir = makeTempDir();
|
const stateDir = makeTempDir();
|
||||||
@@ -308,3 +335,88 @@ describe("installHooksFromPath", () => {
|
|||||||
expect(fs.existsSync(path.join(result.targetDir, "HOOK.md"))).toBe(true);
|
expect(fs.existsSync(path.join(result.targetDir, "HOOK.md"))).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("installHooksFromNpmSpec", () => {
|
||||||
|
it("uses --ignore-scripts for npm pack and cleans up temp dir", async () => {
|
||||||
|
const workDir = makeTempDir();
|
||||||
|
const stateDir = makeTempDir();
|
||||||
|
const pkgDir = path.join(workDir, "package");
|
||||||
|
fs.mkdirSync(path.join(pkgDir, "hooks", "one-hook"), { recursive: true });
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(pkgDir, "package.json"),
|
||||||
|
JSON.stringify({
|
||||||
|
name: "@openclaw/test-hooks",
|
||||||
|
version: "0.0.1",
|
||||||
|
openclaw: { hooks: ["./hooks/one-hook"] },
|
||||||
|
}),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(pkgDir, "hooks", "one-hook", "HOOK.md"),
|
||||||
|
[
|
||||||
|
"---",
|
||||||
|
"name: one-hook",
|
||||||
|
"description: One hook",
|
||||||
|
'metadata: {"openclaw":{"events":["command:new"]}}',
|
||||||
|
"---",
|
||||||
|
"",
|
||||||
|
"# One Hook",
|
||||||
|
].join("\n"),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(pkgDir, "hooks", "one-hook", "handler.ts"),
|
||||||
|
"export default async () => {};\n",
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const run = vi.mocked(runCommandWithTimeout);
|
||||||
|
let packTmpDir = "";
|
||||||
|
const packedName = "test-hooks-0.0.1.tgz";
|
||||||
|
run.mockImplementation(async (argv, opts) => {
|
||||||
|
if (argv[0] === "npm" && argv[1] === "pack") {
|
||||||
|
packTmpDir = String(opts?.cwd ?? "");
|
||||||
|
await packToArchive({ pkgDir, outDir: packTmpDir, outName: packedName });
|
||||||
|
return { code: 0, stdout: `${packedName}\n`, stderr: "", signal: null, killed: false };
|
||||||
|
}
|
||||||
|
throw new Error(`unexpected command: ${argv.join(" ")}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
const hooksDir = path.join(stateDir, "hooks");
|
||||||
|
const result = await installHooksFromNpmSpec({
|
||||||
|
spec: "@openclaw/test-hooks@0.0.1",
|
||||||
|
hooksDir,
|
||||||
|
logger: { info: () => {}, warn: () => {} },
|
||||||
|
});
|
||||||
|
expect(result.ok).toBe(true);
|
||||||
|
if (!result.ok) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect(result.hookPackId).toBe("test-hooks");
|
||||||
|
expect(fs.existsSync(path.join(result.targetDir, "hooks", "one-hook", "HOOK.md"))).toBe(true);
|
||||||
|
|
||||||
|
const packCalls = run.mock.calls.filter(
|
||||||
|
(c) => Array.isArray(c[0]) && c[0][0] === "npm" && c[0][1] === "pack",
|
||||||
|
);
|
||||||
|
expect(packCalls.length).toBe(1);
|
||||||
|
const packCall = packCalls[0];
|
||||||
|
if (!packCall) {
|
||||||
|
throw new Error("expected npm pack call");
|
||||||
|
}
|
||||||
|
const [argv, options] = packCall;
|
||||||
|
expect(argv).toEqual(["npm", "pack", "@openclaw/test-hooks@0.0.1", "--ignore-scripts"]);
|
||||||
|
expect(options?.env).toMatchObject({ NPM_CONFIG_IGNORE_SCRIPTS: "true" });
|
||||||
|
|
||||||
|
expect(packTmpDir).not.toBe("");
|
||||||
|
expect(fs.existsSync(packTmpDir)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects non-registry npm specs", async () => {
|
||||||
|
const result = await installHooksFromNpmSpec({ spec: "github:evil/evil" });
|
||||||
|
expect(result.ok).toBe(false);
|
||||||
|
if (result.ok) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect(result.error).toContain("unsupported npm spec");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
resolveArchiveKind,
|
resolveArchiveKind,
|
||||||
resolvePackedRootDir,
|
resolvePackedRootDir,
|
||||||
} from "../infra/archive.js";
|
} from "../infra/archive.js";
|
||||||
|
import { validateRegistryNpmSpec } from "../infra/npm-registry-spec.js";
|
||||||
import { runCommandWithTimeout } from "../process/exec.js";
|
import { runCommandWithTimeout } from "../process/exec.js";
|
||||||
import { CONFIG_DIR, resolveUserPath } from "../utils.js";
|
import { CONFIG_DIR, resolveUserPath } from "../utils.js";
|
||||||
import { parseFrontmatter } from "./frontmatter.js";
|
import { parseFrontmatter } from "./frontmatter.js";
|
||||||
@@ -356,44 +357,48 @@ export async function installHooksFromArchive(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hook-"));
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hook-"));
|
||||||
const extractDir = path.join(tmpDir, "extract");
|
|
||||||
await fs.mkdir(extractDir, { recursive: true });
|
|
||||||
|
|
||||||
logger.info?.(`Extracting ${archivePath}…`);
|
|
||||||
try {
|
try {
|
||||||
await extractArchive({ archivePath, destDir: extractDir, timeoutMs, logger });
|
const extractDir = path.join(tmpDir, "extract");
|
||||||
} catch (err) {
|
await fs.mkdir(extractDir, { recursive: true });
|
||||||
return { ok: false, error: `failed to extract archive: ${String(err)}` };
|
|
||||||
}
|
|
||||||
|
|
||||||
let rootDir = "";
|
logger.info?.(`Extracting ${archivePath}…`);
|
||||||
try {
|
try {
|
||||||
rootDir = await resolvePackedRootDir(extractDir);
|
await extractArchive({ archivePath, destDir: extractDir, timeoutMs, logger });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return { ok: false, error: String(err) };
|
return { ok: false, error: `failed to extract archive: ${String(err)}` };
|
||||||
}
|
}
|
||||||
|
|
||||||
const manifestPath = path.join(rootDir, "package.json");
|
let rootDir = "";
|
||||||
if (await fileExists(manifestPath)) {
|
try {
|
||||||
return await installHookPackageFromDir({
|
rootDir = await resolvePackedRootDir(extractDir);
|
||||||
packageDir: rootDir,
|
} catch (err) {
|
||||||
|
return { ok: false, error: String(err) };
|
||||||
|
}
|
||||||
|
|
||||||
|
const manifestPath = path.join(rootDir, "package.json");
|
||||||
|
if (await fileExists(manifestPath)) {
|
||||||
|
return await installHookPackageFromDir({
|
||||||
|
packageDir: rootDir,
|
||||||
|
hooksDir: params.hooksDir,
|
||||||
|
timeoutMs,
|
||||||
|
logger,
|
||||||
|
mode: params.mode,
|
||||||
|
dryRun: params.dryRun,
|
||||||
|
expectedHookPackId: params.expectedHookPackId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await installHookFromDir({
|
||||||
|
hookDir: rootDir,
|
||||||
hooksDir: params.hooksDir,
|
hooksDir: params.hooksDir,
|
||||||
timeoutMs,
|
|
||||||
logger,
|
logger,
|
||||||
mode: params.mode,
|
mode: params.mode,
|
||||||
dryRun: params.dryRun,
|
dryRun: params.dryRun,
|
||||||
expectedHookPackId: params.expectedHookPackId,
|
expectedHookPackId: params.expectedHookPackId,
|
||||||
});
|
});
|
||||||
|
} finally {
|
||||||
|
await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
return await installHookFromDir({
|
|
||||||
hookDir: rootDir,
|
|
||||||
hooksDir: params.hooksDir,
|
|
||||||
logger,
|
|
||||||
mode: params.mode,
|
|
||||||
dryRun: params.dryRun,
|
|
||||||
expectedHookPackId: params.expectedHookPackId,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function installHooksFromNpmSpec(params: {
|
export async function installHooksFromNpmSpec(params: {
|
||||||
@@ -411,40 +416,48 @@ export async function installHooksFromNpmSpec(params: {
|
|||||||
const dryRun = params.dryRun ?? false;
|
const dryRun = params.dryRun ?? false;
|
||||||
const expectedHookPackId = params.expectedHookPackId;
|
const expectedHookPackId = params.expectedHookPackId;
|
||||||
const spec = params.spec.trim();
|
const spec = params.spec.trim();
|
||||||
if (!spec) {
|
const specError = validateRegistryNpmSpec(spec);
|
||||||
return { ok: false, error: "missing npm spec" };
|
if (specError) {
|
||||||
|
return { ok: false, error: specError };
|
||||||
}
|
}
|
||||||
|
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hook-pack-"));
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hook-pack-"));
|
||||||
logger.info?.(`Downloading ${spec}…`);
|
try {
|
||||||
const res = await runCommandWithTimeout(["npm", "pack", spec], {
|
logger.info?.(`Downloading ${spec}…`);
|
||||||
timeoutMs: Math.max(timeoutMs, 300_000),
|
const res = await runCommandWithTimeout(["npm", "pack", spec, "--ignore-scripts"], {
|
||||||
cwd: tmpDir,
|
timeoutMs: Math.max(timeoutMs, 300_000),
|
||||||
env: { COREPACK_ENABLE_DOWNLOAD_PROMPT: "0" },
|
cwd: tmpDir,
|
||||||
});
|
env: {
|
||||||
if (res.code !== 0) {
|
COREPACK_ENABLE_DOWNLOAD_PROMPT: "0",
|
||||||
return { ok: false, error: `npm pack failed: ${res.stderr.trim() || res.stdout.trim()}` };
|
NPM_CONFIG_IGNORE_SCRIPTS: "true",
|
||||||
}
|
},
|
||||||
|
});
|
||||||
|
if (res.code !== 0) {
|
||||||
|
return { ok: false, error: `npm pack failed: ${res.stderr.trim() || res.stdout.trim()}` };
|
||||||
|
}
|
||||||
|
|
||||||
const packed = (res.stdout || "")
|
const packed = (res.stdout || "")
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.map((l) => l.trim())
|
.map((l) => l.trim())
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.pop();
|
.pop();
|
||||||
if (!packed) {
|
if (!packed) {
|
||||||
return { ok: false, error: "npm pack produced no archive" };
|
return { ok: false, error: "npm pack produced no archive" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const archivePath = path.join(tmpDir, packed);
|
const archivePath = path.join(tmpDir, packed);
|
||||||
return await installHooksFromArchive({
|
return await installHooksFromArchive({
|
||||||
archivePath,
|
archivePath,
|
||||||
hooksDir: params.hooksDir,
|
hooksDir: params.hooksDir,
|
||||||
timeoutMs,
|
timeoutMs,
|
||||||
logger,
|
logger,
|
||||||
mode,
|
mode,
|
||||||
dryRun,
|
dryRun,
|
||||||
expectedHookPackId,
|
expectedHookPackId,
|
||||||
});
|
});
|
||||||
|
} finally {
|
||||||
|
await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => undefined);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function installHooksFromPath(params: {
|
export async function installHooksFromPath(params: {
|
||||||
|
|||||||
41
src/infra/npm-registry-spec.ts
Normal file
41
src/infra/npm-registry-spec.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
export function validateRegistryNpmSpec(rawSpec: string): string | null {
|
||||||
|
const spec = rawSpec.trim();
|
||||||
|
if (!spec) {
|
||||||
|
return "missing npm spec";
|
||||||
|
}
|
||||||
|
if (/\s/.test(spec)) {
|
||||||
|
return "unsupported npm spec: whitespace is not allowed";
|
||||||
|
}
|
||||||
|
// Registry-only: no URLs, git, file, or alias protocols.
|
||||||
|
// Keep strict: this runs on the gateway host.
|
||||||
|
if (spec.includes("://")) {
|
||||||
|
return "unsupported npm spec: URLs are not allowed";
|
||||||
|
}
|
||||||
|
if (spec.includes("#")) {
|
||||||
|
return "unsupported npm spec: git refs are not allowed";
|
||||||
|
}
|
||||||
|
if (spec.includes(":")) {
|
||||||
|
return "unsupported npm spec: protocol specs are not allowed";
|
||||||
|
}
|
||||||
|
|
||||||
|
const at = spec.lastIndexOf("@");
|
||||||
|
const hasVersion = at > 0;
|
||||||
|
const name = hasVersion ? spec.slice(0, at) : spec;
|
||||||
|
const version = hasVersion ? spec.slice(at + 1) : "";
|
||||||
|
|
||||||
|
const unscopedName = /^[a-z0-9][a-z0-9-._~]*$/;
|
||||||
|
const scopedName = /^@[a-z0-9][a-z0-9-._~]*\/[a-z0-9][a-z0-9-._~]*$/;
|
||||||
|
const isValidName = name.startsWith("@") ? scopedName.test(name) : unscopedName.test(name);
|
||||||
|
if (!isValidName) {
|
||||||
|
return "unsupported npm spec: expected <name> or <name>@<version> from the npm registry";
|
||||||
|
}
|
||||||
|
if (hasVersion) {
|
||||||
|
if (!version) {
|
||||||
|
return "unsupported npm spec: missing version/tag after @";
|
||||||
|
}
|
||||||
|
if (/[\\/]/.test(version)) {
|
||||||
|
return "unsupported npm spec: invalid version/tag";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import fs from "node:fs";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import * as tar from "tar";
|
import * as tar from "tar";
|
||||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import * as skillScanner from "../security/skill-scanner.js";
|
import * as skillScanner from "../security/skill-scanner.js";
|
||||||
|
|
||||||
vi.mock("../process/exec.js", () => ({
|
vi.mock("../process/exec.js", () => ({
|
||||||
@@ -52,6 +52,10 @@ afterEach(() => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
describe("installPluginFromArchive", () => {
|
describe("installPluginFromArchive", () => {
|
||||||
it("installs into ~/.openclaw/extensions and uses unscoped id", async () => {
|
it("installs into ~/.openclaw/extensions and uses unscoped id", async () => {
|
||||||
const stateDir = makeTempDir();
|
const stateDir = makeTempDir();
|
||||||
@@ -487,3 +491,72 @@ describe("installPluginFromDir", () => {
|
|||||||
expect(opts?.cwd).toBe(res.targetDir);
|
expect(opts?.cwd).toBe(res.targetDir);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("installPluginFromNpmSpec", () => {
|
||||||
|
it("uses --ignore-scripts for npm pack and cleans up temp dir", async () => {
|
||||||
|
const workDir = makeTempDir();
|
||||||
|
const stateDir = makeTempDir();
|
||||||
|
const pkgDir = path.join(workDir, "package");
|
||||||
|
fs.mkdirSync(path.join(pkgDir, "dist"), { recursive: true });
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(pkgDir, "package.json"),
|
||||||
|
JSON.stringify({
|
||||||
|
name: "@openclaw/voice-call",
|
||||||
|
version: "0.0.1",
|
||||||
|
openclaw: { extensions: ["./dist/index.js"] },
|
||||||
|
}),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
fs.writeFileSync(path.join(pkgDir, "dist", "index.js"), "export {};", "utf-8");
|
||||||
|
|
||||||
|
const extensionsDir = path.join(stateDir, "extensions");
|
||||||
|
fs.mkdirSync(extensionsDir, { recursive: true });
|
||||||
|
|
||||||
|
const { runCommandWithTimeout } = await import("../process/exec.js");
|
||||||
|
const run = vi.mocked(runCommandWithTimeout);
|
||||||
|
|
||||||
|
let packTmpDir = "";
|
||||||
|
const packedName = "voice-call-0.0.1.tgz";
|
||||||
|
run.mockImplementation(async (argv, opts) => {
|
||||||
|
if (argv[0] === "npm" && argv[1] === "pack") {
|
||||||
|
packTmpDir = String(opts?.cwd ?? "");
|
||||||
|
await packToArchive({ pkgDir, outDir: packTmpDir, outName: packedName });
|
||||||
|
return { code: 0, stdout: `${packedName}\n`, stderr: "", signal: null, killed: false };
|
||||||
|
}
|
||||||
|
throw new Error(`unexpected command: ${argv.join(" ")}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
const { installPluginFromNpmSpec } = await import("./install.js");
|
||||||
|
const result = await installPluginFromNpmSpec({
|
||||||
|
spec: "@openclaw/voice-call@0.0.1",
|
||||||
|
extensionsDir,
|
||||||
|
logger: { info: () => {}, warn: () => {} },
|
||||||
|
});
|
||||||
|
expect(result.ok).toBe(true);
|
||||||
|
|
||||||
|
const packCalls = run.mock.calls.filter(
|
||||||
|
(c) => Array.isArray(c[0]) && c[0][0] === "npm" && c[0][1] === "pack",
|
||||||
|
);
|
||||||
|
expect(packCalls.length).toBe(1);
|
||||||
|
const packCall = packCalls[0];
|
||||||
|
if (!packCall) {
|
||||||
|
throw new Error("expected npm pack call");
|
||||||
|
}
|
||||||
|
const [argv, options] = packCall;
|
||||||
|
expect(argv).toEqual(["npm", "pack", "@openclaw/voice-call@0.0.1", "--ignore-scripts"]);
|
||||||
|
expect(options?.env).toMatchObject({ NPM_CONFIG_IGNORE_SCRIPTS: "true" });
|
||||||
|
|
||||||
|
expect(packTmpDir).not.toBe("");
|
||||||
|
expect(fs.existsSync(packTmpDir)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects non-registry npm specs", async () => {
|
||||||
|
const { installPluginFromNpmSpec } = await import("./install.js");
|
||||||
|
const result = await installPluginFromNpmSpec({ spec: "github:evil/evil" });
|
||||||
|
expect(result.ok).toBe(false);
|
||||||
|
if (result.ok) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect(result.error).toContain("unsupported npm spec");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
resolveArchiveKind,
|
resolveArchiveKind,
|
||||||
resolvePackedRootDir,
|
resolvePackedRootDir,
|
||||||
} from "../infra/archive.js";
|
} from "../infra/archive.js";
|
||||||
|
import { validateRegistryNpmSpec } from "../infra/npm-registry-spec.js";
|
||||||
import { runCommandWithTimeout } from "../process/exec.js";
|
import { runCommandWithTimeout } from "../process/exec.js";
|
||||||
import * as skillScanner from "../security/skill-scanner.js";
|
import * as skillScanner from "../security/skill-scanner.js";
|
||||||
import { CONFIG_DIR, resolveUserPath } from "../utils.js";
|
import { CONFIG_DIR, resolveUserPath } from "../utils.js";
|
||||||
@@ -334,37 +335,41 @@ export async function installPluginFromArchive(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-plugin-"));
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-plugin-"));
|
||||||
const extractDir = path.join(tmpDir, "extract");
|
|
||||||
await fs.mkdir(extractDir, { recursive: true });
|
|
||||||
|
|
||||||
logger.info?.(`Extracting ${archivePath}…`);
|
|
||||||
try {
|
try {
|
||||||
await extractArchive({
|
const extractDir = path.join(tmpDir, "extract");
|
||||||
archivePath,
|
await fs.mkdir(extractDir, { recursive: true });
|
||||||
destDir: extractDir,
|
|
||||||
|
logger.info?.(`Extracting ${archivePath}…`);
|
||||||
|
try {
|
||||||
|
await extractArchive({
|
||||||
|
archivePath,
|
||||||
|
destDir: extractDir,
|
||||||
|
timeoutMs,
|
||||||
|
logger,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
return { ok: false, error: `failed to extract archive: ${String(err)}` };
|
||||||
|
}
|
||||||
|
|
||||||
|
let packageDir = "";
|
||||||
|
try {
|
||||||
|
packageDir = await resolvePackedRootDir(extractDir);
|
||||||
|
} catch (err) {
|
||||||
|
return { ok: false, error: String(err) };
|
||||||
|
}
|
||||||
|
|
||||||
|
return await installPluginFromPackageDir({
|
||||||
|
packageDir,
|
||||||
|
extensionsDir: params.extensionsDir,
|
||||||
timeoutMs,
|
timeoutMs,
|
||||||
logger,
|
logger,
|
||||||
|
mode,
|
||||||
|
dryRun: params.dryRun,
|
||||||
|
expectedPluginId: params.expectedPluginId,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} finally {
|
||||||
return { ok: false, error: `failed to extract archive: ${String(err)}` };
|
await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
let packageDir = "";
|
|
||||||
try {
|
|
||||||
packageDir = await resolvePackedRootDir(extractDir);
|
|
||||||
} catch (err) {
|
|
||||||
return { ok: false, error: String(err) };
|
|
||||||
}
|
|
||||||
|
|
||||||
return await installPluginFromPackageDir({
|
|
||||||
packageDir,
|
|
||||||
extensionsDir: params.extensionsDir,
|
|
||||||
timeoutMs,
|
|
||||||
logger,
|
|
||||||
mode,
|
|
||||||
dryRun: params.dryRun,
|
|
||||||
expectedPluginId: params.expectedPluginId,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function installPluginFromDir(params: {
|
export async function installPluginFromDir(params: {
|
||||||
@@ -468,43 +473,51 @@ export async function installPluginFromNpmSpec(params: {
|
|||||||
const dryRun = params.dryRun ?? false;
|
const dryRun = params.dryRun ?? false;
|
||||||
const expectedPluginId = params.expectedPluginId;
|
const expectedPluginId = params.expectedPluginId;
|
||||||
const spec = params.spec.trim();
|
const spec = params.spec.trim();
|
||||||
if (!spec) {
|
const specError = validateRegistryNpmSpec(spec);
|
||||||
return { ok: false, error: "missing npm spec" };
|
if (specError) {
|
||||||
|
return { ok: false, error: specError };
|
||||||
}
|
}
|
||||||
|
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-npm-pack-"));
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-npm-pack-"));
|
||||||
logger.info?.(`Downloading ${spec}…`);
|
try {
|
||||||
const res = await runCommandWithTimeout(["npm", "pack", spec], {
|
logger.info?.(`Downloading ${spec}…`);
|
||||||
timeoutMs: Math.max(timeoutMs, 300_000),
|
const res = await runCommandWithTimeout(["npm", "pack", spec, "--ignore-scripts"], {
|
||||||
cwd: tmpDir,
|
timeoutMs: Math.max(timeoutMs, 300_000),
|
||||||
env: { COREPACK_ENABLE_DOWNLOAD_PROMPT: "0" },
|
cwd: tmpDir,
|
||||||
});
|
env: {
|
||||||
if (res.code !== 0) {
|
COREPACK_ENABLE_DOWNLOAD_PROMPT: "0",
|
||||||
return {
|
NPM_CONFIG_IGNORE_SCRIPTS: "true",
|
||||||
ok: false,
|
},
|
||||||
error: `npm pack failed: ${res.stderr.trim() || res.stdout.trim()}`,
|
});
|
||||||
};
|
if (res.code !== 0) {
|
||||||
}
|
return {
|
||||||
|
ok: false,
|
||||||
|
error: `npm pack failed: ${res.stderr.trim() || res.stdout.trim()}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const packed = (res.stdout || "")
|
const packed = (res.stdout || "")
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.map((l) => l.trim())
|
.map((l) => l.trim())
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.pop();
|
.pop();
|
||||||
if (!packed) {
|
if (!packed) {
|
||||||
return { ok: false, error: "npm pack produced no archive" };
|
return { ok: false, error: "npm pack produced no archive" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const archivePath = path.join(tmpDir, packed);
|
const archivePath = path.join(tmpDir, packed);
|
||||||
return await installPluginFromArchive({
|
return await installPluginFromArchive({
|
||||||
archivePath,
|
archivePath,
|
||||||
extensionsDir: params.extensionsDir,
|
extensionsDir: params.extensionsDir,
|
||||||
timeoutMs,
|
timeoutMs,
|
||||||
logger,
|
logger,
|
||||||
mode,
|
mode,
|
||||||
dryRun,
|
dryRun,
|
||||||
expectedPluginId,
|
expectedPluginId,
|
||||||
});
|
});
|
||||||
|
} finally {
|
||||||
|
await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => undefined);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function installPluginFromPath(params: {
|
export async function installPluginFromPath(params: {
|
||||||
|
|||||||
Reference in New Issue
Block a user