Skip to content
Open
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
10 changes: 6 additions & 4 deletions workers/lib/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ Return ONLY "NO" if it is a normal email (even if angry, confused, or containing

Respond with exactly one word: YES or NO.`;

const PROMPT_INJECTION_MODEL = "@cf/zai-org/glm-4.7-flash";

export async function isPromptInjection(ai: Ai, bodyHtml: string | null | undefined): Promise<boolean> {
if (!bodyHtml) return false;

const plainText = stripHtmlToText(bodyHtml).trim();
if (plainText.length < 10) return false;

try {
const response = (await ai.run(
// @ts-expect-error — model string not in generated union
"@cf/meta/llama-3.1-8b-instruct-fast",
PROMPT_INJECTION_MODEL,
{
messages: [
{ role: "system", content: INJECTION_PROMPT },
Expand All @@ -42,12 +44,12 @@ export async function isPromptInjection(ai: Ai, bodyHtml: string | null | undefi
)) as { response?: string };

const result = (response?.response || "NO").trim().toUpperCase();

if (result.includes("YES")) {
console.warn("Prompt injection detected in incoming email, blocking auto-draft");
return true;
}

return false;
} catch (e) {
console.error("Prompt injection scanner failed, skipping auto-draft:", (e as Error).message);
Expand Down