hidingmyname opened a new issue, #274:
URL: https://github.com/apache/casbin-cpp/issues/274

   ## What were you trying to achieve?
   
   I wanted to load a CSV policy file that uses double-quoted fields, as 
described in the Casbin [Policy Storage 
docs](https://casbin.apache.org/docs/policy-storage/#loading-policy-from-a-csv-file):
   
   > If a field contains commas, wrap it in double quotes … If a field contains 
both commas and double quotes, wrap it in double quotes and escape internal 
quotes by doubling them
   
   ## What are the expected results?
   
   A quoted field is unquoted when loading: `"admin"` → `admin`, 
`"data1,data2"` → one field `data1,data2`, `""` inside a quoted field → `"`. 
Spaces *inside* the quotes stay part of the value.
   
   ## What are the received results?
   
   The quote characters are stored as part of the value. A request for `admin` 
therefore doesn't match a policy line written as `"admin"`, and `Enforce` 
returns `false`.
   
   ## Steps to reproduce
   
   `quoted_policy.csv`:
   
   ```csv
   p, admin, /, GET
   p,    "admin",  /,  POST
   ```
   
   ```cpp
   #include <iostream>
   #include <casbin/casbin.h>
   
   int main() {
       casbin::Enforcer e("examples/basic_model.conf", "quoted_policy.csv");
       std::cout << std::boolalpha
                 << e.Enforce({"admin", "/", "GET"}) << "\n"    // true
                 << e.Enforce({"admin", "/", "POST"}) << "\n";  // false, 
expected true
   }
   ```
   
   Observed with a gtest on master 
[`0ad64a7`](https://github.com/apache/casbin-cpp/commit/0ad64a739bd841099c6dbbae4a1f60d12e6cbc0a)
 (current HEAD when written):
   
   ```text
   Expected equality of these values:
     e.Enforce({"admin", "/", "POST"})
       Which is: false
     true
   ```
   
   The unquoted `GET` line in the same file is enforced as expected.
   
   ## Cause
   
   
[`LoadPolicyLine`](https://github.com/apache/casbin-cpp/blob/0ad64a739bd841099c6dbbae4a1f60d12e6cbc0a/casbin/persist/adapter.cpp#L28-L43),
 used by the file, filtered-file and string adapters, does `Split(line, ",")` 
and then `Trim` on each token. `Trim` only removes whitespace, so `"admin"` 
keeps its quotes. For the same reason a quoted comma (`"data1,data2"`) is split 
into two fields (from reading the code; not run).
   
   ## Suggested scope
   
   Parse each line with the CSV quoting rules from the docs: strip the outer 
quotes, keep commas inside quotes, turn `""` into `"`, and don't trim 
whitespace *inside* the quotes (`" admin"` stays ` admin`). Unquoted fields 
keep today's trimming. Other Casbin implementations already handle quoted 
fields in their file loaders, e.g. node-casbin 
([apache/casbin-node-casbin#494](https://github.com/apache/casbin-node-casbin/pull/494)).
   
   ## Environment
   
   casbin-cpp master `0ad64a7`, GCC 11.5.0, Linux x86_64, default CMake build.
   
   ## Related issues checked
   
   Searched issues/PRs for `LoadPolicyLine`, quotes/quoted CSV, double quotes. 
The only hit, #132, is about RBAC role links after `LoadPolicyLine`, not 
quoting.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to