This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: otel-tracestate-hmac-provenance-invariant

Cryptographic HMAC Provenance Verification for OTel TraceState and Baggage

Enforces cryptographic HMAC signing and strict provenance verification for W3C TraceState and Baggage headers at untrusted network perimeters. Prevents trace context spoofing, cross-tenant telemetry contamination, and privilege escalation via injected context baggage across service boundaries. Ensures distributed trace contexts are authenticated before downstream propagation or authorization decisions.

Prohibited Anti-Pattern: Blind Propagation of Untrusted TraceState and Baggage Ingress Headers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Cryptographic HMAC Provenance Verification for OTel TraceState and Baggage - Enforces cryptographic HMAC signing and strict provenance verification for W3C TraceState and Baggage headers at untrusted network perimeters. Prevents trace context spoofing, cross-tenant telemetry contamination, and privilege escalation via injected context baggage across service boundaries. Ensures distributed trace contexts are authenticated before downstream propagation or authorization decisions.
globs: src/middleware/**/*.{ts,js}, src/tracing/**/*.{ts,js}, src/server/**/*.{ts,js}, gateway/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-tracestate-hmac-provenance-invariant"
---

# Cryptographic HMAC Provenance Verification for OTel TraceState and Baggage

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Blind Propagation of Untrusted TraceState and Baggage Ingress Headers

## System Constraints
You are a Principal Security and Telemetry Architect enforcing strict cryptographic provenance on OpenTelemetry W3C distributed trace context headers. Untrusted clients and ingress boundaries routinely attempt to inject spoofed baggage items (e.g., tenant_id, role, internal_routing) and forged tracestate tokens. You must ensure that all incoming trace context across untrusted boundaries either has its cryptographic HMAC verified using constant-time comparison or is stripped and re-anchored. Never allow unverified baggage to populate authorization context, multi-tenant scoping, or internal telemetry state.

## Strict Guidelines
- Edge gateways and ingress middlewares MUST NEVER trust or propagate raw inbound W3C `baggage` headers from untrusted network zones without cryptographically validating an accompanying HMAC signature.
- Ingress services MUST verify the HMAC token in the `tracestate` header using a constant-time comparison algorithm (`crypto.timingSafeEqual`) to prevent timing side-channel vulnerabilities.
- When HMAC verification fails or is absent at an untrusted perimeter, services MUST drop the tainted `tracestate` and `baggage` items, generate a new root span context, and log a security audit event.
- Internal microservices MUST sign the canonical serialization of `(trace_id, span_id, tenant_id, timestamp)` before mutating or forwarding `tracestate` to downstream services.
- Services MUST NOT read sensitive authorization attributes, user roles, or tenant bindings directly from OpenTelemetry baggage without independent cryptographically signed proof.

## Bad vs Good Implementations
### Untrusted Baggage Extraction vs. Cryptographic HMAC Ingress Guardrail

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The middleware extracts external headers directly using standard OTel propagation without authenticating the source. An attacker can forge tenant identifiers, authorization levels, or routing metadata inside the W3C Baggage and TraceState headers, escalating privileges and corrupting downstream traces.
```typescript
import { Request, Response, NextFunction } from 'express';
import { propagation, context } from '@opentelemetry/api';

export function ingressTraceContextMiddleware(req: Request, res: Response, next: NextFunction): void {
  // VULNERABILITY: Directly extracts unverified traceparent and baggage from untrusted client
  // An attacker can inject 'baggage: tenantId=corp-victim,role=superadmin'
  const activeContext = propagation.extract(context.active(), req.headers);
  context.with(activeContext, () => {
    // Application code later treats extracted baggage as authoritative tenant context
    next();
  });
}
```

