New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

prisma-rls

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prisma-rls

Prisma client extension for row-level security on any database

  • 0.1.2
  • npm
  • Socket score

Version published
Weekly downloads
280
decreased by-46.56%
Maintainers
1
Weekly downloads
 
Created
Source

Prisma RLS

Published on npm License: MIT

🚧 The package is under active development, public api could be changed

The classic approach to implementing Row-Level Security (RLS) requires a database with built-in support and writing row-level security policies for tables.

This library offers an alternative approach - an extension to the Prisma client that adds additional "where" clauses to all model queries. This approach doesn't require RLS support on the database side (for example, in MySQL).

It's important to keep in mind that this extension doesn't cover raw queries. In such cases, you should take care of it yourself or use classic approach.

How to use it

Define permissions for all models in your schema.

import { Prisma } from "@prisma/client";
import { PermissionsConfig } from "prisma-rls";

export type Context = Record<string, any>;
type RolePermissions = PermissionsConfig<Prisma.TypeMap, Context>;

export const Guest: RolePermissions = {
  Post: {
    read: { published: { equals: true } },
    create: false,
    update: false,
    delete: false,
  },
  User: {
    read: { role: { not: { equals: "admin" } } },
    create: false,
    update: false,
    delete: false,
  },
}

Extend the Prisma client with the rls extension.

import { Prisma, PrismaClient } from "@prisma/client";
import { createRlsExtension } from "prisma-rls";

import { Guest } from "./permissions/guest";

const context: Context = {};
const db = new PrismaClient().$extends(createRlsExtension(Prisma.dmmf, Guest, context));

After that all requests except raw will be executed according to permissions

Permissions registry

Almost always you will have several roles, to describe them in a type-safe way use the following pattern:

import { admin } from "./admin";
import { guest } from "./guest";

type Role = "Admin" | "Guest";
type PermissionsRegistry = Record<Role, RolePermissions>;

export const permissionsRegistry = {
  Admin: admin,
  Guest: guest,
} satisfies PermissionsRegistry;

Context

Since Prisma doesn't support context to pass data to the extensions, you will typically extend the client per request (based on role associated with auth token):

import { Prisma, PrismaClient } from "@prisma/client";
import Fastify, { FastifyRequest } from "fastify";
import { createRlsExtension } from "prisma-rls";

import { permissionsRegistry } from "./permissions";

(async () => {
  const prisma = new PrismaClient();
  const server = Fastify();

  const resolveConext = (request: FastifyRequest) => {
    const role = resolveRole(request.headers.authorization);
    const rolePermissions = permissionsRegistry[role];

    return { db: prisma.$extends(createRlsExtension(Prisma.dmmf, rolePermissions, { role })) };
  }
  
  server.get('/post/count', async function handler(request, reply) {
    const { db } = resolveContext(request);
    return await db.post.count();
  })

  await server.listen({ port: 8080, host: "0.0.0.0" });
})();

Keywords

FAQs

Package last updated on 03 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc