pglite icon indicating copy to clipboard operation
pglite copied to clipboard

Row-level security policy for table not working

Open esciullo opened this issue 2 years ago • 3 comments

Row-level security policy for table is not working: I expected that an exception was throwed by the get_current_security_level() when quering the app_configurations table, but I'm able to query the table. Previously reported with https://github.com/electric-sql/pglite/issues/138

PGlite version: 0.2.4

How reproduce:

import { PGlite } from "@electric-sql/pglite";

const dataDir = `/tmp/rlsTest_${Date.now().toString()}`;

const db = new PGlite(dataDir);

await db.waitReady;

await db.exec(`
CREATE USER test_user WITH PASSWORD 'bla';

GRANT pg_read_all_data TO test_user;

CREATE SCHEMA "playground";

CREATE TABLE IF NOT EXISTS "playground"."app_configurations" (
	"name" varchar PRIMARY KEY NOT NULL,
	"value" text NOT NULL,
	"created_at" timestamp with time zone DEFAULT now(),
	"updated_at" timestamp with time zone,
	"created_by" varchar NOT NULL,
	"updated_by" varchar,
	"is_enabled" boolean DEFAULT true,
	"security_level" integer DEFAULT 0
);

ALTER TABLE playground.app_configurations ENABLE ROW LEVEL SECURITY;

CREATE OR REPLACE FUNCTION playground.get_current_security_level() RETURNS INTEGER LANGUAGE PLPGSQL AS 
$$ 
BEGIN 
  RAISE EXCEPTION 'Not implemented yet!';
END;
$$;

CREATE POLICY app_configurations_security_level ON playground.app_configurations FOR ALL
USING (security_level <= playground.get_current_security_level()) 
WITH CHECK (security_level <= playground.get_current_security_level());

INSERT INTO playground.app_configurations (name, value, created_by, security_level) VALUES ('test', 'test', 'test', 4);`);

await db.close();

const db2 = new PGlite({
  dataDir: dataDir,
  username: "test_user",
});

await db2.waitReady;

console.log("Expect an error here:");

const result = await db2.exec(`SELECT * FROM playground.app_configurations`);

console.log(`But i'm able to fetch ${result.length.toString()} rows:`);
for (const row of result) {
  console.log(row.rows);
}

await db2.close();

esciullo avatar Aug 29 '24 08:08 esciullo

Any chance to look into ? I would really appreciate it. Thanks

esciullo avatar Oct 04 '24 09:10 esciullo

@esciullo I can confirm that this was failing in v0.2.8 but working as of v0.2.9

So I'd guess based on the release notes it was fixed incidentally by the same change that fixed extension OIDs https://github.com/electric-sql/pglite/issues/352

DaveKP avatar Oct 05 '24 14:10 DaveKP

I confirm that works fine in v0.2.9 and v0.2.10, thank you !

esciullo avatar Oct 05 '24 17:10 esciullo