Follow a Request Through a Multi-File Application
Trace an invoice download through routing, session middleware, a controller, a service, and a repository. Run interactive requests and verify the trust boundary.
To follow a request across files, trace both the caller-controlled data and the verified authority that permits the operation. List the entry point, each handoff, the access decision, and the protected result. Then test a request that should succeed and one that should be denied.
This short lecture builds on Understanding Application Architecture. You will inspect one invoice download, experiment with requests against fictional data, and finish with a review check. You can explore the reading and demos freely; sign in to save the completed lecture to your path.
Your brief: whose invoice is it?
Begin by separating what Alice may choose from what the server must establish. She can choose a requested invoice ID. She can also edit any query parameter in the URL. The application must establish her tenant from the verified session and apply that authority when selecting an invoice.
A hidden field, a dropdown populated by the frontend, or a plausible parameter name does not change who controls a request value. Keep that distinction in your notes as you move between files.
1. Follow the request across six stops
The router below is mounted at /api. For a request to /api/invoices/inv-202/download?tenantId=tenant-south, the route runs session middleware, then the controller. The service loads an invoice through a repository and asks private storage for its file.
Select a file to see how the request moves through the application.
router.get(
'/invoices/:invoiceId/download',
requireSession,
downloadInvoice
);
The browser chooses invoiceId and the tenantId query parameter. This router is mounted at /api. Its middleware runs before the controller, but you still need to inspect what each layer actually verifies.
Review question: Where does the requested object meet the verified principal’s authority? Which argument lost that connection?
These are teaching excerpts. The session-verification, database, storage, and error-handling helpers are abstracted so you can focus on the path. In a real repository, inspect those helpers before relying on their contracts. A method’s name alone is insufficient evidence.
2. Keep data and authority on separate lines
Follow three values through the same request. Two are selected by the caller. One is established by the server. The critical question is which value the controller passes as the tenant scope.
| Value | Who establishes it? | What it means |
|---|---|---|
req.params.invoiceId | The caller | Which invoice the caller requests. |
req.query.tenantId | The caller | A claimed scope. It is not proof of permission. |
req.principal.tenantId | Verified server session | The tenant within which Alice is authorized for this feature. |
The repository checks that an invoice matches an ID and a tenant. That condition looks useful, and it is useful when its tenant argument comes from a trustworthy authority. Here, the original controller obtains that argument from the query string.
Consequently, the query can be correctly parameterized and still select an unauthorized tenant’s record. SQL safety and tenant authorization are different properties. Trace both values to the query before concluding that either property holds.
3. Predict, send, and inspect a request
The lab models the shown route using two fictional invoice records. The verified session stays Alice’s North Co session. Changing the query string changes request data; it does not change her identity. For inspection, the simulator represents a downloaded PDF with its invoice metadata.
What will the server return?
tenant-northconst invoiceId = req.params.invoiceId;
const tenantId = String(req.query.tenantId);
const file = await invoiceService.download(invoiceId, tenantId);
res.type('application/pdf').send(file);
GET /api/invoices/inv-202/download?tenantId=tenant-southChoose a prediction to run the request.
Run these three comparisons
- Original controller, foreign invoice. Request
inv-202withtenant-south. Predict the response and run it. Compare the effective query tenant with Alice’s verified tenant. - Patched controller, the same request. Keep those request values and change only the implementation. Predict and run again. Which value now determines scope?
- Patched controller, a legitimate invoice. Request
inv-101, keepingtenant-southin the query string. Verify that North Co’s invoice still downloads. A useful fix preserves the allowed behavior.
Compare your observations
The original foreign-tenant request returns 200 because both selected values match South Co’s invoice. The patched version returns 404 because its lookup uses Alice’s verified North Co tenant. The legitimate North Co request returns 200 under the patched implementation even when the URL claims South Co.
A missing invoice also returns 404. That response alone does not reveal whether an object is absent or outside the caller’s authorized scope. The code trace and the paired tests explain why the boundary holds.
4. Review the fix at the handoff
Carry verified authority into the service
Before opening the patch, identify the single argument whose provenance must change. The requested invoice remains caller-controlled; the scope used to authorize it comes from the verified principal.
const invoiceId = req.params.invoiceId;
const tenantId = String(req.query.tenantId);
const file = await invoiceService.download(invoiceId, tenantId);
res.type('application/pdf').send(file);
Reveal the safer implementation
const invoiceId = req.params.invoiceId;
const tenantId = req.principal.tenantId;
const file = await invoiceService.download(invoiceId, tenantId);
res.type('application/pdf').send(file);
The service and repository now receive the tenant established by session verification. A missing scoped row stops storage access. This implements the stated tenant-wide read policy; a product with owner-only, shared, or administrator access needs its own explicit policy for those cases.
The fix belongs where the controller chooses the scope, while enforcement happens when the repository selects the protected object. Verify both ends of that contract. If another route calls the service, check whether it also supplies authorized scope.
Finally, inspect result delivery. A background export or a generated download URL introduces another handoff. The security question continues until the protected result reaches an authorized consumer.
5. Write your own request trace
Before completing the lecture, explain the path in your own words. Keep the trace short enough that another engineer can verify it against the files:
- Entry and actor: name the route and the verified principal.
- Controlled values: identify the invoice ID and query tenant.
- Authority: identify the session tenant and where the controller forwards it.
- Protected operation: connect the scoped invoice lookup to the private storage read.
- Verification: state the foreign-tenant denial and the legitimate-tenant success result.
For the underlying concepts, read the Express middleware guide, the OWASP Authorization Cheat Sheet, and the OWASP guidance on object references.
Complete your request trace
Loading question…