From 25e75c4dd5eb5be187b1268561ae6a97f714b09f Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 09:48:29 -0500 Subject: [PATCH] fix(auth): normalize absent constraint lists to empty on load (SEC-013, #254) System.Text.Json leaves a positional-record list parameter that is absent from (or null in) the stored blob as null, so a sparse constraints row threw NullReferenceException in HasReadConstraints/HasWriteConstraints and handed the browse-scope provider a null scope. Deserialize now normalizes every list to empty. Reproduced with a sparse-blob test first. --- .../ApiKeyConstraintSerializer.cs | 25 ++++++++++++- .../ApiKeyConstraintSerializerTests.cs | 36 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/ZB.MOM.WW.MxGateway.Server/Security/Authentication/ApiKeyConstraintSerializer.cs b/src/ZB.MOM.WW.MxGateway.Server/Security/Authentication/ApiKeyConstraintSerializer.cs index c0a54185..5fc10118 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/Security/Authentication/ApiKeyConstraintSerializer.cs +++ b/src/ZB.MOM.WW.MxGateway.Server/Security/Authentication/ApiKeyConstraintSerializer.cs @@ -26,6 +26,13 @@ public static class ApiKeyConstraintSerializer /// Members absent from the JSON take their default: rows persisted before /// existed carry no dashboard_tags /// member and deserialize to an untagged key, unchanged in every other respect. + /// + /// The record's list parameters are non-nullable, but the serializer leaves a + /// constructor parameter that is absent from the JSON (or explicitly null) as + /// . Every list is normalized to empty here, at load time, so a + /// sparse blob — hand-edited, or written by another tool — can neither throw in the + /// constraint checks nor hand the browse-scope provider a scope. + /// /// public static ApiKeyConstraints Deserialize(string? json) { @@ -34,6 +41,22 @@ public static class ApiKeyConstraintSerializer return ApiKeyConstraints.Empty; } - return JsonSerializer.Deserialize(json, JsonOptions) ?? ApiKeyConstraints.Empty; + ApiKeyConstraints? parsed = JsonSerializer.Deserialize(json, JsonOptions); + if (parsed is null) + { + return ApiKeyConstraints.Empty; + } + + // The analyzer sees these as never-null; the deserializer does not honour that. + return parsed with + { + ReadSubtrees = OrEmpty(parsed.ReadSubtrees), + WriteSubtrees = OrEmpty(parsed.WriteSubtrees), + ReadTagGlobs = OrEmpty(parsed.ReadTagGlobs), + WriteTagGlobs = OrEmpty(parsed.WriteTagGlobs), + BrowseSubtrees = OrEmpty(parsed.BrowseSubtrees), + }; } + + private static IReadOnlyList OrEmpty(IReadOnlyList? list) => list ?? Array.Empty(); } diff --git a/src/ZB.MOM.WW.MxGateway.Tests/Security/Authentication/ApiKeyConstraintSerializerTests.cs b/src/ZB.MOM.WW.MxGateway.Tests/Security/Authentication/ApiKeyConstraintSerializerTests.cs index 4084f413..d92bbfa2 100644 --- a/src/ZB.MOM.WW.MxGateway.Tests/Security/Authentication/ApiKeyConstraintSerializerTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Tests/Security/Authentication/ApiKeyConstraintSerializerTests.cs @@ -107,4 +107,40 @@ public sealed class ApiKeyConstraintSerializerTests Assert.Same(ApiKeyConstraints.Empty, ApiKeyConstraintSerializer.Deserialize(null)); Assert.Empty(ApiKeyConstraints.Empty.DashboardTags); } + + /// + /// SEC-013: a sparse blob (hand-edited, or written by another tool) omits list members. + /// Every absent list must come back empty rather than null, so the constraint checks and + /// the browse-scope provider never dereference or pass on a null list. + /// + [Fact] + public void Deserialize_SparseJson_YieldsEmptyListsNotNull() + { + ApiKeyConstraints constraints = ApiKeyConstraintSerializer.Deserialize("""{"read_subtrees":["A*"]}"""); + + Assert.Equal(["A*"], constraints.ReadSubtrees); + Assert.NotNull(constraints.WriteSubtrees); + Assert.NotNull(constraints.ReadTagGlobs); + Assert.NotNull(constraints.WriteTagGlobs); + Assert.NotNull(constraints.BrowseSubtrees); + Assert.Empty(constraints.BrowseSubtrees); + Assert.True(constraints.HasReadConstraints); + Assert.False(constraints.HasWriteConstraints); + Assert.False(constraints.IsEmpty); + } + + /// SEC-013: explicit JSON nulls for the list members normalize to empty lists too. + [Fact] + public void Deserialize_ExplicitNullLists_YieldEmptyLists() + { + ApiKeyConstraints constraints = ApiKeyConstraintSerializer.Deserialize( + """{"read_subtrees":null,"write_subtrees":null,"read_tag_globs":null,"write_tag_globs":null,"browse_subtrees":null,"read_alarm_only":true}"""); + + Assert.Empty(constraints.ReadSubtrees); + Assert.Empty(constraints.WriteSubtrees); + Assert.Empty(constraints.ReadTagGlobs); + Assert.Empty(constraints.WriteTagGlobs); + Assert.Empty(constraints.BrowseSubtrees); + Assert.True(constraints.HasReadConstraints); + } }