fix: private knowledgebase access check in Go DAO#15680
fix: private knowledgebase access check in Go DAO#15680jonathanchang31 wants to merge 5 commits into
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAccessible() now allows access when the requester is the KB creator or when the KB permission is ChangesKnowledge Base Access Control Authorization
Sequence DiagramsequenceDiagram
participant Caller
participant KnowledgebaseDAO
participant DB
participant UserTenant
Caller->>KnowledgebaseDAO: Accessible(kbID, userID)
KnowledgebaseDAO->>DB: Query knowledgebase LEFT JOIN user_tenant (filter user_id, active)
DB->>UserTenant: check matching user_tenant row
DB-->>KnowledgebaseDAO: result (creator match OR permission='team' + user_tenant present) with status=valid
KnowledgebaseDAO-->>Caller: allow/deny
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/dao/kb_test.go`:
- Around line 141-197: Add a regression test for the branch in
KnowledgebaseDAO.Accessible where tenant_id == requester but the requester is
not the creator and permission == "me": create a knowledgebase via
testKnowledgebase with ID like "kb-me", set kb.TenantID to "tenant-1" (or the
same value you will use for the requester), set kb.CreatedBy to a different user
(e.g. "other-1"), set kb.Permission = string(entity.TenantPermissionMe), save it
(db.Save), ensure a user/tenant mapping exists for the requester via
testUserTenant (e.g. user "tenant-1" mapped to tenant "tenant-1"), then assert
dao.Accessible("kb-me", "tenant-1") returns false. Reference functions/types:
Accessible, testKnowledgebase, testUserTenant, entity.TenantPermissionMe.
In `@internal/dao/kb.go`:
- Around line 249-253: The access WHERE clause in internal/dao/kb.go currently
includes an authorization bypass `knowledgebase.tenant_id = ?` inside the
Where(...) predicate; remove that `OR knowledgebase.tenant_id = ?` condition so
access is only granted by `knowledgebase.created_by = ?` or
`(knowledgebase.permission = ? AND user_tenant.user_id IS NOT NULL)`. After
removing the condition, update the parameter list passed to Where(...) to drop
the extra userID argument so the placeholders match (keep kbID,
string(entity.StatusValid), userID, string(entity.TenantPermissionTeam)). Ensure
the modified predicate and args are used wherever this query fragment is built.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 196c1000-42e4-4ae5-a20a-49378d620abc
📒 Files selected for processing (2)
internal/dao/kb.gointernal/dao/kb_test.go
🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Fetch PR diff
gh pr diff 15680 --repo infiniflow/ragflow
echo "=== kb.go full content ==="
cat internal/dao/kb.go
echo "=== kb_test.go full content ==="
cat internal/dao/kb_test.goLength of output: 30624
Review of
|
| Severity | |
|---|---|
Missing user_tenant.status = 'valid' in LEFT JOIN |
🔴 High — revoked members bypass the guard |
| Missing test for revoked tenant member | 🟡 Medium |
GetKBByIDAndUserID same-pattern bug |
🔵 Follow-up |
| Whitespace noise | ⚪ Cosmetic |
The fix is in the right direction — just needs the status guard added to the join and one more test case to close the gap.
✅ Action performed
Review finished.
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
|
@coderabbitai Could you plz review my PR quickly? |
|
Tip For best results, initiate chat on the files or code changes.
The one-liner fix for item 1 is: Joins("LEFT JOIN user_tenant ON user_tenant.tenant_id = knowledgebase.tenant_id AND user_tenant.user_id = ? AND user_tenant.status = ?",
userID, string(entity.StatusValid)).And the suggested test for item 2 is in my previous comment. Address those two and the PR should be good to go! 🐇 |
|
Tip For best results, initiate chat on the files or code changes.
The one-liner fix for item 1: Joins("LEFT JOIN user_tenant ON user_tenant.tenant_id = knowledgebase.tenant_id AND user_tenant.user_id = ? AND user_tenant.status = ?",
userID, string(entity.StatusValid)).Once you apply that fix and add the revoked-membership test case (see the suggested test in my first review comment), the PR should be in good shape. 🐇 |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
gh pr diff 15680 --repo infiniflow/ragflow
echo "=== current kb.go ==="
gh api repos/infiniflow/ragflow/contents/internal/dao/kb.go --jq '.content' | base64 -d 2>/dev/null || true
echo "=== current kb_test.go ==="
gh api repos/infiniflow/ragflow/contents/internal/dao/kb_test.go --jq '.content' | base64 -d 2>/dev/null || trueLength of output: 27862
✅ All previously flagged items resolved
Item 1 — the LEFT JOIN now correctly reads: Joins("LEFT JOIN user_tenant ON user_tenant.tenant_id = knowledgebase.tenant_id AND user_tenant.user_id = ? AND user_tenant.status = ?",
userID, string(entity.StatusValid))This closes the privilege-escalation path for soft-deleted memberships. Item 2 — The only remaining item is 🐇 ✨ If you found this review helpful, would you consider giving us a shout-out on X? Thank you for using CodeRabbit! |
|
@yingfeng Could you review my PR? I am going to contribute, I am looking forward to your support. Thanks! |
Summary
This PR fixes
KnowledgebaseDAO.Accessible()so private knowledge bases are not exposed to tenant members unless the knowledge base permission is explicitly set toteam.Before this change, any user joined to the same tenant could access a knowledge base because the access query only checked
user_tenantmembership and ignored theknowledgebase.permissionfield.After this change:
Related Issue
Closes: #15647
Change Type
Real Behavior Proof
Issue validated in current code:
KnowledgebaseDAO.Accessible()joineduser_tenant.knowledgebase.permission.Fixed behavior is covered by new DAO tests:
Result:
Full DAO package validation:
Result:
Whitespace validation:
Result:
Checklist
git diff --check.