Skip to content

Commit fae244c

Browse files
committed
fix(agent-targets): reject inherited keys in resolveTarget
- Update resolveTarget to use hasOwnProperty to exclude inherited prototype keys - Add test to verify resolveTarget returns null for inherited keys like constructor, __proto__, toString, hasOwnProperty - Ensure inherited prototype-chain keys are recognized as unknown targets and rejected explicitly
1 parent b1eccd5 commit fae244c

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/lib/agent-targets.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ describe('TARGET_ALIASES + resolveTarget', () => {
157157
expect(resolveTarget('definitely-not-an-agent')).toBeNull();
158158
});
159159

160+
it('resolveTarget rejects inherited prototype-chain keys as unknown', () => {
161+
// Inherited keys (constructor, __proto__, ...) are truthy on a plain object.
162+
for (const token of ['constructor', '__proto__', 'toString', 'hasOwnProperty']) {
163+
expect(resolveTarget(token), token).toBeNull();
164+
}
165+
});
166+
160167
it('acceptedTargetTokens is exactly the ids plus the aliases', () => {
161168
expect(new Set(acceptedTargetTokens())).toEqual(
162169
new Set([...Object.keys(TARGETS), ...Object.keys(TARGET_ALIASES)]),

src/lib/agent-targets.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ export const TARGET_ALIASES: Record<string, AgentTarget> = {
168168
/** Resolve a `--target` token (id or alias) to a canonical id, or null if unknown. */
169169
export function resolveTarget(raw: string): AgentTarget | null {
170170
if (Object.prototype.hasOwnProperty.call(TARGETS, raw)) return raw as AgentTarget;
171-
return TARGET_ALIASES[raw] ?? null;
171+
// hasOwnProperty avoids inherited keys (constructor, __proto__).
172+
return Object.prototype.hasOwnProperty.call(TARGET_ALIASES, raw)
173+
? TARGET_ALIASES[raw]!
174+
: null;
172175
}
173176

174177
/** Every accepted `--target` token (ids + aliases), for help/error text. */

0 commit comments

Comments
 (0)