fix(auth): normalize absent constraint lists to empty on load (SEC-013, #254)
ci / windows-x86 (push) Failing after 11m43s
ci / java (push) Failing after 12m0s
ci / portable (push) Failing after 14m46s
ci / windows-x86 (push) Failing after 11m43s
ci / java (push) Failing after 12m0s
ci / portable (push) Failing after 14m46s
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:
+24
-1
@@ -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>();
|
||||
}
|
||||
|
||||
+36
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user