From 5a814c26b32ab208779087369939a931c31fae2e Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Thu, 26 Nov 2026 04:29:35 +0530 Subject: [PATCH] fix: don't remove the quotes of a policy field twice when loading it --- src/persist/helper.ts | 8 ++------ test/persist/fileAdapter.test.ts | 16 ++++++++++++++++ test/persist/helper.test.ts | 2 ++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/persist/helper.ts b/src/persist/helper.ts index 26744c28..9b6747b7 100644 --- a/src/persist/helper.ts +++ b/src/persist/helper.ts @@ -105,12 +105,8 @@ export class PolicyLoader { return; } - const values = tokens[0].slice(1).map((v) => { - if (v.startsWith('"') && v.endsWith('"')) { - v = v.slice(1, -1); - } - return v.replace(/""/g, '"').trim(); - }); + // The CSV parser has already removed the quotes and turned "" into " + const values = tokens[0].slice(1).map((v) => v.trim()); policy.policy.push(values); } diff --git a/test/persist/fileAdapter.test.ts b/test/persist/fileAdapter.test.ts index e3a4eda2..143ff6d1 100644 --- a/test/persist/fileAdapter.test.ts +++ b/test/persist/fileAdapter.test.ts @@ -57,6 +57,22 @@ test('savePolicy keeps fields with commas in one field', async () => { await expect(e2.enforce('bob', 'data1', 'write')).resolves.toBe(false); }); +test('savePolicy keeps the double quotes of a field', async () => { + const files = new Map([['policy.csv', '']]); + const e = await newEnforcer(newModel(modelText), new FileAdapter('policy.csv', memoryFileSystem(files))); + await e.addPolicy('alice', '"quoted"', 'read'); + await e.addPolicy('bob', '"', 'read'); + await e.addPolicy('carol', 'a""b', 'read'); + await e.savePolicy(); + + const e2 = await newEnforcer(newModel(modelText), new FileAdapter('policy.csv', memoryFileSystem(files))); + expect(await e2.getPolicy()).toEqual([ + ['alice', '"quoted"', 'read'], + ['bob', '"', 'read'], + ['carol', 'a""b', 'read'], + ]); +}); + test('savePolicy rejects fields with line breaks', async () => { const files = new Map([['policy.csv', 'p, alice, data1, read']]); const e = await newEnforcer(newModel(modelText), new FileAdapter('policy.csv', memoryFileSystem(files))); diff --git a/test/persist/helper.test.ts b/test/persist/helper.test.ts index 1d2fece6..db6d4b6b 100644 --- a/test/persist/helper.test.ts +++ b/test/persist/helper.test.ts @@ -41,6 +41,7 @@ m = r.sub == p.sub && r.obj == p.obj && r.act == p.act `"p", "admin", "/", "PUT"`, `"p","admin",/, "DELETE"`, `p, " admin","/ ", "PATCH"`, + `p, admin, """quoted""", "a""b"`, ]; const expectedPolicy = [ @@ -49,6 +50,7 @@ m = r.sub == p.sub && r.obj == p.obj && r.act == p.act ['admin', '/', 'PUT'], ['admin', '/', 'DELETE'], ['admin', '/', 'PATCH'], + ['admin', '"quoted"', 'a"b'], ]; testdata.forEach((n) => {