Repository navigation
fix(mcp): resolve server names by own-property, not the prototype chain #1983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kevincodex1
merged 20 commits into
Twigpine:main
from
0xfandom:fix/mcp-name-proto-lookup
Sep 21, 2026
Merged
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 0d072f9
test(mcp): restore project mcpServers in afterEach for isolation
0xfandom b495681
fix(mcp): cover the remaining prototype-name mutation and doctor paths
0xfandom 1832e43
fix(mcp): report a reserved __proto__ entry in file config
0xfandom 115b55c
test(mcp): serialize the proto-name suite on the shared mutation lock
0xfandom 3ca1116
fix(mcp): reserve constructor alongside __proto__
0xfandom 2e93581
fix(mcp): reject a config with a reserved server name outright
0xfandom 9400967
fix(mcp): keep reserved-name findings visible in all-server doctor ou…
0xfandom 710ef3f
fix(mcp): do not mutate a fatally poisoned project .mcp.json
0xfandom a030e0d
fix(mcp): surface reserved-name errors in single-server doctor output
0xfandom dad6fc0
fix(mcp): honor the fatal-parse contract across every scope
0xfandom 033c471
test(mcp): pin allowedSettingSources so the local-scope guard is herm…
0xfandom f555c32
fix(mcp): guard scope mutations on raw parse errors, not the source f…
0xfandom 5f4263f
fix(mcp): detect unscoped-remove membership from parsed scope views
0xfandom acac8b9
fix(mcp): read the raw project scope for mutations, not the source view
0xfandom 49ea344
test(mcp): re-pin the poisoned local fixture atomically per mutation
0xfandom e17a884
fix(mcp): surface managed-mcp.json parse errors in enterprise mode
0xfandom 535cb24
fix(mcp): detect unscoped-remove membership source-filter-independent
0xfandom edc3958
fix(mcp): surface managed-mcp.json errors in headless startup
0xfandom d00e246
fix(mcp): enforce enterprise-exclusive boundary in named lookups
0xfandom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
| }) | ||
|
|
||
| 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() | ||
| }) | ||
|
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') | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.