<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/mitigating-ai-agent-context-injection-in-typescript-gp9jlsd9v" -->

---
title: Mitigating AI Agent Context Injection in TypeScript
description: A TypeScript middleware pattern called ContextIsolator is presented to defend LLM agents against indirect prompt injection (context poisoning), where malicious...
canonical: https://daily.dev/posts/mitigating-ai-agent-context-injection-in-typescript-gp9jlsd9v
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Mitigating AI Agent Context Injection in TypeScript | daily.dev
og:description: A TypeScript middleware pattern called ContextIsolator is presented to defend LLM agents against indirect prompt injection (context poisoning), where malicious...
og:url: https://daily.dev/posts/mitigating-ai-agent-context-injection-in-typescript-gp9jlsd9v
og:image: https://api.daily.dev/og/posts/Gp9JlSd9V.png
og:image:alt: Mitigating AI Agent Context Injection in TypeScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Mitigating AI Agent Context Injection in TypeScript

**[SitePoint](https://daily.dev/sources/sitepoint)** · 19 min read · 0 upvotes · 0 comments

## Summary

A TypeScript middleware pattern called ContextIsolator is presented to defend LLM agents against indirect prompt injection (context poisoning), where malicious instructions hidden in retrieved web pages, API responses, or documents get concatenated into an agent's context window alongside trusted system prompts. The approach signs every context frame (system, user, tool-output) with HMAC-SHA256 over its role, source, timestamp, and content, freezes the signed object, and verifies all frames with timing-safe comparison before assembling the final prompt, rejecting the whole prompt if any frame fails. The implementation uses only Node.js's built-in crypto module, adds sub-millisecond overhead, and includes defense-in-depth regex sanitization for tool outputs. It explicitly does not detect adversarial content within legitimately signed frames (the actual injection vector), and must be paired with output validation, least-privilege tool permissions, and LLM-based secondary review.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.sitepoint.com/preventing-ai-agent-context-poisoning-with-typescript-hmac-middleware>

## Questions this post answers

### How can I prevent indirect prompt injection in a TypeScript LLM agent using HMAC signing?

Sign each context frame (system, user, tool-output) with HMAC-SHA256 computed over a length-prefixed concatenation of role, source, timestamp, and content, then verify every frame's HMAC with timing-safe comparison before assembling the prompt, rejecting the whole assembly if any frame fails. Use Node.js's built-in crypto module (randomBytes, createHmac, timingSafeEqual) with a secret of at least 32 characters and no external dependencies.

_daily.dev surfaces implementation patterns like this for developers hardening agent pipelines against injected data._

### Does HMAC frame signing protect an AI agent against a tool that retrieves a webpage already containing injected instructions?

No, HMAC verification only detects tampering after a frame is signed; it does not catch adversarial content within data that was legitimately retrieved and signed as-is. If a tool fetches a webpage that already contains hidden injected instructions, addFrame signs that poisoned content and produces a valid HMAC, so verification passes. Defending against this requires content filtering, output validation, and LLM-based secondary review alongside HMAC integrity checks.

_Developers weighing agent security layers can compare approaches like this on daily.dev before choosing a defense stack._

### Why use HMAC instead of encryption or text delimiters to isolate context frames in an LLM agent?

HMAC gives integrity verification without the unnecessary overhead of full encryption, since signing and verification both happen within the same server-side trust domain and confidentiality isn't the goal. Text delimiters like ===SYSTEM=== are trivially spoofed because an attacker can embed identical delimiters in poisoned content, while HMAC-SHA256 computes in microseconds per frame and requires a 2^256 brute-force search to forge.

_daily.dev helps engineers evaluating security trade-offs like HMAC versus delimiters stay grounded in concrete comparisons._

## Similar posts on daily.dev

- [Agent-as-a-Tool: A New Era of AI Orchestration](https://daily.dev/posts/agent-as-a-tool-a-new-era-of-ai-orchestration-vxph42s1s) · DEV · 2 upvotes · 0 comments
- [Security for AI Agents: Prompt Injection Is Only the Beginning](https://daily.dev/posts/security-for-ai-agents-prompt-injection-is-only-the-beginning-4pgyeh5xl) · Medium · 2 upvotes · 1 comments
- [Agentic AI Security: Defending Against Prompt Injection and Tool Misuse](https://daily.dev/posts/agentic-ai-security-defending-against-prompt-injection-and-tool-misuse-8x1r1ytqn) · Machine Learning Mastery · 1 upvotes · 0 comments

---

Tags: [#ai-agents](https://daily.dev/tags/ai-agents), [#nodejs](https://daily.dev/tags/nodejs), [#typescript](https://daily.dev/tags/typescript), [#prompt-injection](https://daily.dev/tags/prompt-injection)

[View this post on daily.dev](https://daily.dev/posts/mitigating-ai-agent-context-injection-in-typescript-gp9jlsd9v)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Mitigating AI Agent Context Injection in TypeScript","url":"https://daily.dev/posts/mitigating-ai-agent-context-injection-in-typescript-gp9jlsd9v","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/mitigating-ai-agent-context-injection-in-typescript-gp9jlsd9v"},"datePublished":"2026-09-14T21:12:13.811Z","dateModified":"2026-09-14T22:01:22.363Z","description":"A TypeScript middleware pattern called ContextIsolator is presented to defend LLM agents against indirect prompt injection (context poisoning), where malicious...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/047c3287f03f6dff1490c8fae08b6c58?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/047c3287f03f6dff1490c8fae08b6c58?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"SitePoint","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SitePoint","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/sitepoint","url":"https://daily.dev/sources/sitepoint"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/mitigating-ai-agent-context-injection-in-typescript-gp9jlsd9v","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"ai-agents,nodejs,typescript,prompt-injection","timeRequired":"PT19M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SitePoint","item":"https://daily.dev/sources/sitepoint"},{"@type":"ListItem","position":3,"name":"Mitigating AI Agent Context Injection in TypeScript"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/mitigating-ai-agent-context-injection-in-typescript-gp9jlsd9v#faq","mainEntity":[{"@type":"Question","name":"How can I prevent indirect prompt injection in a TypeScript LLM agent using HMAC signing?","acceptedAnswer":{"@type":"Answer","text":"Sign each context frame (system, user, tool-output) with HMAC-SHA256 computed over a length-prefixed concatenation of role, source, timestamp, and content, then verify every frame's HMAC with timing-safe comparison before assembling the prompt, rejecting the whole assembly if any frame fails. Use Node.js's built-in crypto module (randomBytes, createHmac, timingSafeEqual) with a secret of at least 32 characters and no external dependencies. daily.dev surfaces implementation patterns like this for developers hardening agent pipelines against injected data."}},{"@type":"Question","name":"Does HMAC frame signing protect an AI agent against a tool that retrieves a webpage already containing injected instructions?","acceptedAnswer":{"@type":"Answer","text":"No, HMAC verification only detects tampering after a frame is signed; it does not catch adversarial content within data that was legitimately retrieved and signed as-is. If a tool fetches a webpage that already contains hidden injected instructions, addFrame signs that poisoned content and produces a valid HMAC, so verification passes. Defending against this requires content filtering, output validation, and LLM-based secondary review alongside HMAC integrity checks. Developers weighing agent security layers can compare approaches like this on daily.dev before choosing a defense stack."}},{"@type":"Question","name":"Why use HMAC instead of encryption or text delimiters to isolate context frames in an LLM agent?","acceptedAnswer":{"@type":"Answer","text":"HMAC gives integrity verification without the unnecessary overhead of full encryption, since signing and verification both happen within the same server-side trust domain and confidentiality isn't the goal. Text delimiters like ===SYSTEM=== are trivially spoofed because an attacker can embed identical delimiters in poisoned content, while HMAC-SHA256 computes in microseconds per frame and requires a 2^256 brute-force search to forge. daily.dev helps engineers evaluating security trade-offs like HMAC versus delimiters stay grounded in concrete comparisons."}}]}
```

