Skip to content

Commit fe6f84b

Browse files
committed
refactor: Centralize and simplify SQL validation logic
1 parent 03987b9 commit fe6f84b

File tree

4 files changed

+1756
-535
lines changed

4 files changed

+1756
-535
lines changed

internal/agent/tools/data_analysis.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ func (t *DataAnalysisTool) Execute(ctx context.Context, args json.RawMessage) (*
117117
}, fmt.Errorf("modification queries are not allowed")
118118
}
119119

120+
// Validate SQL with comprehensive security checks
121+
// IMPORTANT: Must enable validateSelectStmt to block RangeFunction attacks
122+
_, validation := utils.ValidateSQL(input.Sql,
123+
utils.WithAllowedTables(schema.TableName),
124+
utils.WithSingleStatement(), // Block multiple statements
125+
utils.WithNoDangerousFunctions(), // Block dangerous functions
126+
)
127+
if !validation.Valid {
128+
logger.Warnf(ctx, "[Tool][DataAnalysis] SQL validation failed for session %s: %v", t.sessionID, validation.Errors)
129+
return &types.ToolResult{
130+
Success: false,
131+
Error: fmt.Sprintf("SQL validation failed: %v", validation.Errors),
132+
}, fmt.Errorf("SQL validation failed: %v", validation.Errors)
133+
}
134+
120135
logger.Infof(ctx, "[Tool][DataAnalysis] Received SQL query for session %s: %s", t.sessionID, input.Sql)
121136
// Execute single query and get results
122137
results, err := t.executeSingleQuery(ctx, input.Sql)

0 commit comments

Comments
 (0)