#### Production Standard (Correct)
> **Standard**: Validates the cryptographic HMAC signature embedded in the W3C TraceState header before accepting inbound context. If the signature is missing or fails constant-time verification, the middleware safely discards the tainted trace context and baggage, preventing spoofing and privilege escalation.
```typescript
import { Request, Response, NextFunction } from 'express';
import { propagation, context, ROOT_CONTEXT, trace } from '@opentelemetry/api';
import { createHmac, timingSafeEqual } from 'crypto';

const TRACESTATE_KEY_PREFIX = 'acme_sig';
const HMAC_SECRET = process.env.OTEL_TRACESTATE_HMAC_SECRET!;

function verifyTraceStateProvenance(traceId: string, tenantId: string, signatureHex: string): boolean {
  if (!signatureHex || !traceId || !tenantId) return false;
  const expected = createHmac('sha256', HMAC_SECRET)
    .update(`${traceId}:${tenantId}`)
    .digest();
  const actual = Buffer.from(signatureHex, 'hex');
  return actual.length === expected.length && timingSafeEqual(actual, expected);
}

export function secureIngressTraceMiddleware(req: Request, res: Response, next: NextFunction): void {
  const rawTraceParent = req.headers['traceparent'] as string | undefined;
  const rawTraceState = req.headers['tracestate'] as string | undefined;
  const untrustedTenant = req.headers['x-tenant-id'] as string | undefined;

  // Extract HMAC signature token from W3C tracestate (vendor key format: acme_sig=<hex>)
  const sigMatch = rawTraceState ? new RegExp(`${TRACESTATE_KEY_PREFIX}=([^,;]+)`).exec(rawTraceState) : null;
  const providedSig = sigMatch ? sigMatch[1] : '';
  
  let activeCtx = ROOT_CONTEXT;

  if (rawTraceParent && untrustedTenant && verifyTraceStateProvenance(rawTraceParent.split('-')[1], untrustedTenant, providedSig)) {
    // Provenance cryptographically verified: extract context safely
    activeCtx = propagation.extract(ROOT_CONTEXT, req.headers);
  } else {
    // Provenance failed or absent: purge untrusted baggage & isolate downstream trace context
    const newSpan = trace.getTracer('ingress-gate').startSpan('untrusted-ingress-boundary');
    activeCtx = trace.setSpan(ROOT_CONTEXT, newSpan);
    // Explicitly sanitise inbound baggage
    req.headers['baggage'] = undefined;
  }

  context.with(activeCtx, () => next());
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Cryptographic HMAC Provenance Verification for OTel TraceState and Baggage

Cryptographic HMAC Provenance Verification for OTel TraceState and Baggage defines an industrial-grade behavioral standard for devops engineering. Enforces cryptographic HMAC signing and strict provenance verification for W3C TraceState and Baggage headers at untrusted network perimeters. Prevents trace context spoofing, cross-tenant telemetry contamination, and privilege escalation via injected context baggage across service boundaries. Ensures distributed trace contexts are authenticated before downstream propagation or authorization decisions. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Blind Propagation of Untrusted TraceState and Baggage Ingress Headers Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftBlind Propagation of Untrusted TraceState and Baggage Ingress HeadersCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Edge gateways and ingress middlewares MUST NEVER trust or propagate raw inbound W3C `baggage` headers from untrusted network zones without cryptographically validating an accompanying HMAC signature.

Edge gateways and ingress middlewares MUST NEVER trust or propagate raw inbound W3C `baggage` headers from untrusted network zones without cryptographically validating an accompanying HMAC signature.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Ingress services MUST verify the HMAC token in the `tracestate` header using a constant-time comparison algorithm (`crypto.timingSafeEqual`) to prevent timing side-channel vulnerabilities.

Ingress services MUST verify the HMAC token in the `tracestate` header using a constant-time comparison algorithm (`crypto.timingSafeEqual`) to prevent timing side-channel vulnerabilities.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

When HMAC verification fails or is absent at an untrusted perimeter, services MUST drop the tainted `tracestate` and `baggage` items, generate a new root span context, and log a security audit event.

When HMAC verification fails or is absent at an untrusted perimeter, services MUST drop the tainted `tracestate` and `baggage` items, generate a new root span context, and log a security audit event.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Internal microservices MUST sign the canonical serialization of `(trace_id, span_id, tenant_id, timestamp)` before mutating or forwarding `tracestate` to downstream services.

Internal microservices MUST sign the canonical serialization of `(trace_id, span_id, tenant_id, timestamp)` before mutating or forwarding `tracestate` to downstream services.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Services MUST NOT read sensitive authorization attributes, user roles, or tenant bindings directly from OpenTelemetry baggage without independent cryptographically signed proof.

Services MUST NOT read sensitive authorization attributes, user roles, or tenant bindings directly from OpenTelemetry baggage without independent cryptographically signed proof.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Cryptographic HMAC Provenance Verification for OTel TraceState and Baggage critical for AI coding assistants?

Enforces cryptographic HMAC signing and strict provenance verification for W3C TraceState and Baggage headers at untrusted network perimeters. Prevents trace context spoofing, cross-tenant telemetry contamination, and privilege escalation via injected context baggage across service boundaries. Ensures distributed trace contexts are authenticated before downstream propagation or authorization decisions. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Blind Propagation of Untrusted TraceState and Baggage Ingress Headers) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Cryptographic