Path: redb-route/redb.Route.Llm/Engine/ToolHeaderPolicy.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm/Engine/ToolHeaderPolicy.cs
----
namespace redb.Route.Llm.Engine;
///
/// Decides which headers of the agent's exchange are visible to a tool route.
///
/// The policy is default-deny: a tool never sees the raw inbound header set
/// (an HTTP consumer's Authorization / Cookie must not leak into every
/// tool call just because the tool is mounted on the same host). Only
/// , the run's resolved principal / audit tags, and the
/// names the route author opted into via
/// AgentRequest.PropagateToolHeaders are copied.
///
///
/// Principal and audit tags are propagated as resolved values, not as raw headers.
/// LlmProducer resolves the principal from the ?user= option first
/// (including its ${header.X} expression form) and only falls back to the
/// llm.user.id header; copying the header alone would silently drop the
/// option-driven case. Same for ?audit= tags. The raw headers are used as a
/// fallback so an engine driven without LlmProducer still propagates them.
///
///
/// Trust model: llm.user.id and llm.audit.* are read off the
/// inbound exchange by the producer, so a transport that forwards client headers
/// verbatim (a plain HTTP consumer) lets the caller set them. That is already true
/// for what the engine persists in MessageProps.UserId; propagating the same
/// value to tools does not widen it. The route — not this policy — is responsible
/// for stripping or overwriting client-supplied llm.* headers before the
/// llm:// hop when tools make access decisions on them.
///
///
public static class ToolHeaderPolicy
{
///
/// Headers copied from the agent exchange to every tool call without opt-in:
/// the conversation id and the two correlation-id spellings the framework
/// already treats as ambient.
///
public static readonly IReadOnlyList DefaultHeaders =
[
LlmHeaders.ConversationId,
"X-Correlation-Id",
"CorrelationId"
];
///
/// Fills with the headers a tool route is allowed to see
/// for this run. Existing entries in (the engine's own
/// llm.tool.* stamps) are never overwritten by propagation.
///
/// The agent run — source of the parent exchange, resolved principal, audit tags and opt-in list.
/// Header dictionary of the tool message being built.
public static void Apply(AgentRequest request, IDictionary target)
{
ArgumentNullException.ThrowIfNull(request);
ArgumentNullException.ThrowIfNull(target);
var source = request.Exchange.In?.Headers;
foreach (var name in DefaultHeaders)
Copy(source, target, name);
// Principal: resolved value wins over the raw header (see class remarks).
if (!string.IsNullOrEmpty(request.UserId))
target[LlmHeaders.UserId] = request.UserId;
else
Copy(source, target, LlmHeaders.UserId);
// Audit tags: same rule — resolved snapshot first, raw llm.audit.* as fallback.
if (request.AuditTags is { Count: > 0 })
{
foreach (var tag in request.AuditTags)
target[LlmHeaders.AuditTagPrefix + tag.Key] = tag.Value;
}
else
{
CopyByPrefix(source, target, LlmHeaders.AuditTagPrefix);
}
if (request.PropagateToolHeaders is not { Count: > 0 } optIn) return;
foreach (var pattern in optIn)
{
if (string.IsNullOrWhiteSpace(pattern)) continue;
if (pattern.Length > 1 && pattern[^1] == '*')
CopyByPrefix(source, target, pattern[..^1]);
else
Copy(source, target, pattern);
}
}
private static void Copy(IDictionary? from, IDictionary to, string key)
{
if (from is null) return;
if (from.TryGetValue(key, out var v) && v is not null)
to[key] = v;
}
private static void CopyByPrefix(IDictionary? from, IDictionary to, string prefix)
{
if (from is null || prefix.Length == 0) return;
foreach (var kv in from)
{
if (kv.Value is null) continue;
if (!kv.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) continue;
to[kv.Key] = kv.Value;
}
}
///
/// Parses the CSV form used by the endpoint URI
/// (?propagateToolHeaders=x-tenant-id,x-app-*) into a name list.
/// Returns null for null/empty input so callers can leave the field unset.
///
/// Comma-separated header names; a trailing * marks a prefix match.
public static IReadOnlyList? ParseCsv(string? csv)
{
if (string.IsNullOrWhiteSpace(csv)) return null;
var names = new List();
foreach (var raw in csv.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
var name = System.Web.HttpUtility.UrlDecode(raw).Trim();
if (name.Length > 0) names.Add(name);
}
return names.Count == 0 ? null : names;
}
}