agentleFS
zahidoverflowzahidoverflow/viperclaw/skills/viper-multitenant-isolation/SKILL.md

viper-multitenant-isolation

Skill22 starsChanged 15 days agoMITView on GitHub

Multi-Tenant SaaS Data Isolation & Row-Level Security (RLS) Auditor. Reviews tenant context propagation, ORM query scoping, shared cache keys, and background job isolation.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 4 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Run the following discovery commands across the target repository before manual code review:

    line 30
  2. Distinctive:

    Verify whether an upstream API Gateway, global middleware, or ORM hook already enforces the control before flagging a controller.

    line 62
  3. Distinctive:

    Write a Positive Unit Test confirming authorized, schema-valid requests succeed.

    line 92
  4. Distinctive:

    Write a Negative Security Regression Test confirming unauthorized, cross-tenant, or malformed inputs are deterministically rejected.

    line 93

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Multi-Tenant SaaS Data Isolation & Row-Level Security (RLS) Auditor (viper-multitenant-isolation)

1. Executive Scope, Standards & CWE/OWASP Mapping

  • Frameworks & Standards: AWS SaaS Tenant Isolation Architecture, OWASP Top 10:2025 A01, PostgreSQL RLS Best Practices, CWE-284, CWE-668
  • Primary CWE Mapping: CWE-284 (Improper Access Control Across Tenants), CWE-668 (Exposure of Resource to Wrong Sphere), CWE-488 (Exposure of Data Element to Wrong Session)
  • ViperClaw MCP Tools: viper_taint_scan, viper_diff_oracle, viper_verify_and_report
  • ViperClaw CLI Command: viperclaw taint ./src --db .viperclaw/graph.db

2. Threat Model & Root-Cause Architecture

flowchart LR
  Auth["Verified JWT / Session Tenant ID"] --> Ctx["Request-Scoped ContextVar + DB RLS"] --> Store["Tenant-Prefixed Cache / S3 / Vector Index"] --> Worker["Signed Tenant Context in Async Jobs"]

Root-Cause Analysis

Cross-tenant data leaks in SaaS platforms happen when tenant_id is read from a request header/body instead of the verified session, when a global/singleton variable stores current_tenant across concurrent async requests, when Redis cache keys omit the tenant_id prefix, or when PostgreSQL RLS is enabled without FORCE ROW LEVEL SECURITY (allowing table owners to skip RLS enforcement).


3. Deterministic Discovery & Grep/AST Audit Commands

Run the following discovery commands across the target repository before manual code review:

# 1. Automated ViperClaw analysis
viperclaw taint ./src --db .viperclaw/graph.db

# 2. Rapid static pattern discovery (ripgrep)
rg -n "(tenant_id|org_id|organization_id|workspace_id|account_id)"
rg -n "(current_setting\(.app\.current_tenant|ENABLE ROW LEVEL SECURITY|FORCE ROW LEVEL SECURITY)"
rg -n "(redis\.(get|set)|cache\.(get|set))" ./src

4. Senior Security Engineer 8-Point Audit Checklist

Control IDAudit Verification TargetPass CriteriaFail / Vulnerability Indicator
MT-01Cryptographic Tenant Context Bindingtenant_id is derived exclusively from the verified session/JWT or verified membership table—never from an unverified X-Tenant-ID header.Middleware reads req.headers['x-tenant-id'] without checking if current_user belongs to that tenant.
MT-02PostgreSQL RLS + FORCE ROW LEVEL SECURITYEvery multi-tenant table enables both ENABLE ROW LEVEL SECURITY and FORCE ROW LEVEL SECURITY with current_setting('app.tenant_id', true).Application connects as the table owner without FORCE ROW LEVEL SECURITY, silently skipping all RLS policies!
MT-03Connection-Pool Tenant Context ResetTransaction-scoped SET LOCAL app.tenant_id = ... is used (or DISCARD ALL on pool checkout/checkin) so pooled connections never bleed tenant context.Session-level SET app.tenant_id persists on a PgBouncer/SQLAlchemy pooled connection for the next request.
MT-04Async Coroutine Context Isolation (ContextVar)In Python/Node async servers, tenant state is stored in contextvars.ContextVar / AsyncLocalStorage, never on a module global or class singleton.Singleton service attribute self.tenant_id = ... overwritten by concurrent coroutine from another tenant.
MT-05Tenant-Namespaced Cache Keys (Redis / Memcached)Every cache key includes tenant:{tenant_id}:... (@cache decorators include tenant_id in the key hash).@lru_cache or redis.get(f'report:{report_id}') serves Tenant A's cached report to Tenant B.
MT-06S3 / Object Storage Prefix & KMS IsolationObject keys enforce s3://bucket/{tenant_id}/{uuid} and IAM/presigned policies restrict access to that prefix.Flat S3 bucket path allowing cross-tenant key guessing or overwrite.
MT-07Search & Vector Index Tenant FilteringElasticsearch, Algolia, and vector DB queries inject a mandatory server-side tenant_id filter.Global search autocomplete leaks document titles or user names from other tenants.
MT-08Background Job Tenant Re-ValidationCelery/BullMQ tasks receive tenant_id + actor_id and re-verify membership before executing exports or webhooks.Background export job runs raw unscoped SQL without setting the tenant RLS context.

5. False-Positive Elimination & CVSS v3.1 Triage Rubric

False-Positive Gate

  1. Verify whether an upstream API Gateway, global middleware, or ORM hook already enforces the control before flagging a controller.
  2. Confirm the code path is reachable from an external entrypoint (not dead code, CLI-only admin script, or unit-test fixture).
Severity TierBaseline CVSS v3.1 VectorQualifying Security Impact
CRITICAL (9.3)CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:C/C:H/I:H/A:NUnverified X-Tenant-ID header or pooled connection state bleed granting full access to any tenant's data.
HIGH (8.1)CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:NUnscoped Redis cache key or search index leaking cross-tenant reports or PII.
MEDIUM (6.5)CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:U/C:H/I:N/A:NAsync coroutine race condition occasionally returning another user's response under high load.

6. Production Remediation Patterns (Vulnerable vs. Hardened Code)

-- ❌ VULNERABLE: Missing FORCE ROW LEVEL SECURITY (table owner skips RLS!) & session-wide SET
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices USING (tenant_id = current_setting('app.tenant_id')::uuid);

-- ✅ HARDENED: Enforces FORCE ROW LEVEL SECURITY + WITH CHECK on INSERT/UPDATE
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
ALTER TABLE invoices FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
  FOR ALL
  USING (tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid)
  WITH CHECK (tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid);

7. Regression Testing & Continuous Verification

  1. Write a Positive Unit Test confirming authorized, schema-valid requests succeed.
  2. Write a Negative Security Regression Test confirming unauthorized, cross-tenant, or malformed inputs are deterministically rejected.
  3. Re-run viperclaw taint and viperclaw verify to confirm zero remaining unmitigated flows in .viperclaw/graph.db.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.