Skip to content

Security test suite

src/test/java/org/marmotgraph/core/security verifies that the API reflects the user's permissions (ADR-055).

mvn test -Dgroups=security                                # only this suite
mvn test -Dgroups=known-security-gap -DexcludedGroups=    # the open findings

The suite is also part of a regular mvn test. It needs Docker.

Why it exists

Permission checks live in the services, so nothing in a controller shows whether a check exists. Three API versions delegate to each other, and a unit test with a mocked PermissionService can't detect a missing call or a listing that forgets to restrict its query. This suite therefore tests what users actually get, through the API.

How it works

flowchart LR
    subgraph test["Test JVM"]
        Suite["Test classes"]
        Idp["FakeIdentityProvider<br/>local OIDC server,<br/>RS256 tokens"]
        App["Complete application<br/>production token validation,<br/>claim mapping and aspect"]
    end
    pg[("PostgreSQL<br/>(Testcontainers)")]

    Suite -- "token for persona" --> Idp
    Suite -- "HTTP requests (MockMvc)" --> App
    App -- "validates against" --> Idp
    App --> pg

Only Keycloak is replaced. Token validation, the mapping of claims to roles and the AuthenticationAspect are the production ones.

classDiagram
    class SecurityTestSupport {
        <<abstract>>
        IDENTITY_PROVIDER
        POSTGRES
        getAs(persona, path)
    }
    class EndpointAuthorizationTest {
        PERSONAS
    }
    class SecurityFixture {
        create()
        invite(persona, instance)
    }
    class Persona {
        <<record>>
        label
        grants
        invitedTo
        hasGlobally(functionality)
        hasOnSpace(functionality, space)
        hasOnInstance(functionality, space, instance)
    }
    class EndpointCatalog {
        all() List~Endpoint~
    }
    class Access {
        <<sealed interface>>
    }
    class Public
    class AuthenticatedOnly
    class Filtered
    class NotImplemented
    class Requires
    class EndpointRequests {
        handlers()
    }
    class FakeIdentityProvider

    Access <|.. Public
    Access <|.. AuthenticatedOnly
    Access <|.. Filtered
    Access <|.. NotImplemented
    Access <|.. Requires
    EndpointCatalog "1" *-- "*" Access
    SecurityTestSupport --> SecurityFixture
    SecurityTestSupport --> FakeIdentityProvider
    EndpointAuthorizationTest --|> SecurityTestSupport
    EndpointAuthorizationTest --> EndpointCatalog
    EndpointAuthorizationTest --> EndpointRequests
    EndpointAuthorizationTest --> Persona
    Requires ..> Persona : evaluates

The building blocks

EndpointCatalog is the authoritative classification of every endpoint in every API version. It states what an endpoint is supposed to require:

Access Meaning
Public(justification) No authentication
AuthenticatedOnly(justification) A valid token is enough
Filtered Any user, and the result is reduced to what they can read
NotImplemented Exists but has to provide nothing to anybody
Requires(...) A global, space or instance level functionality

Persona is a user with a combination of roles. The expectations about what each role grants are written down separately from RoleMapping on purpose: a change in production can't silently change what the tests expect.

SecurityFixture creates test data through the API, the way a real administrator would.

Test classes

Test Verifies
EndpointCatalogCompletenessTest The catalog matches the registered endpoints: none missing, stale or classified twice
EndpointAuthenticationTest Endpoints reject missing and invalid tokens
EndpointAuthorizationTest Every endpoint called as every persona: roles on the space, roles on another space, global roles, invited
ReadVisibilityTest Computes what each combination of roles and invitations may read, per instance and stage, and compares it with every read path
SensitiveDataTest Restricted properties and secrets aren't exposed
ManipulationProtectionTest Abuse scenarios: reaching a resource through another one, escaping a space via identifiers or moves, invitation lifecycle
SpecificationAndRobustnessTest Specifications and robustness of the API
RoleFunctionalityContractTest Pins the Persona expectations against RoleMapping and Functionality
KnownSecurityGapsTest Open findings (see below)

EndpointAuthorizationTest only counts a rejection if it's a 401 or 403, as a 400 or 404 doesn't prove that a check took place.

When you change an endpoint

flowchart TD
    A["Add, change or remove an endpoint"] --> B["Update EndpointCatalog<br/>with the intended requirement"]
    B --> C{"Exposes instance<br/>or personal data?"}
    C -- yes --> D["Cover it in ReadVisibilityTest<br/>or SensitiveDataTest"]
    C -- no --> E["mvn test -Dgroups=security"]
    D --> E

Declaring an entry looks like this:

declare(V4, "GET", "/instances/{id}/validation", READ_INSTANCE).releasedRequires(READ_RELEASED_INSTANCE);
declare(V4, "POST", "/invitations/redemption", authenticatedOnly("The token is the capability (ADR-041)"));

State the intention

Write down what the endpoint should require. Copying what the implementation does would defeat the purpose.

An unclassified endpoint fails the build.

Known gaps

KnownSecurityGapsTest (tag known-security-gap) states the desired behavior for open findings. Those tests fail today and are excluded from the build by the excludedGroups property in pom.xml, so the findings remain visible without blocking other work.

When a finding is fixed, move its test to the matching class. If the behavior turns out to be intended, record that in an ADR and turn the test around.