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
8 changes: 1 addition & 7 deletions src/features/mcp/codexcli-mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ValidationResult } from "../../types/ai-file.js";
import { McpServers } from "../../types/mcp.js";
import { readFileContentOrNull, readOrInitializeFileContent } from "../../utils/file.js";
import { warnWithFallback } from "../../utils/logger.js";
import { isRecord } from "../../utils/type-guards.js";
import { isPlainObject, isRecord } from "../../utils/type-guards.js";
import { RulesyncMcp } from "./rulesync-mcp.js";
import {
ToolMcp,
Expand All @@ -21,12 +21,6 @@ const MAX_REMOVE_EMPTY_ENTRIES_DEPTH = 32;

const PROTOTYPE_POLLUTION_KEYS = new Set(["__proto__", "constructor", "prototype"]);

function isPlainObject(value: unknown): value is Record<string, unknown> {
if (!isRecord(value)) return false;
const proto = Object.getPrototypeOf(value);
return proto === null || proto === Object.prototype;
}

function convertFromCodexFormat(codexMcp: Record<string, unknown>): McpServers {
const result: McpServers = {};

Expand Down
7 changes: 1 addition & 6 deletions src/utils/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import matter from "gray-matter";
import { dump, load } from "js-yaml";

import { formatError } from "./error.js";

function isPlainObject(value: unknown): value is Record<string, unknown> {
if (value === null || typeof value !== "object") return false;
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
}
import { isPlainObject } from "./type-guards.js";

function deepRemoveNullishValue(value: unknown): unknown {
if (value === null || value === undefined) {
Expand Down
17 changes: 17 additions & 0 deletions src/utils/type-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@
export function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}

/**
* Stricter sibling of {@link isRecord}: narrows to *plain* objects whose
* prototype is either ``Object.prototype`` or ``null`` — i.e. object
* literals, ``Object.create(null)`` bags, and the output of ``JSON.parse``.
*
* Rejects class instances even though they pass ``isRecord``. This is the
* check needed for prototype-pollution hardening: anything walking
* arbitrary user-supplied keys (frontmatter parsing, MCP config
* conversion, etc.) should reject inputs whose prototype could carry
* malicious accessor descriptors.
*/
export function isPlainObject(value: unknown): value is Record<string, unknown> {
if (!isRecord(value)) return false;
const proto = Object.getPrototypeOf(value);
return proto === null || proto === Object.prototype;
}
Loading