Skip to content

Restrict POST /configuration endpoint to loopback#3669

Merged
souvikghosh04 merged 5 commits into
mainfrom
Usr/sogh/restrict-configuration-endpoint
Jun 17, 2026
Merged

Restrict POST /configuration endpoint to loopback#3669
souvikghosh04 merged 5 commits into
mainfrom
Usr/sogh/restrict-configuration-endpoint

Conversation

@souvikghosh04

@souvikghosh04 souvikghosh04 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Restrict the late-configured POST /configuration endpoint so it is only callable from the loopback interface, with an optional bootstrap-token check on top.

Why

POST /configuration is used in hosted mode (e.g. Azure Static Web Apps) to deliver the runtime config — including the database connection string — to an uninitialized DAB instance. It currently runs before the authentication middleware and has no other access control, which is more permissive than required for what is effectively a host-to-runtime bootstrap channel.

Change

In src/Service/Startup.cs, the existing middleware that gates POST /configuration now also checks IsConfigurationRequestAuthorized(HttpContext) and returns 403 Forbidden if it fails. The check enforces:

  1. The request must originate from a loopback address. Null RemoteIpAddress (in-process callers such as TestServer) is treated as loopback.
  2. If the DAB_CONFIG_AUTH_TOKEN environment variable is set, the request must include a matching X-DAB-CONFIG-AUTH header (fixed-time comparison).

The rest of the middleware ordering is unchanged.

Backward compatibility

  • Azure Static Web Apps platform config injector (posts over loopback inside the container) keeps working unchanged.
  • Existing in-process TestServer tests keep working unchanged.
  • The bootstrap token is opt-in — when the env var is unset, behavior on loopback matches today's behavior.

Tests

  • Unit Tests

The POST /configuration endpoint used in late-configured (hosted) mode now requires the request to originate from a loopback address. An optional bootstrap token (DAB_CONFIG_AUTH_TOKEN env var, X-DAB-CONFIG-AUTH header) can be required as an additional check.

Backward compatible: in-process callers (TestServer) and loopback callers without a token continue to work.

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.

Pull request overview

This PR hardens the late-bound hosted-mode POST /configuration bootstrap endpoint by restricting access to loopback callers and (optionally) requiring a bootstrap token header, reducing exposure prior to the auth middleware running.

Changes:

  • Added IsConfigurationRequestAuthorized(HttpContext) and enforced it in the /configuration gating middleware (returning 403 Forbidden when unauthorized).
  • Introduced optional bootstrap-token validation via DAB_CONFIG_AUTH_TOKEN + X-DAB-CONFIG-AUTH using fixed-time comparison.
  • Added new test coverage for authorization behavior across loopback/non-loopback and token combinations.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/Service/Startup.cs Adds loopback + optional bootstrap-token authorization gating for POST /configuration.
src/Service.Tests/Configuration/ConfigurationEndpointAuthorizationTests.cs Adds unit + TestServer coverage for the new authorization behavior.

Comment thread src/Service/Startup.cs
Comment thread src/Service/Startup.cs Outdated
- Handle ::ffff:127.0.0.1 (dual-stack Kestrel) as loopback by calling MapToIPv4 first
- Fix stray quote in IsConfigurationRequestAuthorized XML doc
- Add matrix row covering IPv4-mapped IPv6 loopback
@souvikghosh04 souvikghosh04 moved this from In Progress to Review In Progress in Data API builder Jun 16, 2026
…tion

Other tests in the assembly (e.g. TestLoadingLocalCosmosSettings) set ASPNETCORE_ENVIRONMENT and DAB_ENVIRONMENT without cleanup. Those env vars caused FileSystemRuntimeConfigLoader to find an environment-specific dab-config.*.json on disk and auto-initialize the runtime, which made POST /configuration return 409 Conflict before the new security middleware was exercised, breaking the EndToEnd matrix in CI.

Snapshot and clear ASPNETCORE_ENVIRONMENT and DAB_ENVIRONMENT before creating the TestServer, and restore them in a finally block. Verified locally by setting ASPNETCORE_ENVIRONMENT=CosmosDb_NoSql in the parent shell and rerunning all 22 tests successfully.

@anushakolan anushakolan 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.

Just a minor comment to address; otherwise LGTM.

It would be helpful to create a tracking task and link it to this PR, along with relevant context. This makes it easier to understand the background and motivations for the change in the future.

Comment thread src/Service.Tests/Configuration/ConfigurationEndpointAuthorizationTests.cs Outdated

@RubenCerna2079 RubenCerna2079 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.

LGTM! Approving with the expectation that the comment from Anusha gets resolved.

Addresses two review comments on PR #3669:

1. (anushakolan) Move DAB_CONFIG_AUTH_TOKEN, ASPNETCORE_ENVIRONMENT, and DAB_ENVIRONMENT snapshot/restore into TestInitialize/TestCleanup so any pre-existing values are preserved verbatim. Previously TestCleanup reset DAB_CONFIG_AUTH_TOKEN to null which could perturb other tests.

2. (RubenCerna2079) Add explicit unit-test matrix rows for private/public IPv4 with correct token, wrong token, and missing header so the 'non-loopback is always denied regardless of token' contract is regression-locked. Matrix grows from 14 to 18 rows; total tests 22 -> 26.

Verified locally by setting both ASPNETCORE_ENVIRONMENT=CosmosDb_NoSql and DAB_CONFIG_AUTH_TOKEN=preexisting-value in the parent shell: all 26 tests pass and both env vars retain their original values after the run.
@souvikghosh04 souvikghosh04 enabled auto-merge (squash) June 17, 2026 04:37
@souvikghosh04 souvikghosh04 merged commit 7a0d7e3 into main Jun 17, 2026
12 checks passed
@souvikghosh04 souvikghosh04 deleted the Usr/sogh/restrict-configuration-endpoint branch June 17, 2026 16:40
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Data API builder Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants