Remediation guides

Fixing Broken Access Control: Ownership, Roles and Tenant Checks

Fix broken access control with server-side ownership checks, tenant isolation and deny-by-default permissions. Includes PHP query and regression test examples.

Broken access control allows a user to perform an action or read data outside their intended permissions. An invoice identifier changed in a URL may be the first visible example, but the underlying defect can affect exports, mobile APIs, background jobs and administrative functions as well.

Start with the permission rule, then fix every place that should enforce it. Replacing sequential identifiers with random ones makes guessing harder; it does not establish permission.

Map the affected operations

List each route that reads or changes the affected record type. Include list, search, download, update, delete, bulk action and asynchronous job endpoints. Document the acting identity, tenant, resource owner, role and required state for each operation.

A permission matrix should describe positive and negative cases. For example, an accounts user might read invoices in their own organisation but cannot change banking details, approve payments or retrieve another organisation's documents.

Scope database access to trusted identity

The following PHP/PDO example illustrates a record lookup after the application has authenticated the user and established their active tenant membership. The session values must come from that trusted process, rather than request fields.

$invoiceId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$invoiceId || empty($_SESSION['tenant_id'])) {
    http_response_code(404);
    exit;
}

$statement = $pdo->prepare(
    'SELECT id, reference, total FROM invoices
     WHERE id = :id AND tenant_id = :tenant'
);
$statement->execute([
    'id' => $invoiceId,
    'tenant' => $_SESSION['tenant_id'],
]);
$invoice = $statement->fetch(PDO::FETCH_ASSOC);
if (!$invoice) {
    http_response_code(404);
    exit;
}

This query demonstrates tenant scoping only. A separate policy must still determine whether this particular role can read the invoice and which fields it may receive. Parameterised queries prevent SQL injection; they do not supply those business permissions.

Enforce the decision at every entry point

Put shared permission rules in a policy or service layer used by all relevant request handlers. Deny access when the identity, tenant membership or policy decision is missing. Keep authentication, authorisation and request validation as explicit steps.

For updates, authorise the requested action and allow only permitted fields. Ignore client-supplied owner, role, approval and tenant values unless an independently authorised workflow allows their change. Where permissions or record state can change concurrently, perform the relevant checks and update within an appropriate transaction.

Downloads need the same checks as JSON responses. A private object reference or a signed storage URL should be issued only after authorisation; the signing process must not turn any supplied storage key into a downloadable file.

Add regression tests that prove separation

ScenarioExpected result
Permitted user requests their own tenant's invoiceOnly authorised fields are returned.
Same user requests a known invoice in another tenantNo invoice content is returned.
Read-only user submits an updateState remains unchanged.
User changes tenant or owner fields in a requestThe change is rejected or safely ignored.
Suspended user or expired session repeats the requestAccess is refused.
Background worker processes a supplied record identifierThe job's authorised tenant and action are enforced.

Use controlled records and assert the database state as well as the HTTP response. A rejected response is insufficient if an unauthorised change has already happened.

Verify and close the finding

Retest the original proof, the related operations and each affected role. Record the deployed build, test identities and evidence of the new behaviour. Review whether previously accessible records or credentials need additional investigation; a code fix cannot establish what happened before it was deployed.

Further reading: OWASP authorisation guidance.

Read our testing methodology or discuss your assessment.

See your first test report this week.

Book a 30 minute demo. We will verify one of your targets with you and run a test against it, so you can judge the results on your own systems.

Book a demo