fix(auth): normalize absent constraint lists to empty on load (SEC-013, #254)
ci / java (push) Failing after 20m50s
ci / portable (push) Failing after 21m6s
ci / windows-x86 (push) Failing after 22m44s

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.
This commit is contained in:
Joseph Doherty
2026-12-03 09:48:29 -05:00
parent e0fc2a5db3
commit 25e75c4dd5
2 changed files with 60 additions and 1 deletions
@@ -26,6 +26,13 @@ public static class ApiKeyConstraintSerializer
/// Members absent from the JSON take their default: rows persisted before
/// <see cref="ApiKeyConstraints.DashboardTags"/> existed carry no <c>dashboard_tags</c>
/// member and deserialize to an untagged key, unchanged in every other respect.
/// <para>
/// The record's list parameters are non-nullable, but the serializer leaves a
/// constructor parameter that is absent from the JSON (or explicitly <c>null</c>) as
/// <see langword="null"/>. 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 <see langword="null"/> scope.
/// </para>
/// </remarks>
public static ApiKeyConstraints Deserialize(string? json)
{
@@ -34,6 +41,22 @@ public static class ApiKeyConstraintSerializer
return ApiKeyConstraints.Empty;
}
return JsonSerializer.Deserialize<ApiKeyConstraints>(json, JsonOptions) ?? ApiKeyConstraints.Empty;
ApiKeyConstraints? parsed = JsonSerializer.Deserialize<ApiKeyConstraints>(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<string> OrEmpty(IReadOnlyList<string>? list) => list ?? Array.Empty<string>();
}
@@ -107,4 +107,40 @@ public sealed class ApiKeyConstraintSerializerTests
Assert.Same(ApiKeyConstraints.Empty, ApiKeyConstraintSerializer.Deserialize(null));
Assert.Empty(ApiKeyConstraints.Empty.DashboardTags);
}
/// <summary>
/// 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.
/// </summary>
[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);
}
/// <summary>SEC-013: explicit JSON nulls for the list members normalize to empty lists too.</summary>
[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);
}
}