fix(agent): scope database queries to authorized shared knowledge bases (#4136)

Resolve database query tenant scopes for authorized shared knowledge bases and add regression coverage for cross-tenant access.

Fixes Tencent/WeKnora#4084
main
AndyZhang 2026-10-12 11:26:34 +08:00 committed by GitHub
parent be581c41e0
commit 21ef506fc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 332 additions and 23 deletions

View File

@ -290,6 +290,7 @@ func searchScopesFromTargets(searchTargets types.SearchTargets) []utils.SearchSc
}
scopes = append(scopes, utils.SearchScope{
KnowledgeBaseID: target.KnowledgeBaseID,
TenantID: target.TenantID,
KnowledgeIDs: knowledgeIDs,
TagIDs: tagIDs,
})

View File

@ -1,9 +1,12 @@
package tools
import (
"database/sql"
"strings"
"testing"
_ "github.com/mattn/go-sqlite3"
"github.com/Tencent/WeKnora/internal/types"
)
@ -24,3 +27,73 @@ func TestDatabaseQueryInjectsEnabledChunkFilter(t *testing.T) {
t.Fatalf("Agent SQL must exclude disabled chunks:\n%s", securedSQL)
}
}
// Execute the secured SQL against synthetic rows so the test checks access,
// rather than just the spelling of an injected predicate.
func TestDatabaseQuerySharedTenantScope(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = db.Close() })
db.SetMaxOpenConns(1)
_, err = db.Exec(`
CREATE TABLE chunks (id TEXT, tenant_id INTEGER, knowledge_base_id TEXT,
knowledge_id TEXT, is_enabled BOOLEAN, deleted_at TEXT);
INSERT INTO chunks VALUES
('shared', 2, 'shared-kb', 'shared-doc', true, NULL),
('own', 1, 'own-kb', 'own-doc', true, NULL),
('unshared', 2, 'private-kb', 'private-doc', true, NULL),
('wrong-owner', 3, 'shared-kb', 'shared-doc', true, NULL),
('swapped-owner', 2, 'own-kb', 'own-doc', true, NULL),
('disabled', 2, 'shared-kb', 'shared-doc', false, NULL),
('deleted', 2, 'shared-kb', 'shared-doc', true, '2026-01-01');`)
if err != nil {
t.Fatal(err)
}
tool := NewDatabaseQueryTool(nil, types.SearchTargets{
{Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "shared-kb", TenantID: 2},
{Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "own-kb", TenantID: 1},
})
secured, err := tool.validateAndSecureSQL("SELECT c.id FROM chunks c ORDER BY c.id", 1)
if err != nil {
t.Fatal(err)
}
rows, err := db.Query(secured)
if err != nil {
t.Fatalf("query %s: %v", secured, err)
}
defer func() { _ = rows.Close() }()
var got []string
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
t.Fatal(err)
}
got = append(got, id)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if strings.Join(got, ",") != "own,shared" {
t.Fatalf("authorized own and shared rows = %v, want [own shared]; SQL: %s", got, secured)
}
}
func TestDatabaseQueryRejectsEmptyScope(t *testing.T) {
for name, targets := range map[string]types.SearchTargets{
"nil": nil,
"empty": {},
"nil target": {nil},
"empty KB": {{Type: types.SearchTargetTypeKnowledgeBase, TenantID: 2}},
"empty document scope": {{Type: types.SearchTargetTypeKnowledge, KnowledgeBaseID: "shared-kb", TenantID: 2}},
} {
t.Run(name, func(t *testing.T) {
tool := NewDatabaseQueryTool(nil, targets)
secured, err := tool.validateAndSecureSQL("SELECT id FROM chunks", 1)
if err == nil || secured != "" {
t.Fatalf("empty scope produced executable SQL %q, error %v", secured, err)
}
})
}
}

View File

