Skip to content

Allow overriding the API token with DEV_PROXY_API_TOKEN - #2010

Merged
waldekmastykarz merged 4 commits into
mainfrom
api-token-env-var-override
Oct 27, 2026
Merged

waldekmastykarz merged 4 commits into
mainfrom
api-token-env-var-override

Conversation

@waldekmastykarz

Copy link
Copy Markdown
Collaborator

Motivated by dev-proxy-tools/aspire#55, which adds Aspire dashboard commands that start/stop recording through the Dev Proxy API. Today the API token is randomly generated at startup, so the AppHost had to read credentials/api-<pid>.token files, which is fragile (Windows paths, stale/shared tokens, container bind-mount permissions). With this change the AppHost generates a token and passes it to the devproxy executable or container through an environment variable.

Changes

  • New DEV_PROXY_API_TOKEN env var (named like DEV_PROXY_CERT_PATH). When it is set and non-empty, Dev Proxy uses it as the API token. Otherwise it generates a random token as before.
  • The value is trimmed. It must be at least 32 characters and use only RFC 6750 bearer-token characters (A-Z a-z 0-9 - . _ ~ + / plus trailing =). An invalid value stops startup (root, detached and stdio) with a clear error and exit code 1. Other commands such as status and stop are not affected.
  • The token is still saved to the per-instance token file, so devproxy status, devproxy api token and devproxy stop keep working. Token display and logging are unchanged.
  • Authentication still uses the existing constant-time comparison.
  • Updated the root command help text and skills/dev-proxy/references/configuration.md.
  • Added ApiSecurityTokenTests covering the generated fallback, override, trimming, too-short values and invalid characters.

Validation

  • dotnet build DevProxy.sln -warnaserror passes, and DevProxy.Tests passes (126 tests).
  • Manual check: DEV_PROXY_API_TOKEN=short devproxy --detach ... exits with DEV_PROXY_API_TOKEN must be at least 32 characters long. With a valid 64-character hex token, the API returns 200 for that token and 401 for a different one. devproxy api token returns the override and devproxy stop works.
Lets orchestrators such as the Aspire integration start Dev Proxy with a
known API token instead of reading per-process token files. Values are
trimmed, must be at least 32 characters, and must be valid Bearer token
characters; invalid values stop startup with a clear error.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@waldekmastykarz
waldekmastykarz requested a review from a team as a code owner October 27, 2026 12:00
Copilot AI balanced review requested due to automatic review settings October 27, 2026 12:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Startup validation can block non-startup commands and checks the inherited token before configuration and CLI environment overrides are applied.

2 open issues
What changed in this PR

Adds a configurable API token so orchestrators such as Aspire can authenticate with Dev Proxy without reading per-process credential files.

Changes:

  • Adds DEV_PROXY_API_TOKEN resolution, trimming, and validation while retaining random-token fallback.
  • Adds startup validation and token-resolution unit tests.
  • Documents the override in CLI help and configuration guidance.
File Description
skills/​dev-proxy/​references/​configuration.md Documents token requirements and retrieval behavior.
DevProxy/​Program.cs Adds early token validation for proxy startup.
DevProxy/​Commands/​DevProxyCommand.cs Adds the environment override to root help.
DevProxy/​ApiSecurity.cs Resolves and validates the token lazily.
DevProxy.Tests/​ApiSecurityTokenTests.cs Tests fallback, overrides, trimming, and invalid values.

Review effort: Balanced

Comment thread DevProxy/Program.cs Resolved
Comment thread DevProxy/Program.cs Resolved
- Restore CRLF line endings in Program.cs and DevProxyCommand.cs
- Resolve and validate the token in the root command after config env and
  --env overrides are applied, logging a clean error and exiting with 1
- Limit the early check to the detached parent, honoring its --env value
  and skipping help, version, and subcommands
- Don't check the token for stdio

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The detached path is only reached for the root command, so the hard-coded
subcommand list isn't needed and would miss plugin commands.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Detached preflight can reject valid effective overrides, and token validation accepts padding-only values.

3 open issues
2 resolved since last review

Review effort: Balanced

Comment thread DevProxy/ApiSecurity.cs Resolved
Comment thread DevProxy/ApiSecurity.cs Resolved
Comment thread DevProxy/Program.cs Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused changes and validation tests address token overrides without any identified blocking issues.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

@waldekmastykarz
waldekmastykarz merged commit 4e605bb into main Oct 27, 2026
7 checks passed
@waldekmastykarz
waldekmastykarz deleted the api-token-env-var-override branch November 28, 2026 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants