<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: maksoud13</title>
    <description>The latest articles on DEV Community by maksoud13 (@maksoud13).</description>
    <link>https://dev.to/maksoud13</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F70824%2Fdcb1d921-a931-4921-b695-0a9ba8d328ad.jpeg</url>
      <title>DEV Community: maksoud13</title>
      <link>https://dev.to/maksoud13</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maksoud13"/>
    <language>en</language>
    <item>
      <title>I Built a Java Agent That Injects Tenant Filters Into Every SQL Query Automatically</title>
      <dc:creator>maksoud13</dc:creator>
      <pubDate>Thu, 19 Nov 2026 19:45:09 +0000</pubDate>
      <link>https://dev.to/maksoud13/i-built-a-java-agent-that-injects-tenant-filters-into-every-sql-query-automatically-4ac2</link>
      <guid>https://dev.to/maksoud13/i-built-a-java-agent-that-injects-tenant-filters-into-every-sql-query-automatically-4ac2</guid>
      <description>&lt;p&gt;&lt;em&gt;Using ByteBuddy, JSqlParser, and AST rewriting to enforce multi-tenant isolation at the JDBC layer — with zero application code changes.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;I am a solo developer, and I spent the last several months building TenantWall — an open-source Java agent that automatically injects &lt;code&gt;WHERE tenant_id = ?&lt;/code&gt; into every SQL query your application executes. It is early stage, it has real limitations, and it just shipped v1.2.0 to Maven Central. This article explains how it works, how to try it in under 5 minutes, and where it currently falls short.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem That Made Me Build This
&lt;/h2&gt;

&lt;p&gt;If you are building a shared-database, shared-schema SaaS application, you have probably written a query like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'PENDING'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in a multi-tenant system, that query silently returns every tenant's pending orders to whoever asked. Customer A sees Customer B's data. No exception is thrown. No warning is logged. The application just returns the wrong rows.&lt;/p&gt;

&lt;p&gt;This is not a hypothetical scenario. It is the most common class of data isolation bug in shared-database SaaS applications, and it compounds quickly. A developer writes a reporting query under deadline pressure. A new team member adds a native SQL query without knowing the tenant filtering convention. A legacy endpoint gets migrated and the WHERE clause doesn't come with it. Each missed filter is a potential GDPR incident.&lt;/p&gt;

&lt;p&gt;The standard answers — Hibernate &lt;code&gt;@Filter&lt;/code&gt; annotations, Spring AOP aspects, custom repository base classes — help, but they all share the same blind spot: they only protect the code paths that the developer remembered to protect. Native JDBC calls bypass them. Complex reporting queries frequently skip them. And they require every developer on the team to remember the convention on every query they write, forever.&lt;/p&gt;

&lt;p&gt;I wanted a solution that worked at the infrastructure level. If the filter is injected at the JDBC layer, it doesn't matter what ORM you use, what query you write, or whether a developer forgot the convention. The filter is always there.&lt;/p&gt;




&lt;h2&gt;
  
  
  How TenantWall Works
&lt;/h2&gt;

&lt;p&gt;TenantWall runs as a Java agent attached to your JVM at startup. It never touches your application code. Here is what happens on every query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP Request (X-Tenant-Id: ACME)
        ↓
  Spring Boot App
        ↓
  ByteBuddy intercepts at JDBC / Hibernate boundary
        ↓
  JSqlParser rewrites SQL AST
        ↓
  SELECT * FROM orders WHERE tenant_id = 'ACME'
        ↓
      Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Layer 1 — Bytecode Interception with ByteBuddy
&lt;/h3&gt;

&lt;p&gt;The agent uses ByteBuddy to instrument two targets at startup: Hibernate 6.x's &lt;code&gt;StatementPreparerImpl&lt;/code&gt; and the standard JDBC &lt;code&gt;Statement&lt;/code&gt; / &lt;code&gt;PreparedStatement&lt;/code&gt; execution path. This dual interception means queries are caught whether they come from Hibernate, MyBatis, Spring Data JPA, or a raw &lt;code&gt;jdbcTemplate.query()&lt;/code&gt; call. Because it operates at the bytecode level, no annotations or base classes are required anywhere in your codebase.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer 2 — AST-Based SQL Rewriting
&lt;/h3&gt;

&lt;p&gt;String manipulation breaks immediately on real-world SQL. A regex that injects &lt;code&gt;WHERE tenant_id = ?&lt;/code&gt; works fine on &lt;code&gt;SELECT * FROM products&lt;/code&gt; and silently corrupts &lt;code&gt;SELECT * FROM products WHERE id = 1 OR 1=1&lt;/code&gt;. TenantWall parses every incoming query into an Abstract Syntax Tree using JSqlParser, then walks the tree to inject the tenant predicate correctly — including across JOINs, subqueries, CTEs, and window functions.&lt;/p&gt;

&lt;p&gt;For complex SQL dialects (CTEs, recursive subqueries, window functions), an optional Python sidecar using &lt;code&gt;sqlglot&lt;/code&gt; handles rewriting across 10 SQL dialects. The sidecar runs locally as a Docker container and communicates with the Java agent over localhost only.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer 3 — Caffeine Caching
&lt;/h3&gt;

&lt;p&gt;Parsing SQL on every request would be unacceptable in production. Rewritten query templates are cached in a bounded Caffeine cache (10,000 entries, 30-minute TTL). On a cache hit, overhead is effectively zero. On a parse miss, the rewrite adds approximately &lt;strong&gt;0.8ms&lt;/strong&gt;. In load testing, agent CPU overhead was 4.3% under sustained load and GC overhead 0.043% — both negligible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Five Tenant Resolution Strategies Built In
&lt;/h2&gt;

&lt;p&gt;TenantWall needs to know the current tenant's identifier at query time. Five strategies ship out of the box:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;header&lt;/code&gt;&lt;/strong&gt; — Reads &lt;code&gt;X-Tenant-Id&lt;/code&gt; from the HTTP request header. Best for APIs and microservices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;jwt&lt;/code&gt;&lt;/strong&gt; — Decodes a Bearer token and extracts a configured claim. Best for OAuth2/OIDC APIs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;session&lt;/code&gt;&lt;/strong&gt; — Reads from &lt;code&gt;JSESSIONID&lt;/code&gt;. Best for traditional web apps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;cookie&lt;/code&gt;&lt;/strong&gt; — Reads a named cookie value. Best for legacy apps and SPAs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;fixed&lt;/code&gt;&lt;/strong&gt; — Uses a hardcoded value. Useful for scripts and batch jobs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If more than one strategy key is present in configuration, TenantWall logs a warning at startup. If the strategy is misconfigured, the application refuses to start rather than silently running unprotected.&lt;/p&gt;




&lt;h2&gt;
  
  
  Setup in Under 5 Minutes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1 — Add the Spring Boot Starter
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;io.github.maksoud13&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;tenantwall-spring-boot-starter&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.2.0&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, download the standalone agent (&lt;code&gt;agent-module-1.2.0.jar&lt;/code&gt;) from Maven Central and attach it at JVM startup without touching &lt;code&gt;pom.xml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;java -javaagent:agent-module-1.2.0.jar=tenantwall.properties -jar your-app.jar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2 — Add Four Lines of Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="na"&gt;tenantwall.enabled&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;tenantwall.filter.column&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;
&lt;span class="na"&gt;tenantwall.tenant.strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;header&lt;/span&gt;
&lt;span class="na"&gt;tenantwall.tenant.headerName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Tenant&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3 — Write Your Queries Normally
&lt;/h3&gt;

&lt;p&gt;This is the entire point. Your existing queries stay exactly as they are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM products"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nativeQuery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a request arrives with &lt;code&gt;X-Tenant-Id: ACME&lt;/code&gt;, TenantWall intercepts and rewrites before execution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- What your code sends:&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;

&lt;span class="c1"&gt;-- What actually hits the database:&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'ACME'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works identically for UPDATE and DELETE queries. An UPDATE without a matching tenant_id cannot affect another tenant's rows because the WHERE clause is injected before the statement executes.&lt;/p&gt;




&lt;h2&gt;
  
  
  For Complex SQL — The Python Sidecar
&lt;/h2&gt;

&lt;p&gt;For queries involving CTEs, window functions, or recursive subqueries, TenantWall routes to an optional Python sidecar that uses &lt;code&gt;sqlglot&lt;/code&gt; for more robust parsing across 10 SQL dialects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run -d --name tenantwall-parser \
  -p 127.0.0.1:8000:8000 \
  -e &lt;span class="nv"&gt;TENANTWALL_PARSER_SECRET&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"your-32-char-secret"&lt;/span&gt; \
  -e &lt;span class="nv"&gt;TENANTWALL_PARSER_ENFORCE_AUTH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 \
  maksoud13/tenantwall-parser:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sidecar binds to &lt;code&gt;127.0.0.1&lt;/code&gt; only — it is never exposed to the network. Communication between the Java agent and the sidecar is authenticated using short-lived HMAC-SHA256 JWTs, so even local traffic cannot be forged.&lt;/p&gt;




&lt;h2&gt;
  
  
  Load Testing Results
&lt;/h2&gt;

&lt;p&gt;TenantWall was load tested against MySQL 8.0 with 10 million rows distributed across 20 tenants, running 1,000 parallel requests over a 90-second window.&lt;/p&gt;

&lt;p&gt;Results:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Test suite: 390 automated tests&lt;/strong&gt; (232 Java unit + 48 Python + 88 integration + 14 strategy + 2 concurrency + 6 JWT E2E)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integration tests: 88/88 passing&lt;/strong&gt; across MySQL 8.0 (30), SQL Server 2022 (29), and Oracle XE 21c (29)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-tenant leaks detected: 0&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-tenant requests blocked: 528 / 528&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;p50 latency (product lookup): ~96ms&lt;/strong&gt; — includes DB query, network, and JSON serialization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;p99 latency (product lookup): ~283ms&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Agent overhead per query: ~0.8ms on cache miss, ~0ms on cache hit&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Agent CPU overhead: 4.3% under sustained load&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Agent GC overhead: 0.043%&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The isolation number is the only one that matters for a security tool. Zero leaks under concurrent load is the bar, and it passed.&lt;/p&gt;

&lt;p&gt;Note: CTE-heavy reporting queries run slower — p50 around 1.5 seconds for complex window function queries. This is a known area of active optimization.&lt;/p&gt;




&lt;h2&gt;
  
  
  Current Limitations — The Honest List
&lt;/h2&gt;

&lt;p&gt;This is an early-stage solo project. Here is what does not work yet or works with caveats:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No first-class PostgreSQL support yet.&lt;/strong&gt; MySQL, SQL Server, and Oracle are verified with the full test suite. The Python parser already supports the &lt;code&gt;postgres&lt;/code&gt; SQL dialect, but PostgreSQL is not yet in the verified integration matrix. If you are on Postgres, hold off or test carefully and report what breaks — Postgres support is the #1 item on the roadmap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Python sidecar is a separate process.&lt;/strong&gt; For applications with complex SQL, you need to run and manage the Docker container alongside your application. It is a single &lt;code&gt;docker run&lt;/code&gt; command, but it is still an operational dependency that some teams will want to avoid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java 21+ only.&lt;/strong&gt; This tool requires Java 21 or later and does not help if your stack is Node.js, Python, Go, or anything that does not run on the JVM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No production reference customers yet.&lt;/strong&gt; This has been tested in controlled load tests but not battle-tested in a live production environment at scale. If you try it in production, I want to hear what happens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stored procedures are partially covered.&lt;/strong&gt; The outer call is filtered, but queries executed internally inside a stored procedure are not intercepted.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Not Just Use Postgres RLS or Hibernate Filters?
&lt;/h2&gt;

&lt;p&gt;These are the first two questions any experienced developer will ask, and they are fair.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Postgres RLS&lt;/strong&gt; is genuinely good if you are on PostgreSQL and can configure database policies. It has two practical constraints: it requires DBA access to configure and maintain, which many application teams do not have, and it locks you to PostgreSQL. If you have a mixed database environment, run on MySQL or Oracle, or cannot touch database configuration, RLS is not an option.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hibernate &lt;code&gt;@FilterDef&lt;/code&gt;&lt;/strong&gt; works well for Hibernate-managed queries but has a blind spot: it does not cover native SQL queries, raw JDBC calls, or any code path that bypasses the ORM. In most real codebases, there are more of these than developers expect.&lt;/p&gt;

&lt;p&gt;TenantWall's specific value is the combination of zero code changes, ORM-agnostic coverage, and database portability. It is not a replacement for RLS where RLS is available and appropriate. It is the right tool when you need coverage across a polyglot data layer or cannot afford to audit every query in an existing codebase.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try It and Tell Me What Breaks
&lt;/h2&gt;

&lt;p&gt;TenantWall v1.2.0 is on Maven Central now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;io.github.maksoud13&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;tenantwall-spring-boot-starter&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.2.0&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Full source, documentation, and test scripts are at &lt;a href="https://github.com/maksoud13/tenantwall" rel="noopener noreferrer"&gt;github.com/maksoud13/tenantwall&lt;/a&gt;. MIT license.&lt;/p&gt;

&lt;p&gt;If you try it on a real query that breaks — a CTE that does not rewrite correctly, a dialect it does not handle, a strategy that misbehaves — open an issue. That is exactly the kind of feedback that makes an early-stage project real. If it works for you, a GitHub star helps other developers find it.&lt;/p&gt;

&lt;p&gt;If you are currently handling multi-tenant isolation a different way — Postgres RLS, separate schemas, Hibernate filters, or something custom — I would genuinely like to know. Drop a comment below.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;TenantWall is an open-source solo project under active development. MIT license. Issues and contributions welcome.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>saas</category>
      <category>security</category>
      <category>springboot</category>
    </item>
  </channel>
</rss>
