diff --git a/docs/areas/base-images.md b/docs/areas/base-images.md index 6da57376..aead8a01 100644 --- a/docs/areas/base-images.md +++ b/docs/areas/base-images.md @@ -108,7 +108,8 @@ a base-image update or vulnerability reaches; vulnerability refreshes and the da - **.NET:** `releases-index.json` under `DockerUpdateGuard:ReleaseMetadata:DotNetBaseUrl` (default `https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/`). Per channel (`channel-version`) the latest - runtime (`latest-runtime`) and whether that release is a security release (`security`). + runtime (`latest-runtime`) and whether that release is a security release (`security`). An entry of `releases-index` + that is not a JSON object is left out; the other entries still count. - **nginx:** the `download/` page under `DockerUpdateGuard:ReleaseMetadata:NginxBaseUrl` (default `https://nginx.org/`). Every `nginx-x.y.z.tar.gz` link counts; a channel's latest is its highest version. - Both URLs are fixed at startup and must be absolute `http`/`https` URLs without an `@` outside their user info ([Hosting](hosting.md)). A feed is loaded at @@ -140,12 +141,14 @@ a base-image update or vulnerability reaches; vulnerability refreshes and the da - Chain depth is capped at 5; base-tag resolution searches at most 150 tags. - Registry outcomes other than *succeeded* never abort the scan; they set *partial* or *failed* and add the registry's message (status code and an excerpt of the response body, see [Integration failures](integration-failures.md#excerpts-of-remote-text)). -- Feed failures (non-success status, missing `releases-index` array, unreadable content) only suppress the runtime +- Feed failures (non-success status, missing `releases-index` array, unreadable content: malformed JSON, a string that does not decode, or a root that is + not a JSON object) only suppress the runtime finding and keep the previous one (observed image and running container); they do not change the scan status. Unreadable content is logged as a warning. ## Related decisions +- [0076: The .NET release index is parsed as a remote JSON object, and an entry that is not one is left out](../decisions/0076-the-dotnet-release-index-is-parsed-as-a-remote-json-object-and-an-entry-that-is-not-one-is-left-out.md) - [0054: A base image is shared from two users among enabled observed images and their containers](../decisions/0054-a-base-image-is-shared-from-two-users-among-enabled-observed-images-and-their-containers.md) - [0042: A runtime container keeps its derived runtime finding while its runtime check cannot be completed](../decisions/0042-a-runtime-container-keeps-its-derived-runtime-finding-while-its-runtime-check-cannot-be-completed.md) diff --git a/docs/areas/integration-failures.md b/docs/areas/integration-failures.md index 3f9b3258..98653f02 100644 --- a/docs/areas/integration-failures.md +++ b/docs/areas/integration-failures.md @@ -31,10 +31,10 @@ operator's logs rely on it; each integration area states only what it adds. failed` (transport failures and responses that break off), `invalid response` or `unexpected error`. The error itself is logged with the failure (a JSON parse failure with a fixed message, [Telemetry](telemetry.md#credentials-and-external-data)). - `invalid response` covers malformed JSON and a JSON string that does not decode as Unicode text, such as a lone - surrogate escape like `\ud800` or bytes that are not UTF-8, a registry answer, or a Docker Engine stats or image inspect answer, whose root is not a JSON object, a Docker Engine container list or image history whose root is not a JSON array, and a Docker Hub access token whose payload segment is not - base64url. The Docker Hub, OCI and Docker Engine clients refuse the + surrogate escape like `\ud800` or bytes that are not UTF-8, a registry answer, a Docker Engine stats or image inspect answer, or the .NET release index, whose root is not a JSON object, a Docker Engine container list or image history whose root is not a JSON array, and a Docker Hub access token whose payload segment is not + base64url. The Docker Hub, OCI and Docker Engine clients and the .NET release feed refuse the whole body for one in any string value or property name; the Portainer client and the vulnerability providers for one - in a field they read; the release feeds report it as `unexpected error`. + in a field they read. - A message the application composed itself from fixed text, configuration names and masked URLs ([Telemetry](telemetry.md#credentials-and-external-data)), and for a rejected registration the stored name of an own image with its control characters replaced, is passed on as it is; it never contains an error's own @@ -185,6 +185,8 @@ negative or an unreadable number is unknown), and the .NET release feed's `lates remembered process-wide. - [0019](../decisions/0019-remote-json-with-undecodable-strings-is-an-invalid-response.md): remote JSON with a string that does not decode is an invalid response. +- [0076](../decisions/0076-the-dotnet-release-index-is-parsed-as-a-remote-json-object-and-an-entry-that-is-not-one-is-left-out.md): the .NET + release index is parsed as a remote JSON object and an entry that is not one is left out. - [0029](../decisions/0029-registry-calls-answer-every-error-but-the-callers-cancellation-as-failed.md): registry calls answer every error but the caller's cancellation as *failed*. - [0066](../decisions/0066-remote-timestamps-without-an-offset-are-read-as-utc.md): remote timestamps without an diff --git a/docs/areas/telemetry.md b/docs/areas/telemetry.md index 8aa05a6e..bff35691 100644 --- a/docs/areas/telemetry.md +++ b/docs/areas/telemetry.md @@ -122,7 +122,7 @@ Every exported signal carries the same resource: - A JSON parse failure of a remote answer, a token payload, the .NET release index or Trivy's report is logged with a fixed message, never the parser's: `Remote JSON is not valid at line , byte (counted from 0)`, with the parser's line and byte position in invariant digits, or `Remote JSON is not valid` when the position is unknown. The - exception has no path and no inner exception, so it carries no remote text; the refusal of a string that does not decode ([Integration failures](integration-failures.md#fixed-failure-reasons)) keeps its own fixed message and its decoding error, which carries no remote text either; the refusal of a registry answer, or of a Docker Engine stats or image inspect answer, whose root is not a JSON object carries the fixed message `The remote response is not a JSON object` and no inner exception; the refusal of a Docker Engine container list or image history whose root is not a JSON array carries `The remote response is not a JSON array` and no inner exception. Every parse of external JSON goes through + exception has no path and no inner exception, so it carries no remote text; the refusal of a string that does not decode ([Integration failures](integration-failures.md#fixed-failure-reasons)) keeps its own fixed message and its decoding error, which carries no remote text either; the refusal of a registry answer, of a Docker Engine stats or image inspect answer, or of the .NET release index, whose root is not a JSON object carries the fixed message `The remote response is not a JSON object` and no inner exception; the refusal of a Docker Engine container list or image history whose root is not a JSON array carries `The remote response is not a JSON array` and no inner exception. Every parse of external JSON goes through `Infrastructure\ExternalJson`. A `Content-Type` charset that cannot be used is refused with the fixed message `The character set of the remote response is not valid` and no inner exception. Decision: [0045](../decisions/0045-json-parse-failures-of-external-data-carry-a-fixed-message-with-their-position.md). diff --git a/docs/decisions/0076-the-dotnet-release-index-is-parsed-as-a-remote-json-object-and-an-entry-that-is-not-one-is-left-out.md b/docs/decisions/0076-the-dotnet-release-index-is-parsed-as-a-remote-json-object-and-an-entry-that-is-not-one-is-left-out.md new file mode 100644 index 00000000..ffc42314 --- /dev/null +++ b/docs/decisions/0076-the-dotnet-release-index-is-parsed-as-a-remote-json-object-and-an-entry-that-is-not-one-is-left-out.md @@ -0,0 +1,55 @@ +# 0076: The .NET release index is parsed as a remote JSON object, and an entry that is not one is left out + +- **Status:** Accepted +- **Date:** 2026-11-28 +- **Area:** Base images +- **Source:** Issue #587 +- **Supersedes:** 0019 — amends its consequence that the release feeds keep reporting a body with a string that does not decode as `unexpected error`: the .NET release index now reads `invalid response` for it. + +## Context + +The .NET release feed's `releases-index.json` is a JSON object whose `releases-index` array holds one object per +channel. `DotNetReleaseMetadataService` parsed it with `ExternalJson` and read its properties without checking the kind +of the root or of an entry, so a root that is not an object, an entry that is not one, or a string that does not decode +made the reader throw `InvalidOperationException`, and the fixed reason +([0009](0009-integration-results-carry-fixed-failure-reasons.md)) was `unexpected error`. The Docker Hub, OCI and Docker +Engine clients answer `invalid response` for such a body +([0073](0073-a-registry-answer-that-is-not-a-json-object-is-an-invalid-response-and-an-entry-that-is-not-one-is-left-out.md), +[0074](0074-a-docker-engine-stats-or-image-inspect-answer-that-is-not-a-json-object-is-an-invalid-response.md), +[0019](0019-remote-json-with-undecodable-strings-is-an-invalid-response.md)) and leave a non-object entry out +([0069](0069-a-docker-engine-list-item-that-is-not-a-json-object-is-left-out.md)). 0019 left the release feeds out +because their text reaches no message. The feed's message reaches no page or scan run either: a failure only suppresses +the runtime finding and is logged as warning 2101. The planning of #585 found the difference. + +## Options considered + +1. **Parse the index with `RemoteJsonDocument.ParseObjectAsync` and leave out an entry that is not an object (chosen)** + — the rule of 0073-0075 through the one helper that holds it; a malformed root and an undecodable string read + `invalid response` as in every other `JsonDocument` client. +2. **Check the root and answer the existing message for a missing `releases-index` array** — true for such a root, + but that message describes a well-formed index without the field, differs from the other clients' reason, and + leaves undecodable strings at `unexpected error`. +3. **A root check of its own that throws `JsonException`, without decoding the strings** — keeps tolerating an + undecodable string in a field the service never reads, but not in one it reads, and copies a check + `RemoteJsonDocument` holds once ([0075](0075-a-docker-engine-container-list-or-image-history-that-is-not-a-json-array-is-an-invalid-response.md), option 4). +4. **Keep `unexpected error`** — two reasons for one malformed body, and a reason that reads like an error of the + application (0074, option 2). +5. **Map `InvalidOperationException` to `invalid response`** — rejected by 0019 and + [0029](0029-registry-calls-answer-every-error-but-the-callers-cancellation-as-failed.md) for the misuse the runtime + reports with the same type. +6. **Refuse the whole index for an entry that is not an object** — the other entries still carry data (0069, 0073). + +## Decision + +Option 1: the .NET release index is parsed as a remote JSON object and refused as an invalid response otherwise or +when one of its strings does not decode; an entry of `releases-index` that is not a JSON object is left out. The rules +are in [Base images](../areas/base-images.md#release-feeds-and-runtime-findings) and +[Integration failures](../areas/integration-failures.md#fixed-failure-reasons). + +## Consequences + +- The feed's failed result reads `invalid response` instead of `unexpected error` for such a body, and warning 2101 + logs the refusal's fixed message; the runtime finding is suppressed as before. +- One undecodable string anywhere in the index, also in a field the service never reads, now fails the whole index, + where before only one in a field it reads did; Microsoft's feed is conforming JSON, so this is accepted. +- A malformed entry no longer hides the other channels of the index. diff --git a/docs/decisions/README.md b/docs/decisions/README.md index f696fdea..1563d0d8 100644 --- a/docs/decisions/README.md +++ b/docs/decisions/README.md @@ -136,4 +136,5 @@ once in the [area document](../areas/README.md) of the record's area, and the re | [0073](0073-a-registry-answer-that-is-not-a-json-object-is-an-invalid-response-and-an-entry-that-is-not-one-is-left-out.md) | A registry answer that is not a JSON object is an invalid response, and an entry that is not one is left out | Accepted | 2026-11-28 | | [0074](0074-a-docker-engine-stats-or-image-inspect-answer-that-is-not-a-json-object-is-an-invalid-response.md) | A Docker Engine stats or image inspect answer that is not a JSON object is an invalid response | Accepted | 2026-11-28 | | [0075](0075-a-docker-engine-container-list-or-image-history-that-is-not-a-json-array-is-an-invalid-response.md) | A Docker Engine container list or image history that is not a JSON array is an invalid response | Accepted | 2026-11-28 | +| [0076](0076-the-dotnet-release-index-is-parsed-as-a-remote-json-object-and-an-entry-that-is-not-one-is-left-out.md) | The .NET release index is parsed as a remote JSON object, and an entry that is not one is left out | Accepted | 2026-11-28 | \ No newline at end of file diff --git a/src/DockerUpdateGuard/Images/DotNetReleaseMetadataService.cs b/src/DockerUpdateGuard/Images/DotNetReleaseMetadataService.cs index ea16cf61..28313b8f 100644 --- a/src/DockerUpdateGuard/Images/DotNetReleaseMetadataService.cs +++ b/src/DockerUpdateGuard/Images/DotNetReleaseMetadataService.cs @@ -152,8 +152,8 @@ private async Task> LoadIndexAsync(CancellationTok await using (responseStream.ConfigureAwait(false)) { - using var jsonDocument = await ExternalJson.ParseAsync(responseStream, cancellationToken) - .ConfigureAwait(false); + using var jsonDocument = await RemoteJsonDocument.ParseObjectAsync(responseStream, cancellationToken) + .ConfigureAwait(false); if (jsonDocument.RootElement.TryGetProperty("releases-index", out var releasesIndexElement) == false || releasesIndexElement.ValueKind != JsonValueKind.Array) @@ -163,6 +163,11 @@ private async Task> LoadIndexAsync(CancellationTok foreach (var releaseElement in releasesIndexElement.EnumerateArray()) { + if (releaseElement.ValueKind != JsonValueKind.Object) + { + continue; + } + var channelVersion = TryGetString(releaseElement, "channel-version"); var latestRuntime = TryGetString(releaseElement, "latest-runtime"); diff --git a/src/Tests/DockerUpdateGuard.Tests/DotNetReleaseMetadataServiceTests.cs b/src/Tests/DockerUpdateGuard.Tests/DotNetReleaseMetadataServiceTests.cs index 05b47bee..10120bbe 100644 --- a/src/Tests/DockerUpdateGuard.Tests/DotNetReleaseMetadataServiceTests.cs +++ b/src/Tests/DockerUpdateGuard.Tests/DotNetReleaseMetadataServiceTests.cs @@ -1,4 +1,5 @@ using System.Net; +using System.Text.Json; using DockerUpdateGuard.Images; using DockerUpdateGuard.Images.Data; @@ -364,5 +365,120 @@ public async Task DotNetReleaseMetadataServiceGetChannelReleaseAsyncWhenIndexHas Assert.AreEqual("The .NET release index does not contain a valid releases-index array", result.Message, "The failure must name the missing array"); } + /// + /// Verify an index whose root is not a JSON object reports the fixed invalid-response reason + /// + /// Index body + /// Task + [TestMethod] + [TestCategory("AC1")] + [DataRow("[]")] + [DataRow("\"text\"")] + [DataRow("42")] + [DataRow("true")] + [DataRow("false")] + [DataRow("null")] + public async Task DotNetReleaseMetadataServiceGetChannelReleaseAsyncWhenIndexRootIsNotAnObjectReportsInvalidResponseAsync(string body) + { + using var handler = new StaticHttpJsonMessageHandler(body); + using var httpClient = new HttpClient(handler) + { + BaseAddress = new Uri("https://dotnet.example.test/"), + }; + var service = new DotNetReleaseMetadataService(httpClient, new TestLogger()); + + var result = await service.GetChannelReleaseAsync("9.0", TestContext.CancellationToken) + .ConfigureAwait(false); + + Assert.AreEqual(ExternalOperationStatus.Failed, result.Status, "An index whose root is not an object must fail the lookup"); + Assert.AreEqual("Loading .NET release metadata failed: invalid response", result.Message, "A root that is not an object must report the fixed invalid-response reason"); + } + + /// + /// Verify an index whose root is not a JSON object is logged with the fixed refusal and no inner exception + /// + /// Task + [TestMethod] + [TestCategory("AC1")] + public async Task DotNetReleaseMetadataServiceGetChannelReleaseAsyncWhenIndexRootIsNotAnObjectLogsFixedRefusalAsync() + { + var logger = new TestLogger(); + + using var handler = new StaticHttpJsonMessageHandler("[]"); + using var httpClient = new HttpClient(handler) + { + BaseAddress = new Uri("https://dotnet.example.test/"), + }; + var service = new DotNetReleaseMetadataService(httpClient, logger); + + await service.GetChannelReleaseAsync("9.0", TestContext.CancellationToken).ConfigureAwait(false); + + var loadFailure = Assert.ContainsSingle(entry => entry.EventId.Id == 2101, + logger.Entries, + "A root that is not an object must be logged once as load failure 2101"); + + Assert.IsInstanceOfType(loadFailure.Exception, "The warning must carry the refusal of a root that is not an object"); + Assert.AreEqual("The remote response is not a JSON object", loadFailure.Exception.Message, "The refusal must carry its fixed message"); + Assert.IsNull(loadFailure.Exception.InnerException, "The refusal must carry no inner exception"); + } + + /// + /// Verify an index with a string that does not decode reports the fixed invalid-response reason + /// + /// Index body + /// Task + [TestMethod] + [TestCategory("AC2")] + [DataRow("""{"releases-index":[{"channel-version":"9\ud800","latest-runtime":"9.0.15"}]}""")] + [DataRow("""{"releases-index":[{"channel-version":"9.0","latest-runtime":"9.0.15","unread":"a\ud800b"}]}""")] + public async Task DotNetReleaseMetadataServiceGetChannelReleaseAsyncWhenIndexStringDoesNotDecodeReportsInvalidResponseAsync(string body) + { + Assert.Contains("\\ud800", body, "The test input must carry the escaped lone surrogate"); + + using var handler = new StaticHttpJsonMessageHandler(body); + using var httpClient = new HttpClient(handler) + { + BaseAddress = new Uri("https://dotnet.example.test/"), + }; + var service = new DotNetReleaseMetadataService(httpClient, new TestLogger()); + + var result = await service.GetChannelReleaseAsync("9.0", TestContext.CancellationToken) + .ConfigureAwait(false); + + Assert.AreEqual(ExternalOperationStatus.Failed, result.Status, "A string that does not decode must fail the lookup"); + Assert.AreEqual("Loading .NET release metadata failed: invalid response", result.Message, "A string that does not decode must report the fixed invalid-response reason"); + } + + /// + /// Verify an index entry that is not a JSON object is left out and a valid entry beside it is returned + /// + /// Entry that is not an object + /// Task + [TestMethod] + [TestCategory("AC3")] + [DataRow("42")] + [DataRow("\"text\"")] + [DataRow("[]")] + [DataRow("null")] + [DataRow("true")] + public async Task DotNetReleaseMetadataServiceGetChannelReleaseAsyncWhenIndexEntryIsNotAnObjectLeavesItOutAsync(string entry) + { + var body = $$"""{"releases-index":[{{entry}},{"channel-version":"9.0","latest-runtime":"9.0.15"}]}"""; + + using var handler = new StaticHttpJsonMessageHandler(body); + using var httpClient = new HttpClient(handler) + { + BaseAddress = new Uri("https://dotnet.example.test/"), + }; + var service = new DotNetReleaseMetadataService(httpClient, new TestLogger()); + + var result = await service.GetChannelReleaseAsync("9.0", TestContext.CancellationToken) + .ConfigureAwait(false); + + Assert.AreEqual(ExternalOperationStatus.Succeeded, result.Status, "An entry that is not an object must not fail the index"); + Assert.IsNotNull(result.Data, "The valid entry beside it must be returned"); + Assert.AreEqual(new Version(9, 0, 15), result.Data.LatestRuntimeVersion, "The valid entry must carry its latest runtime"); + } + #endregion // Methods } \ No newline at end of file