@ -154,6 +154,9 @@ type sqlValidator struct {
// SearchScope describes one allowed knowledge scope for SQL query injection.
// Empty KnowledgeIDs and TagIDs means the whole KB is in scope.
type SearchScope struct {
// TenantID is the authorized owner of this KB. Zero uses the caller tenant
// when tenant isolation is enabled. Scopes must be authorized by the caller.
TenantID uint64
KnowledgeBaseID string
KnowledgeIDs []string
TagIDs []string
@ -653,7 +656,9 @@ func WithSearchScopeFilter(kbIDs []string, knowledgeIDs []string) SQLValidationO
}
// WithSearchScopes restricts queries using structured OR scopes. Each scope can
// represent a full KB, specific documents, or a tag-constrained KB.
// represent a full KB, specific documents, or a tag-constrained KB. Nonzero
// TenantID values grant access to that owner within that scope, so scopes must
// come from server-side authorization, never directly from untrusted input.
func WithSearchScopes(scopes []SearchScope) SQLValidationOption {
return func(v *sqlValidator) {
if len(scopes) == 0 {
@ -1059,6 +1064,12 @@ func (v *sqlValidator) injectTenantConditions(sql string, tablesInQuery map[stri
var conditions []string
for tableName, alias := range tablesInQuery {
if v.tablesWithTenantID[tableName] {
// Structured scopes bind each owner to its own KB and document/tag
// restrictions below. A global caller filter would reject shared KBs.
if v.enableSearchScopeFilter && len(v.searchScopes) > 0 &&
(tableName == "knowledge_bases" || tableName == "knowledges" || tableName == "chunks") {
continue
}
if tableName == "tenants" {
conditions = append(conditions, fmt.Sprintf("%s.id = %d", alias, v.tenantID))
} else {
@ -1168,17 +1179,17 @@ func (v *sqlValidator) injectStructuredSearchScopeConditions(sql string, tablesI
var conditions []string
if alias, ok := tablesInQuery["knowledge_bases"]; ok {
if cond := buildKnowledgeBaseScopeCondition(alias, v.searchScopes); cond != "" {
if cond := v.buildKnowledgeBaseScopeCondition(alias, v.searchScopes); cond != "" {
conditions = append(conditions, cond)
}
}
if alias, ok := tablesInQuery["knowledges"]; ok {
if cond := buildKnowledgeScopeCondition(alias, v.searchScopes); cond != "" {
if cond := v.buildKnowledgeScopeCondition(alias, v.searchScopes); cond != "" {
conditions = append(conditions, cond)
}
}
if alias, ok := tablesInQuery["chunks"]; ok {
if cond := buildChunkScopeCondition(alias, v.searchScopes); cond != "" {
if cond := v.buildChunkScopeCondition(alias, v.searchScopes); cond != "" {
conditions = append(conditions, cond)
}
}
@ -1189,19 +1200,29 @@ func (v *sqlValidator) injectStructuredSearchScopeConditions(sql string, tablesI
return InjectAndConditions(sql, strings.Join(conditions, " AND "))
}
func buildKnowledgeBaseScopeCondition(alias string, scopes []SearchScope) string {
kbIDs := uniqueScopeKBIDs(scopes)
if len(kbIDs) == 0 {
return ""
func (v *sqlValidator) buildKnowledgeBaseScopeCondition(alias string, scopes []SearchScope) string {
clauses := make([]string, 0, len(scopes))
for _, scope := range scopes {
if scope.KnowledgeBaseID == "" {
continue
}
clause := fmt.Sprintf("%s.id = %s", alias, quoteString(scope.KnowledgeBaseID))
if tenant := v.scopeTenantCondition(alias, scope); tenant != "" {
clause = "(" + clause + " AND " + tenant + ")"
}
clauses = append(clauses, clause)
}
return fmt.Sprintf("%s.id IN (%s)", alias, strings.Join(quoteStringSlice(kbIDs), ", "))
if len(clauses) == 0 {
return "false"
}
return joinOrClauses(clauses)
}
// buildScopeClause renders one scope. A scope's document whitelist and tag
// filter are ANDed: each is an independent narrowing of the same KB, so
// applying only one of them would admit rows the other excludes. Scopes are
// ORed against each other by joinOrClauses because they are alternatives.
func buildScopeClause(alias, knowledgeIDColumn string, scope SearchScope) string {
func (v *sqlValidator) buildScopeClause(alias, knowledgeIDColumn string, scope SearchScope) string {
if scope.KnowledgeBaseID == "" {
return ""
}
@ -1220,43 +1241,52 @@ func buildScopeClause(alias, knowledgeIDColumn string, scope SearchScope) string
alias, knowledgeIDColumn, strings.Join(quoteStringSlice(scope.TagIDs), ", "),
))
}
if tenant := v.scopeTenantCondition(alias, scope); tenant != "" {
conditions = append(conditions, tenant)
}
if len(conditions) == 1 {
return conditions[0]
}
return "(" + strings.Join(conditions, " AND ") + ")"
}
func buildKnowledgeScopeCondition(alias string, scopes []SearchScope) string {
func (v *sqlValidator) buildKnowledgeScopeCondition(alias string, scopes []SearchScope) string {
clauses := make([]string, 0, len(scopes))
for _, scope := range scopes {
if clause := buildScopeClause(alias, "id", scope); clause != "" {
if clause := v.buildScopeClause(alias, "id", scope); clause != "" {
clauses = append(clauses, clause)
}
}
if len(clauses) == 0 {
return "false"
}
return joinOrClauses(clauses)
}
func buildChunkScopeCondition(alias string, scopes []SearchScope) string {
func (v *sqlValidator) buildChunkScopeCondition(alias string, scopes []SearchScope) string {
clauses := make([]string, 0, len(scopes))
for _, scope := range scopes {
if clause := buildScopeClause(alias, "knowledge_id", scope); clause != "" {
if clause := v.buildScopeClause(alias, "knowledge_id", scope); clause != "" {
clauses = append(clauses, clause)
}
}
if len(clauses) == 0 {
return "false"
}
return joinOrClauses(clauses)
}
func uniqueScopeKBIDs(scopes []SearchScope) []string {
seen := make(map[string]bool, len(scopes))
out := make([]string, 0, len(scopes))
for _, scope := range scopes {
if scope.KnowledgeBaseID == "" || seen[scope.KnowledgeBaseID] {
continue
// scopeTenantCondition keeps ownership in the same OR branch as the KB and
// document/tag restrictions. Missing ownership retains caller-only isolation.
func (v *sqlValidator) scopeTenantCondition(alias string, scope SearchScope) string {
tenantID := scope.TenantID
if tenantID == 0 {
if !v.enableTenantInjection {
return ""
}
seen[scope.KnowledgeBaseID] = true
out = append(out, scope.KnowledgeBaseID)
tenantID = v.tenantID
}
return out
return fmt.Sprintf("%s.tenant_id = %d", alias, tenantID)
}
func joinOrClauses(clauses []string) string {

View File

@ -0,0 +1,205 @@
package utils
import (
"database/sql"
"reflect"
"strings"
"testing"
_ "github.com/mattn/go-sqlite3"
)
func sharedTenantFixture(t *testing.T) *sql.DB {
t.Helper()
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
db.SetMaxOpenConns(1)
t.Cleanup(func() { _ = db.Close() })
_, err = db.Exec(`
CREATE TABLE knowledge_bases (id TEXT, tenant_id INTEGER, name TEXT);
CREATE TABLE knowledges (id TEXT, tenant_id INTEGER, knowledge_base_id TEXT, title TEXT);
CREATE TABLE chunks (id TEXT, tenant_id INTEGER, knowledge_base_id TEXT, knowledge_id TEXT);
CREATE TABLE knowledge_tag_relations (knowledge_id TEXT, tag_id TEXT);
CREATE TABLE sessions (id TEXT, tenant_id INTEGER);
CREATE TABLE tenants (id INTEGER, name TEXT);
INSERT INTO knowledge_bases VALUES
('own-kb', 1, 'own'), ('shared-kb', 2, 'shared'),
('private-kb', 2, 'private'), ('shared-kb', 1, 'wrong-owner'),
('own-kb', 2, 'swapped-owner');
INSERT INTO knowledges VALUES
('own-doc', 1, 'own-kb', 'own'), ('shared-doc', 2, 'shared-kb', 'shared'),
('private-doc', 2, 'private-kb', 'private'),
('shared-doc', 1, 'shared-kb', 'wrong-owner'),
('own-doc', 2, 'own-kb', 'swapped-owner'),
('other-doc', 2, 'shared-kb', 'outside-document'),
('untagged-doc', 2, 'shared-kb', 'outside-tag');
INSERT INTO chunks VALUES
('own', 1, 'own-kb', 'own-doc'), ('shared', 2, 'shared-kb', 'shared-doc'),
('private', 2, 'private-kb', 'private-doc'),
('wrong-owner', 1, 'shared-kb', 'shared-doc'),
('swapped-owner', 2, 'own-kb', 'own-doc'),
('outside-document', 2, 'shared-kb', 'other-doc'),
('outside-tag', 2, 'shared-kb', 'untagged-doc');
INSERT INTO knowledge_tag_relations VALUES
('own-doc', 'own-tag'), ('shared-doc', 'shared-tag'), ('other-doc', 'shared-tag');
INSERT INTO sessions VALUES ('own-session', 1), ('other-session', 2);
INSERT INTO tenants VALUES (1, 'own-tenant'), (2, 'other-tenant');`)
if err != nil {
t.Fatal(err)
}
return db
}
func assertSharedTenantQuery(t *testing.T, db *sql.DB, query string, want []string, opts ...SQLValidationOption) {
t.Helper()
secured, validation, err := ValidateAndSecureSQL(query, opts...)
if err != nil || !validation.Valid {
t.Fatalf("secure query %q: validation=%+v, err=%v", query, validation, err)
}
rows, err := db.Query(secured)
if err != nil {
t.Fatalf("execute secured SQL %s: %v", secured, err)
}
defer func() { _ = rows.Close() }()
var got []string
for rows.Next() {
var value string
if err := rows.Scan(&value); err != nil {
t.Fatal(err)
}
got = append(got, value)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("results = %v, want %v; secured SQL: %s", got, want, secured)
}
}
func TestStructuredScopeTenantPairing(t *testing.T) {
db := sharedTenantFixture(t)
scopes := []SearchScope{
{TenantID: 1, KnowledgeBaseID: "own-kb", KnowledgeIDs: []string{"own-doc"}, TagIDs: []string{"own-tag"}},
{
TenantID: 2, KnowledgeBaseID: "shared-kb",
KnowledgeIDs: []string{"shared-doc", "untagged-doc"}, TagIDs: []string{"shared-tag"},
},
}
for _, query := range []string{
"SELECT name FROM knowledge_bases ORDER BY name",
"SELECT k.title FROM knowledges k ORDER BY k.title",
"SELECT c.id FROM chunks c ORDER BY c.id",
`SELECT "Shared Rows".id FROM chunks "Shared Rows" ORDER BY "Shared Rows".id`,
"SELECT c.id FROM chunks c JOIN knowledges k ON k.id = c.knowledge_id ORDER BY c.id",
"SELECT c.id FROM chunks c JOIN chunks d ON c.knowledge_id = d.knowledge_id ORDER BY c.id",
"SELECT c.id FROM chunks c JOIN knowledge_bases kb ON kb.id = c.knowledge_base_id ORDER BY c.id",
"SELECT c.id FROM chunks c WHERE c.id IN (SELECT c.id FROM chunks c) ORDER BY c.id",
} {
t.Run(query, func(t *testing.T) {
assertSharedTenantQuery(t, db, query, []string{"own", "shared"},
WithTenantIsolation(1), WithSearchScopes(scopes))
})
}
}
func TestStructuredScopeTenantTagOnly(t *testing.T) {
db := sharedTenantFixture(t)
assertSharedTenantQuery(t, db, "SELECT c.id FROM chunks c ORDER BY c.id", []string{"outside-document", "shared"},
WithTenantIsolation(1), WithSearchScopes([]SearchScope{
{TenantID: 2, KnowledgeBaseID: "shared-kb", TagIDs: []string{"shared-tag"}},
}))
}
func TestStructuredScopeTenantZeroFallback(t *testing.T) {
db := sharedTenantFixture(t)
for _, query := range []string{
"SELECT name FROM knowledge_bases ORDER BY name",
"SELECT title FROM knowledges ORDER BY title",
"SELECT id FROM chunks ORDER BY id",
} {
t.Run(query, func(t *testing.T) {
assertSharedTenantQuery(t, db, query, []string{"own"},
WithTenantIsolation(1), WithSearchScopes([]SearchScope{
{KnowledgeBaseID: "own-kb"},
}))
})
}
}
func TestStructuredScopeInvalidTenantScopesFailClosed(t *testing.T) {
db := sharedTenantFixture(t)
for _, table := range []string{"knowledge_bases", "knowledges", "chunks"} {
t.Run(table, func(t *testing.T) {
assertSharedTenantQuery(t, db, "SELECT id FROM "+table, nil,
WithTenantIsolation(1), WithSearchScopes([]SearchScope{{TenantID: 2}, {}}))
})
}
// Invalid alternatives must not turn an OR restriction into unrestricted access.
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own"},
WithTenantIsolation(1), WithSearchScopes([]SearchScope{
{TenantID: 2}, {TenantID: 1, KnowledgeBaseID: "own-kb"},
}))
}
func TestStructuredScopePreservesOtherTenantTables(t *testing.T) {
db := sharedTenantFixture(t)
opts := []SQLValidationOption{
WithTenantIsolation(1, "chunks", "sessions", "tenants"),
WithSearchScopes([]SearchScope{
{TenantID: 2, KnowledgeBaseID: "shared-kb", KnowledgeIDs: []string{"shared-doc"}},
}),
}
assertSharedTenantQuery(t, db, "SELECT id FROM sessions ORDER BY id", []string{"own-session"}, opts...)
assertSharedTenantQuery(t, db, "SELECT name FROM tenants ORDER BY name", []string{"own-tenant"}, opts...)
assertSharedTenantQuery(t, db, "SELECT c.id || ':' || s.id FROM chunks c CROSS JOIN sessions s",
[]string{"shared:own-session"}, opts...)
}
func TestStructuredScopePreservesLegacyTenantFiltering(t *testing.T) {
db := sharedTenantFixture(t)
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own", "wrong-owner"},
WithTenantIsolation(1), WithSearchScopeFilter([]string{"own-kb", "shared-kb"}, nil))
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own", "wrong-owner"},
WithTenantIsolation(1), WithSearchScopes(nil))
// A zero-tenant scope without tenant injection retains its original optional semantics.
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own", "swapped-owner"},
WithSearchScopes([]SearchScope{{KnowledgeBaseID: "own-kb"}}))
secured, _, err := ValidateAndSecureSQL("SELECT id FROM chunks",
WithSearchScopes([]SearchScope{{KnowledgeBaseID: "own-kb"}}))
if err != nil || strings.Contains(secured, "tenant_id") {
t.Fatalf("optional scope unexpectedly injected tenant filter: %s; error=%v", secured, err)
}
}
// The owner/KB predicate must remain bound to the outer table when a
// user-chosen alias also occurs inside an injected subquery.
func TestStructuredScopeTagAliasRetainsTenantBinding(t *testing.T) {
db := sharedTenantFixture(t)
assertSharedTenantQuery(t, db,
"SELECT ktr.id FROM chunks ktr WHERE ktr.knowledge_id = 'shared-doc' ORDER BY ktr.id", []string{"shared"},
WithTenantIsolation(1), WithSearchScopes([]SearchScope{
{TenantID: 2, KnowledgeBaseID: "shared-kb", TagIDs: []string{"shared-tag"}},
}))
}
func TestStructuredScopeCallerZeroRemainsRestricted(t *testing.T) {
db := sharedTenantFixture(t)
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", nil,
WithTenantIsolation(0), WithSearchScopes([]SearchScope{{KnowledgeBaseID: "own-kb"}}))
// Explicitly authorized ownership also constrains a scope used on its own.
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own"},
WithSearchScopes([]SearchScope{{TenantID: 1, KnowledgeBaseID: "own-kb"}}))
}
func TestStructuredScopeStillRejectsCompoundQuery(t *testing.T) {
_, validation, err := ValidateAndSecureSQL(
"SELECT id FROM chunks UNION SELECT id FROM chunks ORDER BY id",
WithTenantIsolation(1), WithSearchScopes([]SearchScope{{TenantID: 2, KnowledgeBaseID: "shared-kb"}}),
)
if err == nil || validation.Valid {
t.Fatal("structured owner scopes must not permit previously rejected compound queries")
}
}