Skip to content

chore(api): replace any with concrete request types in AuthServiceInterface #169

Description

@cristim

Deferred from PR LeanerCloud/cloud-commitments-cli#1737, where CodeRabbit raised it. Filing so the suggestion is tracked rather than lost.

What

AuthServiceInterface (internal/api/types.go) passes request bodies as any across the group and user API boundary:

CreateUserAPI(ctx context.Context, req any) (any, error)
UpdateUserAPI(ctx context.Context, actorUserID, userID string, req any) (any, error)
CreateGroupAPI(ctx context.Context, actorUserID string, req any) (any, error)
UpdateGroupAPI(ctx context.Context, actorUserID, groupID string, req any) (any, error)

Each implementation opens with a type assertion and returns fmt.Errorf("invalid request type") on mismatch, so a caller passing the wrong concrete type fails at runtime rather than at compile time.

Why it was not done in LeanerCloud/cloud-commitments-cli#1737

The any signature is pre-existing design; LeanerCloud/cloud-commitments-cli#1737 only added a parameter to it. Converting to concrete types means touching every implementation and every call site of AuthServiceInterface — the real service, the authServiceAdapter in internal/server/app.go, MockAuthService, the mockAuthForExchange stub, and the handler call sites — inside a PR whose purpose was closing a p1 privilege-escalation issue (LeanerCloud/cloud-commitments-cli#1550). Mixing an interface refactor into a security review makes both harder to review, and the security fix is the time-critical half.

The original reason for any is recorded in internal/auth/service_api.go: "They use any to avoid import cycles with the api package." Any fix has to address that constraint — most likely by moving the request types into a shared package, or by defining the API-side types and converting at the adapter rather than inside the service.

Scope

  • internal/api/types.go — the interface
  • internal/auth/service_api.go — the four implementations plus their type assertions
  • internal/server/app.go — authServiceAdapter
  • internal/api/mocks_test.go, internal/api/handler_ri_exchange_test.go — test doubles
  • handler call sites in internal/api/handler_groups.go, handler_users.go

Worth doing as a standalone refactor with no behaviour change, so the diff is reviewable as exactly that.

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions