fix: harden AI tool access check

This commit is contained in:
2026-06-17 08:57:59 +08:00
parent 8bdde45e89
commit c5b7b68210
3 changed files with 29 additions and 4 deletions

View File

@@ -100,8 +100,9 @@ public final class AiToolRegistry {
if (!employee.toolNames().contains(tool.name())) {
blockers.add("Employee " + employee.id() + " is not allowed to use tool " + tool.name());
}
Set<String> granted = new LinkedHashSet<>(request.grantedScopes() == null
? List.of() : request.grantedScopes());
List<String> requestScopes = request == null || request.grantedScopes() == null
? List.of() : request.grantedScopes();
Set<String> granted = new LinkedHashSet<>(requestScopes);
List<String> missingScopes = new ArrayList<>();
for (String scope : tool.requiredScopes()) {
if (!granted.contains(scope)) {
@@ -116,11 +117,12 @@ public final class AiToolRegistry {
if (!missingScopes.isEmpty()) {
blockers.add("Missing scopes: " + String.join(",", missingScopes));
}
String targetEnv = text(request.targetEnv()).toLowerCase(Locale.ROOT);
String targetEnv = text(request == null ? null : request.targetEnv()).toLowerCase(Locale.ROOT);
if (tool.localOnly() && !isLocalEnv(targetEnv)) {
blockers.add("Tool " + tool.name() + " is local-only, targetEnv=" + targetEnv);
}
if (request.productionWrite() || isProductionEnv(targetEnv)) {
boolean productionWrite = request != null && request.productionWrite();
if (productionWrite || isProductionEnv(targetEnv)) {
if (!tool.productionWriteAllowed()) {
blockers.add("Tool " + tool.name() + " is not allowed to perform production writes");
}