-
Notifications
You must be signed in to change notification settings - Fork 1
Initial commit: Add agent.ts #2
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
base: master
Are you sure you want to change the base?
Conversation
| text: msg.content, | ||
| }, | ||
| ], | ||
| }; | ||
| } else { | ||
| // User messages use input_text type | ||
| return { | ||
| role: msg.role, | ||
| content: [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new multi-turn conversation support only runs guardrails on the last user message, but passes the full conversation history (including unchecked earlier messages) to all agents. An attacker can inject malicious prompts in earlier messages that will bypass jailbreak detection and prompt injection guardrails.
Suggested Fix
Run guardrails on ALL user messages in the conversation history, not just the last one:
// Extract ALL user messages for guardrails check
const userMessages = conversationHistory
.filter((m) => "role" in m && m.role === "user")
.map((m) => {
if ("content" in m && Array.isArray(m.content) && m.content[0] && "text" in m.content[0]) {
return m.content[0].text;
}
return "";
})
.filter((text) => text.length > 0);
// Check each user message through guardrails
for (const messageText of userMessages) {
const guardrailsResult = await runGuardrails(
messageText,
jailbreakGuardrailConfig,
context
);
if (guardrailsHasTripwire(guardrailsResult)) {
const guardrailsOutput = buildGuardrailFailOutput(guardrailsResult ?? []);
return { ...guardrailsOutput, tracking };
}
}| result.metadata.usage.promptTokens || | ||
| usage.promptTokens; | ||
| usage.completionTokens = | ||
| result.metadata.usage.completion_tokens || | ||
| result.metadata.usage.completionTokens || | ||
| usage.completionTokens; | ||
| usage.totalTokens = | ||
| result.metadata.usage.total_tokens || | ||
| result.metadata.usage.totalTokens || | ||
| usage.totalTokens; | ||
| } | ||
|
|
||
| // Estimate tokens based on text length if no usage data | ||
| if (usage.totalTokens === 0 && result.finalOutput) { | ||
| const outputLength = | ||
| typeof result.finalOutput === "string" | ||
| ? result.finalOutput.length | ||
| : JSON.stringify(result.finalOutput).length; | ||
| usage.completionTokens = Math.ceil(outputLength / 4); | ||
| usage.promptTokens = 50; // Rough estimate for prompt | ||
| usage.totalTokens = usage.promptTokens + usage.completionTokens; | ||
| } | ||
|
|
||
| // If total not provided, calculate it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new message array input has no validation on array length or individual message sizes. Attackers can submit extremely large message arrays or very long messages, causing excessive OpenAI API costs and potential service degradation through token-based denial of service.
Suggested Fix
Add input size limits before processing:
// Define limits
const MAX_MESSAGES = 50;
const MAX_MESSAGE_LENGTH = 10000;
const MAX_TOTAL_LENGTH = 50000;
if (workflow.messages && workflow.messages.length > 0) {
// Validate message count
if (workflow.messages.length > MAX_MESSAGES) {
throw new Error(`Message history exceeds maximum of ${MAX_MESSAGES} messages`);
}
// Validate lengths
let totalLength = 0;
for (const msg of workflow.messages) {
const msgLength = msg.content?.length || 0;
if (msgLength > MAX_MESSAGE_LENGTH) {
throw new Error(`Message exceeds maximum length of ${MAX_MESSAGE_LENGTH} characters`);
}
totalLength += msgLength;
}
if (totalLength > MAX_TOTAL_LENGTH) {
throw new Error(`Total content exceeds maximum of ${MAX_TOTAL_LENGTH} characters`);
}
conversationHistory = convertToAgentInputItems(workflow.messages);
} else if (workflow.input_as_text) {
if (workflow.input_as_text.length > MAX_MESSAGE_LENGTH) {
throw new Error(`Input exceeds maximum length of ${MAX_MESSAGE_LENGTH} characters`);
}
conversationHistory = [
{ role: "user", content: [{ type: "input_text", text: workflow.input_as_text }] },
];
}
No description provided.