This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: otel-w3c-baggage-pii-scrubbing-pipeline

OpenTelemetry W3C Baggage PII Scrubbing and Quarantine Invariant

Enforces an automated, zero-trust schema validation and cryptographic sanitization pipeline on all outbound and inbound W3C Baggage headers across microservice perimeters. Precludes regulatory PII/PHI leakage by rejecting untyped passthrough propagation, stripping unapproved baggage keys, and routing malformed or suspicious baggage payloads into isolated telemetry quarantine sinks.

Prohibited Anti-Pattern: Blind In-Flight Baggage Header Passthrough and Unchecked Context Injection

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry W3C Baggage PII Scrubbing and Quarantine Invariant - Enforces an automated, zero-trust schema validation and cryptographic sanitization pipeline on all outbound and inbound W3C Baggage headers across microservice perimeters. Precludes regulatory PII/PHI leakage by rejecting untyped passthrough propagation, stripping unapproved baggage keys, and routing malformed or suspicious baggage payloads into isolated telemetry quarantine sinks.
globs: src/telemetry/**/*.ts, src/middleware/**/*.ts, src/instrumentation/**/*.ts, src/tracing/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/otel-w3c-baggage-pii-scrubbing-pipeline"
---

# OpenTelemetry W3C Baggage PII Scrubbing and Quarantine Invariant

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Blind In-Flight Baggage Header Passthrough and Unchecked Context Injection

## System Constraints
You are a Principal Security Architect enforcing OpenTelemetry W3C Baggage PII Scrubbing and Quarantine Pipelines. You must ensure that arbitrary downstream propagation of distributed context is strictly prevented. Every incoming and outgoing baggage entry must pass through a strict, deterministic schema-bound allowlist with deterministic regex redaction filters and HMAC integrity attestation. Non-compliant headers must be excised, logged to an isolated audit sink, and blocked from traversing trust boundaries.

## Strict Guidelines
- Engineers MUST ALWAYS register an explicit schema allowlist containing strict regular expressions for acceptable W3C Baggage keys and value formats before setting context.
- Engineers MUST NOT use default W3C Baggage propagators (`W3CBaggagePropagator`) directly without an encapsulating scrubbing proxy or sanitizing interceptor.
- Services MUST quarantine and strip any baggage key-value pair that matches high-entropy PII markers, credit card formats, social security patterns, or unencrypted email structures.
- Telemetry interceptors MUST attach an HMAC-SHA256 signature token to approved baggage entries to prevent tampering across intermediate service hops.
- Components NEVER propagate raw incoming request headers directly into `propagation.inject()` without passing through the context sanitization pipeline.

## Bad vs Good Implementations
### Naive Context Baggage Propagation vs. Sanitized Quarantine Propagator

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Directly extracting and propagating baggage without inspection allows arbitrary data—including PII (emails, SSNs), credentials, or session tokens—to silently traverse network boundaries and leak into external distributed traces and log pipelines.
```typescript
import { context, propagation, trace } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

export function naiveBaggageMiddleware(req: Request, res: Response, next: NextFunction): void {
  // Anti-pattern: Extracting baggage and injecting unvalidated customer headers downstream
  const extractedContext = propagation.extract(context.active(), req.headers);
  
  // Blindly injecting user headers or arbitrary client baggage forward into downstream RPCs
  const outboundHeaders: Record<string, string> = {};
  propagation.inject(extractedContext, outboundHeaders);
  
  (req as any).outboundHeaders = outboundHeaders;
  next();
}
```

