fix: use ptype in GetNamedPermissionsForUser for subject index - #1760
fix: use ptype in GetNamedPermissionsForUser for subject index#1760Arshil-Khan wants to merge 1 commit into
Conversation
) Fix GetNamedPermissionsForUser when the named policy type defines columns in a different order (e.g. dom, sub, obj, act). Previously, GetNamedPermissionsForUser looked up the subject field index against hardcoded "p" instead of ptype, causing the user to be placed in the wrong column and overwritten when a domain was passed. Fixes apache#1759
bitflicker64
left a comment
There was a problem hiding this comment.
LGTM. Verified locally: the new test fails on master with exactly the two symptoms from #1759 (empty result without a domain, bob's row leaking with one) and passes with the one-line change. GetPermissionsForUser still goes through this function with "p", so the default path is unchanged.
Not for this PR, but worth a follow-up since #1759's reporter will likely hit it next: GetNamedImplicitPermissionsForUser has the same shape of bug. It resolves the domain index via ptype but matches the subject with a hardcoded rule[0], so on this PR's own model (p2 = dom, sub, obj, act) it returns [] for alice with or without a domain.
| subIndex, err := e.GetFieldIndex("p", constant.SubjectIndex) | ||
| subIndex, err := e.GetFieldIndex(ptype, constant.SubjectIndex) | ||
| if err != nil { | ||
| subIndex = 0 |
There was a problem hiding this comment.
Pre-existing, not blocking: the subIndex = 0 fallback here still swallows a missing <ptype>_sub token (with no SetFieldIndex call), while the domain lookup a few lines down returns the error instead. Filtering on column 0 by accident would reproduce the same "other users' rows" symptom this PR fixes. Might be worth returning the error in a follow-up so the two lookups behave the same way.
| t.Fatal(err) | ||
| } | ||
|
|
||
| got, err := e.GetNamedPermissionsForUser("p2", "alice") |
There was a problem hiding this comment.
Nit: testGetNamedPermissionsForUser at the top of this file already wraps this call plus the Array2DEquals check, so the two assertions could each become a one-liner. Fine as is if you would rather keep the test self-contained.
|
@hsluoyz this one is ready to merge from my side: one-line fix, and the new test fails on master and passes with the change. The |
What happened
In
GetNamedPermissionsForUser(ptype string, user string, domain ...string), the subject field index was looked up against hardcoded"p"instead of the passedptype:When a named policy definition has a column order differing from
p(e.g.p2 = dom, sub, obj, act),subIndexwas resolved based onprather thanptype. Consequently:GetNamedPermissionsForUser("p2", "alice")wrote the user into the domain column (index 0), returning[].GetNamedPermissionsForUser("p2", "alice", "tenant1")wrotealiceto index 0 and subsequently overwrote index 0 withtenant1. The subject column remained empty (wildcard), returning permissions for all users in that domain.Fix
ptypeinstead of"p"intoe.GetFieldIndex(ptype, constant.SubjectIndex)inGetNamedPermissionsForUser.TestNamedPermissionsForUserWithDifferentColumnOrderinrbac_api_test.goverifying both no-domain and with-domain queries when the named policy has a custom column order.Fixes #1759
Verification
make test(go test -race -v ./...) passes.golangci-lint runpasses with 0 issues.