The Bug AI Generated Wasn't a Crash. It Was Someone Else's Data.
The Bug AI Generated Wasn't a Crash. It Was Someone Else's Data. Nothing fails. The endpoint returns 200. The JSON is well-formed. The request was authenticated, the role was correct, the permission was present โ and t
The Bug AI Generated Wasn't a Crash. It Was Someone Else's Data.
Nothing fails. The endpoint returns 200. The JSON is well-formed. The request was authenticated, the role was correct, the permission was present โ and the order in the response belongs to a different tenant.
This is the failure mode that keeps me up at night about AI-assisted backend work. Not the crash. The well-behaved leak.
I maintain rust-webx, working alongside rust-ef and rust-dix. This piece is about what a framework can do when the most dangerous mistake is not malformed code but a missing check โ a line the model never wrote because nothing in the happy path asked for it.
Authentication is not authorization, and the gap is where bugs live
Ask a model for "a secure endpoint" and it will add authentication. Often it will add a role check too. What it tends to skip is the second question: does this caller own this particular row?
That check needs two things the router does not have on its own โ a path parameter value and the loaded resource. It cannot be fully decided from route metadata.
rust-webx splits the problem. Tenant presence is a framework gate:
#[post("/api/orders")]
#[authorize(permission = "tenant.order.write", tenant)]
impl IRequest<OrderModel> for CreateOrderRequest {}
The tenant flag is compiled into RouteEntry.authorization as AuthorizeRule::tenant(), which means the endpoint catalog can be queried: catalog.requires_tenant(method, path). Permission sync tools read the same metadata the runtime enforces.
The semantics are narrow and explicit. Anonymous caller: 401. Authenticated caller with neither a tenant_id nor a tenant claim: 403. On #[authorize(dynamic, tenant)], the tenant check runs before the dynamic authorizer, so a custom authorizer cannot quietly ignore it.
Instance-level ownership stays in the handler, where the resource is available:
let order = load_order(&mut self.ctx, &req.id).await?;
req.user.require_same_tenant(&order.tenant_id)?;
require_owner and require_same_tenant turn a comparison into a typed 403. They do not replace the resource lookup; they make the step after the lookup hard to forget once you are writing the handler.
Honest boundary: a model can still omit that line. No attribute macro can see a path parameter value and a database row at compile time. The framework can make the gate loud and the check short; it cannot think for you.
A missing authorizer does not become "allow"
Here is a subtler one. Dynamic authorization delegates the decision to an IDynamicAuthorizer โ useful when permissions live in a database and a snapshot in a JWT would go stale.
Generated code sometimes registers the attribute and forgets the implementation. In many frameworks that ends up as a default-allow path.
rust-webx refuses to boot:
#[delete("/api/orders/{id}")]
#[authorize(dynamic, permission = "order.delete")]
impl IRequest<()> for DeleteOrderRequest {}
No IDynamicAuthorizer registered โ Host::build() fails. There is no fallback to "authenticated is enough." The request-time guard returns 500 as a second line of defense, but the intended failure is at startup.
#[authorize(dynamic)] can be combined with role, permission and tenant in one attribute. Rules are ANDed across attributes and any-of within a rule array. The declared rules are passed to the authorizer unchanged, with zero allocation on the fast path by using the static slices directly.
Why this matters here: fail-open behavior is the natural output of code optimized to make a test go green. An implementation that does nothing is easier to produce than one that makes a decision. Fail-closed removes that option.
Identity arrives as data, not as parsing code
Handlers in generated code often contain a few lines that decode a token, pull out claims, and hand them to a service. Those lines are a rich source of mistakes: the wrong claim, a missing check on tenant_id, a fallback to an empty string.
#[fromclaim] user: IdentityUser binds the authenticated identity as a request field. The handler reads req.user; it does not parse anything. For public endpoints that still want to know who is visiting, #[fromclaim(optional)] viewer: Option<IdentityUser> makes anonymous access explicit โ it is None, not an error.
Two #[fromclaim] IdentityUser fields on one request are a compile error. A specific #[fromclaim("key")] whose key is missing produces 400 with a field path, not a vague 403.
Response encoding follows the same ownership discipline. #[authorize] metadata is visible at runtime through IEndpointCatalog, so permission synchronization and admin navigation read the same source as the request guard. One declaration, two consumers.
Ports do not no-op
The third class of missing check is infrastructure: an idempotency store that silently allows retries, a session store that falls back to memory, an email sender that logs and continues.
rust-webx models these as ports. IIdempotencyStore, IOutbox, ISessionStore, IObjectStore, IMeter, IAdvisoryLock, IEmailSender โ some have explicit in-memory implementations you register yourself, others deliberately have no default. A missing adapter fails at startup as a DI error.
This is not a claim that the framework prevents data leaks. It is a claim about where the failure lands. A missing authorizer, a missing tenant gate, and a missing adapter all surface before a request is served rather than inside one.
What to be careful about
-
#[authorize]only sees the route pattern. Instance-level ownership still belongs in the handler, where the row is available. - Static role and permission rules use claims from the token. If permissions change often, use
#[authorize(dynamic)]and accept the database read. There is no free freshness. - Fail-closed startup is inconvenient for quick scripts. The framework is built for services, not one-off probes.
- Authorization metadata does not replace a permission model. You still have to decide what
tenant.order.writemeans in your domain. - Multi-instance realtime state is in-process by default. Clusters need shared stores and a transport with broadcast support.
None of these are hidden in the docs, and none of them are reasons to skip the checks.
The review that is worth doing
Take one existing endpoint that reads a resource by ID. Look at the handler and ask three questions: is authentication declared, is tenant presence declared, and is ownership checked after the resource is loaded?
Then convert the first two into attributes and the third into require_same_tenant. Run Host::builder().try_build().
If the host refuses to boot, you found a missing authorizer before an attacker did. That is a good afternoon.
Repositories:
rust-webx ยท rust-ef ยท rust-dix
What is the check in your authorization path that exists only in a code review document?
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.