Skip to content
Merged
8 changes: 6 additions & 2 deletions docs/areas/docker-instances.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ on the snapshots and samples it produces.

- Discovery lists **all** containers of an instance, including stopped ones. A container without an ID, with an empty or
blank ID, or with an ID longer than 200 characters, and an item of the list that is not a JSON object, is left out.
A container list whose root is not a JSON array fails discovery with `invalid response`, and no image is inspected.
- Per container it records: the container ID; the name (the first listed name that is a string, without its leading `/`, or the
container ID when it has none); the image reference as the Engine reports it; the local image ID; the compose
project (`com.docker.compose.project` label), the stack name (`com.docker.stack.namespace`) and the swarm service
Expand Down Expand Up @@ -121,7 +122,7 @@ on the snapshots and samples it produces.
control characters replaced by spaces ([Integration failures](integration-failures.md#remote-text-fields)). The
creation time of a history entry is the Engine's Unix time in seconds, read as UTC; `0`, a negative, an out-of-range
or an unreadable value is unknown. A history entry that is not a JSON object is left out and the other entries are
still read.
still read. A history whose root is not a JSON array answers *failed* with `invalid response`.
An item of the repo tags, repo digests, environment variables, root file-system layers or of a history entry's tags that is
not a JSON string (a number, `true`/`false`, `null`, an object, an array) is left out and the other items are still
read ([0071](../decisions/0071-a-docker-engine-string-list-item-that-is-not-a-json-string-is-left-out.md)).
Expand Down Expand Up @@ -227,7 +228,8 @@ on the snapshots and samples it produces.
shown in the UI.
- A message or log entry that names an endpoint shows its URL with the user info replaced by `***`; connecting still
uses the configured URL.
- A resource collection fails for an instance when the list request fails or times out, or when stats were requested
- A resource collection fails for an instance when the list request fails or times out, when the list's root is not a JSON
array (`invalid response`, no stats request), or when stats were requested
and no container could be sampled (`Docker container resource sampling failed for '<name>': no container stats
could be read`); it records **no** samples for that instance in that round and logs the failure once as warning 2103, besides
warning 3107 for a list request that broke and warning 3111 for each container left out; other instances are not
Expand Down Expand Up @@ -257,6 +259,8 @@ on the snapshots and samples it produces.
Docker Engine container without an ID.
- [0074](../decisions/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.
- [0075](../decisions/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.

## Not here

Expand Down
2 changes: 1 addition & 1 deletion docs/areas/integration-failures.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ 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, and a Docker Hub access token whose payload segment is not
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
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`.
Expand Down
2 changes: 1 addition & 1 deletion docs/areas/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <l>, byte <b> (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. 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, 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
`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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ still read. The rules are in [Docker instances](../areas/docker-instances.md).
samples no container and succeeds with count 0, and a history succeeds without entries, after which derived runtime
findings that rested on history text can be deactivated instead of kept. An Engine able to send such items can send
an empty list to the same effect today.
- A root that is not a JSON array keeps failing the call. An item of an unexpected kind inside an entry's string list
- A root that is not a JSON array keeps failing the call, with `invalid response` by
[0075](0075-a-docker-engine-container-list-or-image-history-that-is-not-a-json-array-is-an-invalid-response.md). An item of an unexpected kind inside an entry's string list
(a number among a history entry's tags, say) is decided by
[0071](0071-a-docker-engine-string-list-item-that-is-not-a-json-string-is-left-out.md).
- Revisit, with a warning per left-out item, if operators need to find an Engine that sends such items.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ The planning of #581 found the difference.
3. **Refuse a non-object host info answer too** β€” 0068 rejected it (its option 4): the collector takes a failed and a
zero host memory alike as 0, so only a log entry would change.
4. **Refuse a container list or image history root that is not an array in the same change** β€” the same kind of
defect, but a new parse rule for arrays beyond what the issue asks; proposed as a follow-up issue.
defect, but a new parse rule for arrays beyond what the issue asks; decided by
[0075](0075-a-docker-engine-container-list-or-image-history-that-is-not-a-json-array-is-an-invalid-response.md).
5. **Map the reader's `InvalidOperationException` to `invalid response`** β€” rejected by 0019 and 0029 for the misuse
the runtime reports with the same type.

Expand All @@ -48,5 +49,5 @@ in [Docker instances](../areas/docker-instances.md) and

- Warnings 3111 and 3112 and the failed inspect result read `invalid response` for such a body instead of
`unexpected error`; which container or image is affected, and how, does not change.
- A container list or image history whose root is not an array still reads `unexpected error` until the follow-up
issue decides it.
- A container list or image history whose root is not an array reads `invalid response` by
[0075](0075-a-docker-engine-container-list-or-image-history-that-is-not-a-json-array-is-an-invalid-response.md).
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 0075: A Docker Engine container list or image history that is not a JSON array is an invalid response

- **Status:** Accepted
- **Date:** 2026-11-28
- **Area:** Docker instances
- **Source:** Issue #585
- **Supersedes:** β€”

## Context

Discovery and resource sampling read the Engine's container list, the image history reads the list of history
entries; each answer is a JSON array, and each item that is not a JSON object is left out
([0069](0069-a-docker-engine-list-item-that-is-not-a-json-object-is-left-out.md)). The client enumerated the root
without checking its kind, so a root of another kind (an object, a string, a number, `true`/`false`, `null`) made the
reader throw `InvalidOperationException`, and the fixed reason
([0009](0009-integration-results-carry-fixed-failure-reasons.md)) of the failed discovery, resource collection or
history was `unexpected error`. Since
[0074](0074-a-docker-engine-stats-or-image-inspect-answer-that-is-not-a-json-object-is-an-invalid-response.md) a stats
or image inspect answer that is not a JSON object reads `invalid response`, so two malformed answers of the same Engine
read differently. Only a broken or hostile Engine, or a proxy in front of it, sends such a body. The planning of #583
found the difference.

## Options considered

1. **Parse the container list and the image history as a JSON array where the body is parsed (chosen)** β€” the rule of
0074 and [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)
for the other root kind: the root is refused as malformed JSON is, so the existing handlers answer
`invalid response` without a new handler; the outcome (a failed call) is unchanged.
2. **Keep `unexpected error`** β€” two reasons for one malformed Engine answer, depending on its root kind, and a reason
that reads like an error of the application.
3. **Read a root that is not an array as an empty list, as 0069 does for an item** β€” discovery would succeed without
containers, a resource round with no samples and a history without entries, so a malformed answer would read as an
Engine without containers and derived runtime findings resting on history text could be deactivated; an item is left
out because the other items still carry data, a root carries none.
4. **Check the root kind at each of the three call sites** β€” three copies of one check and its message where the
object check of 0073 sits in one place, so a later call site could miss it.
5. **Map the reader's `InvalidOperationException` to `invalid response`** β€” rejected by
[0019](0019-remote-json-with-undecodable-strings-is-an-invalid-response.md) 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.

## Decision

Option 1: the Engine's container list, for discovery and for resource sampling, and its image history are parsed as a
JSON array and refused as an invalid response otherwise; the host info keeps the rule of
[0068](0068-an-unreadable-docker-engine-stats-or-host-memory-number-counts-as-missing.md). The reasons are in
[Docker instances](../areas/docker-instances.md) and
[Integration failures](../areas/integration-failures.md#fixed-failure-reasons).

## Consequences

- A failed discovery, resource collection or history caused by such a body reads `invalid response` instead of
`unexpected error`, in its result and in a scan run's error message; the calls still fail as before, and warnings
3104, 3107 and 3109 still name the instance, now with the refusal's fixed message `The remote response is not a JSON
array` as their error.
- Every Engine answer the client reads as an object or an array now refuses the other kinds; only the host info reads
any root.
1 change: 1 addition & 0 deletions docs/decisions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ once in the [area document](../areas/README.md) of the record's area, and the re
| [0072](0072-a-registry-string-list-item-that-is-not-a-json-string-is-left-out.md) | A registry string list item that is not a JSON string is left out | Accepted | 2026-11-28 |
| [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 |
<!-- project:end index -->
6 changes: 3 additions & 3 deletions src/DockerUpdateGuard/Docker/DockerInstanceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ private static async Task<ExternalOperationResult<IReadOnlyList<DockerImageHisto

await using (responseStream.ConfigureAwait(false))
{
using var jsonDocument = await RemoteJsonDocument.ParseAsync(responseStream, requestTimeoutSource.Token)
using var jsonDocument = await RemoteJsonDocument.ParseArrayAsync(responseStream, requestTimeoutSource.Token)
.ConfigureAwait(false);
var historyEntries = new List<DockerImageHistoryEntryData>();

Expand Down Expand Up @@ -1215,7 +1215,7 @@ public async Task<ExternalOperationResult<IReadOnlyList<RuntimeContainerDescript

await using (responseStream.ConfigureAwait(false))
{
using var jsonDocument = await RemoteJsonDocument.ParseAsync(responseStream, requestTimeoutSource.Token)
using var jsonDocument = await RemoteJsonDocument.ParseArrayAsync(responseStream, requestTimeoutSource.Token)
.ConfigureAwait(false);
var containers = ParseContainers(jsonDocument.RootElement);

Expand Down Expand Up @@ -1282,7 +1282,7 @@ public async Task<ExternalOperationResult<IReadOnlyList<RuntimeContainerResource

await using (responseStream.ConfigureAwait(false))
{
using var jsonDocument = await RemoteJsonDocument.ParseAsync(responseStream, requestTimeoutSource.Token)
using var jsonDocument = await RemoteJsonDocument.ParseArrayAsync(responseStream, requestTimeoutSource.Token)
.ConfigureAwait(false);

return await ReadContainerSamplesAsync(httpClient,
Expand Down
37 changes: 33 additions & 4 deletions src/DockerUpdateGuard/Infrastructure/RemoteJsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DockerUpdateGuard.Infrastructure;

/// <summary>
/// Parses a remote JSON response and refuses a body whose strings do not decode as Unicode text; a parse failure carries no remote text
/// Parses a remote JSON response and refuses a body whose strings do not decode as Unicode text or, where asked, whose root is not of the expected kind; a parse failure carries no remote text
/// </summary>
internal static class RemoteJsonDocument
{
Expand All @@ -19,6 +19,11 @@ internal static class RemoteJsonDocument
/// </summary>
private const string NotAnObjectMessage = "The remote response is not a JSON object";

/// <summary>
/// Message of the exception for a response whose root is not a JSON array
/// </summary>
private const string NotAnArrayMessage = "The remote response is not a JSON array";

#endregion // Constants

#region Static methods
Expand Down Expand Up @@ -53,15 +58,39 @@ public static async Task<JsonDocument> ParseAsync(Stream stream, CancellationTok
/// <param name="stream">Stream to parse</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Parsed document</returns>
public static async Task<JsonDocument> ParseObjectAsync(Stream stream, CancellationToken cancellationToken)
public static Task<JsonDocument> ParseObjectAsync(Stream stream, CancellationToken cancellationToken)
{
return ParseRootAsync(stream, JsonValueKind.Object, NotAnObjectMessage, cancellationToken);
}

/// <summary>
/// Parse a stream into a JSON document whose root is an array and verify that every string decodes
/// </summary>
/// <param name="stream">Stream to parse</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Parsed document</returns>
public static Task<JsonDocument> ParseArrayAsync(Stream stream, CancellationToken cancellationToken)
{
return ParseRootAsync(stream, JsonValueKind.Array, NotAnArrayMessage, cancellationToken);
}

/// <summary>
/// Parse a stream into a JSON document, verify that every string decodes and that the root is of the expected kind
/// </summary>
/// <param name="stream">Stream to parse</param>
/// <param name="rootKind">Expected kind of the root</param>
/// <param name="refusalMessage">Message of the exception for a root of another kind</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Parsed document</returns>
private static async Task<JsonDocument> ParseRootAsync(Stream stream, JsonValueKind rootKind, string refusalMessage, CancellationToken cancellationToken)
{
var document = await ParseAsync(stream, cancellationToken).ConfigureAwait(false);

if (document.RootElement.ValueKind != JsonValueKind.Object)
if (document.RootElement.ValueKind != rootKind)
{
document.Dispose();

throw new JsonException(NotAnObjectMessage);
throw new JsonException(refusalMessage);
}

return document;
Expand Down
Loading