@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
179 lines • 6.55 kB
JavaScript
import { existsSync, realpathSync } from "node:fs";
import { isAbsolute, relative, resolve } from "node:path";
import { diagnostic } from "./diagnostics.js";
export { applyHookOutputSafety, DEFAULT_STDERR_LIMIT_BYTES, DEFAULT_STDOUT_LIMIT_BYTES, } from "./output-bounds.js";
export const DEFAULT_HOOK_TIMEOUT_SECONDS = 600;
const MINIMAL_INHERITED_ENV = [
"PATH",
"HOME",
"USER",
"USERNAME",
"LOGNAME",
"SHELL",
"TMPDIR",
"TMP",
"TEMP",
"SystemRoot",
"ComSpec",
"PATHEXT",
];
const PLUGIN_ROOT_TEMPLATE_TOKEN = "$" + "{PLUGIN_ROOT}";
const PLUGIN_ROOT_TOKENS = [PLUGIN_ROOT_TEMPLATE_TOKEN, "$PLUGIN_ROOT", "%PLUGIN_ROOT%"];
export function resolveHookTimeoutSeconds(handler) {
const timeout = handler.config.timeout;
if (timeout === undefined) {
return DEFAULT_HOOK_TIMEOUT_SECONDS;
}
if (!isValidHookTimeoutSeconds(timeout)) {
throw new Error("Invalid command hook timeout reached runtime execution.");
}
return timeout;
}
export function isValidHookTimeoutSeconds(timeout) {
return Number.isFinite(timeout) && timeout > 0;
}
export function buildHookEnvironment(options) {
const env = {};
for (const key of MINIMAL_INHERITED_ENV) {
const value = options.sourceEnv[key];
if (value !== undefined)
env[key] = value;
}
for (const key of options.envPassthrough ?? []) {
const value = options.sourceEnv[key];
if (value !== undefined)
env[key] = value;
}
const pluginEnv = pluginEnvForSource(options.handler.source);
if (pluginEnv !== undefined) {
env.PLUGIN_ROOT = pluginEnv.PLUGIN_ROOT;
env.PLUGIN_DATA = pluginEnv.PLUGIN_DATA;
env.CLAUDE_PLUGIN_ROOT = pluginEnv.CLAUDE_PLUGIN_ROOT;
env.CLAUDE_PLUGIN_DATA = pluginEnv.CLAUDE_PLUGIN_DATA;
}
env.SENPI_HOOK_SOURCE = options.handler.source.sourcePath;
env.SENPI_HOOK_EVENT = options.input.event;
return env;
}
export function validateHookHandlerSafety(handler) {
if (handler.source.scope !== "plugin" || handler.source.pluginRoot === undefined)
return [];
return [
...validateCommandField(handler, "command", handler.config.command),
...(handler.config.commandWindows === undefined
? []
: validateCommandField(handler, "commandWindows", handler.config.commandWindows)),
];
}
function validateCommandField(handler, field, command) {
const diagnostics = [];
for (const target of extractPluginRootTargets(command, handler.source.pluginRoot ?? "")) {
const path = `hooks.${handler.event}[${handler.groupIndex}].hooks[${handler.handlerIndex}].${field}`;
if (!isContained(resolve(handler.source.pluginRoot ?? ""), target.resolvedPath)) {
diagnostics.push(diagnostic({
code: "invalid_command_target",
event: handler.event,
message: `Plugin command target is outside plugin root: ${target.raw}`,
path,
}, handler.source));
continue;
}
if (!existsSync(target.resolvedPath)) {
diagnostics.push(diagnostic({
code: "missing_command_target",
event: handler.event,
message: `Plugin command target does not exist: ${target.raw}`,
path,
}, handler.source));
continue;
}
const realTarget = realpathSync.native(target.resolvedPath);
const realRoot = realpathSync.native(resolve(handler.source.pluginRoot ?? ""));
if (!isContained(realRoot, realTarget)) {
diagnostics.push(diagnostic({
code: "invalid_command_target",
event: handler.event,
message: `Plugin command target resolves outside plugin root: ${target.raw}`,
path,
}, handler.source));
}
}
return diagnostics;
}
function extractPluginRootTargets(command, pluginRoot) {
const targets = [];
for (const word of readCommandWords(command)) {
for (const token of PLUGIN_ROOT_TOKENS) {
if (!word.expanded.includes(token))
continue;
const expanded = word.expanded.replaceAll(token, pluginRoot).replaceAll("\\", "/");
targets.push({ raw: word.raw, resolvedPath: resolve(expanded) });
}
}
return targets;
}
function readCommandWords(command) {
const words = [];
let index = 0;
while (index < command.length) {
while (index < command.length && isCommandWordTerminator(command[index]))
index += 1;
if (index >= command.length)
break;
const start = index;
let expanded = "";
while (index < command.length && !isCommandWordTerminator(command[index])) {
const char = command[index];
if (char === "'" || char === '"' || char === "`") {
const quote = char;
index += 1;
while (index < command.length && command[index] !== quote) {
expanded += command[index];
index += 1;
}
if (command[index] === quote)
index += 1;
continue;
}
expanded += char;
index += 1;
}
words.push({ expanded, raw: command.slice(start, index) });
}
return words;
}
function isCommandWordTerminator(char) {
return (char === undefined ||
char === " " ||
char === "\t" ||
char === "\n" ||
char === ";" ||
char === "&" ||
char === "|" ||
char === "<" ||
char === ">" ||
char === ")");
}
function isContained(root, target) {
const rel = relative(root, target);
return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
}
function pluginEnvForSource(source) {
if (!("pluginEnv" in source))
return undefined;
const pluginEnv = source.pluginEnv;
if (!isPluginHookEnv(pluginEnv))
return undefined;
return pluginEnv;
}
function isPluginHookEnv(value) {
return (isRecord(value) &&
typeof value.PLUGIN_ROOT === "string" &&
typeof value.PLUGIN_DATA === "string" &&
typeof value.CLAUDE_PLUGIN_ROOT === "string" &&
typeof value.CLAUDE_PLUGIN_DATA === "string");
}
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
//# sourceMappingURL=safety.js.map