Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions docs/hooks/plugins.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,31 @@ A rewrite in `updatedInput` must match the tool's input schema and pass the tool

The transcript keeps the model's original tool call. Xum adds a note to the tool result that names the fields the hook changed, for example `[plugin:guard] Rewrote this tool call's input (fields: script).` The note never contains the values, because a hook can put a secret into the input, and the model provider and the chat log see the note.

Xum honors an explicit deny or block when the rest of the output breaks a Codex rule, for example with an unknown field. It does not honor one when the output is malformed (a wrong type or a value outside an enum). See the list below. Xum does not support `permissionDecision: "ask"`, `updatedMCPToolOutput`, or `suppressOutput`.
Xum honors an explicit deny or block when the rest of the output breaks a Codex rule, for example with an unknown field. It does not honor one when the output is malformed (a wrong type or a value outside an enum). Xum does not support `permissionDecision: "ask"`, `updatedMCPToolOutput`, or `suppressOutput`. The two lists below say what each case does.

### What blocks a `PreToolUse` call

A broken guard must not let a call through. Xum blocks a `PreToolUse` call when the hook's output:

- Is malformed: invalid JSON, a wrong type, or a value outside an enum. A deny in the same output does not apply.
- Fails Codex's schema: an unknown field, or `hookSpecificOutput` tagged for another event. An explicit deny next to an unknown field still applies, with its own reason.
- Starts with `{` or `[` and is not a JSON object, such as `[]` or a list of findings. Plain text that does not start with `{` or `[` is ignored, a bare JSON scalar such as `42` included.
- Uses a decision or field that Xum does not support: `permissionDecision: "allow"` without `updatedInput`, `permissionDecision: "ask"`, the legacy `decision: "approve"`, `continue: false`, `stopReason`, `suppressOutput`, `updatedInput` without `allow`, or a reason without a decision. Codex runs the call in these cases. Xum blocks it.
- Has a result text over 1 MiB (1,048,576 characters).

The model sees `Tool call blocked by plugin '<plugin>' hook: its output is malformed: <reason>` as the tool error. You see a notice that names the plugin and says to turn the hook off in Settings > Plugins.

A rewrite (`updatedInput`) next to an unknown or unsupported field makes Xum deny the call too, because the hook allowed only its rewrite. If the filled `input` of a `PreToolUse` hook is over 64 KiB (65,536 characters) as JSON, and the hook can run, Xum also denies the call. A model can pad a command to get past a guard, so Xum does not let it through.

### What fails open

In these cases, Xum logs a warning and continues without the hook's output:

- The hook times out, errors, or returns an MCP error.
- The server or tool is missing, disabled, or not running.
- The output is malformed: invalid JSON, a wrong type, or a value outside an enum. A deny or block in the same output is ignored too.
- The output has an unknown field. Xum then ignores the hook's other output, with two exceptions: on `PreToolUse`, `PostToolUse`, and `UserPromptSubmit` an explicit deny or block still applies, and a `PreToolUse` rewrite (`updatedInput`) makes Xum deny the call, because the hook allowed only its rewrite. On `Stop`, an unknown field voids the whole output, a block included. On every event, Xum still shows a `systemMessage` to you.
- The result text is over 1 MiB (1,048,576 characters).
- On every event: the hook times out, errors, or returns an MCP error result (`isError`).
- On every event: the server or tool is missing, disabled, or not running.
- On `PostToolUse`, `UserPromptSubmit`, and `Stop`: the output is malformed, fails Codex's schema, starts with `{` or `[` and is not a JSON object, or is over 1 MiB. A deny or block in a malformed output is ignored too. With an unknown field, an explicit block on `PostToolUse` and `UserPromptSubmit` still applies. On `Stop`, an unknown field voids the whole output, a block included. On `UserPromptSubmit`, text that does not start with `{` or `[` is context, as the table above says.

One case fails closed. If the filled `input` of a `PreToolUse` hook is over 64 KiB (65,536 characters) as JSON, and the hook can run, Xum denies the call. A model can pad a command to get past a guard, so Xum does not let it through.
When an output parses but breaks a Codex rule (an unknown field, an unsupported decision), Xum still shows its `systemMessage` to you.

### Size limits

Expand Down
51 changes: 33 additions & 18 deletions src/node/services/agentPlugins/codexHookOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
* Reads a Codex `mcp_tool` hook's output into one decision per event, with Codex's rules
* (openai/codex codex-rs/hooks/src/engine/output_parser.rs and hooks/src/events/*.rs).
*
* Two failure kinds, both fail open in the runner:
* Two failure kinds:
* - `parse*` throws when the output does not match Codex's output schema: an unknown field
* (Codex's wire structs use `deny_unknown_fields`; the generated output schemas set
* `additionalProperties: false`), a wrong type or a value outside an enum. Nothing of it applies.
* `additionalProperties: false`), a wrong type, a value outside an enum, or text that starts
* with `{` or `[` and is not a JSON object. Nothing of it applies.
* - A result with `invalid` set parsed but breaks a semantic rule (a reason without a decision, a
* field the event does not support). Codex still shows its `systemMessage` and ignores the rest.
*
* Xum differences:
* - An explicit PreToolUse deny and a PostToolUse block or stop always apply, also when Codex
* would void the output for a semantic rule (no reason, suppressOutput, continue: false next to
* a deny). A trusted guard's refusal must not let a model-controlled call through (Codex
* security review PRRT_kwDOPxxmWM6sQaTD). Only a malformed output fails open.
* security review PRRT_kwDOPxxmWM6sQaTD).
* - A malformed PreToolUse output blocks the call (Codex fails it open, D8).
* - JSON null on any optional field reads as absent (Codex PRRT_kwDOPxxmWM6rgpoH).
* - `structuredContent` is read when the result has no text (Codex reads text only).
*/
Expand Down Expand Up @@ -165,8 +167,7 @@ function hasSpecificDecision(specific: Record<string, unknown>): boolean {
/**
* Xum honors an explicit deny even when Codex would void the output for a missing reason or an
* extra field next to it (Codex security review PRRT_kwDOPxxmWM6sQaTD): a trusted guard that
* denies a model-controlled call must not let it run. Only a malformed output (readWire) fails
* open, as in Codex.
* denies a model-controlled call must not let it run.
*/
const NO_REASON = "No reason given by the hook.";

Expand Down Expand Up @@ -337,8 +338,8 @@ interface Wire {

/**
* The output object, checked against Codex's schema for `event`. Returns null for an empty
* result (no text, no structuredContent) and for a result that is not an object (Codex ignores
* both); throws for a malformed one.
* result (no text, no object structuredContent) and for plain text (Codex ignores both); throws
* for a malformed one.
*/
function readWire(
result: unknown,
Expand Down Expand Up @@ -380,14 +381,22 @@ function checkFieldTypes(
}
}

/** JSON text; text that looks like JSON but does not parse is a malformed output. */
function parseText(text: string, event: HookEventName): unknown {
/**
* The output object in JSON text, or undefined for plain text. Codex's looks_like_json rule: text
* that starts with `{` or `[` must be a JSON object, so a PreToolUse guard that answers with a
* list of findings cannot let the call through (D8). Other text, a bare JSON scalar such as `true`
* included, is plain text.
*/
function parseText(text: string, event: HookEventName): Record<string, unknown> | undefined {
if (!looksLikeJson(text)) return undefined;
let value: unknown;
try {
return JSON.parse(text);
value = JSON.parse(text);
} catch {
if (looksLikeJson(text)) throw new Error(`returned invalid ${event} JSON output`);
return undefined;
throw new Error(`returned invalid ${event} JSON output`);
}
if (!isPlainObject(value)) throw new Error(`returned ${event} JSON output that is not an object`);
return value;
}

/** Codex requires hookSpecificOutput.hookEventName to name this event. */
Expand Down Expand Up @@ -430,15 +439,18 @@ function looksLikeJson(text: string): boolean {
return text.startsWith("{") || text.startsWith("[");
}

/** The first field the event's Codex schema does not list, as a semantic error message. */
/**
* A semantic error message when a field is not in the event's Codex schema. It never names the
* field: a PreToolUse block shows this message to the model and the user, and a hook's field
* name can hold a secret.
*/
function findUnknown(
record: Record<string, unknown>,
allowed: string[],
where: string
): string | undefined {
const key = Object.keys(record).find((candidate) => !allowed.includes(candidate));
return key !== undefined
? `returned ${where} with the unknown field ${JSON.stringify(key).slice(0, 80)}`
return Object.keys(record).some((key) => !allowed.includes(key))
? `returned ${where} with an unknown field`
: undefined;
}

Expand All @@ -451,12 +463,15 @@ function typed(record: Record<string, unknown>, fields: string[], type: string)
}
}

/** Codex's enums allow only these exact values (" deny ", "Block" are malformed). */
/**
* Codex's enums allow only these exact values (" deny ", "Block" are malformed). The error never
* quotes the value, for the reason in findUnknown.
*/
function enumField(record: Record<string, unknown>, field: string, allowed: string[]) {
const value = record[field];
if (value == null) return undefined;
if (typeof value !== "string" || !allowed.includes(value)) {
throw new Error(`invalid ${field} ${JSON.stringify(value).slice(0, 80)}`);
throw new Error(`invalid ${field} value`);
}
return value;
}
Expand Down
Loading
Loading