fix(security): enforce KB access on wiki reads and related IDOR paths

Gate wiki GET routes and initialization config reads with KBAccessRead,
and bind async task progress endpoints to the caller tenant via task ID
parsing. Closes cross-tenant read paths reported in GHSA-ph8f-4qwx-89rr
and related initialization/progress leaks.
This commit is contained in:
wizardchen
2026-07-02 11:59:50 +08:00
committed by lyingbug
parent ab790bcbd9
commit a17a2250ed
9 changed files with 297 additions and 35 deletions
+33 -18
View File
@@ -63,36 +63,51 @@ func GenerateTaskIDWithPrefix(prefix string, tenantID uint64, businessID ...stri
// ParseTaskID parses a task ID generated by GenerateTaskID and returns its components.
// Returns taskType, tenantID, timestamp, uuid, businessID, and error.
//
// Task types may contain underscores (e.g. "faq_import", "kb_clone"), so the
// parser locates the tenant/timestamp pair rather than assuming parts[0] is
// the full task type.
func ParseTaskID(taskID string) (taskType string, tenantID uint64, timestamp int64, uuidPart string, businessID string, err error) {
parts := strings.Split(taskID, "_")
if len(parts) < 4 {
err = fmt.Errorf("invalid task ID format: %s", taskID)
return
}
taskType = parts[0]
tenantID, err = strconv.ParseUint(parts[1], 10, 64)
if err != nil {
err = fmt.Errorf("invalid tenant ID in task ID: %s", parts[1])
tenantIdx := -1
for i := 1; i < len(parts)-2; i++ {
candidateTenant, parseErr := strconv.ParseUint(parts[i], 10, 64)
if parseErr != nil || candidateTenant == 0 {
continue
}
candidateTS, parseErr := strconv.ParseInt(parts[i+1], 10, 64)
if parseErr != nil || candidateTS < 1_000_000_000_000 {
continue
}
tenantID = candidateTenant
timestamp = candidateTS
tenantIdx = i
break
}
if tenantIdx < 0 {
err = fmt.Errorf("invalid task ID format: %s", taskID)
return
}
timestamp, err = strconv.ParseInt(parts[2], 10, 64)
if err != nil {
err = fmt.Errorf("invalid timestamp in task ID: %s", parts[2])
return
taskType = strings.Join(parts[:tenantIdx], "_")
uuidPart = parts[tenantIdx+2]
if len(parts) > tenantIdx+3 {
businessID = strings.Join(parts[tenantIdx+3:], "_")
}
uuidPart = parts[3]
if len(parts) > 4 {
businessID = parts[4]
}
return
}
// TaskTenantID extracts the tenant ID embedded in a GenerateTaskID task ID.
func TaskTenantID(taskID string) (uint64, error) {
_, tenantID, _, _, _, err := ParseTaskID(taskID)
return tenantID, err
}
// sanitizeTaskType ensures task type is safe for use in task ID
func sanitizeTaskType(taskType string) string {
// Replace colons and other special characters with underscores