Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .claude/rules/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`;
`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
Expand Down
5 changes: 3 additions & 2 deletions .claude/skills/polhem-add-bo-method/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ public virtual <Action>Result <Action>(<Action>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<T>`, 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)

Expand Down
2 changes: 1 addition & 1 deletion PolhemJsonRpc.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
and TemplateJsonRpcVersionTests (tests/Polhem.Templates.UnitTests) fails when they disagree.
-->
<PropertyGroup>
<PolhemJsonRpcVersion>[1.2.0]</PolhemJsonRpcVersion>
<PolhemJsonRpcVersion>[1.4.0]</PolhemJsonRpcVersion>
</PropertyGroup>
<Import Project="$(MSBuildThisFileDirectory)PolhemJsonRpc.local.props"
Condition="Exists('$(MSBuildThisFileDirectory)PolhemJsonRpc.local.props')" />
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion README.zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion docs/en/getting-started/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
4 changes: 2 additions & 2 deletions docs/zh-TW/getting-started/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- source: en/getting-started/getting-started.md blob: 360e81bc649c1b1289800ca3075b6717245ec7f8 -->
<!-- source: en/getting-started/getting-started.md blob: 032d60d0e9a99b2aabca4a1a2582253464579a49 -->
# 快速上手

[English](../../en/getting-started/getting-started.md) · [← 文件索引](../README.md)
Expand Down Expand Up @@ -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
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ namespace Polhem.Analyzers.Conventions
/// methods are reported.
/// </para>
/// <para>
/// The methods considered are the ones an action name can resolve to, by the same rule as
/// <c>JsonRpcMethod.IsResolvableAction</c> (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
/// <see cref="object"/> 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
/// <c>JsonRpcMethod.IsResolvableAction</c> (Polhem.JsonRpc.Server) narrowed by <c>PolhemMethodPolicy</c> 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 <see cref="object"/> member. Reporting anything else would be noise about methods that cannot be
/// called; missing any of them would leave a callable method unreported.
/// </para>
/// </remarks>
Expand Down Expand Up @@ -113,9 +113,11 @@ private static void AnalyzeType(
/// <param name="method">The method to test.</param>
/// <returns><c>true</c> when the method forms part of the API surface.</returns>
/// <remarks>
/// Mirrors <c>JsonRpcMethod.IsResolvableAction</c>, 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: <c>JsonRpcMethod.IsResolvableAction</c>, which the dispatcher
/// applies before the framework looks for an access declaration, narrowed to one parameter by
/// <c>PolhemMethodPolicy</c>, 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.
/// </remarks>
private static bool IsApiCandidate(IMethodSymbol method)
=> method.MethodKind == MethodKind.Ordinary
Expand Down
4 changes: 2 additions & 2 deletions src/Polhem.Api.Core/JsonRpc/ActionPayloadType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ internal static class ActionPayloadType
/// Returns the type an encoded body addressed to <paramref name="method"/> is decoded into.
/// </summary>
/// <param name="method">
/// A resolved action; <c>JsonRpcMethod.IsResolvableAction</c> (Polhem.JsonRpc.Server) guarantees it takes
/// exactly one parameter.
/// A resolved action. It takes exactly one parameter: <c>PolhemMethodPolicy</c> (Polhem.Api.Server) admits no
/// other, although <c>JsonRpcMethod.IsResolvableAction</c> also resolves a streaming method that takes two.
/// </param>
/// <returns>The request counterpart of the parameter type when one exists; otherwise the parameter type.</returns>
public static Type Resolve(MethodInfo method)
Expand Down
13 changes: 11 additions & 2 deletions src/Polhem.Api.Server/Dispatch/PolhemMethodPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@
namespace Polhem.Api.Server.Dispatch
{
/// <summary>
/// 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
/// <see cref="Polhem.Definition.Attributes.ApiAccessControlAttribute"/>.
/// </summary>
/// <remarks>
/// <para>
/// Whether the caller may use the method with the payload format it chose is checked later, by
/// <see cref="PolhemAccessFilter"/>, which reads the format from the request's envelope.
/// </para>
/// <para>
/// The dispatcher also resolves a streaming method, whose second parameter is an
/// <see cref="IJsonRpcStream{T}"/>, and leaves the decision to this policy. The framework does not stream: its
/// items would leave without <see cref="Polhem.Api.Core.Conversion.ApiOutputConverter"/>, which
/// <see cref="PolhemPayloadFilter"/> applies to the return value only. So such a method is refused, whatever
/// it declares.
/// </para>
/// </remarks>
public sealed class PolhemMethodPolicy : IJsonRpcMethodPolicy
{
/// <inheritdoc/>
public bool IsCallable(MethodInfo method)
{
ArgumentNullException.ThrowIfNull(method);
return ApiAccessValidator.FindAccessControl(method) != null;
return method.GetParameters().Length == 1 && ApiAccessValidator.FindAccessControl(method) != null;
}
}
}
2 changes: 1 addition & 1 deletion src/Polhem.Api.Server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Polhem.Api.Server/README.zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 對應錯誤。
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Which business-object methods <see cref="PolhemMethodPolicy"/> lets an action name reach.
/// </summary>
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<int> StreamDeclared(SampleArgs args, IJsonRpcStream<SampleItem> items) => Task.FromResult(args.Value + Offset);
}

[ApiAccessControl(ApiProtectionLevel.Encrypted, ApiAccessRequirement.Authenticated)]
public sealed class DeclaredClassObject
{
public int Offset { get; set; }

public Task<int> Stream(SampleArgs args, IJsonRpcStream<SampleItem> items) => Task.FromResult(args.Value + Offset);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="Polhem.Hosting" Version="POLHEM_PACKAGE_VERSION" />
<PackageReference Include="Polhem.JsonRpc.AspNetCore" Version="[1.2.0]" />
<PackageReference Include="Polhem.JsonRpc.AspNetCore" Version="[1.4.0]" />
<!--#if (database == "SqlServer") -->
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.1.1" />
<!-- Above the version the driver pulls in, which carries advisories GHSA-846f-f764-76mx and GHSA-2fmv-rv2c-59x7. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="Polhem.Hosting" Version="POLHEM_PACKAGE_VERSION" />
<PackageReference Include="Polhem.JsonRpc.AspNetCore" Version="[1.2.0]" />
<PackageReference Include="Polhem.JsonRpc.AspNetCore" Version="[1.4.0]" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="10.0.14" />
<!--#if (database == "SqlServer") -->
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.1.1" />
Expand Down