|
27 | 27 | import io.github.malonetalk.service.ChatExecutionService; |
28 | 28 | import io.github.malonetalk.service.ChatExecutionService.Execution; |
29 | 29 | import io.github.malonetalk.service.ChatExecutionService.QueryTask; |
| 30 | +import io.github.malonetalk.service.SqlJoinValidationService; |
30 | 31 | import io.github.malonetalk.service.SqlPermissionService; |
31 | 32 | import java.io.IOException; |
32 | 33 | import java.sql.Connection; |
@@ -65,6 +66,7 @@ public class SqlExecutor { |
65 | 66 |
|
66 | 67 | private final DynamicDataSourceManager dynamicDataSourceManager; |
67 | 68 | private final SqlPermissionService sqlPermissions; |
| 69 | + private final SqlJoinValidationService joinValidation; |
68 | 70 |
|
69 | 71 | public SqlExecution executeDetailed(DatasourcePo datasource, String sql, ToolCallContext ctx) { |
70 | 72 | var authorized = authorizeSelect(datasource, sql, ctx.userId()); |
@@ -98,16 +100,22 @@ static String truncate(String message) { |
98 | 100 | } |
99 | 101 |
|
100 | 102 | public AuthorizedSqlBo authorizeSelect(DatasourcePo datasource, String sql, Integer userId) { |
101 | | - return sqlPermissions.authorize(userId, datasource, parseAllowedSelect(sql)); |
| 103 | + var authorized = sqlPermissions.authorize(userId, datasource, parseAllowedSelect(sql)); |
| 104 | + // Authorization rewrites the AST; validate the original query only after access is checked. |
| 105 | + joinValidation.validate(datasource.getId(), parseAllowedSelect(sql)); |
| 106 | + return authorized; |
102 | 107 | } |
103 | 108 |
|
104 | 109 | public AuthorizedSqlBo authorizeSelect( |
105 | 110 | DatasourcePo datasource, |
106 | 111 | String sql, |
107 | 112 | Integer userId, |
108 | 113 | Map<String, Set<String>> previousColumns) { |
109 | | - return sqlPermissions.authorize( |
110 | | - userId, datasource, parseAllowedSelect(sql), previousColumns); |
| 114 | + var authorized = |
| 115 | + sqlPermissions.authorize( |
| 116 | + userId, datasource, parseAllowedSelect(sql), previousColumns); |
| 117 | + joinValidation.validate(datasource.getId(), parseAllowedSelect(sql)); |
| 118 | + return authorized; |
111 | 119 | } |
112 | 120 |
|
113 | 121 | public String validateSelectSql(String sql) { |
|
0 commit comments