Urgent.News

What's breaking now, across hundreds of outlets.

Tech

AND binds tighter than OR: the tenant filter bug I fixed in three places

Single-database multi-tenancy in Laravel usually starts with one line: a global scope that adds where tenant_id = ? to every query. It works in the demo. Then someone writes a query with an OR in it. I maintain laravel-tenancy , a single-database tenancy package that is meant to fail closed. While hardening it I found the same mistake in three different code paths, and all three come down to…

The bug in question arises from the way Laravel's Eloquent ORM handles the precedence of AND and OR operators in SQL queries. When a tenant filter is applied using a global scope, Eloquent automatically groups OR conditions in parentheses to ensure the tenant filter is applied correctly. However, this protection fails when the tenant filter is added in other ways, resulting in a vulnerability that can expose sensitive data.

The issue occurs when a query is constructed using OR conditions and a tenant filter, but the OR operation takes precedence over the AND operation. For example, the query `select * from invoices where status = 'draft' or status = 'void' and tenant_id = 5` is interpreted as `where status = 'draft' or (status = 'void' and tenant_id = 5)`. Consequently, all draft invoices from every tenant are retrieved, while only the void invoices from the specified tenant are intended.

Laravel's applyScopes() method, which is responsible for adding where clauses to query builders, already wraps OR conditions in parentheses to maintain the correct logical grouping. However, this safeguard does not apply to all instances where a tenant filter is added. The helper function group() was introduced to address these overlooked scenarios, ensuring that all tenant conditions are grouped together with AND operators, preventing unintended data leakage.

The fix involves modifying the code paths that add tenant conditions to ensure they always use this helper function to properly group the WHERE clauses. This simple yet effective solution prevents the bug from affecting any code that adds tenant predicates, whether through macros, custom builders, repository base classes, or custom pivot relations. By grouping all tenant conditions in one helper function, the solution maintains the integrity of SQL queries and ensures data security.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

System Design Roadmap: What to Learn and in What Order

System Design Roadmap: What to Learn and in What Order Most system design roadmaps are really just a pile of topics — CAP theorem, sharding, consistent hashing, message queues — listed as if you could…

  • Stage 1: Focus on networking/HTTP, databases, and OS concurrency
  • Stage 2: Learn SQL vs. NoSQL, indexing, caching, and CDNs
  • Stage 4: Master CAP theorem, consistency models, and consensus algorithms

I built Catalog.cool - a directory of Shopify stores and ecommerce design inspiration

Catalog.cool is a project I've been working on to make finding Shopify design references easier. It lets you explore real Shopify stores, browse different industries and design styles, and look at…

  • Catalog.cool simplifies discovery of Shopify design references
  • Platform filters stores by design style, color schemes, typography
  • Users view desktop/mobile designs, research eCommerce UX patterns

Introduction to AWS Cloud Computing: From Zero to Cloud

Understanding Amazon Web Services and its key services Cloud computing is now a key technology in today’s society. It is used for a number of purposes, for example web portals, photo libraries and…

  • Amazon EC2 enables cloud virtual servers for web applications.
  • Amazon S3 provides secure online data storage for images and data.
  • AWS IAM manages user access and authorization within AWS.

How a Solo Developer Built 1,185 Free Interactive Lessons

TL;DR One developer, 1,185 lessons. LearnCoachAssist Labs has 16 Skill Paths with 221 units, 1,185 lessons and 11,706 Problems, built by one developer. Written with AI help, computed by code.

  • Developer created 1,185 free interactive lessons for LearnCoachAssist Labs.
  • Shared core maintains consistency across 16 Path apps without a development team.

More from Tuesday, 1 December 2026 →