Skip to content

Code Review · Access control

Should this user get the download link?

Illustrative walkthrough written for this guide. This is an authored example, not a recorded AI grading result.

Scenario and requirements

Review a report-download endpoint in a team SaaS app. Authentication already happens before this code. There are no other access checks.

Key requirements

Only completed reports from the user's organization may receive links. Missing or foreign reports return 404; unfinished reports in the user's organization return 409.

report = Report.objects.get(id=report_id)
return storage.create_download_url(report.file_key)

Public requirements

  • Users may download reports only from their own organization.
  • Issue a download link only after the report file has finished generating.
  • Return the same 404 response for a missing report and a report from another organization. Return 409 for an unfinished report in the user's organization.
  • Report IDs are random, but a user may learn an ID belonging to another organization.
  • Give a review decision, your reasoning, the required changes, and proposed tests.

Initial answer

There is already a login check, and the IDs are random, so access control looks fine. I would approve after adding a 404 response for a report that does not exist.

Feedback and evidence

You identified how to handle a missing report. Organization access and file completion are still unchecked.

From the answera 404 response for a report that does not exist
Why it mattersThis is a useful change. A report from another organization must receive the same response, too.
From the answerThere is already a login check
Why it mattersKnowing who the user is does not establish permission to access this report's organization.
From the answerthe IDs are random
Why it mattersRandom IDs do not stop a user who knows another organization's report ID. The lookup has no organization condition.
From the answerI would approve after adding a 404 response
Why it mattersApproval needs evidence that organization access and file completion are checked, not just that missing reports return 404.

Improved answer

Request changes. Looking up a report by ID alone can give a download link to someone who knows another organization's report ID.

Look up the report using both its ID and an organization ID verified by the server. Return 404 if that lookup finds nothing.

For a report in the user's organization, return 409 while generation is incomplete. Issue a link only after the file is ready.

I could approve a revision that meets these published requirements.

Alternatives and boundaries

Different implementations can be valid when they meet the same public requirements.

What would count as an equivalent fix?

A database access policy is also a valid approach if it enforces the same organization boundary, response behavior, and completion check. The protection matters more than a particular implementation or pattern name.

These are proposed checks for this example. They are not reported test results or a complete security audit.

Proposed checks

These are proposed checks for this example, not reported test results.

  • A completed report in the user's organization returns a download link.
  • A report from another organization and a missing report return the same 404 response.
  • An unfinished report in the user's organization returns 409 and no download link.
  • Changing an organization ID in the request cannot change the user's access rights.

Put your reasoning into practice

What would you change, and why?

Choose a scenario and explain your own decision. Guest practice offers a selection of scenarios, subject to a daily limit. Sign in for detailed feedback and saved history.