|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Net.Http.Json; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Azure.DataApiBuilder.Service.Controllers; |
| 10 | +using Microsoft.AspNetCore.Http; |
| 11 | +using Microsoft.AspNetCore.TestHost; |
| 12 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 13 | +using static Azure.DataApiBuilder.Service.Tests.Configuration.ConfigurationEndpoints; |
| 14 | +using static Azure.DataApiBuilder.Service.Tests.Configuration.TestConfigFileReader; |
| 15 | + |
| 16 | +namespace Azure.DataApiBuilder.Service.Tests.Configuration; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// Tests for the security mitigation on the POST /configuration endpoint (CWE-306). |
| 20 | +/// Verifies that the endpoint is restricted to loopback addresses and optionally |
| 21 | +/// gated behind a bootstrap token (DAB_CONFIG_AUTH_TOKEN / X-DAB-CONFIG-AUTH header). |
| 22 | +/// </summary> |
| 23 | +[TestClass] |
| 24 | +public class ConfigurationEndpointAuthorizationTests |
| 25 | +{ |
| 26 | + // Names of environment variables this test class manipulates. Snapshotted in |
| 27 | + // TestInitialize and restored in TestCleanup so each test starts and ends from |
| 28 | + // the same global state regardless of what other tests in the assembly do. |
| 29 | + private const string ASPNETCORE_ENVIRONMENT_VAR = "ASPNETCORE_ENVIRONMENT"; |
| 30 | + private const string DAB_ENVIRONMENT_VAR = "DAB_ENVIRONMENT"; |
| 31 | + |
| 32 | + private string _originalAuthToken; |
| 33 | + private string _originalAspNetEnvironment; |
| 34 | + private string _originalDabEnvironment; |
| 35 | + |
| 36 | + [TestInitialize] |
| 37 | + public void Initialize() |
| 38 | + { |
| 39 | + // Snapshot the originals so they can be restored verbatim in TestCleanup. |
| 40 | + _originalAuthToken = Environment.GetEnvironmentVariable(Startup.CONFIG_AUTH_TOKEN_ENV_VAR); |
| 41 | + _originalAspNetEnvironment = Environment.GetEnvironmentVariable(ASPNETCORE_ENVIRONMENT_VAR); |
| 42 | + _originalDabEnvironment = Environment.GetEnvironmentVariable(DAB_ENVIRONMENT_VAR); |
| 43 | + |
| 44 | + // Other tests in the assembly (e.g. TestLoadingLocalCosmosSettings) set |
| 45 | + // ASPNETCORE_ENVIRONMENT / DAB_ENVIRONMENT without cleanup. Those env vars cause |
| 46 | + // FileSystemRuntimeConfigLoader to find an environment-specific dab-config.*.json on |
| 47 | + // disk and auto-initialize the runtime, which makes POST /configuration return |
| 48 | + // 409 Conflict before our security middleware can be exercised. Clear them so each |
| 49 | + // test starts from an uninitialized runtime; the originals are restored in cleanup. |
| 50 | + Environment.SetEnvironmentVariable(ASPNETCORE_ENVIRONMENT_VAR, null); |
| 51 | + Environment.SetEnvironmentVariable(DAB_ENVIRONMENT_VAR, null); |
| 52 | + } |
| 53 | + |
| 54 | + [TestCleanup] |
| 55 | + public void Cleanup() |
| 56 | + { |
| 57 | + Environment.SetEnvironmentVariable(Startup.CONFIG_AUTH_TOKEN_ENV_VAR, _originalAuthToken); |
| 58 | + Environment.SetEnvironmentVariable(ASPNETCORE_ENVIRONMENT_VAR, _originalAspNetEnvironment); |
| 59 | + Environment.SetEnvironmentVariable(DAB_ENVIRONMENT_VAR, _originalDabEnvironment); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Validates IsConfigurationRequestAuthorized across the full matrix of inputs: |
| 64 | + /// remote IP (loopback v4/v6, private/public, null/in-process), configured bootstrap |
| 65 | + /// token, and provided X-DAB-CONFIG-AUTH header value. |
| 66 | + /// </summary> |
| 67 | + /// <param name="remoteIp"> |
| 68 | + /// Source IP for the simulated request. Use null to model an in-process call |
| 69 | + /// (e.g. TestServer) where there is no underlying TCP connection. |
| 70 | + /// </param> |
| 71 | + /// <param name="configuredToken">Value to set DAB_CONFIG_AUTH_TOKEN to, or null to leave unset.</param> |
| 72 | + /// <param name="providedHeader">Value of the X-DAB-CONFIG-AUTH header, or null to omit.</param> |
| 73 | + /// <param name="expected">Expected authorization result.</param> |
| 74 | + [DataTestMethod] |
| 75 | + // --- No bootstrap token configured --- |
| 76 | + [DataRow("127.0.0.1", null, null, true, DisplayName = "Loopback IPv4, no token => allow")] |
| 77 | + [DataRow("::1", null, null, true, DisplayName = "Loopback IPv6, no token => allow")] |
| 78 | + [DataRow("::ffff:127.0.0.1", null, null, true, DisplayName = "IPv4-mapped IPv6 loopback (dual-stack Kestrel), no token => allow")] |
| 79 | + [DataRow(null, null, null, true, DisplayName = "In-process (null IP), no token => allow")] |
| 80 | + [DataRow("192.168.1.100", null, null, false, DisplayName = "Private IPv4, no token => deny")] |
| 81 | + [DataRow("10.0.0.1", null, null, false, DisplayName = "Private IPv4 (10/8), no token => deny")] |
| 82 | + [DataRow("172.17.0.1", null, null, false, DisplayName = "Docker bridge IP, no token => deny")] |
| 83 | + [DataRow("203.0.113.50", null, null, false, DisplayName = "Public IPv4, no token => deny")] |
| 84 | + // --- Bootstrap token configured --- |
| 85 | + [DataRow("127.0.0.1", "secret", "secret", true, DisplayName = "Loopback + correct token => allow")] |
| 86 | + [DataRow("127.0.0.1", "secret", "wrong", false, DisplayName = "Loopback + wrong token => deny")] |
| 87 | + [DataRow("127.0.0.1", "secret", null, false, DisplayName = "Loopback + missing header => deny")] |
| 88 | + [DataRow("192.168.1.50", "secret", "secret", false, DisplayName = "Private IPv4 + correct token => still deny (non-loopback)")] |
| 89 | + [DataRow("192.168.1.50", "secret", "wrong", false, DisplayName = "Private IPv4 + wrong token => deny")] |
| 90 | + [DataRow("203.0.113.50", "secret", "secret", false, DisplayName = "Public IPv4 + correct token => still deny (non-loopback)")] |
| 91 | + [DataRow("203.0.113.50", "secret", "wrong", false, DisplayName = "Public IPv4 + wrong token => deny")] |
| 92 | + [DataRow("203.0.113.50", "secret", null, false, DisplayName = "Public IPv4 + missing header => deny")] |
| 93 | + [DataRow(null, "secret", null, false, DisplayName = "In-process + missing header => deny")] |
| 94 | + [DataRow(null, "secret", "secret", true, DisplayName = "In-process + correct token => allow")] |
| 95 | + public void IsConfigurationRequestAuthorized_Matrix( |
| 96 | + string remoteIp, |
| 97 | + string configuredToken, |
| 98 | + string providedHeader, |
| 99 | + bool expected) |
| 100 | + { |
| 101 | + // Arrange |
| 102 | + Environment.SetEnvironmentVariable(Startup.CONFIG_AUTH_TOKEN_ENV_VAR, configuredToken); |
| 103 | + |
| 104 | + DefaultHttpContext httpContext = new(); |
| 105 | + httpContext.Connection.RemoteIpAddress = remoteIp is null ? null : IPAddress.Parse(remoteIp); |
| 106 | + if (providedHeader is not null) |
| 107 | + { |
| 108 | + httpContext.Request.Headers[Startup.CONFIG_AUTH_HEADER] = providedHeader; |
| 109 | + } |
| 110 | + |
| 111 | + // Act |
| 112 | + bool result = Startup.IsConfigurationRequestAuthorized(httpContext); |
| 113 | + |
| 114 | + // Assert |
| 115 | + Assert.AreEqual(expected, result); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// End-to-end test against the full middleware pipeline via TestServer. Drives both |
| 120 | + /// /configuration (v1) and /configuration/v2 with every token combination and verifies |
| 121 | + /// the HTTP status code returned by the security middleware. |
| 122 | + /// </summary> |
| 123 | + /// <param name="configurationEndpoint">The endpoint being tested.</param> |
| 124 | + /// <param name="configuredToken">Value to set DAB_CONFIG_AUTH_TOKEN to, or null to leave unset.</param> |
| 125 | + /// <param name="providedHeader">Value of the X-DAB-CONFIG-AUTH header, or null to omit.</param> |
| 126 | + /// <param name="expectedStatus">Expected HTTP status code from the POST.</param> |
| 127 | + [DataTestMethod, TestCategory(TestCategory.COSMOSDBNOSQL)] |
| 128 | + // No token configured -- backward-compatible loopback success |
| 129 | + [DataRow(CONFIGURATION_ENDPOINT, null, null, HttpStatusCode.OK)] |
| 130 | + [DataRow(CONFIGURATION_ENDPOINT_V2, null, null, HttpStatusCode.OK)] |
| 131 | + // Token configured and correct -- allowed |
| 132 | + [DataRow(CONFIGURATION_ENDPOINT, "integration-token", "integration-token", HttpStatusCode.OK)] |
| 133 | + [DataRow(CONFIGURATION_ENDPOINT_V2, "integration-token", "integration-token", HttpStatusCode.OK)] |
| 134 | + // Token configured but wrong -- forbidden |
| 135 | + [DataRow(CONFIGURATION_ENDPOINT, "correct-token", "wrong-token", HttpStatusCode.Forbidden)] |
| 136 | + [DataRow(CONFIGURATION_ENDPOINT_V2, "correct-token", "wrong-token", HttpStatusCode.Forbidden)] |
| 137 | + // Token configured but header missing -- forbidden |
| 138 | + [DataRow(CONFIGURATION_ENDPOINT, "required-token", null, HttpStatusCode.Forbidden)] |
| 139 | + [DataRow(CONFIGURATION_ENDPOINT_V2, "required-token", null, HttpStatusCode.Forbidden)] |
| 140 | + public async Task EndToEnd_PostConfiguration_StatusCodeMatrix( |
| 141 | + string configurationEndpoint, |
| 142 | + string configuredToken, |
| 143 | + string providedHeader, |
| 144 | + HttpStatusCode expectedStatus) |
| 145 | + { |
| 146 | + // Arrange -- ASPNETCORE_ENVIRONMENT / DAB_ENVIRONMENT are already cleared by |
| 147 | + // TestInitialize so the runtime starts uninitialized and our middleware is |
| 148 | + // exercised. Only the per-row auth token still needs to be set here; TestCleanup |
| 149 | + // restores the original value. |
| 150 | + Environment.SetEnvironmentVariable(Startup.CONFIG_AUTH_TOKEN_ENV_VAR, configuredToken); |
| 151 | + |
| 152 | + using TestServer server = new(Program.CreateWebHostFromInMemoryUpdatableConfBuilder(Array.Empty<string>())); |
| 153 | + using HttpClient httpClient = server.CreateClient(); |
| 154 | + |
| 155 | + HttpRequestMessage request = new(HttpMethod.Post, configurationEndpoint) |
| 156 | + { |
| 157 | + Content = BuildPostContent(configurationEndpoint), |
| 158 | + }; |
| 159 | + if (providedHeader is not null) |
| 160 | + { |
| 161 | + request.Headers.Add(Startup.CONFIG_AUTH_HEADER, providedHeader); |
| 162 | + } |
| 163 | + |
| 164 | + // Act |
| 165 | + HttpResponseMessage postResult = await httpClient.SendAsync(request); |
| 166 | + |
| 167 | + // Assert |
| 168 | + Assert.AreEqual(expectedStatus, postResult.StatusCode); |
| 169 | + } |
| 170 | + |
| 171 | + /// <summary> |
| 172 | + /// Builds the JSON post body appropriate for the given configuration endpoint version. |
| 173 | + /// </summary> |
| 174 | + private static JsonContent BuildPostContent(string endpoint) |
| 175 | + { |
| 176 | + Config.ObjectModel.RuntimeConfig config = ReadCosmosConfigurationFromFile() with { Schema = "@env('schema')" }; |
| 177 | + const string graphqlSchema = @" |
| 178 | + type Entity { |
| 179 | + id: ID! |
| 180 | + name: String! |
| 181 | + } |
| 182 | + "; |
| 183 | + |
| 184 | + if (endpoint == CONFIGURATION_ENDPOINT) |
| 185 | + { |
| 186 | + return JsonContent.Create(new ConfigurationPostParameters( |
| 187 | + Configuration: config.ToJson(), |
| 188 | + Schema: graphqlSchema, |
| 189 | + ConnectionString: "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", |
| 190 | + AccessToken: null)); |
| 191 | + } |
| 192 | + |
| 193 | + if (endpoint == CONFIGURATION_ENDPOINT_V2) |
| 194 | + { |
| 195 | + return JsonContent.Create(new ConfigurationPostParametersV2( |
| 196 | + Configuration: config.ToJson(), |
| 197 | + ConfigurationOverrides: "{}", |
| 198 | + Schema: graphqlSchema, |
| 199 | + AccessToken: null)); |
| 200 | + } |
| 201 | + |
| 202 | + throw new ArgumentException($"Unknown endpoint: {endpoint}", nameof(endpoint)); |
| 203 | + } |
| 204 | +} |
0 commit comments