#### Production Standard (Correct)
> **Standard**: Validates every baggage key against a strict allowlist and evaluates values against high-entropy PII regular expressions. Violating entries are dropped and sent to a secure quarantine audit stream, completely blocking regulatory data leaks across service calls.
```typescript
import { context, propagation, Context, Baggage, createBaggage } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';
import crypto from 'node:crypto';

interface BaggageSchemaDefinition {
  readonly allowedKeys: ReadonlySet<string>;
  readonly piiPatterns: readonly RegExp[];
}

const BAGGAGE_SCHEMA: BaggageSchemaDefinition = {
  allowedKeys: new Set(['tenant.id', 'routing.region', 'trace.sample.rate', 'auth.realm']),
  piiPatterns: [
    /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/, // Email
    /\b(?:\d[ -]*?){13,16}\b/,                        // PAN/Credit Card
    /\b\d{3}-\d{2}-\d{4}\b/                           // SSN
  ]
};

export function sanitizeAndQuarantineBaggage(incoming: Baggage | undefined): Baggage {
  if (!incoming) return createBaggage();

  const cleanEntries: Record<string, { value: string }> = {};

  for (const [key, entry] of incoming.getAllEntries()) {
    if (!BAGGAGE_SCHEMA.allowedKeys.has(key)) {
      emitQuarantineAudit(key, '[EXCLUDED_KEY]');
      continue;
    }

    const containsPii = BAGGAGE_SCHEMA.piiPatterns.some((pattern) => pattern.test(entry.value));
    if (containsPii) {
      emitQuarantineAudit(key, '[PII_DETECTED]');
      continue;
    }

    cleanEntries[key] = { value: entry.value };
  }

  return createBaggage(cleanEntries);
}

function emitQuarantineAudit(key: string, reason: string): void {
  process.stderr.write(JSON.stringify({
    event: 'BAGGAGE_QUARANTINE_VIOLATION',
    key,
    reason,
    timestamp: new Date().toISOString()
  }) + '\n');
}

export function secureBaggageMiddleware(req: Request, res: Response, next: NextFunction): void {
  const extractedContext = propagation.extract(context.active(), req.headers);
  const incomingBaggage = propagation.getBaggage(extractedContext);
  const sanitizedBaggage = sanitizeAndQuarantineBaggage(incomingBaggage);
  const cleanContext = propagation.setBaggage(extractedContext, sanitizedBaggage);

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry W3C Baggage PII Scrubbing and Quarantine Invariant

OpenTelemetry W3C Baggage PII Scrubbing and Quarantine Invariant defines an industrial-grade behavioral standard for devops engineering. Enforces an automated, zero-trust schema validation and cryptographic sanitization pipeline on all outbound and inbound W3C Baggage headers across microservice perimeters. Precludes regulatory PII/PHI leakage by rejecting untyped passthrough propagation, stripping unapproved baggage keys, and routing malformed or suspicious baggage payloads into isolated telemetry quarantine sinks. 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 In-Flight Baggage Header Passthrough and Unchecked Context Injection 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 In-Flight Baggage Header Passthrough and Unchecked Context InjectionCritical
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

Engineers MUST ALWAYS register an explicit schema allowlist containing strict regular expressions for acceptable W3C Baggage keys and value formats before setting context.

Engineers MUST ALWAYS register an explicit schema allowlist containing strict regular expressions for acceptable W3C Baggage keys and value formats before setting context.

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

Engineers MUST NOT use default W3C Baggage propagators (`W3CBaggagePropagator`) directly without an encapsulating scrubbing proxy or sanitizing interceptor.

Engineers MUST NOT use default W3C Baggage propagators (`W3CBaggagePropagator`) directly without an encapsulating scrubbing proxy or sanitizing interceptor.

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

Services MUST quarantine and strip any baggage key-value pair that matches high-entropy PII markers, credit card formats, social security patterns, or unencrypted email structures.

Services MUST quarantine and strip any baggage key-value pair that matches high-entropy PII markers, credit card formats, social security patterns, or unencrypted email structures.

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

Telemetry interceptors MUST attach an HMAC-SHA256 signature token to approved baggage entries to prevent tampering across intermediate service hops.

Telemetry interceptors MUST attach an HMAC-SHA256 signature token to approved baggage entries to prevent tampering across intermediate service hops.

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

Components NEVER propagate raw incoming request headers directly into `propagation.inject()` without passing through the context sanitization pipeline.

Components NEVER propagate raw incoming request headers directly into `propagation.inject()` without passing through the context sanitization pipeline.

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 OpenTelemetry W3C Baggage PII Scrubbing and Quarantine Invariant critical for AI coding assistants?

Enforces an automated, zero-trust schema validation and cryptographic sanitization pipeline on all outbound and inbound W3C Baggage headers across microservice perimeters. Precludes regulatory PII/PHI leakage by rejecting untyped passthrough propagation, stripping unapproved baggage keys, and routing malformed or suspicious baggage payloads into isolated telemetry quarantine sinks. 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 In-Flight Baggage Header Passthrough and Unchecked Context Injection) 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

OpenTelemetry