Authorization with Strapi policies

Strapi policies decide whether a request may reach a controller, which makes them the place for ownership and business rules. This page explains how to scope them, read the user, and return errors clients can act on.

On this page

When Strapi policies are the right place to enforce authorization

Consider a team where editors and end users may only change the entries they created. A role grant can say that a user may update articles, but it cannot say that the user may update only the articles they own. That per-request decision is where a Strapi policy fits: a policy is a function that runs before a route handler and answers one question, whether this request may proceed. It receives the request context, reads the authenticated user from ctx.state.user, and returns true or false. Policies do not change the response or transform data. They gate access.

The distinction from Users & Permissions matters for design. Role permissions in the Users & Permissions plugin decide which actions a role may call on which content types, and they are the right tool for coarse access such as who can read published articles at all. Middlewares are different again: they wrap the request or the Document Service call to add behavior such as logging, timing or data shaping, and they do not normally stop a request on the basis of who is asking. A custom policy is worth writing when the rule depends on the request or the stored record, such as ownership, a status field or a combination of user attributes, and when that rule must be enforced consistently across several routes. Before you write one, confirm in your own project which routes already use default permissions, because a policy attached to a route only protects that route, and a global policy changes behavior everywhere it is registered.

Plan the decision before the code. List every route that exposes the protected content, including custom controllers and any GraphQL or populate paths that can return the same entries, and decide which of them the policy must cover. Decide what a denied request returns and confirm that your Strapi version and error handling produce the status code your clients expect. Finally, agree who owns the rule when a business requirement changes, since a policy that only one developer understands becomes a risk during an upgrade.

Global policies versus route policies

A global policy is registered for the whole server and applies to every route that references it, while a route policy is attached to one route in that route's config block. The difference sets the blast radius of a change. A global policy that is edited changes behavior for every endpoint that uses it, so a fix can reach routes nobody intended to touch. A route policy changes only the endpoint it is attached to, which keeps the effect local but means the protection exists only where someone remembered to add it.

  • Blast radius: a global change reaches every route that uses it, a route change reaches one endpoint.
  • Default coverage: global rules protect new routes automatically, route rules protect only what is listed.
  • Visibility: route policies sit next to the handler they guard, so the rule and the endpoint are read together.
  • Ordering: check how policies combine in your Strapi version before you rely on a global rule to override a route rule.
  • Naming: name policies by the rule they enforce so that reviewers can tell from the route config what is being checked.

Before approving a design, ask the team to list every policy each route inherits and every policy it declares, and confirm both lists against the Strapi version in use.

How to build an ownership policy step by step

This sequence turns an ownership rule into a Strapi policy that runs before a route handler, so a request only reaches the controller when the signed-in user owns the target document. Each step ends with a check you can run against your own project.

  1. Create the policy file in your project

    Add a policy file under src/policies/ (for example, is-owner.ts in an illustrative project) and export a function that receives the context, the policy config and the Strapi instance. Start with a stub that returns true only for a logged-in user, so the wiring is verified before any ownership logic exists. Check that the file loads by restarting Strapi and confirming it starts without errors. If your project uses JavaScript, the same structure applies with a .js file.

  2. Read the authenticated user from the context

    Inside the policy, read the user from ctx.state.user, which Strapi sets when a request carries a valid token or session. If the value is missing, return false so Strapi responds with a 403 error for the request. Keep this check separate from the ownership comparison so that anonymous and authenticated requests fail for clear reasons. Test with an anonymous request and confirm the policy denies it before any database read happens.

  3. Load the target document with the Document Service

    Read the documentId from the route params and load the entry with strapi.documents('api::article.article').findOne(), using the content type UID from your own schema. Request only the fields the ownership check needs, such as an owner relation, so the policy does not expose more data than necessary. If the document does not exist, return false or a not-found response that matches your API conventions. Verify by calling the route with a known document ID and logging the returned owner field in a development environment.

  4. Compare ownership against the signed-in user

    Compare the owner's ID or relation with ctx.state.user.id, and return true only when they match. Use the owner field your content model defines, and confirm in your schema whether the relation is a single relation to the user or another structure. Be explicit about edge cases such as documents with no owner, which should be denied by default. Check the result with two test users: the owner should pass, and a second authenticated user on the same document should be refused.

  5. Attach the policy to the route

    In the route definition file, add the policy name to the route's config.policies array so it runs before the controller. Use a route policy, not a global policy, when the rule applies to one endpoint, because a global policy would run on every route. Confirm the route file loads and that Strapi lists the policy in the route config. Then repeat the test matrix on that route: anonymous request, non-owner, and owner, each returning the expected status.

  6. Confirm the result for each request type

    Run a short test matrix against the attached route and record the status for each case: anonymous, authenticated non-owner, owner, and a request for a document that does not exist. Anonymous and non-owner requests should be refused, the owner should reach the controller, and the missing document should return an error that does not reveal other records. Automate these cases with your test runner before the route ships so later changes do not remove the rule silently. Keep the test output as evidence for your review team.

