Skip to content

feat: expand env references in agent target headers#169

Merged
jithin23-kv merged 2 commits into
KeyValueSoftwareSystems:masterfrom
jithin23-kv:feat/agent-target-header-env-expansion
Jul 6, 2026
Merged

feat: expand env references in agent target headers#169
jithin23-kv merged 2 commits into
KeyValueSoftwareSystems:masterfrom
jithin23-kv:feat/agent-target-header-env-expansion

Conversation

@jithin23-kv

@jithin23-kv jithin23-kv commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Problem

target.headers on agent (HTTP) targets only accepted literal strings, so the
only way to send an auth header (e.g. x-api-key) was to paste the raw secret
into the opfor config file. MCP targets already supported ${VAR} expansion
for urlHeaders — agent targets were the odd one out, and there was no way to
keep secrets out of version-controlled config.

Solution

Added the same ${VAR} expansion agent targets already lacked but MCP targets
had, so header values can reference environment variables instead of hardcoding
secrets.

Changes

  • core/src/targets/agentTarget.ts: added expandEnvInHeaders() and applied it
    to config.headers before merging into the outgoing request (mirrors the
    existing MCP urlHeaders pattern, via the shared getEnv provider so it also
    works for browser-extension targets).
  • docs/cli.md: documented ${VAR} expansion support next to the
    target.headers field description.

Summary by CodeRabbit

  • New Features
    • Header values for HTTP requests now support environment variable placeholders like ${VAR}, allowing dynamic values to be inserted before requests are sent.
  • Documentation
    • Updated the CLI reference to show environment-based header values in examples and clarify placeholder support.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@jithin23-kv, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 37 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 36a61e23-ef92-428e-ba59-24506cb9b2ff

📥 Commits

Reviewing files that changed from the base of the PR and between 262b9b0 and 53b0cba.

📒 Files selected for processing (5)
  • core/src/lib/env.ts
  • core/src/targets/agentTarget.ts
  • core/src/targets/httpClient.ts
  • core/src/targets/mcpTarget.ts
  • runners/cli/src/ui/server.ts

Walkthrough

The HTTP agent target now expands ${VAR} placeholders in configured header values by resolving them via an environment provider before sending requests, falling back to an empty string when unset. Documentation for target.headers is updated to reflect this expansion syntax instead of literal secret values.

Changes

Header expansion

Layer / File(s) Summary
Env expansion helper and wiring
core/src/targets/agentTarget.ts
Adds expandEnvInHeaders helper that replaces ${VAR} tokens in header values via getEnv, and applies it when building HTTP request headers from config.headers.
Header docs update
docs/cli.md
Updates the target.headers reference example and description to use ${TARGET_API_KEY}-style expansion instead of literal secrets.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AgentTarget
  participant expandEnvInHeaders
  participant getEnv
  AgentTarget->>expandEnvInHeaders: config.headers
  expandEnvInHeaders->>getEnv: resolve ${VAR} names
  getEnv-->>expandEnvInHeaders: value or ""
  expandEnvInHeaders-->>AgentTarget: expanded headers
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: expanding env references in agent target headers.
Description check ✅ Passed The description covers Problem, Solution, and Changes with meaningful detail, but it omits Issue, How to test, and Screenshots sections.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
core/src/targets/agentTarget.ts (2)

55-63: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Silent empty-string fallback on missing env var can mask config errors.

When a referenced ${VAR} is undefined, the header value silently becomes an empty string instead of surfacing feedback. A misspelled var name or missing env var will produce a working-looking request with a blank header (e.g., empty x-api-key), causing confusing downstream 401s that are hard to trace back to the config. This exact failure mode (silent empty value → confusing auth failure) is a common recurring issue in similar ${VAR} expansion implementations across other tools.

Consider logging a warning (or throwing, matching the strictness elsewhere in this file) that names the missing variable, so failures are actionable per the error-message guideline.

💡 Proposed fix to warn on unresolved variables
 function expandEnvInHeaders(headers: Record<string, string>): Record<string, string> {
   const out: Record<string, string> = {};
   for (const [k, v] of Object.entries(headers)) {
-    out[k] = v.replace(/\$\{([^}]+)\}/g, (_, name) => getEnv(name.trim()) ?? "");
+    out[k] = v.replace(/\$\{([^}]+)\}/g, (_, name) => {
+      const trimmed = name.trim();
+      const value = getEnv(trimmed);
+      if (value === undefined) {
+        console.warn(`[agentTarget] header "${k}" references undefined env var "${trimmed}"`);
+      }
+      return value ?? "";
+    });
   }
   return out;
 }

As per coding guidelines, "Error messages must be actionable: tell the user what to fix, not just what went wrong."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core/src/targets/agentTarget.ts` around lines 55 - 63, The expandEnvInHeaders
helper is silently replacing unresolved ${VAR} references with an empty string,
which can hide missing or misspelled env vars. Update expandEnvInHeaders to
either warn or throw when getEnv(name.trim()) returns undefined, and include the
missing variable name in the message so the issue is actionable. Keep the
behavior consistent with the strictness used elsewhere in agentTarget, and make
sure the fix is applied in the header expansion path rather than at individual
call sites.

Source: Coding guidelines


55-63: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Share the ${VAR} header expansion helper core/src/targets/agentTarget.ts and core/src/targets/mcpTarget.ts both implement the same regex/replace logic. Move it into core/src/lib/env.ts so header/env expansion stays consistent in one place.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core/src/targets/agentTarget.ts` around lines 55 - 63, The `${VAR}` header
expansion logic is duplicated in the agent and MCP target code, so centralize it
in the shared env utilities. Move the helper currently named expandEnvInHeaders
into core/src/lib/env.ts, keep its behavior unchanged, and update the callers in
AgentTarget and the MCP target to use the shared function so both targets rely
on the same implementation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@core/src/targets/agentTarget.ts`:
- Around line 55-63: The expandEnvInHeaders helper is silently replacing
unresolved ${VAR} references with an empty string, which can hide missing or
misspelled env vars. Update expandEnvInHeaders to either warn or throw when
getEnv(name.trim()) returns undefined, and include the missing variable name in
the message so the issue is actionable. Keep the behavior consistent with the
strictness used elsewhere in agentTarget, and make sure the fix is applied in
the header expansion path rather than at individual call sites.
- Around line 55-63: The `${VAR}` header expansion logic is duplicated in the
agent and MCP target code, so centralize it in the shared env utilities. Move
the helper currently named expandEnvInHeaders into core/src/lib/env.ts, keep its
behavior unchanged, and update the callers in AgentTarget and the MCP target to
use the shared function so both targets rely on the same implementation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5af9a9f1-541a-4363-977e-38844a8ac52e

📥 Commits

Reviewing files that changed from the base of the PR and between 121edfd and 262b9b0.

📒 Files selected for processing (2)
  • core/src/targets/agentTarget.ts
  • docs/cli.md

@jithin23-kv

Copy link
Copy Markdown
Collaborator Author

@CodeRabbit review

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@jithin23-kv jithin23-kv merged commit 605a9b5 into KeyValueSoftwareSystems:master Jul 6, 2026
8 checks passed
@jithin23-kv jithin23-kv deleted the feat/agent-target-header-env-expansion branch July 6, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants