Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e5e74b6
fix(mcp): resolve server names by own-property, not the prototype chain
0xfandom Jul 16, 2026
0d072f9
test(mcp): restore project mcpServers in afterEach for isolation
0xfandom Jul 17, 2026
b495681
fix(mcp): cover the remaining prototype-name mutation and doctor paths
0xfandom Jul 20, 2026
1832e43
fix(mcp): report a reserved __proto__ entry in file config
0xfandom Jul 22, 2026
115b55c
test(mcp): serialize the proto-name suite on the shared mutation lock
0xfandom Jul 22, 2026
3ca1116
fix(mcp): reserve constructor alongside __proto__
0xfandom Jul 23, 2026
2e93581
fix(mcp): reject a config with a reserved server name outright
0xfandom Jul 27, 2026
9400967
fix(mcp): keep reserved-name findings visible in all-server doctor ou…
0xfandom Jul 27, 2026
710ef3f
fix(mcp): do not mutate a fatally poisoned project .mcp.json
0xfandom Jul 28, 2026
a030e0d
fix(mcp): surface reserved-name errors in single-server doctor output
0xfandom Jul 28, 2026
dad6fc0
fix(mcp): honor the fatal-parse contract across every scope
0xfandom Jul 29, 2026
033c471
test(mcp): pin allowedSettingSources so the local-scope guard is herm…
0xfandom Aug 5, 2026
f555c32
fix(mcp): guard scope mutations on raw parse errors, not the source f…
0xfandom Aug 5, 2026
5f4263f
fix(mcp): detect unscoped-remove membership from parsed scope views
0xfandom Aug 5, 2026
acac8b9
fix(mcp): read the raw project scope for mutations, not the source view
0xfandom Aug 6, 2026
49ea344
test(mcp): re-pin the poisoned local fixture atomically per mutation
0xfandom Aug 7, 2026
e17a884
fix(mcp): surface managed-mcp.json parse errors in enterprise mode
0xfandom Aug 12, 2026
535cb24
fix(mcp): detect unscoped-remove membership source-filter-independent
0xfandom Aug 12, 2026
edc3958
fix(mcp): surface managed-mcp.json errors in headless startup
0xfandom Aug 13, 2026
d00e246
fix(mcp): enforce enterprise-exclusive boundary in named lookups
0xfandom Aug 18, 2026
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
13 changes: 9 additions & 4 deletions src/cli/handlers/mcp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,22 @@ export async function mcpRemoveHandler(name: string, options: {
const projectConfig = getCurrentProjectConfig();
const globalConfig = getGlobalConfig();

// Check if server exists in project scope (.mcp.json)
// Check if server exists in project scope (.mcp.json). These maps are plain
// objects from JSON config, so a `!!servers[name]` / `?.[name]` check treats
// inherited Object.prototype members ('constructor', '__proto__', …) as
// present. `mcp remove constructor` would then report the reserved name as
// living in multiple scopes instead of "No MCP server found". Gate on
// own-property.
const {
servers: projectServers
} = getMcpConfigsByScope('project');
const mcpJsonExists = !!projectServers[name];
const mcpJsonExists = Object.hasOwn(projectServers, name);

// Count how many scopes contain this server
const scopes: Array<Exclude<ConfigScope, 'dynamic'>> = [];
if (projectConfig.mcpServers?.[name]) scopes.push('local');
if (Object.hasOwn(projectConfig.mcpServers ?? {}, name)) scopes.push('local');
if (mcpJsonExists) scopes.push('project');
if (globalConfig.mcpServers?.[name]) scopes.push('user');
if (Object.hasOwn(globalConfig.mcpServers ?? {}, name)) scopes.push('user');
if (scopes.length === 0) {
cliError(`No MCP server found with name: "${name}"`);
} else if (scopes.length === 1) {
Expand Down
176 changes: 176 additions & 0 deletions src/services/mcp/config.protoName.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { afterEach, beforeEach, expect, test } from 'bun:test'

import {
acquireSharedMutationLock,
releaseSharedMutationLock,
} from '../../test/sharedMutationLock.js'
import {
getCurrentProjectConfig,
getGlobalConfig,
saveCurrentProjectConfig,
saveGlobalConfig,
} from '../../utils/config.js'
import {
addMcpConfig,
getMcpConfigByName,
parseMcpConfig,
removeMcpConfig,
} from './config.js'

// The MCP `servers` maps are plain objects built from JSON config, so a bare
// `servers[name]` lookup exposes inherited Object.prototype members. A user
// running `openclaude mcp get constructor` (or `__proto__`, `toString`, …) must
// get "not found", not the Object constructor cast as a server config.
const PROTO_NAMES = [
'constructor',
'__proto__',
'toString',
'hasOwnProperty',
'valueOf',
'isPrototypeOf',
]

let savedGlobalMcp: ReturnType<typeof getGlobalConfig>['mcpServers']
let savedProjectMcp: ReturnType<typeof getCurrentProjectConfig>['mcpServers']

let savedNodeEnv: string | undefined

beforeEach(async () => {
// This suite swaps NODE_ENV and the process-wide global/project MCP configs,
// so it has to be serialized against the other state-mutating suites bun may
// run alongside it -- otherwise they observe the injected fixtures, or their
// updates are clobbered when this teardown restores a stale snapshot.
await acquireSharedMutationLock('services/mcp/config.protoName.test.ts')
savedNodeEnv = process.env.NODE_ENV
process.env.NODE_ENV = 'test'
savedGlobalMcp = getGlobalConfig().mcpServers
savedProjectMcp = getCurrentProjectConfig().mcpServers
saveGlobalConfig(config => ({
...config,
mcpServers: { realserver: { command: 'echo', args: [] } },
}))
saveCurrentProjectConfig(config => ({
...config,
mcpServers: { locallyreal: { command: 'echo', args: [] } },
}))
})

afterEach(() => {
try {
saveGlobalConfig(config => ({ ...config, mcpServers: savedGlobalMcp }))
saveCurrentProjectConfig(config => ({
...config,
mcpServers: savedProjectMcp,
}))
if (savedNodeEnv === undefined) {
delete process.env.NODE_ENV
} else {
process.env.NODE_ENV = savedNodeEnv
}
} finally {
releaseSharedMutationLock()
}
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

test('resolves a real server by name', () => {
const found = getMcpConfigByName('realserver')
expect(found).not.toBeNull()
expect(found?.scope).toBe('user')
})

test('returns null for a plainly missing name', () => {
expect(getMcpConfigByName('nope-not-here')).toBeNull()
})

test('returns null for Object.prototype member names', () => {
// Before the fix each of these resolved to an inherited function (truthy),
// so the lookup returned a bogus config and callers skipped their not-found
// guard.
for (const name of PROTO_NAMES) {
expect(getMcpConfigByName(name)).toBeNull()
}
})

test('rejects adding a server under a reserved name', async () => {
// "constructor" persists, but the schema then rejects the whole mcpServers
// object on the next read -- so adding it takes down every other server in
// that scope too.
await expect(
addMcpConfig('constructor', { command: 'echo', args: [] }, 'user'),
).rejects.toThrow('reserved')
})

test('rejects adding a server named __proto__', async () => {
// "__proto__" passes the character check but assigning it on a plain object
// hits the prototype setter, so the server would be reported as added and
// silently vanish rather than becoming an own property.
await expect(
addMcpConfig('__proto__', { command: 'echo', args: [] }, 'user'),
).rejects.toThrow('reserved')
})

test('reports proto-name removal as not found instead of succeeding', async () => {
// Before the fix the scoped-removal existence checks accepted inherited
// members, so `mcp remove constructor -s user` claimed success while leaving
// the real configuration untouched.
for (const name of PROTO_NAMES) {
await expect(removeMcpConfig(name, 'user')).rejects.toThrow(
'No user-scoped MCP server found',
)
}
})

test('surfaces a __proto__ entry and rejects the whole file', () => {
// A hand-authored .mcp.json can carry this name: the schema accepts it, but
// copying it into a plain object hits the prototype setter, so the entry
// vanished with no diagnostic and the user could not tell why their server
// did not exist. It must be reported, not silently discarded.
// Parsed from text, exactly as the real file is: JSON.parse creates a true
// own "__proto__" key, which an object literal would not.
const { config, errors } = parseMcpConfig({
configObject: JSON.parse(
'{"mcpServers":{"__proto__":{"command":"echo","args":[]},' +
'"realone":{"command":"echo","args":[]}}}',
),
expandVars: false,
scope: 'project',
filePath: '/tmp/.mcp.json',
})

const protoError = errors.find(e => e.path === 'mcpServers.__proto__')
expect(protoError).toBeDefined()
expect(protoError?.message).toContain('reserved')
expect(protoError?.mcpErrorMetadata?.severity).toBe('fatal')
// A fatal reserved name rejects the whole input (config: null), the same as
// "constructor" does when the schema fails -- so a caller that branches on
// config cannot start with the bad entry quietly dropped and the error lost.
expect(config).toBeNull()
})

test('still allows adding and removing a real server name', async () => {
await addMcpConfig('addedserver', { command: 'echo', args: [] }, 'user')
expect(getMcpConfigByName('addedserver')).not.toBeNull()
await removeMcpConfig('addedserver', 'user')
expect(getMcpConfigByName('addedserver')).toBeNull()
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

test('names a reserved constructor entry instead of failing the whole file', () => {
// The schema rejects the entire mcpServers object for this name, so every
// other server in the scope stops loading. Before, the only diagnostic was a
// generic "does not adhere to schema" against `mcpServers`, which does not
// say which entry is at fault.
const { errors } = parseMcpConfig({
configObject: JSON.parse(
'{"mcpServers":{"constructor":{"command":"echo","args":[]},' +
'"realone":{"command":"echo","args":[]}}}',
),
expandVars: false,
scope: 'project',
filePath: '/tmp/.mcp.json',
})

const named = errors.find(e => e.path === 'mcpServers.constructor')
expect(named).toBeDefined()
expect(named?.message).toContain('reserved')
expect(named?.mcpErrorMetadata?.severity).toBe('fatal')
})
Loading