Which policy configuration fits which business rule

This table compares four ways to enforce a rule in Strapi: a global policy, a route policy with arguments, a policy that calls a service, and a permission-based check with no custom code. Read each row for when the option fits, where it falls short, and what to test before relying on it. Confirm each option's behavior against the Strapi 5 version your project runs, because policy and permission details change between releases.

OptionWhen it fitsLimits and what to test
Global policyA rule that applies to every route in the API, such as requiring a signed-in user for all custom endpointsHard to scope per route; test that public routes still respond and that the rule does not block the admin panel or health checks
Route policy with argumentsA rule tied to one route, such as checking a role or a status value passed in the route configArguments are fixed in code, so they cannot vary per request; test each route in both allowed and denied cases
Policy that calls a serviceAn ownership rule that needs a database lookup, such as allowing edits only when the user owns the entryAdds a query per request and can drift from the controller's view of the data; test ownership on other users' entries and on missing IDs
Permission-based check, no custom codeRole-level access to content types and actions configured in the admin panelCannot express row-level ownership or field-level rules; confirm what the Users and Permissions and roles settings support in your version

Returning errors clients can act on

The status code tells the client what to do next, so policies should return distinct codes for distinct failures. A 401 Unauthorized means the request carried no valid identity, so the client should authenticate and retry. A 403 Forbidden means Strapi knows who the caller is and a policy denied the action, so retrying with the same token will fail again. A 404 Not Found is the choice for resources the caller must not know exist, such as another user's draft entry, because a 403 on that record confirms it is there. Decide per content type which of these leaks information you can accept, and write that decision down before the build starts so that every custom route follows the same rule.

Policy failures reach API consumers through the standard error envelope of each API, so check what your installed version returns for a denied request on a custom route, a core route and a GraphQL operation. Confirm the exact body shape, the error name and whether a policy can set a custom message without leaking internal logic. Thrown errors from a policy or controller should be built with the framework's error helpers rather than plain strings, so the status code is set deliberately. For REST and GraphQL to stay consistent, map each rule to one error category and one message template in a shared helper, and add tests that send the same denied request through both transports and compare status and error name.

Treat message wording as part of the contract. Front-end teams will branch on error names or codes, so changing a message from one release to the next can break their handling even when the status stays the same. Ask your Strapi partner or the vendor which error fields are stable in the version you run, and record them in your API documentation. Also confirm how Strapi formats errors raised inside GraphQL resolvers in that version, since the response structure there can differ from REST. Verify these points against the installed release rather than assuming behavior from older Strapi versions.

How a request passes through policies before the controller

The diagram starts at the client, which is outside the trust boundary, and follows an authenticated request into Strapi. Route matching selects the handler, then global and route policies run in order before any controller code executes. The user object is read from the request context at the policy stage, and a denied check returns an error response without reaching the Document Service. Check that every policy that guards a content write is attached to the route it protects.

Request flow through policies, with the denial branch and the point where the user is read.

Security mistakes that make policies ineffective

A Strapi policy only protects what it actually checks, on the path a request really takes. The mistakes below share one root cause: the access decision is made on something the client controls or on a value that was never verified, so each one can be closed with a specific review step.

Relying on the client

Hiding buttons in the admin panel or in a front end does not stop a direct API call. Every custom route that changes or reads content needs a server-side policy, and the policy must run for the same route that the front end calls. Review the route list and confirm each custom endpoint has a policy attached, including routes added later by plugins or generated code. Test with a token for a user who should be refused, not only with the interface.

Strapi SSO docs

Checking the wrong document

A policy that loads a record by the ID in the URL, but then updates a different ID taken from the request body, approves a change it never inspected. The same risk appears when a policy checks a draft while the public API serves the published version. Confirm the policy resolves the exact document the controller will act on, using the same identifier and status, and that a request cannot point the check at one record and the write at another.

Trusting user-supplied owner fields

If the request body can set an owner or author field, a user can assign content to someone else or claim ownership of records they do not control. Ownership should be written by the server from the authenticated user in the context, and any client-supplied owner value should be ignored or rejected. Review every create and update path, including bulk imports and integrations that call the same services.

Missing populate paths

A policy on the entry route does not protect relations that a populate query pulls in. A user may be allowed to read a public article but still receive private related records, drafts or user data through a nested populate. Review which relations each route can return, restrict populate depth, and confirm that the permissions for related content types are enforced for the same user. Sanitizing output is part of the same check.

Failing open on errors

A policy that catches an exception and returns true lets the request through whenever the user lookup, database query or token parsing fails. The safe default is to deny on any error and return a proper error status. Review every return path in each policy, test what happens when the database is unavailable or the user is missing from the context, and confirm that a thrown error does not silently grant access.

Global policies that get bypassed

