diff --git a/.claude/rules/security.md b/.claude/rules/security.md index 043fac85..fb922dc8 100644 --- a/.claude/rules/security.md +++ b/.claude/rules/security.md @@ -46,8 +46,10 @@ public class OrderBO : FormBusinessObject } ``` -What an action name can reach is decided by `JsonRpcMethod.IsResolvableAction` (Polhem.JsonRpc.Server): a public, non-generic instance -method with exactly one parameter that is not a property or event accessor and does not override an `object` member. +What an action name can reach is decided by `JsonRpcMethod.IsResolvableAction` (Polhem.JsonRpc.Server) and `PolhemMethodPolicy`: a public, +non-generic instance method with exactly one parameter that is not a property or event accessor and does not override an +`object` member. The dispatcher also resolves a streaming method, whose second parameter is an `IJsonRpcStream`; +`PolhemMethodPolicy` refuses it, since its items would skip `ApiOutputConverter`. Static methods and accessors are not reachable, so a class-level attribute no longer publishes them. It still publishes **every** such method the class and its subclasses declare (the attribute is inherited), including methods added later for internal use, and POLHEM3001 stays silent about all of them because the class attribute counts as a diff --git a/.claude/skills/polhem-add-bo-method/SKILL.md b/.claude/skills/polhem-add-bo-method/SKILL.md index 8e8ddf41..38d94a51 100644 --- a/.claude/skills/polhem-add-bo-method/SKILL.md +++ b/.claude/skills/polhem-add-bo-method/SKILL.md @@ -244,8 +244,9 @@ public virtual Result (Args args) `ApiAccessRequirement` are in their XML docs (`src/Polhem.Definition/Security/`); choose the lowest protection level the data allows. A method with no attribute on itself, its base definition or its class is refused at call time, and `POLHEM3001` reports it at build time -- What an action name can reach is decided by `JsonRpcMethod.IsResolvableAction` (Polhem.JsonRpc.Server): a public, non-generic instance - method with exactly one parameter. Keep helper methods with one parameter non-public, or they become reachable +- What an action name can reach is decided by `JsonRpcMethod.IsResolvableAction` (Polhem.JsonRpc.Server) and `PolhemMethodPolicy`: + a public, non-generic instance method with exactly one parameter. The policy refuses a streaming method, whose second + parameter is an `IJsonRpcStream`, although the dispatcher resolves it. Keep helper methods with one parameter non-public, or they become reachable under a class-level attribute - The BO does not reimplement fallback logic (fallback belongs to the Repository) diff --git a/PolhemJsonRpc.props b/PolhemJsonRpc.props index 0b57bbd7..be5b19f0 100644 --- a/PolhemJsonRpc.props +++ b/PolhemJsonRpc.props @@ -23,7 +23,7 @@ and TemplateJsonRpcVersionTests (tests/Polhem.Templates.UnitTests) fails when they disagree. --> - [1.2.0] + [1.4.0] diff --git a/README.md b/README.md index dec5a891..7ec1f4d8 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Each assembly below ships as a NuGet package of the same name. A JSON-RPC server ```bash dotnet add package Polhem.Hosting -dotnet add package Polhem.JsonRpc.AspNetCore --version 1.2.0 +dotnet add package Polhem.JsonRpc.AspNetCore --version 1.4.0 dotnet add package Polhem.Db ``` diff --git a/README.zh-TW.md b/README.zh-TW.md index 47ce02fb..d6ad373f 100644 --- a/README.zh-TW.md +++ b/README.zh-TW.md @@ -73,7 +73,7 @@ Polhem Framework 是一套採用 **N-Tier + Clean Architecture + MVVM** 混合 ```bash dotnet add package Polhem.Hosting -dotnet add package Polhem.JsonRpc.AspNetCore --version 1.2.0 +dotnet add package Polhem.JsonRpc.AspNetCore --version 1.4.0 dotnet add package Polhem.Db ``` diff --git a/docs/en/getting-started/getting-started.md b/docs/en/getting-started/getting-started.md index 360e81bc..032d60d0 100644 --- a/docs/en/getting-started/getting-started.md +++ b/docs/en/getting-started/getting-started.md @@ -26,7 +26,7 @@ Each step links to the document that covers it in depth. Everything shown here i dotnet new web -o MyApp.Server cd MyApp.Server dotnet add package Polhem.Hosting -dotnet add package Polhem.JsonRpc.AspNetCore --version 1.2.0 +dotnet add package Polhem.JsonRpc.AspNetCore --version 1.4.0 dotnet add package Polhem.Db dotnet add package Microsoft.Data.Sqlite ``` diff --git a/docs/zh-TW/getting-started/getting-started.md b/docs/zh-TW/getting-started/getting-started.md index 56f5673d..3f15276f 100644 --- a/docs/zh-TW/getting-started/getting-started.md +++ b/docs/zh-TW/getting-started/getting-started.md @@ -1,4 +1,4 @@ - + # 快速上手 [English](../../en/getting-started/getting-started.md) · [← 文件索引](../README.md) @@ -26,7 +26,7 @@ dotnet new web -o MyApp.Server cd MyApp.Server dotnet add package Polhem.Hosting -dotnet add package Polhem.JsonRpc.AspNetCore --version 1.2.0 +dotnet add package Polhem.JsonRpc.AspNetCore --version 1.4.0 dotnet add package Polhem.Db dotnet add package Microsoft.Data.Sqlite ``` diff --git a/src/Polhem.Analyzers/Conventions/BusinessObjectAccessControlAnalyzer.cs b/src/Polhem.Analyzers/Conventions/BusinessObjectAccessControlAnalyzer.cs index fab9fb62..bd8e7cd6 100644 --- a/src/Polhem.Analyzers/Conventions/BusinessObjectAccessControlAnalyzer.cs +++ b/src/Polhem.Analyzers/Conventions/BusinessObjectAccessControlAnalyzer.cs @@ -23,10 +23,10 @@ namespace Polhem.Analyzers.Conventions /// methods are reported. /// /// - /// The methods considered are the ones an action name can resolve to, by the same rule as - /// JsonRpcMethod.IsResolvableAction (Polhem.JsonRpc.Server): public, non-static, non-generic, ordinary (not an - /// accessor, operator or constructor), taking exactly one parameter, and not an override of a - /// member. Reporting anything else would be noise about methods that cannot be + /// The methods considered are the ones an action name can reach, by the rule of + /// JsonRpcMethod.IsResolvableAction (Polhem.JsonRpc.Server) narrowed by PolhemMethodPolicy to one + /// parameter: public, non-static, non-generic, ordinary (not an accessor, operator or constructor), taking exactly + /// one parameter, and not an override of a member. Reporting anything else would be noise about methods that cannot be /// called; missing any of them would leave a callable method unreported. /// /// @@ -113,9 +113,11 @@ private static void AnalyzeType( /// The method to test. /// true when the method forms part of the API surface. /// - /// Mirrors JsonRpcMethod.IsResolvableAction, which the dispatcher applies before the framework - /// looks for an access declaration. Static methods, accessors and generic methods are excluded because - /// the dispatcher refuses to resolve an action name to them, not because they happen to be rare. + /// Mirrors what an action name can reach: JsonRpcMethod.IsResolvableAction, which the dispatcher + /// applies before the framework looks for an access declaration, narrowed to one parameter by + /// PolhemMethodPolicy, which refuses the streaming methods the dispatcher also resolves. Static + /// methods, accessors and generic methods are excluded because the dispatcher refuses to resolve an action + /// name to them, not because they happen to be rare. /// private static bool IsApiCandidate(IMethodSymbol method) => method.MethodKind == MethodKind.Ordinary diff --git a/src/Polhem.Api.Core/JsonRpc/ActionPayloadType.cs b/src/Polhem.Api.Core/JsonRpc/ActionPayloadType.cs index 91e50a09..61bea4d9 100644 --- a/src/Polhem.Api.Core/JsonRpc/ActionPayloadType.cs +++ b/src/Polhem.Api.Core/JsonRpc/ActionPayloadType.cs @@ -47,8 +47,8 @@ internal static class ActionPayloadType /// Returns the type an encoded body addressed to is decoded into. /// /// - /// A resolved action; JsonRpcMethod.IsResolvableAction (Polhem.JsonRpc.Server) guarantees it takes - /// exactly one parameter. + /// A resolved action. It takes exactly one parameter: PolhemMethodPolicy (Polhem.Api.Server) admits no + /// other, although JsonRpcMethod.IsResolvableAction also resolves a streaming method that takes two. /// /// The request counterpart of the parameter type when one exists; otherwise the parameter type. public static Type Resolve(MethodInfo method) diff --git a/src/Polhem.Api.Server/Dispatch/PolhemMethodPolicy.cs b/src/Polhem.Api.Server/Dispatch/PolhemMethodPolicy.cs index edd0c4b7..2e375be4 100644 --- a/src/Polhem.Api.Server/Dispatch/PolhemMethodPolicy.cs +++ b/src/Polhem.Api.Server/Dispatch/PolhemMethodPolicy.cs @@ -5,12 +5,21 @@ namespace Polhem.Api.Server.Dispatch { /// - /// Admits a business-object method when it, its base definition or its class carries + /// Admits a business-object method that takes one parameter when it, its base definition or its class carries /// . /// /// + /// /// Whether the caller may use the method with the payload format it chose is checked later, by /// , which reads the format from the request's envelope. + /// + /// + /// The dispatcher also resolves a streaming method, whose second parameter is an + /// , and leaves the decision to this policy. The framework does not stream: its + /// items would leave without , which + /// applies to the return value only. So such a method is refused, whatever + /// it declares. + /// /// public sealed class PolhemMethodPolicy : IJsonRpcMethodPolicy { @@ -18,7 +27,7 @@ public sealed class PolhemMethodPolicy : IJsonRpcMethodPolicy public bool IsCallable(MethodInfo method) { ArgumentNullException.ThrowIfNull(method); - return ApiAccessValidator.FindAccessControl(method) != null; + return method.GetParameters().Length == 1 && ApiAccessValidator.FindAccessControl(method) != null; } } } diff --git a/src/Polhem.Api.Server/README.md b/src/Polhem.Api.Server/README.md index e3a9b02b..ebc47235 100644 --- a/src/Polhem.Api.Server/README.md +++ b/src/Polhem.Api.Server/README.md @@ -24,7 +24,7 @@ - The protocol is [`Polhem.JsonRpc.Server`](https://github.com/polhem-dev/polhem-jsonrpc): its dispatcher parses `ProgId.Action` method identifiers and invokes the target method. Which methods an action name can reach is decided - by `JsonRpcMethod.IsResolvableAction` there. + by `JsonRpcMethod.IsResolvableAction` there, narrowed by `PolhemMethodPolicy` to methods with one parameter. - `Dispatch/` plugs Polhem into it: `PolhemObjectFactory` checks the API key and the `Authorization` header of an HTTP call and obtains the business object from `IBusinessObjectFactory`; `PolhemAccessFilter` applies `[ApiAccessControl]`; `PolhemPayloadFilter` opens and writes the payload envelope through diff --git a/src/Polhem.Api.Server/README.zh-TW.md b/src/Polhem.Api.Server/README.zh-TW.md index b2f5ec11..5fa2b7a4 100644 --- a/src/Polhem.Api.Server/README.zh-TW.md +++ b/src/Polhem.Api.Server/README.zh-TW.md @@ -23,7 +23,7 @@ - 協定由 [`Polhem.JsonRpc.Server`](https://github.com/polhem-dev/polhem-jsonrpc) 處理:它的 dispatcher 解析 `ProgId.Action` 方法識別碼並呼叫目標方法。一個 action 名稱能呼叫到哪些方法,由該套件的 - `JsonRpcMethod.IsResolvableAction` 決定。 + `JsonRpcMethod.IsResolvableAction` 決定,再由 `PolhemMethodPolicy` 限縮為只有一個參數的方法。 - `Dispatch/` 把 Polhem 接上它:`PolhemObjectFactory` 檢查 HTTP 呼叫的 API key 與 `Authorization` header,並從 `IBusinessObjectFactory` 取得商業物件;`PolhemAccessFilter` 套用 `[ApiAccessControl]`;`PolhemPayloadFilter` 經 `Polhem.JsonRpc.Payload.Server` 開啟與寫出 payload 外殼;`PolhemParameterBinder` 繫結參數;`PolhemExceptionMapper` 對應錯誤。 diff --git a/tests/Polhem.Api.Server.UnitTests/Dispatch/PolhemMethodPolicyTests.cs b/tests/Polhem.Api.Server.UnitTests/Dispatch/PolhemMethodPolicyTests.cs new file mode 100644 index 00000000..47c35fb1 --- /dev/null +++ b/tests/Polhem.Api.Server.UnitTests/Dispatch/PolhemMethodPolicyTests.cs @@ -0,0 +1,69 @@ +using System.ComponentModel; +using System.Reflection; +using Polhem.Api.Server.Dispatch; +using Polhem.Definition.Attributes; +using Polhem.Definition.Security; +using Polhem.JsonRpc.Server; + +namespace Polhem.Api.Server.UnitTests.Dispatch +{ + /// + /// Which business-object methods lets an action name reach. + /// + public class PolhemMethodPolicyTests + { + private static bool IsCallable(string name) + => new PolhemMethodPolicy().IsCallable(typeof(SampleObject).GetMethod(name, BindingFlags.Public | BindingFlags.Instance)!); + + [Fact] + [DisplayName("A method with one parameter that declares its access is callable")] + public void IsCallable_DeclaredOneParameter_ReturnsTrue() + { + Assert.True(IsCallable(nameof(SampleObject.Get))); + } + + [Fact] + [DisplayName("A method with one parameter that declares no access is not callable")] + public void IsCallable_UndeclaredOneParameter_ReturnsFalse() + { + Assert.False(IsCallable(nameof(SampleObject.Undeclared))); + } + + [Theory] + [DisplayName("A streaming method is not callable, whether it declares its access itself or through its class")] + [InlineData(typeof(SampleObject), nameof(SampleObject.StreamDeclared))] + [InlineData(typeof(DeclaredClassObject), nameof(DeclaredClassObject.Stream))] + public void IsCallable_StreamingMethod_ReturnsFalse(Type type, string name) + { + var method = type.GetMethod(name, BindingFlags.Public | BindingFlags.Instance)!; + + Assert.True(JsonRpcMethod.IsResolvableAction(method)); + Assert.False(new PolhemMethodPolicy().IsCallable(method)); + } + + public sealed record SampleArgs(int Value); + + public sealed record SampleItem(int Value); + + public sealed class SampleObject + { + public int Offset { get; set; } + + [ApiAccessControl(ApiProtectionLevel.Encrypted, ApiAccessRequirement.Authenticated)] + public int Get(SampleArgs args) => args.Value + Offset; + + public int Undeclared(SampleArgs args) => args.Value + Offset; + + [ApiAccessControl(ApiProtectionLevel.Encrypted, ApiAccessRequirement.Authenticated)] + public Task StreamDeclared(SampleArgs args, IJsonRpcStream items) => Task.FromResult(args.Value + Offset); + } + + [ApiAccessControl(ApiProtectionLevel.Encrypted, ApiAccessRequirement.Authenticated)] + public sealed class DeclaredClassObject + { + public int Offset { get; set; } + + public Task Stream(SampleArgs args, IJsonRpcStream items) => Task.FromResult(args.Value + Offset); + } + } +} diff --git a/tools/Polhem.Templates/content/polhem-avalonia/src/PolhemApp.Server/PolhemApp.Server.csproj b/tools/Polhem.Templates/content/polhem-avalonia/src/PolhemApp.Server/PolhemApp.Server.csproj index d7013ba1..f6051197 100644 --- a/tools/Polhem.Templates/content/polhem-avalonia/src/PolhemApp.Server/PolhemApp.Server.csproj +++ b/tools/Polhem.Templates/content/polhem-avalonia/src/PolhemApp.Server/PolhemApp.Server.csproj @@ -11,7 +11,7 @@ - + diff --git a/tools/Polhem.Templates/content/polhem-blazor-wasm/src/PolhemApp.Server/PolhemApp.Server.csproj b/tools/Polhem.Templates/content/polhem-blazor-wasm/src/PolhemApp.Server/PolhemApp.Server.csproj index 1b400517..7abfa809 100644 --- a/tools/Polhem.Templates/content/polhem-blazor-wasm/src/PolhemApp.Server/PolhemApp.Server.csproj +++ b/tools/Polhem.Templates/content/polhem-blazor-wasm/src/PolhemApp.Server/PolhemApp.Server.csproj @@ -11,7 +11,7 @@ - +