Global policies apply to every route, but a route can be configured to skip or replace a policy, and a new route can be added without the expected protection. Confirm which policies are global and which are attached per route, and make sure a route that should be protected is not left with an empty policy list. Keep a written list of expected protections and compare it against the route configuration after each change.

Questions teams ask before building Strapi authorization rules

What drives the cost of building Strapi authorization policies?

The main driver is how many distinct ownership and business rules the policies must enforce, and how many routes and content types they touch. A small set of global rules on one content type costs far less to design, build and test than role-based rules that depend on relations, status fields and team membership across many endpoints. Other cost drivers include how many roles exist, whether rules change often, and how much test coverage the team expects before release. In a quote, ask for the list of rules, the routes each rule covers, the test cases planned, and whether the work includes handover documentation. Strapi itself is open source, so the cost is labor and review, not a license fee for the policies.

How long does a typical Strapi policy change take?

Duration depends mainly on rule complexity. A single ownership check on one route is a small change that can be reviewed and tested quickly, while a rule that reads the user's role, a relation and a workflow status across several endpoints needs design, edge-case tests and a review of how it interacts with existing permissions. No supplied timing data exists for this work, so ask a partner to state the planned phases, the number of rules in scope and the testing included before agreeing a timeline. Your own team can estimate more reliably once the rule list is written down.

What must our team provide before a partner starts?

The partner needs an accurate data model and a role matrix. The data model shows the content types, relations and fields that identify an owner, such as an author relation or a team field. The role matrix shows which roles may read, create, update, publish or delete each content type, and which exceptions apply. Also provide the current Strapi version, the database in use, a test environment with realistic but non-sensitive data, and the list of routes that are public or customized. Without these inputs, rules tend to be guessed, and guessed rules are the most common source of rework.

Can an in-house engineer build Strapi authorization policies?

Yes, a developer comfortable with Node.js and the Strapi server code can write policies, provided they understand how the user is read from the request context and how errors should be returned. The harder part is consistent design: deciding which rules are global and which belong to a single route, and testing that owner checks cannot be bypassed through other endpoints or populated relations. Teams with limited Strapi experience often benefit from an external review of the policy design and tests. Ask any partner whether it will hand over code, tests and documentation so your team can maintain the rules afterward.

Do policies require Strapi's Enterprise Edition?

No. Custom policies are part of the server code in your project, and the Community Edition is licensed under the MIT Expat license, so you can write and run them without an Enterprise license. The Enterprise Edition, under a separate Strapi license, adds features such as audit logs and review workflows, which are listed on the self-hosted pricing page. Those features help you prove who changed content and when, but they do not replace authorization logic. Confirm your edition needs against the current pricing page and your compliance requirements before deciding.

Do audit logs or review workflows replace policy checks?

No. Audit logs record activity and review workflows control editorial steps, but neither stops an unauthorized request from reading or changing data on its own. Policies enforce who may call an endpoint and which records they may touch. Treat logs and workflows as evidence and process controls that sit on top of enforced authorization. Ask a partner to show how denied requests are logged and tested, and confirm with your compliance team which evidence they need from the Enterprise features.

What should a quote for Strapi policies include?

A useful quote lists the rules in scope, the content types and routes each rule covers, the roles involved, and the test approach, including negative tests that confirm other users are denied. It should state what is delivered, such as policy code, Jest or Supertest tests, documentation and a handover session, and what is excluded, such as changes to the Users & Permissions plugin or the admin panel. Ask which Strapi version and Node.js version the work targets, whether a retest is included, and what happens if the data model changes during the build. Fixed-scope quotes are easier to compare when these items are written down.

How DeployOpen scopes and delivers Strapi authorization work

DeployOpen approaches Authorization with Strapi policies as a fixed-scope piece of work agreed in writing before build starts. We map which routes need ownership checks and business rules, build the policies in your own infrastructure, test them, and hand over the code and documentation your team needs to maintain them.

How we scope

  • We list every custom route and content operation that needs an ownership or business-rule check.
  • We record which roles, user states and relations each policy reads from the request context.
  • We agree in writing which routes use global policies and which use route-level policies, with the expected error responses.

How we build

  • We write policies that read the authenticated user from context and return consistent 401 and 403 responses.
  • We keep business rules in services so policies stay small and the same checks can be reused across routes.
  • We test allowed and denied requests for each role, including users who try to access another owner's entries.

How we hand over

  • You receive the policy code, a map of which routes each policy protects, and the test suite that covers them.
  • We document the policy ordering, how to add a new protected route, and which checks must run before any upgrade.
  • We walk your engineers through the design so they can extend the rules without rewriting the authorization model.

How we support

  • Ongoing support, Strapi 5 upgrades and continuous testing are available as retainers after handover.
  • A retainer can cover re-running the authorization tests after each Strapi minor release you adopt.
  • Scope changes to new routes or roles are quoted from a written change to the original scope.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.